HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* j. A4 ~& n9 Q1 d2 d
7 ~( J# W3 |9 b: A+ r6 S% E/ q public Object buildActions () {+ t8 t, [2 Q, h) N/ S @! z: \
super.buildActions();
: D7 q) p. h5 g5 x8 Z7 z 8 j) }- m, w1 C. U9 z3 x
// Create the list of simulation actions. We put these in
$ c0 `# Z; e. \ // an action group, because we want these actions to be
5 q+ H6 b8 s7 W% ]2 T& W // executed in a specific order, but these steps should7 P7 E7 ]' B h4 _4 Q- L- T
// take no (simulated) time. The M(foo) means "The message
2 z3 r* V# Y7 r/ f* N" n3 e // called <foo>". You can send a message To a particular
2 `' K3 H* w! a5 C3 i7 L // object, or ForEach object in a collection.
2 G: b; x+ b- I$ v 3 T% F& V# o6 \3 i2 p
// Note we update the heatspace in two phases: first run
, e* o( m7 i" N, [1 g // diffusion, then run "updateWorld" to actually enact the
9 B8 _9 m( D) } // changes the heatbugs have made. The ordering here is
, X/ g8 }) u& p. T // significant!$ e$ Z! h) ]% U% Y3 Q: {5 w6 c
1 D# {% U4 G' ? ~( ?& ^, q! y
// Note also, that with the additional
! k; p9 {( u3 T+ m. J // `randomizeHeatbugUpdateOrder' Boolean flag we can1 q$ m7 b3 ^0 ^ A& D( n
// randomize the order in which the bugs actually run
. O7 ^. U, h' k. r- e' | // their step rule. This has the effect of removing any* s& H$ x) e' Z* o' q M2 x
// systematic bias in the iteration throught the heatbug
& k3 H1 V& h; o6 d // list from timestep to timestep
+ U1 {. K+ T$ d. b 2 N( r# m: a) r* r# P
// By default, all `createActionForEach' modelActions have
( ]) Q& B: ?+ J! ^8 h // a default order of `Sequential', which means that the
( z) u$ r A/ V" y& n1 a3 t1 i p0 ]& A // order of iteration through the `heatbugList' will be
& h! K; \( d+ K, ^' a/ @3 r9 I4 f // identical (assuming the list order is not changed9 W6 K7 s. j/ z4 r
// indirectly by some other process).
8 [. C9 G0 h+ B- e- |. L. |
- `" \& N* _1 m* ] modelActions = new ActionGroupImpl (getZone ());
' ^& s( T9 y, F. V8 y' V1 G3 e6 F1 R W' k: j' G# V
try {4 ?/ D5 ]: W) V0 N* S" M
modelActions.createActionTo$message8 z' V7 M% [0 G. i; t
(heat, new Selector (heat.getClass (), "stepRule", false));
6 h7 c3 ~6 [/ O$ m } catch (Exception e) {3 P0 c$ t# f5 }6 e
System.err.println ("Exception stepRule: " + e.getMessage ());4 z Y5 @; e- w% M- K9 z8 Y5 ~
}
, a* w4 X! V2 |7 P8 h4 K- \
( N1 |: y3 T5 ~* A- `% u try {
5 A! H9 L1 P1 J; x5 k& m5 \( Q3 k& c Heatbug proto = (Heatbug) heatbugList.get (0);
3 |7 [% P7 d; c3 b Selector sel = , V7 W- j' M1 A8 i& {) _5 O
new Selector (proto.getClass (), "heatbugStep", false);* t9 r7 Q5 z j, {
actionForEach =1 K% j( S' @2 d( c
modelActions.createFActionForEachHomogeneous$call3 b; |6 K% D; ^6 D( R0 L# j5 s
(heatbugList,
9 R9 k- d' I6 H; J: R new FCallImpl (this, proto, sel,
* ?( `' B# B: r4 }+ y) Z new FArgumentsImpl (this, sel)));/ w- f! U& ]( K
} catch (Exception e) {, i* z* y2 j# {) R( a
e.printStackTrace (System.err);4 b; N z. q: \8 T% B- y
}, t3 Y/ J! E5 Q' r. J
$ c9 ?8 Q# d) S, X( R, G) Y syncUpdateOrder ();. U7 j ?9 H; e2 ?
9 ]; g; j; X' `( D try {
7 R3 L& T. J. ]2 {& B( i' J modelActions.createActionTo$message 1 i( M& l) Z- @, ^* Y- o6 a
(heat, new Selector (heat.getClass (), "updateLattice", false));9 N% u1 J! Z- d
} catch (Exception e) {
' A& \; Y, Q4 O3 }' D' {5 I System.err.println("Exception updateLattice: " + e.getMessage ());
0 h# r. B, |5 v }
3 D6 F c4 Z' U2 ^1 j 5 [9 e0 k b/ I! F5 v' C! D" c
// Then we create a schedule that executes the6 i- ~6 q% F ^( E [
// modelActions. modelActions is an ActionGroup, by itself it
( z5 e9 P9 R# O( n. w' h // has no notion of time. In order to have it executed in
0 o% @ M4 w2 I/ f7 @. L- l* V // time, we create a Schedule that says to use the D: N: B" k- C; X8 ^/ N. V
// modelActions ActionGroup at particular times. This
1 V* F% m) q, Z6 q1 d // schedule has a repeat interval of 1, it will loop every
# r, Z% ?2 M* O! ]6 K7 l& \ // time step. The action is executed at time 0 relative to
" I% p% u+ o% P0 I' V5 h7 i! h) B // the beginning of the loop.
; S7 {$ p8 C& N2 d7 R- l8 V* a* T0 ]0 x, Z5 v
// This is a simple schedule, with only one action that is
" H5 v; h1 \) }9 i7 F // just repeated every time. See jmousetrap for more: s5 x+ `3 O9 T6 g1 S5 \
// complicated schedules./ S4 H( d8 {. B0 V, H" ~0 p
- U1 M6 V3 _. p2 i/ p modelSchedule = new ScheduleImpl (getZone (), 1);+ F9 D" T) j# e! O
modelSchedule.at$createAction (0, modelActions);) Q8 K# x; j% j
6 a Y2 S$ p4 { \ ~1 I return this; S2 ?, Y( h+ r3 @" ^
} |