HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* t* W' j) D9 T1 z8 X& U$ |; D& c( h; [, U
public Object buildActions () {* Q! t- ?/ M4 j) O6 c" H& j
super.buildActions();
, Y5 S8 w$ l) @ ! _; {! X9 v1 g* g$ g" n
// Create the list of simulation actions. We put these in
+ Z' v/ } i% d3 F8 R- {" o // an action group, because we want these actions to be* R5 Y" X/ _3 B- m
// executed in a specific order, but these steps should
- U8 K& y. H+ b( Y5 p& t) Q! E0 O // take no (simulated) time. The M(foo) means "The message. V9 ^9 M3 v" O- u3 A2 t- X9 L
// called <foo>". You can send a message To a particular
5 A$ [1 t8 ]" X // object, or ForEach object in a collection.1 e2 e& ~' V! ]; J6 d, S4 Y
) ^& q* W0 }& Q/ O0 X2 g2 i
// Note we update the heatspace in two phases: first run5 D( x4 _4 _& _) F
// diffusion, then run "updateWorld" to actually enact the
4 S) W# N: O5 G- c8 h // changes the heatbugs have made. The ordering here is
5 h Y+ s! \% W H4 K // significant!
2 \" u8 E6 v8 J6 q
$ \$ C- [8 d ` U8 Y6 O // Note also, that with the additional
* Q+ o0 {- Z' U1 J1 Q // `randomizeHeatbugUpdateOrder' Boolean flag we can
$ g1 r" O& p) D5 g2 F3 Q // randomize the order in which the bugs actually run
. `4 P: v9 H4 g6 R, r6 m // their step rule. This has the effect of removing any& b7 C2 h' g* L' ^, N" D; ~, ~
// systematic bias in the iteration throught the heatbug8 N- z. W, k& a8 {( ~# @+ i
// list from timestep to timestep$ K! [6 p3 h0 [1 D
* m8 V, [" t9 P0 X0 k7 |
// By default, all `createActionForEach' modelActions have
0 j* v% {4 o( P0 C4 L! l. u7 I* @% } // a default order of `Sequential', which means that the
6 J+ J7 {. G2 r. n // order of iteration through the `heatbugList' will be l2 `7 }9 h6 i. M3 l6 c* w/ S
// identical (assuming the list order is not changed
7 y& w4 C0 ?) Z( O, P // indirectly by some other process)., D$ R: O0 p& t+ E) {
& B( W' n* v/ O) ^) G0 ~
modelActions = new ActionGroupImpl (getZone ());7 H6 \6 A" Y. c6 U ]; A/ |$ p
* W( g) k% q# ^0 Q4 v* G2 d try {" X$ x! V: T# T+ s& ?* H
modelActions.createActionTo$message
7 b5 x/ P! H2 {$ J& b3 P- i (heat, new Selector (heat.getClass (), "stepRule", false));- _& j4 ~+ ` _
} catch (Exception e) {, [ D- M$ j2 ]3 [4 ~
System.err.println ("Exception stepRule: " + e.getMessage ());6 _/ g* f8 U! q0 A" B# c! H
}
0 x* f' H. r) P, u/ }
7 G+ y! a: ^! @. J% A# u8 Y8 T try {
|7 j5 c9 T }3 M& i6 G1 y+ \! p Heatbug proto = (Heatbug) heatbugList.get (0);) B0 B& ?+ m+ I7 V8 D3 U
Selector sel =
+ L1 |" J) E! s5 o ? q new Selector (proto.getClass (), "heatbugStep", false);; ~) _8 k( o* f! _
actionForEach =. K1 @4 R9 W i3 J( b# ?
modelActions.createFActionForEachHomogeneous$call& l- J; i+ t# B: E: t2 Y" O8 }
(heatbugList,# r4 d3 I: K a. h% ^/ [0 ]# h9 |5 ?
new FCallImpl (this, proto, sel,
$ G8 T2 B: F2 N+ e new FArgumentsImpl (this, sel)));7 f. Q; I" P, O/ a
} catch (Exception e) {1 t, A) b2 x6 r& U
e.printStackTrace (System.err);
5 b: ]- u, \2 P% H7 Y }) G- T0 d+ V) g1 @4 f, B
# V! t X9 T6 B& _
syncUpdateOrder ();
1 E$ Z$ t( _; T( Q. w' e, p5 q4 n' i2 G+ r( C, ^$ n1 K0 u# ?
try {' x' \6 U8 W% ]! }7 {! F
modelActions.createActionTo$message - W5 d! o; h! e2 u& l* w
(heat, new Selector (heat.getClass (), "updateLattice", false));6 |/ k6 [$ Q# p8 _% n
} catch (Exception e) {
0 X" z; v0 t( x! K System.err.println("Exception updateLattice: " + e.getMessage ());# e8 L: Q3 d4 b
}
+ b+ S9 w" ^, f8 |; s L1 ~" A# E; E3 T
// Then we create a schedule that executes the
1 |7 a. l& B- G7 O // modelActions. modelActions is an ActionGroup, by itself it$ A. Q) s* h s# k
// has no notion of time. In order to have it executed in; X7 S1 ], Q3 A! ^- a3 }! p3 T
// time, we create a Schedule that says to use the5 T* n" F6 Q4 F( y: J" G
// modelActions ActionGroup at particular times. This0 W: j7 u! o" v1 Y a. D; H$ h0 n
// schedule has a repeat interval of 1, it will loop every
! F5 i0 n5 o# N* d. r" d // time step. The action is executed at time 0 relative to
0 S" B' l' }& P( `5 D // the beginning of the loop.# ~, i& N; e/ m2 p
1 K% c4 W0 w0 R9 D$ ?/ b$ j // This is a simple schedule, with only one action that is
% l; `! o' d8 g- l; }' t // just repeated every time. See jmousetrap for more8 P( c" [( k; _6 }: L) R
// complicated schedules.
/ g2 `9 u i; W8 n$ b, B+ ~0 |7 D
2 n Z. H3 S; M% x8 I+ T: J; e6 Q modelSchedule = new ScheduleImpl (getZone (), 1);3 c5 v( ?$ I7 e* E6 s2 w' K B/ u
modelSchedule.at$createAction (0, modelActions);& z9 M( D" W4 }6 X i/ d
! }$ ?* n* i5 N; i; x0 [3 L return this;) |% I. ~, ^( b. y; p" c
} |