HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 Y; n, K4 M2 W& s) T1 @( R
. ]- M% z2 I& f. |+ P
public Object buildActions () {1 H1 ~' r, b b# A
super.buildActions();
8 M; {# d$ v5 i) h) I
. \# j) Z4 y- W8 J2 @ // Create the list of simulation actions. We put these in0 a8 J; U: {4 g& d
// an action group, because we want these actions to be
, o$ ]" G2 d! }2 @% a0 d/ O // executed in a specific order, but these steps should8 w2 P6 K0 F( U! r, b1 s
// take no (simulated) time. The M(foo) means "The message
$ h: \7 f+ u N" v: F0 p // called <foo>". You can send a message To a particular8 a$ N. S! D) Y. [
// object, or ForEach object in a collection.
7 W' a+ _8 h' `. J" {7 Q2 M
! a2 i4 D5 u9 w/ C // Note we update the heatspace in two phases: first run! U6 i7 O* K/ N/ {" ~
// diffusion, then run "updateWorld" to actually enact the: w6 x, @, j. s3 x2 O g2 ^
// changes the heatbugs have made. The ordering here is* h3 ~: f4 L' c" e @/ I
// significant!4 K" r+ O. S! ]: U, K( ~- q
& T0 K3 R) V9 @& ^4 p9 f
// Note also, that with the additional
4 P* g- w- i& t5 z" B7 w8 ]$ { // `randomizeHeatbugUpdateOrder' Boolean flag we can8 f& I9 w! Y" l" H* p9 D( T
// randomize the order in which the bugs actually run
' |) X }$ i$ K2 n! P ^ // their step rule. This has the effect of removing any
9 X9 g& s% v) c8 |5 }" E# m // systematic bias in the iteration throught the heatbug4 c6 s n# D. f4 R" f
// list from timestep to timestep
- A3 H. h+ g0 m( h9 N' w ' X- j8 O0 x% m ~9 b3 Q
// By default, all `createActionForEach' modelActions have! X# P* o: w( ]" q
// a default order of `Sequential', which means that the0 T9 H8 r! v1 L+ Q' G# j
// order of iteration through the `heatbugList' will be
* B1 B1 l) [2 a: \- R/ K: A // identical (assuming the list order is not changed
& b- `. v% ]6 m p // indirectly by some other process).4 C$ @8 c. r5 L" I2 N! j2 f3 y
, a- @+ O7 x4 W. v& d9 v modelActions = new ActionGroupImpl (getZone ());( e, F2 Y1 H. C5 c* F
: N! f# j8 A1 K8 s- s try {
/ o3 o- e7 C+ Q+ j5 }% H modelActions.createActionTo$message
0 O5 _$ U! F! f! ^ (heat, new Selector (heat.getClass (), "stepRule", false));
' X" W- q. E; e; o1 U4 U } catch (Exception e) {- |: M. a* m% }% H0 U1 u
System.err.println ("Exception stepRule: " + e.getMessage ());
# X/ F. P/ R9 |4 G5 b3 q! F }
$ q- b+ Q0 b0 I! H2 O0 @7 b1 S' G$ G) a# q/ a3 g9 w
try {: q4 V# p$ I, l ]: r
Heatbug proto = (Heatbug) heatbugList.get (0);" k: U2 p6 L% l7 o( D; T! L
Selector sel = , u2 q/ b* |# I* K3 C" d1 Q) `
new Selector (proto.getClass (), "heatbugStep", false);1 H4 s" Y! v5 o# }' j( t; i! i
actionForEach =
9 ~ Z) z4 c2 h/ v( f modelActions.createFActionForEachHomogeneous$call
7 d% m! a |9 c (heatbugList,7 ]# X4 u+ E: \8 M
new FCallImpl (this, proto, sel,4 K' x. K4 X e
new FArgumentsImpl (this, sel)));1 I1 Q$ a4 l# L
} catch (Exception e) {+ h$ L; a- d% p3 P0 w: a( m
e.printStackTrace (System.err);
$ K; y. d5 F: X }
Q9 x! _7 V- }1 C! P ' V* b& v" J& u
syncUpdateOrder ();
: c1 ~) N3 z% q% l
( ]0 P$ x9 f+ L. q; k try {' J2 c6 Z# h! C( y
modelActions.createActionTo$message 5 A- H3 E9 Z+ o1 }: U+ n5 l
(heat, new Selector (heat.getClass (), "updateLattice", false));
& ~( b6 t" ]5 Z; {8 O) L& G: p } catch (Exception e) {
8 y( t$ W+ y' v# a& B System.err.println("Exception updateLattice: " + e.getMessage ());
) X9 \1 }) I/ q: e$ |, L2 H" M }
4 o$ R" j" J: g' t 7 g8 x7 B9 h3 q7 j6 p1 Q: }, v
// Then we create a schedule that executes the
# Z( ?+ D% q! O" i // modelActions. modelActions is an ActionGroup, by itself it2 |! F7 |# S6 ^, M! L- R
// has no notion of time. In order to have it executed in/ \3 a/ h& V, Q/ a% s
// time, we create a Schedule that says to use the
0 h: m: p4 {5 s% V, U" ~( @ // modelActions ActionGroup at particular times. This0 @0 `! K9 E2 ~
// schedule has a repeat interval of 1, it will loop every, p- p% }) e$ d( b1 h. b
// time step. The action is executed at time 0 relative to
0 G, Z* i) L, D // the beginning of the loop.
z' H/ O: a( s" x- t, s" [2 h* t* _0 z2 c+ W
// This is a simple schedule, with only one action that is
o2 r3 N( h* S* R // just repeated every time. See jmousetrap for more5 W5 ]9 ?' A0 G
// complicated schedules." }3 a8 U i+ K# K! b- a% \" o! ?
- B8 }5 ?4 `( |2 r
modelSchedule = new ScheduleImpl (getZone (), 1);
& f4 j. F# _! B% r+ C$ T8 q modelSchedule.at$createAction (0, modelActions);3 u" [: q- o$ ~6 H3 r! l
# K4 U) Y2 b/ m9 f( y
return this;
% x1 A# A! p6 Z: ]# L" \ } |