HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" n( k) y& h. Q/ i
, Y2 Y3 ]8 y, W1 q4 _7 @8 G9 |, N5 L public Object buildActions () {
1 {, k3 c5 ?& Z6 k super.buildActions();3 s4 }% f( \+ o7 g
2 Q, t2 F! S8 I2 ^% z
// Create the list of simulation actions. We put these in6 P, k% }( ~" U5 `' g% m( V. r
// an action group, because we want these actions to be: }7 @5 @+ Z0 ~4 I* u6 s
// executed in a specific order, but these steps should! _. O( z% e6 N) N' y
// take no (simulated) time. The M(foo) means "The message/ E. j& y" `# o
// called <foo>". You can send a message To a particular
6 Y& m% h* W% W4 g( ]6 v2 g // object, or ForEach object in a collection.
! R+ ?1 {+ v( z, q3 p( Z" q
6 d8 O( o5 I. L/ g+ M // Note we update the heatspace in two phases: first run- L. E/ C+ `6 {2 z1 R
// diffusion, then run "updateWorld" to actually enact the$ o% ^6 ~! A B @
// changes the heatbugs have made. The ordering here is
% |6 o* A$ Z7 o( E( V) B8 t& t* j // significant!' Q4 |4 \1 L; o& g4 g4 C, U
3 ]9 O ~, B4 a: N4 K
// Note also, that with the additional
% a: }) [4 i y9 s5 E: {! w- P // `randomizeHeatbugUpdateOrder' Boolean flag we can
8 q7 m% w# @: S- |' J0 j. O( u // randomize the order in which the bugs actually run
6 N* h: G! g ?" [ // their step rule. This has the effect of removing any) L4 x/ R$ s8 \/ i7 n
// systematic bias in the iteration throught the heatbug
8 u3 s9 [) L8 i( M // list from timestep to timestep4 q* w- z! x- U/ y- s! A8 O
! }; f( J: N7 M; E! S! }, j5 K // By default, all `createActionForEach' modelActions have+ }( o0 y5 \4 c3 q$ N9 W
// a default order of `Sequential', which means that the2 z6 t2 z! o6 m l5 }. A
// order of iteration through the `heatbugList' will be
$ R7 y' N% T8 | // identical (assuming the list order is not changed/ w0 a8 T$ a; |; t& k9 A
// indirectly by some other process).
& g8 u9 v. N0 x' C y- v6 m 5 @/ F- ]" |7 k3 K3 Q+ o7 }
modelActions = new ActionGroupImpl (getZone ());5 [' E4 n2 L2 k7 f9 n0 F
% l! l% r- |6 v" f* X2 W. ]' N try {4 |; X) F4 l* G$ [
modelActions.createActionTo$message
* [* O9 l+ [; y) j7 C (heat, new Selector (heat.getClass (), "stepRule", false));
0 @" I& X; h2 @# U } catch (Exception e) {2 a; z* d* D4 Y# M9 E
System.err.println ("Exception stepRule: " + e.getMessage ());
$ H7 _6 D7 ]) w4 z% X. w }
/ h8 A5 Q. C: K2 A5 p; x; J" K6 n6 j$ K s7 {
try {
9 g$ S( E1 a6 ]: k9 l8 T. H+ ]) y Heatbug proto = (Heatbug) heatbugList.get (0);3 F$ g- @/ E# W$ P$ \- n3 K9 ?
Selector sel =
) [0 f( s8 U/ ^$ d new Selector (proto.getClass (), "heatbugStep", false);+ q! ~1 ]% [5 o
actionForEach =
( E; \7 h$ j. B modelActions.createFActionForEachHomogeneous$call
' ^% z# V% v' [# f! y (heatbugList,
: w% T6 Y4 U9 a# Z: f/ n% T) i new FCallImpl (this, proto, sel,
% w, |7 O6 y9 p# s9 p7 j7 L new FArgumentsImpl (this, sel)));! h1 c. S! |5 I. O8 t, b& B
} catch (Exception e) {( Z9 Y# K" q/ S- P% }, v- ?
e.printStackTrace (System.err);% s! ?* \1 L' M1 J
}8 n( J0 t$ M J; g$ C! V
: }8 u7 x. ~% J' a! R+ w$ ?
syncUpdateOrder ();
4 n) b1 D9 o# H+ u2 I' \
' n8 G5 o- }* I3 j; V" C( }0 C7 ` try {) F8 i9 F) e9 X0 B
modelActions.createActionTo$message
4 x# z& m3 D7 f# o( `; ~9 g) u (heat, new Selector (heat.getClass (), "updateLattice", false));
6 z( v, }/ m; h# k; t% ? } catch (Exception e) {5 V6 f2 }$ F( C: A
System.err.println("Exception updateLattice: " + e.getMessage ());
6 X, K6 @/ ]; G. T0 o3 v }
) F9 ]7 Q+ I6 F/ D3 a; F: i
: V( ~4 G+ Y/ z4 d' m* R! j // Then we create a schedule that executes the, Z& e4 G! ?* ?, h; _
// modelActions. modelActions is an ActionGroup, by itself it
* Z. Z+ ~" t5 A# {+ N( Y4 `- a // has no notion of time. In order to have it executed in
3 `" N9 O; D4 `: c // time, we create a Schedule that says to use the
# Q2 s) f2 ]% u: N5 H$ k // modelActions ActionGroup at particular times. This0 [% z& D1 ~, p& Y4 g
// schedule has a repeat interval of 1, it will loop every
- j: g. _6 @9 q/ d$ p // time step. The action is executed at time 0 relative to
R( K& Q& f, @4 @ // the beginning of the loop.1 {, x5 b3 E2 K
1 A# L) d. N5 @ // This is a simple schedule, with only one action that is
+ G6 f% J+ Q% V A" _0 s: m. ^ c0 T // just repeated every time. See jmousetrap for more
& @) q# Z' p) N$ n // complicated schedules.8 i2 x. T' l: N7 Y* M
( s( ?0 b, h% s. m. w* s* L
modelSchedule = new ScheduleImpl (getZone (), 1);8 y" x% [) A' x2 c
modelSchedule.at$createAction (0, modelActions);
0 W$ H3 E% O5 R1 ~ : |/ E3 Q6 n8 F( K
return this;4 T1 N$ M' ^" P& x
} |