HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: l& E2 ?1 X3 W, u+ N. F2 L$ h/ K
$ y7 t6 X7 I. Q \- t* W. m public Object buildActions () {
$ x3 j9 d8 ^8 l B$ K super.buildActions();
; [/ v" o- @0 Z/ ] / A j* x; s! p/ h( s
// Create the list of simulation actions. We put these in
0 {7 N0 ?3 C5 L5 [: A // an action group, because we want these actions to be
9 Z- F1 C! Y1 G. o( j3 E3 X // executed in a specific order, but these steps should
$ d* s& |- |3 G" D // take no (simulated) time. The M(foo) means "The message
5 T4 B* F2 f$ f4 Q& ]& w8 L // called <foo>". You can send a message To a particular
4 Z4 ?' `3 p% Q( w: L" q // object, or ForEach object in a collection.1 U* D# t" {( E% I- t# P8 u5 ]
8 p' B1 L0 r$ f k // Note we update the heatspace in two phases: first run
2 s- U3 b* |& V. i/ h% D& g/ U // diffusion, then run "updateWorld" to actually enact the( N. h6 p+ Z ]
// changes the heatbugs have made. The ordering here is* A! g5 M8 Z/ @5 d% ?4 j X- N
// significant!: S$ a! H- e1 m
& y$ o- Z) X/ {" E1 s. S
// Note also, that with the additional
1 `) L1 ^8 m/ S // `randomizeHeatbugUpdateOrder' Boolean flag we can
# A' |& C! }* `. q Z1 k7 z // randomize the order in which the bugs actually run
3 m# } H: P; z6 f j // their step rule. This has the effect of removing any
) e* d- i c0 e/ T // systematic bias in the iteration throught the heatbug
k" w& j- Y9 U // list from timestep to timestep
+ _9 p( B7 j- A9 m9 l
, r [) b5 T- M8 b2 n2 r" D9 h // By default, all `createActionForEach' modelActions have: V7 m1 F6 c' }4 S$ k1 j1 M6 D+ J
// a default order of `Sequential', which means that the
6 W' y7 L0 u- M6 q // order of iteration through the `heatbugList' will be& Y; B* H( O, x1 t
// identical (assuming the list order is not changed' }; Y4 y9 K. T! N$ q/ T
// indirectly by some other process).9 r# E2 P- U; Z3 o/ ^" Y
, o9 x- J4 \' x j! g modelActions = new ActionGroupImpl (getZone ());
( B4 b$ H& c. O( R7 P
4 e4 \+ Z& R* k/ J8 x try {- Q' b p; N' B; I
modelActions.createActionTo$message
( `" Q* w1 ]9 Q9 d (heat, new Selector (heat.getClass (), "stepRule", false));+ g- m1 [6 J0 e: `: [
} catch (Exception e) {* V1 g" w' q' m6 n4 q) {
System.err.println ("Exception stepRule: " + e.getMessage ());
5 r/ z: O# f4 D }
5 q* F7 |% z6 E" j" ?5 R
8 q: E" B$ Q# N. r: Y2 v9 m- q* N! ~ try {; [' H' t) ~! M
Heatbug proto = (Heatbug) heatbugList.get (0);) t9 w* b; ]. M' B0 q0 q
Selector sel = / t* U; s0 y- b: y- }
new Selector (proto.getClass (), "heatbugStep", false);2 p; m8 C1 i5 d
actionForEach =. V6 l! B2 C2 a4 d
modelActions.createFActionForEachHomogeneous$call
8 _1 I" j2 L" V& B (heatbugList,
1 L& I4 m& W3 Y5 Y new FCallImpl (this, proto, sel,
7 R9 f5 a# Q* T/ q new FArgumentsImpl (this, sel)));
3 m* ^& N1 z* @6 e6 c } catch (Exception e) {
5 W0 o8 d$ ~4 G0 O4 H e.printStackTrace (System.err);
a0 N7 _ Y4 B) v }
: M) j# ?! \( D$ i* w
* H4 p; k- |0 d H4 q5 O2 _ syncUpdateOrder ();
+ H% B$ i1 o* X! \) B2 g3 Q5 K
try {
+ r n% s5 b# I V modelActions.createActionTo$message
0 ^% F' a( E) z0 Z' d5 d (heat, new Selector (heat.getClass (), "updateLattice", false));& a: d" ~# e2 }) Q; d$ K
} catch (Exception e) {
; N- d9 A* k# x4 e( H5 N. z System.err.println("Exception updateLattice: " + e.getMessage ());; E+ V$ B8 L5 N& e: t/ w" X
}
6 |# |5 P0 j) U, Q! ^ 5 l7 c, [: ~$ W
// Then we create a schedule that executes the
: C4 u& q+ r% g/ m+ I; f9 m // modelActions. modelActions is an ActionGroup, by itself it
$ P3 F/ E" m ~; ^' ] // has no notion of time. In order to have it executed in
# }, `- d/ B, _9 x6 |9 {4 ^ // time, we create a Schedule that says to use the) j) G' w7 Y. [ O; h# P- f
// modelActions ActionGroup at particular times. This
( M. \ w" _3 N; M // schedule has a repeat interval of 1, it will loop every9 N+ \7 }) i% e' w0 T- O: F; r
// time step. The action is executed at time 0 relative to
* o+ H+ _7 W/ M7 F/ Z" V // the beginning of the loop.
8 z5 ^. s4 U# s; Q
1 a: J2 T) C& \$ x5 [: K1 a+ B // This is a simple schedule, with only one action that is
0 r$ j+ M2 v5 ~- |8 B, ^ // just repeated every time. See jmousetrap for more
2 y& h+ O' R2 f; w( y7 `6 U0 r) M( m // complicated schedules.
% F3 k6 F( u: Z0 o/ A
8 Y: l# U0 L$ E; @5 G modelSchedule = new ScheduleImpl (getZone (), 1);- w% |( N! O2 K% ?1 B" ]) W& O
modelSchedule.at$createAction (0, modelActions);
& w/ r* K( w( t8 }- o! V9 X$ i
+ T5 {' j3 O" e- ^. R) [- K return this;9 ^7 H* k- a5 x1 i( |
} |