HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
c$ c+ u' u1 _* T" g' C
9 G& F& \- v- | ~! ?3 e public Object buildActions () {. t, n2 {7 g: ]- \
super.buildActions();) K, Z& u+ o1 d7 |; N- W1 b
9 T' h, i2 E# h1 g8 w( q0 X: G
// Create the list of simulation actions. We put these in7 k! u6 \) v. r8 _
// an action group, because we want these actions to be
+ z* T8 l; |8 M% u E6 h* C+ k1 d // executed in a specific order, but these steps should% y4 Q1 e6 b8 g l! Z R
// take no (simulated) time. The M(foo) means "The message" H! i5 b" K: I
// called <foo>". You can send a message To a particular7 _( B# K& E( o, _- _7 l9 J& F% R
// object, or ForEach object in a collection.* u. g, D$ M/ l8 n
- t" D1 L, ~: G o
// Note we update the heatspace in two phases: first run
8 u: e* R. p) a" J# _: Q // diffusion, then run "updateWorld" to actually enact the
2 d; O, m h4 D4 W8 x0 i* [4 m // changes the heatbugs have made. The ordering here is
; z/ o( H% E" w1 Z' g$ ^ // significant!- k+ R' ^3 I3 T* K, v
, ]7 c, `" @3 x7 ?
// Note also, that with the additional
( Y6 d/ Y( D! j1 X, m // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ \3 C5 A: K+ _3 w1 U- s // randomize the order in which the bugs actually run$ s: G9 Y8 P0 g4 K
// their step rule. This has the effect of removing any
7 F5 C2 {/ P( r/ e // systematic bias in the iteration throught the heatbug
& T+ k# L# `4 V* k9 w n M // list from timestep to timestep
3 r7 h& Q' K* b/ d/ m' D
7 N& g5 ~' V5 H4 m+ @ // By default, all `createActionForEach' modelActions have
1 a# T# j- y5 P( l% Z' S$ I: | // a default order of `Sequential', which means that the/ R0 a+ c7 R4 t- F/ }
// order of iteration through the `heatbugList' will be2 p* K+ ?9 q9 N
// identical (assuming the list order is not changed+ }) P$ p. J: B8 p
// indirectly by some other process).
9 j6 m& g7 E& [* w4 u & O, v. ~5 J3 q" s% S& n% l& r( e1 G
modelActions = new ActionGroupImpl (getZone ());7 H8 ]' Y4 w: W b! k
! A- j0 \+ d: ]% ?3 i
try {% h8 S! r& t: X" Z" Q5 P* V
modelActions.createActionTo$message
7 ~1 y+ S4 c% l/ \; E9 g (heat, new Selector (heat.getClass (), "stepRule", false));" t" @; u+ s- K [1 k0 p2 p; O5 p8 g2 n
} catch (Exception e) {
Y6 R' o- `1 V, h System.err.println ("Exception stepRule: " + e.getMessage ());
R/ P w0 E2 E, Y# y( d }* V0 M# y" D; x% r+ b8 K, x# `
$ d% V5 B( I# t' _" Z) h0 x* \
try {2 I3 S! |2 q8 s' T
Heatbug proto = (Heatbug) heatbugList.get (0);7 W1 u2 T# p# g- ~1 g
Selector sel =
, P2 S& q2 K7 q3 K; L new Selector (proto.getClass (), "heatbugStep", false);9 P. s( [, l$ x2 t+ \% z8 U0 M
actionForEach =+ H8 f: O/ d3 H
modelActions.createFActionForEachHomogeneous$call
+ W n4 ?! v0 v4 @5 j (heatbugList,! @% o# F, _' t
new FCallImpl (this, proto, sel,2 i7 X2 i/ Q/ [3 Y% D( ?
new FArgumentsImpl (this, sel)));9 c+ T6 T% |" K' s
} catch (Exception e) {; { \1 u+ w4 q2 F+ L( G$ a
e.printStackTrace (System.err);
1 c P" T0 N+ ]$ y' M }
0 M6 f0 ]7 l( Y3 O* p0 c
# l: O! E" L# J% Y! t syncUpdateOrder ();' v, y* S/ n/ m0 h1 x
0 {0 E/ d4 L/ z5 D
try {
: g2 ~6 j1 ` x9 l8 r modelActions.createActionTo$message
$ X6 p; r/ f. H9 j (heat, new Selector (heat.getClass (), "updateLattice", false));2 S( R1 t2 [) x* Z# {
} catch (Exception e) {
4 O( o7 L4 P% r& G, e, M System.err.println("Exception updateLattice: " + e.getMessage ());
& J1 T5 M8 G' X% X$ I- \& Y( z }
9 S$ k; b8 d' }1 n$ n
1 q7 g# Z" `/ W; z- \% O& d // Then we create a schedule that executes the3 s& a8 E0 ~# i0 _/ I
// modelActions. modelActions is an ActionGroup, by itself it; u7 b! K. u" h9 V* H3 _' Q6 n
// has no notion of time. In order to have it executed in7 g- |$ B: d- i" L* H5 |1 V+ [3 f0 K7 O
// time, we create a Schedule that says to use the% b f5 [; v& M+ _. G" Y% ]% e
// modelActions ActionGroup at particular times. This3 _ O2 U& s) i& V3 L8 _2 J
// schedule has a repeat interval of 1, it will loop every
; b# v8 Q: n# A" y9 R! I // time step. The action is executed at time 0 relative to. {4 h( k1 V! @7 B( F# t8 s
// the beginning of the loop.' l$ I7 K5 }4 D/ N% T; B. i" r8 F0 x
$ A; u2 B7 W/ L t( g! y# A
// This is a simple schedule, with only one action that is: j) P9 g/ @& e8 R- A8 c2 E+ T
// just repeated every time. See jmousetrap for more! {2 ]4 ?$ `% ]
// complicated schedules.! g3 d/ X1 x# s; M2 {
% H8 ~2 J4 d" T. h2 t
modelSchedule = new ScheduleImpl (getZone (), 1);
. E" H$ J* D0 s modelSchedule.at$createAction (0, modelActions);0 D' c5 w" ?% b
& E3 ?& X) l) V. |* x: s( \0 Q
return this;
3 O4 V$ b$ W) ^4 K1 I1 t( F- {- H2 M } |