HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
X l. s* _4 T3 M1 ^4 e- }, @6 j$ z2 \+ V
public Object buildActions () {
& V' ^+ u* R% t- r" c8 ]1 H/ s super.buildActions();( J/ G: R4 \" @8 P- Q* t6 f
2 L4 R- [" K3 H( R* U2 H // Create the list of simulation actions. We put these in
: X4 N1 C+ N0 x" x" x% b // an action group, because we want these actions to be
9 W1 l7 t4 u6 C' B! A& B // executed in a specific order, but these steps should
* h, C7 q! p: b! [ // take no (simulated) time. The M(foo) means "The message/ [$ E/ T# y1 \) s" I
// called <foo>". You can send a message To a particular! u' q; }1 t6 G2 d! `- I
// object, or ForEach object in a collection.
* r+ W+ P V) V+ z# a
4 ^! l; k% u2 W1 x // Note we update the heatspace in two phases: first run1 t( }+ Y" f9 J0 Q9 c* k0 a
// diffusion, then run "updateWorld" to actually enact the& E6 R: g4 B3 G& n D8 p1 K
// changes the heatbugs have made. The ordering here is5 J. [1 n, y/ d. V6 M
// significant!$ j/ F h- I8 F9 g* @2 g
; C2 Y/ B9 T; _! f" [2 E // Note also, that with the additional3 _- \' O( L+ m' y3 J
// `randomizeHeatbugUpdateOrder' Boolean flag we can8 o0 f$ w# q0 t6 G5 ]- J! j" x
// randomize the order in which the bugs actually run+ X9 P* G! f. y8 h S m
// their step rule. This has the effect of removing any3 l- f+ h" g f) F$ E
// systematic bias in the iteration throught the heatbug. y7 x3 U1 p- f% t$ q/ R$ x
// list from timestep to timestep
+ Z0 O7 E8 V1 g c
$ Y0 h" ?4 x2 _( } // By default, all `createActionForEach' modelActions have0 n' `* z7 h6 A3 M5 L h( p @
// a default order of `Sequential', which means that the$ }4 W% @3 @* H* C+ o% m! O
// order of iteration through the `heatbugList' will be- u, ~$ e8 t+ H5 e: o Y6 k
// identical (assuming the list order is not changed
( v$ A B3 p, R0 } // indirectly by some other process).7 k$ i: [, w! q# J* K; G% I
: K7 R9 b3 _! ]5 X4 z0 y3 D
modelActions = new ActionGroupImpl (getZone ());
8 v8 Y+ ~0 w( u* H. w2 X/ Z
! r" a7 E; A1 Z% G3 f; @5 B, p try {
2 b* ]0 i! M$ n6 d, h modelActions.createActionTo$message
/ V4 b+ A% K2 {0 t/ s; p+ a (heat, new Selector (heat.getClass (), "stepRule", false));
0 ~: X O& f4 R } catch (Exception e) {
; o# E% w1 p" D: m6 ?$ } System.err.println ("Exception stepRule: " + e.getMessage ());
' K& k$ k* G/ r+ e* B8 f% X) |& u9 \( i* r }
9 e! j% i* ?0 \/ q
/ s, m8 E# ]7 t5 ] try {7 @ [; j9 y k' T
Heatbug proto = (Heatbug) heatbugList.get (0);
A0 z" w: D9 C' v) A" S% G Selector sel = : p9 f3 l1 V) Y2 {) e* G
new Selector (proto.getClass (), "heatbugStep", false);7 J& t9 I* w* |3 y. t4 y. r
actionForEach =: F* U! }9 B; d. w" L- G0 Q
modelActions.createFActionForEachHomogeneous$call
6 P9 z2 k: ?$ j$ r. [7 i (heatbugList,' f. c6 S x, m1 f, n: \
new FCallImpl (this, proto, sel,1 E+ c& L0 [- \
new FArgumentsImpl (this, sel)));+ R1 K- P# ` `" j4 Q) B/ J" S
} catch (Exception e) {! g3 _% J1 Q. v" n1 g
e.printStackTrace (System.err);
& K0 q: X/ V" h4 L2 j( M$ C }
6 a8 ]4 @ _# Q4 y) Z
( G1 D, w: l9 r# h1 s syncUpdateOrder ();* v2 L( z: G" p
6 s; A8 {: B# c* k: n5 L9 o$ d% Y
try {
- I9 K6 |& A1 d5 t6 {% _6 X+ ?" O modelActions.createActionTo$message
0 n+ M6 m" U3 w4 @9 J: |6 ] (heat, new Selector (heat.getClass (), "updateLattice", false));" z* j5 }; c. `$ N
} catch (Exception e) {2 U/ N: y7 H: O) ~
System.err.println("Exception updateLattice: " + e.getMessage ());
+ a6 A6 X% D& d Q$ A A7 L }
- i, G% p; d0 _2 ^ ' y$ W$ l% I9 ^4 S
// Then we create a schedule that executes the! l+ E& \4 L9 N ~& p" E! ~2 C
// modelActions. modelActions is an ActionGroup, by itself it
5 d) h3 x" q7 C% z% Q: J9 Q+ t // has no notion of time. In order to have it executed in1 \8 ]0 h+ N8 E0 W. R4 Q2 |& S
// time, we create a Schedule that says to use the' K0 K1 p3 e3 a/ H
// modelActions ActionGroup at particular times. This
n9 v% Q7 M6 [" I; t // schedule has a repeat interval of 1, it will loop every1 u/ q0 t* G3 u
// time step. The action is executed at time 0 relative to
& d' d4 U% l% b' ]8 O2 ^3 E9 k // the beginning of the loop.
; H# ~, u o: o5 ]: W' R. b% w9 i% q( d) L4 z2 A
// This is a simple schedule, with only one action that is
& J \& F# h5 ?3 Z' T9 v) @ // just repeated every time. See jmousetrap for more# s- [" ?8 ?0 }; }, C
// complicated schedules.
8 j1 L$ w) ~2 L' Q
3 r# g9 ]+ I0 L9 h modelSchedule = new ScheduleImpl (getZone (), 1);: j! D6 \. B" M- p' r* l
modelSchedule.at$createAction (0, modelActions);
' s! b4 m: b t9 \1 A! w; [+ T6 l
% J* Y! G9 ?" o: i return this;
/ c' u: p; Y+ c! K3 K! G } |