HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 M4 u3 F, w; T; O9 _/ ~/ L1 z3 r2 t
# s2 M0 q; n$ v public Object buildActions () {
& W: k& a: b( ~. F# @( x, e/ r4 T super.buildActions();
4 e3 K# ?; Q1 K- R/ r8 ?. Y % S3 b; _/ w/ n0 ?
// Create the list of simulation actions. We put these in
2 V8 R6 A+ J& }& o8 j6 o // an action group, because we want these actions to be
4 ~: ?; ~0 Z! b+ t! a // executed in a specific order, but these steps should9 W: y! d$ z( P9 p' x& c( i
// take no (simulated) time. The M(foo) means "The message
# P7 X" O8 I, c // called <foo>". You can send a message To a particular* l' u& F! T3 T _* `5 |1 a
// object, or ForEach object in a collection.5 O6 w) }, t( A& Z
) J) B2 r5 t! K" f& l/ M$ a
// Note we update the heatspace in two phases: first run
( N$ M8 }+ `# m4 Q) P/ V. N // diffusion, then run "updateWorld" to actually enact the' N# i( p: y+ e& D! B# g1 t
// changes the heatbugs have made. The ordering here is$ ` q" P$ h5 T- k! ^3 @; o! z, ]
// significant!; ^, j Z9 H# ~+ g, u
; _' Z$ l% ?3 m+ }) E9 _1 ^' B; z, ^ // Note also, that with the additional0 h- n _& z( C. r
// `randomizeHeatbugUpdateOrder' Boolean flag we can
F- I7 ?8 R7 k/ a // randomize the order in which the bugs actually run4 }# I4 s1 p5 j9 [/ b* I; u
// their step rule. This has the effect of removing any
) x3 m# m i+ \/ ]8 S // systematic bias in the iteration throught the heatbug9 J/ |* g. D# M, M; m x% j
// list from timestep to timestep R! y" _! R# |/ J* J
' k. c8 D i, B: ?( o
// By default, all `createActionForEach' modelActions have
- `2 G; C' N& I* l, u3 ?* L // a default order of `Sequential', which means that the4 r7 P, c4 \$ X7 f% b
// order of iteration through the `heatbugList' will be8 N, t7 G+ R q$ i3 V
// identical (assuming the list order is not changed0 `% a" Q$ D. B* ]+ Y+ O* L
// indirectly by some other process).
, U, C% o4 d6 {; z! d L( G 3 V# N# v& N) j
modelActions = new ActionGroupImpl (getZone ());
7 R1 P o8 T& }8 |4 |
+ T2 C) {+ e8 s try {$ c8 L; F2 P& \! [
modelActions.createActionTo$message
7 o4 T: |9 O7 u& z3 S (heat, new Selector (heat.getClass (), "stepRule", false));" O1 I( \7 x3 y
} catch (Exception e) {& n7 q# n8 M% p; v/ K* b8 x( e7 J0 k) F
System.err.println ("Exception stepRule: " + e.getMessage ());
( I/ ^( b& d: l" s$ l8 N }- Q- ^+ r1 L6 M
2 ]$ [" C9 Q& I7 k. S
try {
7 W0 @. t4 _' T1 {5 E% I Heatbug proto = (Heatbug) heatbugList.get (0);
8 C0 G% N" ~. \. Q- w: e Selector sel = & M3 }2 o8 f8 g. A$ r: V7 @
new Selector (proto.getClass (), "heatbugStep", false);$ u5 r H7 C# S& n
actionForEach =! p, m# v" [# K
modelActions.createFActionForEachHomogeneous$call
: n- A+ a: {: p% W' M( y (heatbugList,
8 G$ w4 q6 r0 i, U new FCallImpl (this, proto, sel,
5 i; D. k* |+ h6 \1 T4 ^ new FArgumentsImpl (this, sel)));
8 Z! _: c J3 t# e: [' k } catch (Exception e) {
5 Q9 ~% ?6 k% Y( r) \& M7 Q e.printStackTrace (System.err);
1 m3 d$ ~ ^' F& _4 l }
9 A/ J/ Z8 H) {$ r$ l1 U. ` * {2 Z6 t7 L# X U
syncUpdateOrder ();
9 `4 `% p( X/ _" M$ j" j& Z h. T, s# J, C$ T q5 E6 M' K) n: T
try {' I' i- ~1 @3 Y
modelActions.createActionTo$message 5 g9 e+ e/ Q9 y) n: k' M8 q
(heat, new Selector (heat.getClass (), "updateLattice", false));
! L5 W# S$ x% W, M z6 U } catch (Exception e) {, u6 {5 s$ { }
System.err.println("Exception updateLattice: " + e.getMessage ());
$ j B" Q' C& r8 R! m: u }
( r4 [5 S% f) Q+ G8 \" E. @8 M
6 o9 I0 G r H& @7 S // Then we create a schedule that executes the$ x- J9 T) L( L" x. R8 g
// modelActions. modelActions is an ActionGroup, by itself it8 V' p' ]7 k9 B; e( w- y
// has no notion of time. In order to have it executed in7 S' P7 @9 u% J6 X0 v
// time, we create a Schedule that says to use the
6 G8 K% i* J3 `3 a: N // modelActions ActionGroup at particular times. This5 Z0 G! h8 M% G. |4 [
// schedule has a repeat interval of 1, it will loop every
0 D1 J& i! z8 S3 M // time step. The action is executed at time 0 relative to
6 ?9 d3 V0 x7 v% S2 s: r8 ?! y- Y // the beginning of the loop.; t, e% H3 H1 ~- ?* P8 s5 |4 S; B8 ~
7 j9 z' d# G1 d, s0 ~* k // This is a simple schedule, with only one action that is) a# s0 T# M9 ~0 B" S& t v
// just repeated every time. See jmousetrap for more: W, O" ^. u) P; c6 E
// complicated schedules.
! C5 u8 l8 ~5 a0 U ^; m( j5 y
6 w, O7 s; w8 C' f/ J; ?& Z modelSchedule = new ScheduleImpl (getZone (), 1);8 i* f) ^1 X' l
modelSchedule.at$createAction (0, modelActions);
/ Y1 D2 N7 V! W p* p) X: Y: z + t$ v C- v7 u1 A0 i0 ]9 |7 y
return this;
$ e* i4 I# Q* M) M } |