HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 x4 a/ U5 k; A" u0 u/ ]# \
( R2 b. r3 R; u3 Z
public Object buildActions () {6 `+ B* @9 e* y; k; Q8 d' b4 O, Q
super.buildActions();
M1 @8 Y1 _4 K% b# I5 J9 Y$ C% J
* F! K: H( f7 K5 g% H8 N // Create the list of simulation actions. We put these in& k( S1 i7 K* J: ]0 Y" \9 s
// an action group, because we want these actions to be2 p- H( R- m9 w9 a
// executed in a specific order, but these steps should8 K6 w- \5 n+ r: p: X, E; n
// take no (simulated) time. The M(foo) means "The message f8 G* B! w% _6 \9 P" `, G/ C" G
// called <foo>". You can send a message To a particular; o" U) `2 s# n, b3 w" `
// object, or ForEach object in a collection.
( Y5 W2 H9 s9 O9 N9 w( P
6 T8 I; j. [7 u( T // Note we update the heatspace in two phases: first run7 l# I J" `0 j3 ~: X& V1 Y5 i
// diffusion, then run "updateWorld" to actually enact the
- _+ A& D; _* l3 M6 c0 N& c // changes the heatbugs have made. The ordering here is
# G9 o4 V2 ~: v. p9 V // significant!
; T6 l8 `4 l/ N; M% b ( L$ ^' ?0 j3 d
// Note also, that with the additional) V# Q1 P& q; r: r
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. B* Z3 D" o, U. l+ b+ S4 w% M! A // randomize the order in which the bugs actually run4 }$ b# _$ I e! X2 b0 v
// their step rule. This has the effect of removing any- g( w! w' J1 E/ [, w& b
// systematic bias in the iteration throught the heatbug
# ?$ J4 o1 H, J$ H // list from timestep to timestep
) ]! A! ^1 K; r" [( {' S. D* `& q$ r * a- D" V# @( K/ t# x# M' K& x' `% h
// By default, all `createActionForEach' modelActions have- `. j" i2 K) K6 z" ]* F0 I2 t( V
// a default order of `Sequential', which means that the
; X# B- v8 e0 |9 G' ? // order of iteration through the `heatbugList' will be$ c6 o: Y5 R( \; `6 m" `
// identical (assuming the list order is not changed) w: K* o7 V" F; v; S
// indirectly by some other process).# y2 D( m( l8 d0 B# f ]% S
5 R" B7 v6 x( v/ Z. k2 L% [ modelActions = new ActionGroupImpl (getZone ());
+ Y" \2 k2 S0 S+ O! V: `% ^) C
9 ?4 [# g! d+ @4 v) a8 N. {9 d try {
- e S4 i0 [* N modelActions.createActionTo$message
% `) `3 K0 l) D4 Q% `4 Q (heat, new Selector (heat.getClass (), "stepRule", false));
0 w7 l( a3 @& I' Q } catch (Exception e) {) t" {) n/ r7 A& x
System.err.println ("Exception stepRule: " + e.getMessage ());% U( ]1 E7 o4 @+ L! [" z0 W6 s4 ~
}; k: S' z C3 j
) a1 e, d; m8 B* X% _# A try {( M; O* `+ Y _" y' D
Heatbug proto = (Heatbug) heatbugList.get (0);. i# K- E) f* a% o4 k. Q
Selector sel = / P R4 R- i6 `) u% D
new Selector (proto.getClass (), "heatbugStep", false);1 Y0 K/ y% U0 n L( C
actionForEach =
+ F) [3 r" U4 ~. z1 U) [/ }; \ modelActions.createFActionForEachHomogeneous$call8 o+ v/ s9 N" h6 r9 }
(heatbugList,6 L' x9 ]8 G0 T" c8 V; j. O
new FCallImpl (this, proto, sel,$ s& t, o" k5 [. u
new FArgumentsImpl (this, sel)));' S9 s! {2 L5 K& v
} catch (Exception e) {
) @) U# C3 f/ B e.printStackTrace (System.err);
. j2 X1 A- K5 H) C) G, E5 T4 x }
1 D7 n- s! a% `# `/ w
2 v( q) [- y3 b syncUpdateOrder ();
# i/ q: j& C. h5 R# ]. `
& U$ q/ S4 F: k- O, s/ @+ [& I% V try {8 v4 g+ E0 U/ x+ }; J, m7 @
modelActions.createActionTo$message 2 @6 P1 }0 I5 n0 G0 o& a6 O+ o; i
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 S& m( }) J H* U! ^ } catch (Exception e) {4 K3 M T% V6 {. {5 M; k
System.err.println("Exception updateLattice: " + e.getMessage ());1 q: X; J) W" ], P3 q; M
}
0 C4 h8 ]8 x; O 7 ~$ b1 v2 `* E- Z/ P! r: f
// Then we create a schedule that executes the
2 j. g, s5 D) m7 u9 q // modelActions. modelActions is an ActionGroup, by itself it
$ \3 `, g% a: p E! v // has no notion of time. In order to have it executed in
7 b7 f0 g/ q1 W- K // time, we create a Schedule that says to use the/ U/ S) D% W$ T/ J9 f. J
// modelActions ActionGroup at particular times. This- ^" r w+ R- v& d: _( p$ c
// schedule has a repeat interval of 1, it will loop every2 n5 m) C' h' X# X! p5 V$ H
// time step. The action is executed at time 0 relative to
}4 Q! p+ J9 n( l+ Q! o4 z) _ // the beginning of the loop.
# X3 ?( V3 e( B6 ]* ~ L/ U6 h6 ~$ ~3 w8 N" Y1 |
// This is a simple schedule, with only one action that is3 H, h) l$ r! R6 P' X0 w
// just repeated every time. See jmousetrap for more
. B- a4 k' o+ g) ~4 U0 E // complicated schedules., x& C/ Z1 E {% f; P( e/ n* Y
! q' o( e: [5 G% }: s+ \( Z modelSchedule = new ScheduleImpl (getZone (), 1);
% f' a" X7 e- o8 d9 K+ K modelSchedule.at$createAction (0, modelActions);
0 M0 y) K( A. h' V0 N
3 V. n! Q+ Z% W# H9 a9 t B% h return this;0 x2 C5 E! f) k0 z4 c3 R
} |