HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, q& p8 L; [, v/ g: y c0 f6 V8 T4 _
public Object buildActions () {( b0 d# X z) u z$ D
super.buildActions();
" w0 S: Y% p7 X& |6 F8 R3 s- D 4 {/ p; `) A; W& I. B1 }6 e/ Y
// Create the list of simulation actions. We put these in
' {2 P. _- Y0 E' ]' ^! o6 e // an action group, because we want these actions to be! s- L; Y+ j; x4 V' ?# O& `7 m
// executed in a specific order, but these steps should- H. S# Q0 j" n: [6 ~4 |
// take no (simulated) time. The M(foo) means "The message4 Z" Z* ?( `. J- f
// called <foo>". You can send a message To a particular9 \" j; T2 c5 @9 e
// object, or ForEach object in a collection.: g! Z6 e; t X$ x7 L
' v# t ]* I5 m, D9 V, O J4 m
// Note we update the heatspace in two phases: first run
& B ~' P1 ^5 y // diffusion, then run "updateWorld" to actually enact the" R2 F. H% B7 W: ?0 s
// changes the heatbugs have made. The ordering here is
# s8 |- w% o* T$ V' |# Q // significant!
. t5 R- p7 y: D6 F
- ?# p4 @2 V& m // Note also, that with the additional$ v7 ]$ V9 T- j
// `randomizeHeatbugUpdateOrder' Boolean flag we can( ^0 p% E7 D" j) H% v
// randomize the order in which the bugs actually run" ~9 K" y1 `) | N
// their step rule. This has the effect of removing any
" j% |- Y' G8 ^$ e" |' B // systematic bias in the iteration throught the heatbug& [; T! U# R S6 v& F
// list from timestep to timestep
. h" F" y( U4 `; Q- K
. u* F$ h; v) F // By default, all `createActionForEach' modelActions have
9 N4 h( F5 B2 o# K. @# X3 Y2 n5 U // a default order of `Sequential', which means that the
$ j* P& U" r2 P" A // order of iteration through the `heatbugList' will be
% Z- }/ E! n" t ` // identical (assuming the list order is not changed
8 H3 Q; B; D& T" n" } // indirectly by some other process).! i: F9 {$ o. [" P5 P) S4 H
" V* Y% v2 h/ t- d! } modelActions = new ActionGroupImpl (getZone ());
7 Q' S9 X3 ]) b: I& G) G) }. q
; w4 L E v; T( X0 `9 ]8 Q" c try {' E, y- W) t7 b, u" `5 m O, y
modelActions.createActionTo$message
7 H1 j. d8 a* b (heat, new Selector (heat.getClass (), "stepRule", false));, e( }7 j; c( X& L3 x
} catch (Exception e) {
! h2 f7 s9 x6 g0 u! F% F/ o System.err.println ("Exception stepRule: " + e.getMessage ());& [7 q% _% f+ g, }: `
}' x* D( a' M& C; ^$ T0 [! x
' p8 ^5 T: Q- K; v0 J) L: V0 L
try {
R) l' x+ o& n8 ^$ [7 G! f Heatbug proto = (Heatbug) heatbugList.get (0);
$ x* l A! g- s a' b0 @ Selector sel =
- {8 I; W8 Q& ^5 q new Selector (proto.getClass (), "heatbugStep", false);% _ I D, y; T4 m
actionForEach =
: Z+ E2 K v n* O6 N% ? modelActions.createFActionForEachHomogeneous$call- |" l) L0 ~% ]5 ?& W2 \! C
(heatbugList,9 X3 z# Y+ ~$ F8 Y h
new FCallImpl (this, proto, sel,
! {$ b) C4 n: _ new FArgumentsImpl (this, sel)));/ B q7 G- w& h9 i
} catch (Exception e) {
4 M5 }# `5 H0 a$ n1 z, U* r, q# V5 \ e.printStackTrace (System.err);
1 P( a( J% G, n" m$ T: y% o$ p }" e+ G7 j/ M( H# D0 [
7 v) G' r6 f3 d4 @$ b, x. O0 X syncUpdateOrder ();' L! I; X2 o# B1 ^
& J( R& h# b3 |( {7 D
try {
, u; v6 u3 c% c! }4 M; j# } modelActions.createActionTo$message
$ T r' g0 g- Y, s( O4 \( q: O* s; } (heat, new Selector (heat.getClass (), "updateLattice", false));
! D; ]3 k U' L7 a } catch (Exception e) {) R5 P- N# S; r6 O2 ]& ?% M8 L' l
System.err.println("Exception updateLattice: " + e.getMessage ());; ]. G# {* A# @) \* Y+ V
}
. I: A1 ?6 V- W1 s( `+ W 9 o# n! Y6 L* [& c: a) @1 g& f
// Then we create a schedule that executes the
/ k5 b* m9 A4 ^7 d* {( Y // modelActions. modelActions is an ActionGroup, by itself it+ f `5 R* F; F5 z: B5 w3 ~. R
// has no notion of time. In order to have it executed in
" E% c2 g! t& M C! y+ J // time, we create a Schedule that says to use the
2 A. O8 z% m4 B( f2 [; S! t // modelActions ActionGroup at particular times. This n. W5 u* L* J$ w t1 j) Y
// schedule has a repeat interval of 1, it will loop every
9 X: ^- a: U, a% Y* D: L6 u3 Q // time step. The action is executed at time 0 relative to5 L# ^: H7 i/ P) l
// the beginning of the loop.% d, W3 B9 o4 U7 e/ P6 K; f
/ b/ S/ `# O/ @% q
// This is a simple schedule, with only one action that is
. i0 U4 W+ q% Q! V% d9 z; S // just repeated every time. See jmousetrap for more
1 s% L- x. ~' k // complicated schedules.; e7 {' o, h+ k( C4 O- B( u2 B9 x
4 X j: R6 K& N
modelSchedule = new ScheduleImpl (getZone (), 1);! t7 B# C7 @* S% B. O3 L
modelSchedule.at$createAction (0, modelActions);
) }+ B$ W/ i1 ^3 v/ T% e4 U - m3 S9 z' A6 ^/ ?+ t, ^' B8 n
return this;
3 K6 X% I! _0 o: e5 ]1 `# \ } |