HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* ]4 e" p/ u" O& H' G
5 r/ o5 T. s) j, g
public Object buildActions () {
K( M; G% Y3 P( K3 I super.buildActions();0 t" }3 }$ H. |7 u' @
9 V$ N% p* e8 E+ N4 a // Create the list of simulation actions. We put these in
) o: @1 I' o; X' n* K' j, B4 | // an action group, because we want these actions to be
8 d w% b, S& ^# Y! v$ e# h& _ // executed in a specific order, but these steps should' _- |' n' g1 d; U
// take no (simulated) time. The M(foo) means "The message8 @) `5 u6 x. }7 A
// called <foo>". You can send a message To a particular; X2 G' Q& s9 ~" ]
// object, or ForEach object in a collection.
+ j& L! U, f T( V9 ]
$ I3 K0 D! |0 e$ n( y0 g // Note we update the heatspace in two phases: first run
) v$ C6 J, h1 j( k0 ] // diffusion, then run "updateWorld" to actually enact the
4 m+ d! B' E$ q o% j$ n // changes the heatbugs have made. The ordering here is
$ o+ ^5 _, d1 V; E // significant!
- Q1 I* d, L2 t3 m& |# H8 \& l + Y5 H/ s/ I E" a% E4 f; Z8 o. _
// Note also, that with the additional. ~1 e$ T7 h9 M- T5 c" E P- c* p
// `randomizeHeatbugUpdateOrder' Boolean flag we can# @& I+ Q9 M! |. f, Y3 q
// randomize the order in which the bugs actually run
1 ?( T+ R# Y6 M3 R: O, j/ S) @, X // their step rule. This has the effect of removing any- Q5 n# I* N. V1 ~# Y
// systematic bias in the iteration throught the heatbug
8 K& e$ Z9 Q& J1 P // list from timestep to timestep
( u3 ]) w! I- n) [3 E: E8 B: u * b# b, U+ X% t I) }9 m% S7 ^- \
// By default, all `createActionForEach' modelActions have
6 H9 J6 T3 j0 h+ a // a default order of `Sequential', which means that the W! e, u$ P' v+ m
// order of iteration through the `heatbugList' will be6 Z( o7 d$ L- f u
// identical (assuming the list order is not changed
9 W: Z" W9 }8 R0 s/ E/ M2 i // indirectly by some other process).
. x! z2 J( L: `0 I1 y5 k8 e, B
/ \. g6 x$ w3 ?6 S modelActions = new ActionGroupImpl (getZone ());
/ ]( M) S4 r' {. |) K# ^
( z0 E* n$ I# S9 ? try {
/ p1 ?+ l5 G0 ^" a5 y/ b modelActions.createActionTo$message, s; c( q9 o8 o" z
(heat, new Selector (heat.getClass (), "stepRule", false));( ]7 T! R+ B0 p: k4 Y0 i
} catch (Exception e) {/ |1 I& h, h M G# x' ?
System.err.println ("Exception stepRule: " + e.getMessage ());
0 p2 A4 m% H1 [2 {3 O2 B" L }) m( d$ h+ a: Z$ |; o6 @- h
* P1 g0 c3 c; ~* I- u, e try {6 S) @, M: Y% U ]6 X' p* |
Heatbug proto = (Heatbug) heatbugList.get (0);
+ O- X4 N, X* \. w Selector sel = - `; s+ x( N1 c q7 s! V$ e/ V
new Selector (proto.getClass (), "heatbugStep", false);7 w3 k7 ]8 `1 {0 b
actionForEach =; l, j! Z, k3 I: ?% d
modelActions.createFActionForEachHomogeneous$call
8 Q/ @& k7 Z9 {" r1 n1 r (heatbugList,3 m* ?( m7 t: ~, ?% H" z$ q; E
new FCallImpl (this, proto, sel,
, {5 e0 L0 r/ g8 q9 c0 S new FArgumentsImpl (this, sel)));
( f$ n7 f% H; C1 k' w } catch (Exception e) {
: l( J% @ Y2 v6 ?$ @5 e* o( e e.printStackTrace (System.err);$ X4 P1 V( O& {4 e
}, ?4 U( z. ~ ?8 U$ R( m& S
7 A7 J. J: W1 R* L0 q; P0 q
syncUpdateOrder (); w2 n/ ?6 m0 x
% V) F4 [8 b+ U& r; Z; j9 r try {
- ^* D8 x+ s. j; o6 @, m ?7 i1 C! ~ modelActions.createActionTo$message
: m$ w' \+ R0 R& w+ x$ ? (heat, new Selector (heat.getClass (), "updateLattice", false));3 G' x1 D$ R* E2 [1 b; p/ B9 \
} catch (Exception e) {% f5 B5 u4 H' p4 z" a
System.err.println("Exception updateLattice: " + e.getMessage ());
4 A* s1 R7 a# t4 Z1 i# i7 o/ o }
" l' H6 p$ z0 r$ V 6 P1 U( ^6 h% ]+ _# w& g. p
// Then we create a schedule that executes the
( o9 {- H2 U W // modelActions. modelActions is an ActionGroup, by itself it) O7 s6 j8 a0 C2 K' C5 M2 M b) o
// has no notion of time. In order to have it executed in
, ?% p% I! H0 F5 P0 h // time, we create a Schedule that says to use the
: A$ Q* H$ s5 E. B0 z# b# G* [ // modelActions ActionGroup at particular times. This
5 p! m! w- a5 T/ o( t4 l1 e // schedule has a repeat interval of 1, it will loop every
# H- z2 J6 F* c, |, X/ J // time step. The action is executed at time 0 relative to( p/ V7 _. ~, \- ?* {7 i r" V" q
// the beginning of the loop.
4 V: O3 z8 \' ?7 C! U2 L: j! W
4 K, h, F. X; }5 R, S/ v // This is a simple schedule, with only one action that is
1 h, U" Z; ^( `# F6 h9 _ // just repeated every time. See jmousetrap for more3 `+ W/ S+ w2 V. O" ^+ C
// complicated schedules.
0 n( ~, Y" e5 K) v( E" o0 r : f& n+ S& ?5 r6 _4 M
modelSchedule = new ScheduleImpl (getZone (), 1);1 P) g6 {9 |1 [4 B
modelSchedule.at$createAction (0, modelActions);
+ r; h" i3 W* v
2 {& [8 s8 B z, n: m+ }8 K0 \ return this;* Z% }# E0 b V/ M0 H+ Y
} |