HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' I" C$ x8 d; q' y+ [
/ N6 W$ {, v4 V$ Q/ |, n+ q public Object buildActions () {6 |6 U' K2 Q8 k I1 V
super.buildActions();
6 S7 {$ j! T/ i. [4 h( u) p 1 W1 M- @. L3 c$ S
// Create the list of simulation actions. We put these in& V0 J2 K2 K- R
// an action group, because we want these actions to be" T5 z. u, V1 X3 A' k
// executed in a specific order, but these steps should
) _$ ]7 Y; I H$ ~ // take no (simulated) time. The M(foo) means "The message
8 N7 ^) L% l% {0 P9 ^( F# Z! C // called <foo>". You can send a message To a particular* a0 R4 P1 [/ Y/ ?) a
// object, or ForEach object in a collection.
4 e2 N O( |6 I5 u! D2 q! |9 S 8 f9 D. Z6 K4 H( U7 x' B
// Note we update the heatspace in two phases: first run) j) F% z2 M: K& E
// diffusion, then run "updateWorld" to actually enact the
5 C/ K( m1 `& ]8 M9 Y // changes the heatbugs have made. The ordering here is# Z+ @4 e6 ^, G6 G: ?4 F0 G% M
// significant!
6 b2 _* e; c* n# G5 t- P3 d: | ( v7 I9 t8 Q5 V$ |, R0 h* \
// Note also, that with the additional
, ?) d& y1 G1 I; j: J2 m // `randomizeHeatbugUpdateOrder' Boolean flag we can. O0 l; O& J1 P6 N4 f7 z" h
// randomize the order in which the bugs actually run
* w- a; q% R) c- ^4 \' b/ b3 I // their step rule. This has the effect of removing any/ P& }+ p! h0 x# v0 x
// systematic bias in the iteration throught the heatbug
* _5 Q L1 B/ s" n& B; L1 S+ P // list from timestep to timestep
& E; D3 x& d( A/ c+ _
" V+ {7 ^ d2 m2 h9 Y/ b4 X8 ? // By default, all `createActionForEach' modelActions have
! i: J) w5 p+ ] // a default order of `Sequential', which means that the
J$ l4 ]! m: I+ T& S& r // order of iteration through the `heatbugList' will be, r0 f& O# c1 Y- p( D% b
// identical (assuming the list order is not changed/ d7 Z" _ H9 x
// indirectly by some other process).
4 U) F5 l( }& D6 |& G$ M) [, u, \ ! a8 w B3 ?. L% N! B5 c) h+ V
modelActions = new ActionGroupImpl (getZone ());
! K, z, a5 ^' C3 V+ w) }5 I# Y% e( P+ c* |- h C: M( F
try {
0 Z+ G4 Q: u; b2 Q; k& u modelActions.createActionTo$message5 `+ W3 g, a9 y0 N( C; a
(heat, new Selector (heat.getClass (), "stepRule", false));
E5 P# w: W W2 A, R } catch (Exception e) {0 _" y A9 s) l' O
System.err.println ("Exception stepRule: " + e.getMessage ());6 a+ F$ u4 S" l/ p! r
}* H8 ^8 w- r, W7 ^
( ^* B% K3 S+ q* H- c* {: q
try {
/ Y, m- R, I! W: J3 O: S Heatbug proto = (Heatbug) heatbugList.get (0);
4 O& Y- j+ K( @3 A Selector sel =
5 u+ \+ C0 @4 h) i new Selector (proto.getClass (), "heatbugStep", false);( Z& [8 w. e( w
actionForEach =
5 d8 t- v& L* T) h# K modelActions.createFActionForEachHomogeneous$call
; I. D. k- Z3 Z (heatbugList,
0 ~ o' i. G2 @ new FCallImpl (this, proto, sel,9 i, D p3 Q3 b8 I, O
new FArgumentsImpl (this, sel)));
6 N6 U5 I8 ]; `1 c } catch (Exception e) {8 T6 M' t, z/ T Q
e.printStackTrace (System.err);
9 s9 D" o/ A. e7 W }; k! C# C- n1 z' L0 E% _
Q c8 s$ r# i3 S( E" y
syncUpdateOrder ();$ X$ q( t2 Y2 }1 v6 ~
: { t: n( l( x( V4 }( J1 o
try {3 g6 S3 v- r l' I; t) V
modelActions.createActionTo$message
8 x1 u0 x( _- h: Y& Y; k (heat, new Selector (heat.getClass (), "updateLattice", false));0 P! F; Z% j; ^( v. E9 U
} catch (Exception e) {
3 N$ R% M/ S9 x9 g) {3 D1 } System.err.println("Exception updateLattice: " + e.getMessage ());
* d) I: u6 Q2 z* [- ]6 Y }( S: H, e6 \$ {( a
0 V Y; n1 v( c% Y) }# E, a
// Then we create a schedule that executes the0 i+ e0 u2 r- }: p+ z" o( j4 y% G
// modelActions. modelActions is an ActionGroup, by itself it
4 V! @& j' m' u: n* I. w // has no notion of time. In order to have it executed in L! i/ h* F2 W/ v# F+ s
// time, we create a Schedule that says to use the& b8 C3 X& P4 e- |/ y; T
// modelActions ActionGroup at particular times. This& P- u6 {$ b' L7 p+ w
// schedule has a repeat interval of 1, it will loop every" `: W9 h8 t0 l' f9 D, \* w e3 m$ F
// time step. The action is executed at time 0 relative to
1 n0 G/ F/ ^. H7 Z* k6 ? // the beginning of the loop.
$ n# A! E3 X5 H( ?+ C9 }. X
4 G. P, D/ z' ]% o7 ~5 t // This is a simple schedule, with only one action that is
4 \: ]0 y3 y; M8 _, O! M, l // just repeated every time. See jmousetrap for more
' [* k9 N3 F3 _- b b4 R* H // complicated schedules.
+ D6 r5 X5 ^: `/ A; n
! ]8 F& V2 w: U) D. H4 L o modelSchedule = new ScheduleImpl (getZone (), 1);$ j( G5 e4 R6 e
modelSchedule.at$createAction (0, modelActions);! V: K, f2 F9 S
" L% U. v0 c$ [ return this;
( [/ D1 C3 c# Y& V/ m! s } |