HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- B. c2 F! X! |7 ]
& X- @9 i) A J$ r7 G public Object buildActions () { i' Y- s; D, v7 W
super.buildActions();" x* b5 b* u9 h( _$ N3 n5 d
8 m9 ?2 s l$ {* Q2 N
// Create the list of simulation actions. We put these in, T' ^' \: p8 X' Q
// an action group, because we want these actions to be5 O0 r3 K5 x% P! z; s- ?
// executed in a specific order, but these steps should1 U$ T) V9 q- D* K2 O# E9 M
// take no (simulated) time. The M(foo) means "The message
, V9 \$ M) f1 Y5 b# {4 | // called <foo>". You can send a message To a particular
5 j1 Y- ~2 t+ Z e% J; w // object, or ForEach object in a collection.
1 N# e) H7 D' q+ m/ q5 B ) k" u y/ _; `. g$ J
// Note we update the heatspace in two phases: first run
$ L% J; h# d# j; ~$ Q! `+ }- Z* r // diffusion, then run "updateWorld" to actually enact the1 }3 s0 |6 _! Z4 K
// changes the heatbugs have made. The ordering here is
* l6 m( N2 k7 T: { _ // significant!
2 d% }% T, Z$ c. `
( k7 Y' G8 V m+ i- Q // Note also, that with the additional" w4 P' `4 Q9 S: d1 |7 m8 a
// `randomizeHeatbugUpdateOrder' Boolean flag we can- w# l F% T" o( k9 ~4 B
// randomize the order in which the bugs actually run/ ~ M) I9 v# A9 V: ^( b
// their step rule. This has the effect of removing any
, T/ _. L0 C5 Q // systematic bias in the iteration throught the heatbug
) A z6 C: v7 M& u( e3 a* U6 v) g // list from timestep to timestep3 z+ Y! Z& @8 S) L2 o* w
$ L3 U- a3 c" x0 T% C7 v, e
// By default, all `createActionForEach' modelActions have
* P" C$ h# ]4 R( Z( w# g: O // a default order of `Sequential', which means that the" W0 q0 q% P7 x( \. }
// order of iteration through the `heatbugList' will be z5 |( `) N* f$ s+ y
// identical (assuming the list order is not changed
6 r- y& g& z1 _( W: `) E2 i // indirectly by some other process).+ Z2 I" s& Z6 [! d1 D& H/ z9 I( Y
9 ~9 N2 o% t8 B) e
modelActions = new ActionGroupImpl (getZone ());/ \" j! @, j% y3 K4 b3 U
, ^6 E- b- `' f
try {' y5 i' Z6 {; y ?% J# o
modelActions.createActionTo$message
5 j% p7 Y( k6 v, n9 Q1 d0 E" l' s( E (heat, new Selector (heat.getClass (), "stepRule", false));9 S, h' w- n U5 a R) @! ~4 e! ]
} catch (Exception e) { U: I: p6 F. T3 `5 g
System.err.println ("Exception stepRule: " + e.getMessage ());
+ a% L6 g4 [1 U; |/ [: N7 Y [ }
4 f" X5 r" l/ a& M* e! }4 I8 A
' m( f& S7 E9 Y7 _+ w" W. u+ \3 {1 f$ H try {
2 E& V6 Z6 v# q x. M' b; D Heatbug proto = (Heatbug) heatbugList.get (0);5 T: y8 x9 i2 i/ u! o/ x; H
Selector sel =
5 F) {" ? ]6 h9 n new Selector (proto.getClass (), "heatbugStep", false);$ m* R4 n% Q- @7 v" j( g2 X6 @
actionForEach =' `& J$ e) \3 B; j8 c/ @$ P
modelActions.createFActionForEachHomogeneous$call
% V- d% J! L: i5 ?3 d) { (heatbugList,4 E& x( b1 }& |
new FCallImpl (this, proto, sel,) H% w7 H% Y# [7 }/ q
new FArgumentsImpl (this, sel)));
8 N5 D' R8 b' z: J! b9 W; b } catch (Exception e) {4 g! C* i% m$ [6 p' W+ G
e.printStackTrace (System.err);
/ a- h* \2 y0 g+ r2 G2 M: s( m }
" T2 n2 @# V2 I7 k) m7 Y$ C0 c & B# n0 s& ^9 k6 u' H
syncUpdateOrder ();
. W, y& j2 j, y0 I4 s. `7 {3 M+ I/ N7 K" K3 x4 `: y
try {
3 x: N% z( }/ t. e; J0 S$ t0 f modelActions.createActionTo$message
" ]$ @" i* K$ l/ s; f. g3 j! t7 f (heat, new Selector (heat.getClass (), "updateLattice", false));
; Z% a2 M0 x/ j& X" R. J6 O } catch (Exception e) {; e2 z- M" q# z( u1 U, f1 `
System.err.println("Exception updateLattice: " + e.getMessage ());
2 p' N5 n" E. y$ p& {* H( K }7 ]; r. L( X* I9 B
* `( K& C* e1 |5 t3 y2 ~9 V/ p
// Then we create a schedule that executes the
9 @3 z$ s4 }1 S& e' `. P // modelActions. modelActions is an ActionGroup, by itself it
4 ^7 L) L1 E6 k& n$ q // has no notion of time. In order to have it executed in+ m' D; o' Y d3 y! Q
// time, we create a Schedule that says to use the# l. W/ ^, K% g+ l. x- u
// modelActions ActionGroup at particular times. This
8 e X* E" B' A- ` // schedule has a repeat interval of 1, it will loop every
( Y) C$ y- H% d: V' V, W // time step. The action is executed at time 0 relative to
8 m7 T3 G; n3 Y* B* C; p // the beginning of the loop.
. o. ^( A* ]- v# L
' D1 ~0 R0 ~! o, H7 g // This is a simple schedule, with only one action that is9 ?# r2 m3 u# M) S* I2 x
// just repeated every time. See jmousetrap for more. p* B& a+ C* o# x
// complicated schedules.' Z3 s& m8 B8 \) u
0 O8 y; N; ?* Y& L
modelSchedule = new ScheduleImpl (getZone (), 1);
0 \- w* s& B/ a& Y+ u1 D modelSchedule.at$createAction (0, modelActions);
( x4 G3 J2 e! c4 j' d
( g3 J$ n7 A7 C8 F4 p+ U. T return this;, J' J2 n3 D/ p/ i0 D
} |