HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ U. ^+ [7 C: r/ J1 M9 _4 c! M/ W/ n% i/ p2 L6 f8 w% k$ z
public Object buildActions () {- V! J, v# z; S7 M0 ^1 r. Q+ G
super.buildActions();
: s e! H, X( R- ~+ l. g % T4 b0 M& S2 v# V" e! z
// Create the list of simulation actions. We put these in2 o1 g5 r0 a" a$ q
// an action group, because we want these actions to be
$ ^" h5 ^5 r" i% h( k // executed in a specific order, but these steps should
4 b3 e+ \/ s& j. x: u% }& K // take no (simulated) time. The M(foo) means "The message& S1 d3 r4 a" I; v4 m) Y r9 Y
// called <foo>". You can send a message To a particular8 C% n* N/ ^* } y2 |
// object, or ForEach object in a collection.0 g$ v. I+ U* U. m5 d
: M$ A1 ]# m9 Y1 p- E6 ]) i // Note we update the heatspace in two phases: first run
' G, N8 w& y2 n3 { x# ~" m // diffusion, then run "updateWorld" to actually enact the
( r/ H2 R- ~5 f5 ^ // changes the heatbugs have made. The ordering here is
6 l1 h, d* O3 O. z // significant!9 J, |: _. N- ?. C: x
6 S5 W% C6 y! @- g' z
// Note also, that with the additional M8 P1 a" C& E, f1 ~! X$ V; H7 `
// `randomizeHeatbugUpdateOrder' Boolean flag we can
8 X/ u2 o: a1 e: n+ s6 y // randomize the order in which the bugs actually run; g& @1 G) y8 s2 \; @2 J
// their step rule. This has the effect of removing any
6 N! j) y4 X2 a, u7 D4 {* _ // systematic bias in the iteration throught the heatbug/ t# @! ^8 O8 w
// list from timestep to timestep8 y' R9 h' Q- {% F
" K: A* f! D" Q& f! Z" u9 g* k // By default, all `createActionForEach' modelActions have- |7 k# u. E: \$ R) \0 Q
// a default order of `Sequential', which means that the
) E( k7 S) Q/ h3 l // order of iteration through the `heatbugList' will be
* Q- P( A( W7 l% n- L9 F // identical (assuming the list order is not changed
* Q6 o* _5 g# w7 Z a; V // indirectly by some other process).$ V3 I2 q6 Q! l) w3 R
: o; U% q* i( l modelActions = new ActionGroupImpl (getZone ());, i; {9 g# f; M' C9 S
5 k' V) W ^6 G' H2 O
try {- Z8 i* [2 r6 d' Z
modelActions.createActionTo$message' X# s5 M& o2 A8 W* B5 a
(heat, new Selector (heat.getClass (), "stepRule", false));
% c7 @5 C( t% Q% A1 R; P } catch (Exception e) {
2 l4 W3 ~+ w7 i; D System.err.println ("Exception stepRule: " + e.getMessage ());$ U7 v$ k# d: G. n( p2 A) W
}
) a9 B* L- x, A4 M6 t
) X. C: y; }- T4 p/ w try {
- H; s* F7 J9 m9 D: p Heatbug proto = (Heatbug) heatbugList.get (0);
/ U8 d, W" h+ c Selector sel =
5 R* ^ k+ j/ A0 q( E new Selector (proto.getClass (), "heatbugStep", false);
* g+ ^. B4 [ P6 V! K actionForEach =
; l; o: _ X; f- X2 C2 G' m modelActions.createFActionForEachHomogeneous$call7 x# _2 M2 }. ^ L7 C
(heatbugList,* L9 B; M" n1 Y/ v8 D/ |. e; H
new FCallImpl (this, proto, sel,9 A7 H; k- }! b8 [
new FArgumentsImpl (this, sel)));
3 r' t5 @6 |; q% c8 H: _" Q' b } catch (Exception e) {+ l) Y! A* `/ a z- Z' q# A4 x
e.printStackTrace (System.err);
, n% ^8 U) y! P- S- | }+ Q4 G) O" P+ }. z% w& B
% v5 i- d# P, o
syncUpdateOrder ();
4 d6 L1 J+ e5 j& T/ s- E# c2 u: [0 r# h8 l) n. n1 {7 s- ?2 b7 v- p
try {
; I6 N5 d2 q; Y2 u+ u5 B; t modelActions.createActionTo$message
) N- Z6 ?7 P' S) ?3 ^( ?" D2 x (heat, new Selector (heat.getClass (), "updateLattice", false));
" V3 g' F5 g- t6 w1 v& ? } catch (Exception e) {$ `% _( I( ~0 @" i: W$ h
System.err.println("Exception updateLattice: " + e.getMessage ());
( v1 I8 N. z+ X: w* l }
+ u: [9 k& l6 F5 v4 p3 g
+ A- C) Q/ `! X. r5 [: m( W // Then we create a schedule that executes the
& h# o0 b% w2 a8 X* `# c/ X // modelActions. modelActions is an ActionGroup, by itself it
! y y5 ~1 p* ?4 H/ C" C& O6 \ // has no notion of time. In order to have it executed in, ]' H# p$ ~$ U1 w3 x
// time, we create a Schedule that says to use the
8 V( `8 h" _' S# _ N* u$ s // modelActions ActionGroup at particular times. This
) D2 t R8 A) u0 |2 V. h- d! H // schedule has a repeat interval of 1, it will loop every0 U+ T, C, Q2 p9 t: y- m8 U( \
// time step. The action is executed at time 0 relative to
3 T% }: _$ W- z: m" [: J // the beginning of the loop.
k0 Q* d& C* x8 k9 G8 S e6 W) { Y; \
// This is a simple schedule, with only one action that is, r3 t. x" p L5 F
// just repeated every time. See jmousetrap for more
$ \5 E/ W; I+ W6 }+ L0 Z. Z // complicated schedules.$ @# l i, s2 L" U
1 w) ?0 b' ^6 d' E" z' u5 g9 L3 y
modelSchedule = new ScheduleImpl (getZone (), 1);
* u3 H/ Y3 ]7 s' T. M$ h, [1 Q7 u modelSchedule.at$createAction (0, modelActions);
$ N. M# ?* l- h5 C3 O # A, V6 t: Y2 Q5 G
return this;
( L* `2 F2 [! S! N! u } |