HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# [9 P g+ Q: p x, p
+ c! V, P9 G% Y% P4 t: f public Object buildActions () {
, F1 R" \- ?7 a$ p2 ]$ K0 s {6 h super.buildActions();* o/ R$ Q7 b: I
6 l) d3 ]7 N( z
// Create the list of simulation actions. We put these in
5 l; q0 b' G3 I4 [5 c6 P& a0 }6 E) z // an action group, because we want these actions to be& L+ }3 B |, _8 \
// executed in a specific order, but these steps should
( z8 k6 d) Q- ^ // take no (simulated) time. The M(foo) means "The message
# q' [4 m! K' z* t$ {2 m // called <foo>". You can send a message To a particular
( n- u+ `' i5 K* r8 O // object, or ForEach object in a collection." P+ n) q1 i" B
a( `& }# h7 r9 p // Note we update the heatspace in two phases: first run* z7 S6 D) P8 J/ h- |/ B
// diffusion, then run "updateWorld" to actually enact the4 _% C) G. W f
// changes the heatbugs have made. The ordering here is4 B7 f9 F0 L& \. C* V
// significant!) o$ Z" a. I* `) {4 c4 [
. u1 ~: s* ^) U' y' z2 n // Note also, that with the additional$ [. J7 W0 m* N7 r$ e& S
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ m9 `& n/ B( `: ?2 g: b8 z$ s7 V
// randomize the order in which the bugs actually run
! v* h* ` o1 O) f, \. ~- i // their step rule. This has the effect of removing any# B' g! D, m3 K* t& v0 p+ a
// systematic bias in the iteration throught the heatbug
6 D9 b4 _. u( x // list from timestep to timestep" @- g& j: |3 c: ]
- W5 K" W, h! U, q // By default, all `createActionForEach' modelActions have
( [0 O9 N; W$ p9 Z // a default order of `Sequential', which means that the
. L6 Y4 l' `# ~: ~ // order of iteration through the `heatbugList' will be
$ ^" V: h8 \, I; o4 C# z) _3 @ // identical (assuming the list order is not changed
6 x+ x; H7 q5 H; j // indirectly by some other process).8 |6 H% c0 }- e( O5 I1 M
) N7 h0 \, j" O- M$ \" A modelActions = new ActionGroupImpl (getZone ());
) {. }3 Z! d$ n% o% ]# |6 |8 Q% z3 d+ C" E% V
try {2 y2 S* ?1 u. G
modelActions.createActionTo$message
* _, o* v3 ^2 e5 l: q (heat, new Selector (heat.getClass (), "stepRule", false));) g* i! \8 @6 W0 O
} catch (Exception e) {( N4 U) q4 [7 I$ }
System.err.println ("Exception stepRule: " + e.getMessage ());# T" x6 z8 [ u! y! }4 k- B' k
}
' c; `; z2 N/ v( C! q! w" D( S9 Q3 t! F' ~; n; R6 A. F
try {
3 a: v t+ G, X, `+ Q @6 J Heatbug proto = (Heatbug) heatbugList.get (0);
# M5 ?% |2 [, J* O/ V5 C8 |5 ] Selector sel = ) F/ v: r$ h- k1 ]: V& |
new Selector (proto.getClass (), "heatbugStep", false);! w* M ]; a6 A, R
actionForEach =( k+ n' H! y- O* J
modelActions.createFActionForEachHomogeneous$call8 b+ z8 c/ C' t& j; e
(heatbugList,/ a! b* n) o9 S- G3 v
new FCallImpl (this, proto, sel,
) N3 e, ^* `5 u new FArgumentsImpl (this, sel)));4 k* B3 F: V% M. P5 T$ {0 }0 d. y
} catch (Exception e) {( j6 A9 s& U6 A
e.printStackTrace (System.err);
% a5 r7 ]; _) J' S# x6 L }
6 l6 ]. g0 H* z# l8 E" R $ p* k- N0 m& l5 m6 w/ e
syncUpdateOrder ();
- D+ f5 `5 ^# m0 A: v f
: f" e8 o" f" G7 [ try {
8 j( d! b5 M7 K4 P; b0 e* t8 D6 n modelActions.createActionTo$message
t) P4 J, B% D9 @ (heat, new Selector (heat.getClass (), "updateLattice", false));" I. e7 e/ C8 h( I4 D* ^) J2 G
} catch (Exception e) {' \4 \6 P7 N$ T! V
System.err.println("Exception updateLattice: " + e.getMessage ());* v+ [/ c" O9 i4 }0 h% a3 ~
}
; S1 J/ F) Y. n% i9 i0 A9 t D
# x: M7 v* T5 s // Then we create a schedule that executes the3 h! Q3 N# y# {' x1 {1 ]
// modelActions. modelActions is an ActionGroup, by itself it- ]% V) I& k# k
// has no notion of time. In order to have it executed in9 d' D- r/ A; ]" I! i
// time, we create a Schedule that says to use the' O$ |( C5 y5 c6 M d1 P
// modelActions ActionGroup at particular times. This3 H5 W; J9 z! K- m, B/ W
// schedule has a repeat interval of 1, it will loop every
3 M1 K$ S* ]5 ]) Y8 y9 l& [ // time step. The action is executed at time 0 relative to
1 Y O; `, C( c! N* I // the beginning of the loop.) C7 ]6 p/ y" B6 d9 L
2 y9 w: p- s7 C0 q1 q6 I // This is a simple schedule, with only one action that is2 u8 e" U1 }; B9 X& r* V8 h
// just repeated every time. See jmousetrap for more, A3 i A& I, p2 ]9 k
// complicated schedules.0 m/ a$ G u5 N% ^, t
& D2 a7 f' G/ ] modelSchedule = new ScheduleImpl (getZone (), 1);
$ ^% r, q; z1 t* r0 P modelSchedule.at$createAction (0, modelActions);
0 f9 j0 n3 W! F( O" \- z0 E 8 ~5 \2 b# g; T! p9 w- Q9 X4 K
return this;
: B8 l7 r/ p* U } |