HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! j$ A; j! m$ f' U5 w
7 S; i6 |' \7 u$ ]# i+ j+ V2 Z4 z public Object buildActions () {
# N# d; E6 M* N% r) F" N2 U- t super.buildActions();
3 a l0 ~9 c' O7 E " h7 }: R9 j- g. n& Y% F( K
// Create the list of simulation actions. We put these in7 S* |- i/ ~6 b; Y& z8 h4 M
// an action group, because we want these actions to be t. L, M( G: |& U$ T" [8 F- O) ~
// executed in a specific order, but these steps should0 O$ B1 c9 s( | }9 o% T7 i/ A) |2 L
// take no (simulated) time. The M(foo) means "The message
0 w5 P6 \' A9 r9 d4 [8 x+ O3 h- j // called <foo>". You can send a message To a particular
' q" Q/ X$ S! e6 ]! Q( c // object, or ForEach object in a collection.
0 ^: \0 s* [5 s4 k9 N& Y+ d* @
& J# \% }3 L( b# K // Note we update the heatspace in two phases: first run9 R: i6 W8 A* h. H' o, M
// diffusion, then run "updateWorld" to actually enact the
6 g' d/ q. A9 [1 V" h8 R // changes the heatbugs have made. The ordering here is( H% g4 v( L! B
// significant!
( W& E a- ~) M$ q! _
* U$ f/ m) ]% C4 A6 G // Note also, that with the additional
/ n% o1 \1 _: N) B6 k$ ~+ H; W // `randomizeHeatbugUpdateOrder' Boolean flag we can }) X6 U+ L; u
// randomize the order in which the bugs actually run
% @( }% E0 Y$ X // their step rule. This has the effect of removing any& s8 Q2 L- I* q/ a( R
// systematic bias in the iteration throught the heatbug' M! D5 p8 d6 ~8 f) J* M
// list from timestep to timestep
) s+ r0 }8 ^) ^, D' r f
2 V8 p. s+ E# c# `' D& U% m // By default, all `createActionForEach' modelActions have
- r( J0 l) S7 S C // a default order of `Sequential', which means that the7 g6 l$ i/ L4 i( N6 ], x0 d6 V
// order of iteration through the `heatbugList' will be. a2 Y+ u" T$ o/ ^
// identical (assuming the list order is not changed
$ z) k/ k8 Z2 E9 Y // indirectly by some other process).
" M, K% Y7 l8 m, s$ ]
# Z( t8 e) B# a) { modelActions = new ActionGroupImpl (getZone ());
8 B% C4 b$ ?; ?( Q* b6 v; M6 M- V4 d8 y8 @3 e9 K
try {7 `0 i7 I: D/ h' q. G8 u, B7 `/ A
modelActions.createActionTo$message' O( \( B( y; g% r( h* K
(heat, new Selector (heat.getClass (), "stepRule", false));( v) a" o' V6 P* Y1 [) N3 E- r& a
} catch (Exception e) {
7 u2 W% G" W- D& Q1 ]" _5 b System.err.println ("Exception stepRule: " + e.getMessage ());
5 k: ?( m( S. ^9 s2 B+ h, m: R }2 \; o. Q, I: |) K- y
7 v5 Y! g* i) p0 M* ?7 s. k6 G9 ~
try { L7 j3 X1 f! O
Heatbug proto = (Heatbug) heatbugList.get (0);
) V3 Y2 H! s$ I, x0 Q, N$ _ Selector sel =
! j- n0 f/ q- ~- h* z new Selector (proto.getClass (), "heatbugStep", false);
) i( @4 k& c8 h4 o2 b actionForEach =2 S, y8 u: n: i- r; u1 V
modelActions.createFActionForEachHomogeneous$call
8 U/ B# b! K: L (heatbugList,
0 [# e, e C) u* M* `8 ?# H new FCallImpl (this, proto, sel,: U8 u+ O0 V1 E9 s6 E
new FArgumentsImpl (this, sel)));
" e! \4 |8 s- W* H! e, G } catch (Exception e) {6 T1 k, U/ g$ |, j
e.printStackTrace (System.err);! P9 }; C0 }2 v# b8 z7 _
}2 c: H7 r& ?, y
/ d U0 H, ` _/ n# c6 A syncUpdateOrder ();5 x6 H2 p4 a" `6 |/ F
. X( p. K1 P4 h9 ?2 R5 ^7 F try {. s" a, R6 k. {$ ]& h
modelActions.createActionTo$message
# S! E' R) G. M g (heat, new Selector (heat.getClass (), "updateLattice", false));7 J9 `0 l$ |8 R6 g
} catch (Exception e) {6 _2 N5 j5 f+ g, V+ B# f
System.err.println("Exception updateLattice: " + e.getMessage ());0 g) W# {) Q4 `$ h9 j# l1 b
}
$ n$ b1 a& d! L& N% K9 O $ g5 z) D2 c" H- _+ e8 P, i: h- x# x
// Then we create a schedule that executes the& L' J3 K. F$ b2 E) O* R2 u* [6 \
// modelActions. modelActions is an ActionGroup, by itself it
8 e6 q, n- m, ]: D( j. v1 S8 g // has no notion of time. In order to have it executed in
) H! X3 q# h6 Y3 w1 i% U& d6 W // time, we create a Schedule that says to use the
# q/ [. j) m7 H9 T% V // modelActions ActionGroup at particular times. This/ v! [0 M- _& i
// schedule has a repeat interval of 1, it will loop every1 h5 {* G0 B, k9 l* `* @
// time step. The action is executed at time 0 relative to
3 n1 Y& b! w* _. S0 H! M // the beginning of the loop.
a. Y. ?, K* F
. |! U; N: v' D% X1 x, m7 c // This is a simple schedule, with only one action that is9 A5 k ]0 t9 v' x3 c+ E
// just repeated every time. See jmousetrap for more
, E6 W" X. `6 A5 U/ S. I // complicated schedules.7 Y7 |" @1 h' _7 g+ j$ T
! T5 @; ~- h* L( y4 d2 ?7 e
modelSchedule = new ScheduleImpl (getZone (), 1); q6 p: U2 b0 a/ _* h1 p
modelSchedule.at$createAction (0, modelActions);
! k D9 s, u. y( ^& B : E9 N8 c% f! X1 t
return this;
4 J$ P/ C% l) f8 X0 ?8 ~' L } |