HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 J" ]7 f% s9 W
! U1 E! n# ^5 g* {$ Z5 H | public Object buildActions () {
' v6 @" M" Y: H+ V( U4 z super.buildActions();
+ Q" u7 n: v% y7 c( E9 p 4 J( D) ?2 Y" a$ B
// Create the list of simulation actions. We put these in
+ T, F: y0 R3 [$ ] // an action group, because we want these actions to be4 A1 z h- u, l( n* ]( a
// executed in a specific order, but these steps should
5 G" o5 \/ K7 z8 }- N! d // take no (simulated) time. The M(foo) means "The message
5 S2 ]2 c, k" p4 A+ w: c // called <foo>". You can send a message To a particular$ g$ [( H/ d& S0 D$ j0 M* z
// object, or ForEach object in a collection.- p- D. H. q2 l* [" H: O* [2 _
2 i2 y/ |( B2 o e( E* b' j: k
// Note we update the heatspace in two phases: first run) H/ Z6 T) |4 }
// diffusion, then run "updateWorld" to actually enact the
( x2 f% r2 v4 E, }' L+ \; G9 Y // changes the heatbugs have made. The ordering here is+ l. f. F' \! M
// significant!: |: c7 N! N! P! [9 C( T6 @# Q
0 g( T: y: A6 A7 U* m
// Note also, that with the additional! e& Z( l+ }, q4 N( V
// `randomizeHeatbugUpdateOrder' Boolean flag we can! H$ \' Y# u3 S/ a% i. a
// randomize the order in which the bugs actually run8 A9 }; Q4 p/ Q r
// their step rule. This has the effect of removing any% _/ ~2 N4 ?# f, ^' K! k, b
// systematic bias in the iteration throught the heatbug
) y( `7 ]; ?# ?4 j# o+ [& j' G, t // list from timestep to timestep
. z9 r: G! I X; x& n0 P" g( f5 g
5 y! A7 `+ d/ j" B4 A; \ // By default, all `createActionForEach' modelActions have
1 U; b) o0 m# d( o // a default order of `Sequential', which means that the
- [+ Q8 _. |! j7 a% z: a- Q H& y# S // order of iteration through the `heatbugList' will be4 f" E) _+ ~' [9 C7 V; d
// identical (assuming the list order is not changed" i) ^3 a+ n" e% j7 E3 T7 ]
// indirectly by some other process).
7 z$ \4 D8 s Q! F. u
* W8 T N* |' g( c: x2 L modelActions = new ActionGroupImpl (getZone ());; h- l! T' z8 T7 U0 ^
$ E- A+ c. L- _0 U try {
; w4 W. I. a: h3 n; ~% G* z! i modelActions.createActionTo$message) `: \ C) V" @) h/ a5 |" Z
(heat, new Selector (heat.getClass (), "stepRule", false));
3 {5 S! f" N- w1 l } catch (Exception e) {# T9 P3 }9 Z1 n- g7 P4 q
System.err.println ("Exception stepRule: " + e.getMessage ());
3 o( \6 k# N& u, D$ e }
1 W( Q2 B# ?# x9 t
]2 U& i- H; Q, ^# n4 V try {( D. ?* X* R F; E2 i& @
Heatbug proto = (Heatbug) heatbugList.get (0);
2 L' w9 V- R: Y5 e: v Selector sel = ! `9 B, `) T0 }3 `7 s
new Selector (proto.getClass (), "heatbugStep", false);
; A4 e; n' l2 A+ r6 b% Q actionForEach =
" i7 }& R D v" ]. b6 H( m$ h modelActions.createFActionForEachHomogeneous$call
v) L- C1 Y: Z1 n6 B4 c, ^" Q (heatbugList,
. B6 K: g/ T& J7 n& c new FCallImpl (this, proto, sel,
. P& X* `7 y( @8 o6 D# L new FArgumentsImpl (this, sel)));
2 i! P8 ]2 L5 p+ i, q } catch (Exception e) {; i: J2 g4 L U* Y- j. P
e.printStackTrace (System.err);
% k" I' I/ c* @2 W4 f }
9 g- W4 w2 m. [+ [8 G) n. S
) g0 L- l8 n1 ^# x1 e9 t, b syncUpdateOrder ();) p2 A( O( V2 [6 D5 P1 Q# F4 F
% `$ y/ [+ }' d8 S( b9 ^
try {, o1 W6 z* V# h* Q
modelActions.createActionTo$message
. |# ~& k$ `3 O& n. d* Q& I& F (heat, new Selector (heat.getClass (), "updateLattice", false)); A5 j" o% y2 n" q
} catch (Exception e) {
1 `, P |. m9 V* ` System.err.println("Exception updateLattice: " + e.getMessage ());
+ f1 p- x }6 p }/ ^0 F6 v+ I m7 G, q0 T' j' Y
% |+ a! v8 @2 \0 n% Q* x // Then we create a schedule that executes the
$ I* l. g6 [4 p, y3 e // modelActions. modelActions is an ActionGroup, by itself it
0 [ T) \! g6 Z+ Q // has no notion of time. In order to have it executed in5 c/ N) x1 C& X3 ?
// time, we create a Schedule that says to use the
9 q2 n( _3 w4 b9 t // modelActions ActionGroup at particular times. This9 k* z8 |( E# D8 V+ P n. C
// schedule has a repeat interval of 1, it will loop every( u7 w- y L0 S$ m5 d1 O, C/ a
// time step. The action is executed at time 0 relative to7 y6 u5 {! C5 s! Z
// the beginning of the loop.# i5 t+ t( G3 U# P
+ |! U1 L; Q3 J3 W4 V2 f! X
// This is a simple schedule, with only one action that is
4 J: q/ ~0 W5 A+ ^! c h m // just repeated every time. See jmousetrap for more/ h+ B, E# J6 e7 |2 o! L
// complicated schedules.
/ v' s( C9 @ K8 i4 U1 A/ F
- C) J4 e, ^0 O- |! o. y( R* `" V" r modelSchedule = new ScheduleImpl (getZone (), 1);
+ ~4 \- t6 n1 m. _ modelSchedule.at$createAction (0, modelActions);3 o8 A$ t1 Z4 K4 `: e
8 O$ `3 e# A* S2 o1 l4 F- C
return this;
$ A6 E4 V& W! L6 M! h' G4 J/ E } |