HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 Z! c r- @" V# H8 l2 }( C
1 f& w, N i# F: X3 i$ J
public Object buildActions () {
6 {1 Z, E' i0 t# M* r. @& V super.buildActions();9 v5 D0 q6 T) P6 W5 t
$ [( X& x3 d, {7 i0 D5 a
// Create the list of simulation actions. We put these in1 v) ]. l4 h' m$ d0 o
// an action group, because we want these actions to be
, B4 O9 v$ g4 Z K/ j* |: S* o4 k // executed in a specific order, but these steps should/ S# e$ z/ E: L3 B; G
// take no (simulated) time. The M(foo) means "The message; x6 E: P7 _, }/ M3 r
// called <foo>". You can send a message To a particular$ K' b) l4 b" g$ ]
// object, or ForEach object in a collection.
, |+ G4 r" Z7 K# o% E 7 H4 }+ y+ Q) |' ?8 T* @
// Note we update the heatspace in two phases: first run
+ {* M$ H; ?3 D2 ` // diffusion, then run "updateWorld" to actually enact the0 a$ u8 R2 Q$ x5 u9 x8 ~
// changes the heatbugs have made. The ordering here is
1 o6 ^) z% |1 _5 X' Z: |, S/ B // significant!* _: U: q( d3 \+ w
5 w, X+ l/ W& t9 s // Note also, that with the additional
, S$ `6 U3 i9 ]$ d' z2 j% d // `randomizeHeatbugUpdateOrder' Boolean flag we can
1 ]8 |& K8 l5 q% g7 _" v% j- [ // randomize the order in which the bugs actually run
( H0 A- d( u' b3 F+ _1 C // their step rule. This has the effect of removing any
; L: ?6 u, O5 n6 x // systematic bias in the iteration throught the heatbug
- r. i8 U0 w3 e // list from timestep to timestep
+ g ^. f& d) w3 b2 t7 U" g3 m 7 G: \/ Z+ H c4 I! d' z
// By default, all `createActionForEach' modelActions have
% U4 l) g! |; Q/ x `# q // a default order of `Sequential', which means that the6 g7 P% N" x4 u
// order of iteration through the `heatbugList' will be
& o' U# W4 R9 P( I // identical (assuming the list order is not changed h' T: l8 A; U6 i
// indirectly by some other process).* Z! |' x$ k7 x+ b9 A; K
! `! c \6 ?; q$ y
modelActions = new ActionGroupImpl (getZone ());
1 G* K, H* u. i& B9 O8 q# G# o: T' T2 `2 u( ] H$ D; R
try {
+ `5 }* Z. b0 \4 J modelActions.createActionTo$message
) [% C" s& G* ?9 Y% h0 m$ y' j (heat, new Selector (heat.getClass (), "stepRule", false));0 N+ B7 k# [, A! z( v
} catch (Exception e) {
0 a- S& A8 Q* N& B System.err.println ("Exception stepRule: " + e.getMessage ());6 C' {! H2 c; E. m, ], F
}
0 u: |6 ^6 C8 u, k7 Y2 s' h- N9 e( j0 y
try {8 ^4 F5 B- m: l( T/ ?6 L& s
Heatbug proto = (Heatbug) heatbugList.get (0);4 w0 c _0 O8 M1 v4 @4 j: T
Selector sel = 5 s3 k4 o: J% C6 o8 n S
new Selector (proto.getClass (), "heatbugStep", false);6 L" r" L N; ?
actionForEach =4 A* W9 I; q" X/ {/ C! D; d2 Y
modelActions.createFActionForEachHomogeneous$call
5 x8 }, p# ]! ]$ K/ S! E: k (heatbugList,: A9 |/ d5 q* J+ `( s3 l
new FCallImpl (this, proto, sel,7 x5 i/ \! Q" }# i' Y, p2 g/ w
new FArgumentsImpl (this, sel)));
1 B8 K. }6 I* [( f& n1 Z( Z } catch (Exception e) {6 R7 h7 J" X4 v
e.printStackTrace (System.err);
' z7 v+ L& }5 p6 `6 Q9 t }
( h# M4 \2 \, C9 g s6 L - L) y# } I2 u# e" K
syncUpdateOrder ();
/ G0 ], n6 D. [; t4 ]4 k
5 Q3 k1 D/ \2 H$ X try {* |( ^( n! D% z# E) _" r' u
modelActions.createActionTo$message
9 L# @8 o1 a8 j0 Z5 O* A U8 [ (heat, new Selector (heat.getClass (), "updateLattice", false));
H6 b$ J" F% n5 F) P5 s* f( e% H } catch (Exception e) {
6 J- H; g0 ]6 U: i/ a' @ System.err.println("Exception updateLattice: " + e.getMessage ());$ T8 _- V- d+ v1 ^8 v, D4 B. w
}6 q+ S( h! P A4 g$ y) `) f) q2 P Y
5 c/ [5 ]1 I" j2 P" q
// Then we create a schedule that executes the
! d1 |" Y; Q; g2 H3 i: M // modelActions. modelActions is an ActionGroup, by itself it
, t' I, S _5 { E; [ // has no notion of time. In order to have it executed in
- g$ u: j6 c% Z0 S0 v4 J // time, we create a Schedule that says to use the0 | J$ k1 B+ f; c1 y
// modelActions ActionGroup at particular times. This/ _' `) V$ V- u
// schedule has a repeat interval of 1, it will loop every; w9 _4 W R* ^& n7 |4 w B H% d
// time step. The action is executed at time 0 relative to
. I( T) z* N9 H3 ` h& T& W t // the beginning of the loop.+ c0 N. q9 w) ~" S- d
9 F _4 ?6 p3 E // This is a simple schedule, with only one action that is2 s9 O5 Z5 r) S! k2 Q0 y/ r o2 F6 A
// just repeated every time. See jmousetrap for more, F9 o+ ^& a+ H1 u% a
// complicated schedules.5 N7 Q. j( Y% ]# _$ T! o& D6 h( `0 s
% V5 ~* D! A, U! m1 `
modelSchedule = new ScheduleImpl (getZone (), 1);: F3 | }4 ?4 E5 d
modelSchedule.at$createAction (0, modelActions);
, A+ ]( J7 X* L$ T" K & Z# }+ ?, w2 ^6 w0 I' J! `
return this;# r% e# r2 ?! _' `8 V4 Q
} |