HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 R1 G6 v, ~& o1 p* c
" I& \- W& L0 _ public Object buildActions () {& D: M( M) L6 F4 O$ D/ X' m. _
super.buildActions();3 R( ]0 q4 k5 A
- ?% Q" {, t8 G) ~0 B3 V3 V- x% N' K
// Create the list of simulation actions. We put these in
}3 z' U; n4 ` i% q$ j% d7 S // an action group, because we want these actions to be3 \8 u' o; g, p: {
// executed in a specific order, but these steps should/ E' `& ]4 X" h# `% {
// take no (simulated) time. The M(foo) means "The message
9 A* s4 k' \" C // called <foo>". You can send a message To a particular
" V6 q$ U* i% N" L8 _! M# Z A // object, or ForEach object in a collection.
+ m c w: Y& I: R * P; `( I, _! x; U9 s
// Note we update the heatspace in two phases: first run5 s1 T3 I" o" a+ s( C" ~6 u
// diffusion, then run "updateWorld" to actually enact the5 K0 F# X% F' T5 J9 L9 Z
// changes the heatbugs have made. The ordering here is6 L' c' U5 j+ C* v
// significant!* J- ?" S2 `, R M2 \; c' F. R. o
( f Q/ C+ `! N& X3 y* z3 H+ E8 _
// Note also, that with the additional
' e! S9 z% o* m$ f // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 F$ b8 J; r7 i+ D // randomize the order in which the bugs actually run
8 s$ h0 m9 }8 v // their step rule. This has the effect of removing any
7 d" W; v3 x& V( {2 Y z% |4 G // systematic bias in the iteration throught the heatbug
$ M4 G3 U1 q; e( @ // list from timestep to timestep( X( A" H. K* }+ ]6 n. O
p* }5 w) Z# z' o& L
// By default, all `createActionForEach' modelActions have
. h5 n W" e1 x$ N; x& S // a default order of `Sequential', which means that the
5 Z7 H# @+ \, B; h+ k) ]1 P // order of iteration through the `heatbugList' will be( Q0 [: d0 d: Q4 r8 w! l& V$ A( H
// identical (assuming the list order is not changed- n, O; X4 N7 B! h/ K+ T$ F% Q; M! l
// indirectly by some other process).
- z2 Y) Y) P- X6 W* c
% R" _: A- @7 x8 r% J" @2 N" B modelActions = new ActionGroupImpl (getZone ());+ ]' W4 m/ v( a; S- _; E& L
: Z; E1 V* Q9 L
try {
$ ]' N/ K: `' P0 t% R( x' ~ modelActions.createActionTo$message
5 z+ W! k# F+ r; } (heat, new Selector (heat.getClass (), "stepRule", false));8 p* K! i: U7 p. d2 X
} catch (Exception e) {
7 n% a% R" ] {$ p( \ System.err.println ("Exception stepRule: " + e.getMessage ());
( I2 O% z) k6 D }/ k2 Y$ p- s% G4 @( M
) k' ?$ b3 p2 w% X0 B4 `! n try {
$ b+ q" Q, s j | Heatbug proto = (Heatbug) heatbugList.get (0);
; }' e; j8 z6 j4 s( t$ S Selector sel = 6 U9 E1 U% B) f: g
new Selector (proto.getClass (), "heatbugStep", false);' f2 H* {9 e; h& C- k
actionForEach =$ P) c& G3 S) M9 ~1 V
modelActions.createFActionForEachHomogeneous$call
" b2 u C" S/ e+ q! A/ D7 L (heatbugList,
) j! S" N: D$ U0 y' { new FCallImpl (this, proto, sel,
" w" K8 P6 M1 s9 M new FArgumentsImpl (this, sel)));
( m' q* ?! r& w } catch (Exception e) {: O! ^% f7 |. ]
e.printStackTrace (System.err);
$ `2 c r4 z. C. t5 v ^ W, h3 [* R } O/ Z3 @; l. ?% a" i
' ~2 H7 r' z8 M3 V% b
syncUpdateOrder ();
4 g+ U1 ]* R6 Q E* k$ w% X1 ]: ` C( s: Y
try {5 @. u! l" j. Y+ C1 y% y6 ]7 I8 g! V
modelActions.createActionTo$message - l! E0 G$ b- y" \# r) S7 ^
(heat, new Selector (heat.getClass (), "updateLattice", false));" F2 {$ ^5 W) G$ H& I& V
} catch (Exception e) {5 E6 ]8 w+ M/ J- v4 }
System.err.println("Exception updateLattice: " + e.getMessage ());9 M4 s/ M, ?4 n$ G2 U
}
7 d) H& b0 h. E) Q! ~- z' N & ?! K8 R1 Q7 @- t" I
// Then we create a schedule that executes the
4 [3 g) f9 p8 y# H% E1 o // modelActions. modelActions is an ActionGroup, by itself it
' a& ]5 a2 z) a3 k8 F! C! E // has no notion of time. In order to have it executed in. e3 ?- i" R" L
// time, we create a Schedule that says to use the' H! H. X6 a5 c! [; ^0 C1 @ i
// modelActions ActionGroup at particular times. This
, B* r# V$ N% y // schedule has a repeat interval of 1, it will loop every
], G: O L- z // time step. The action is executed at time 0 relative to
. C; i8 N3 r) V, @# z // the beginning of the loop.
* ^ S1 U! t5 q; S# |9 c' u5 e
3 O4 s* y0 Z6 a4 A // This is a simple schedule, with only one action that is
9 T/ k; j2 e- S" L6 e( Q7 p" {3 p) d // just repeated every time. See jmousetrap for more
! u8 D& a4 ^7 S0 i // complicated schedules.( k1 J& C: V% G% s0 ~9 C
& J1 \& M$ {5 n8 m `1 W modelSchedule = new ScheduleImpl (getZone (), 1);
" {$ `8 T) }3 e+ |/ z modelSchedule.at$createAction (0, modelActions);
$ {9 ?9 t% N( O3 Z! ~ ; B2 B, \+ t, k
return this;
! c, H6 f7 c! L7 i0 [9 J2 U } |