HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) {; O" z: D1 P/ F* m" j) y" ~( a: S; h
public Object buildActions () {) |" z a. ], y3 Q0 Y' c. |" J
super.buildActions();
/ v6 Q. K8 @% m/ R+ C+ v$ V+ Z1 P
6 y: m! z1 u2 D/ S4 G0 | // Create the list of simulation actions. We put these in
+ W0 v/ I. }: `6 H) Q% A, W // an action group, because we want these actions to be" L" [6 Q5 [0 L6 C* h9 d! t
// executed in a specific order, but these steps should1 V" Z. ~, u* C2 _
// take no (simulated) time. The M(foo) means "The message
) D6 j, o% N' J/ _6 M4 r& F0 g4 { // called <foo>". You can send a message To a particular
8 s% J& h" T% |- k // object, or ForEach object in a collection.. {# l9 X/ v" m1 e
6 M( g2 m, T. d: U. p+ q // Note we update the heatspace in two phases: first run, L1 w( ^+ R5 [) ?
// diffusion, then run "updateWorld" to actually enact the
0 f6 u0 Q5 ~7 D+ a( P // changes the heatbugs have made. The ordering here is) ~4 z" W e2 m3 m6 k
// significant!4 _( p& P! d3 }
2 k$ `' d$ J* J
// Note also, that with the additional& a) L: H4 N5 r% D# `1 ?0 W
// `randomizeHeatbugUpdateOrder' Boolean flag we can, I* J2 j" d! b+ l8 e
// randomize the order in which the bugs actually run
" D7 }! S* N$ |9 ~ // their step rule. This has the effect of removing any \9 @4 q# B9 p1 p6 T
// systematic bias in the iteration throught the heatbug8 k/ T6 H/ p# ~+ f. d: S$ m
// list from timestep to timestep
8 i# d0 s3 Z% M, v! P k
& S1 n; z! H4 ]) T5 k; V0 l4 z // By default, all `createActionForEach' modelActions have
! e a: W$ S& f: x // a default order of `Sequential', which means that the
0 _6 R/ M9 z, Q // order of iteration through the `heatbugList' will be
7 y+ d7 @. a7 K; P2 C // identical (assuming the list order is not changed% @6 k: Y1 N* X- J" c
// indirectly by some other process).
- v0 O1 u* J+ i; c% T
5 J) i6 |& Z' j* x9 ]- ] modelActions = new ActionGroupImpl (getZone ());8 M2 B$ Y- |: Q- n! g' h8 y
; z6 o* m' C( L/ c N0 L try {
: L; T# N# u M7 D( o" t7 | modelActions.createActionTo$message2 k) Z3 w) s' C4 r# @
(heat, new Selector (heat.getClass (), "stepRule", false));
5 ^3 P0 t* |/ R0 }+ l+ H% v2 h } catch (Exception e) {
8 N2 y! n( r1 I Q2 W1 v" F, g System.err.println ("Exception stepRule: " + e.getMessage ());
, i( Y3 U7 m t7 L0 K3 ^' l2 d- |6 x, N }
% e7 g( I0 e$ p# k) @5 b( Q: S6 u0 n4 q0 F
try {$ Z. ~% m, f" b+ `0 D( }: W& ^/ I& C$ W% j
Heatbug proto = (Heatbug) heatbugList.get (0);
) G1 }/ j' f: l5 `5 ^. x, [ Selector sel = / o8 v" d) H/ K/ s* g5 W/ o
new Selector (proto.getClass (), "heatbugStep", false);: G# K. n; d0 d" m
actionForEach =
; h5 s# ?( G9 U* l; \ modelActions.createFActionForEachHomogeneous$call, N) i/ Z* Q$ Y E4 t: i
(heatbugList,6 s. R/ D8 g1 |7 g# \. B7 D. l
new FCallImpl (this, proto, sel,( z- W) `; s/ w4 h0 w. ~4 Z D
new FArgumentsImpl (this, sel)));
$ l( j2 `3 O5 z" F } catch (Exception e) {; b% s/ {: h4 I) A
e.printStackTrace (System.err);3 k7 ^" L& Q7 n8 ?. F) V7 n
}
( ~* ^$ Q3 J1 b# F$ C9 g
, [! K) p7 O. h' y! W/ V8 S syncUpdateOrder ();! P K/ i6 l! K1 e0 z, g# {) O
1 C' d( J4 J# q% r, c1 ^9 j try {. `! G$ I% r3 F
modelActions.createActionTo$message 8 K% |- H; W- K" ]% h. p' |+ g9 {
(heat, new Selector (heat.getClass (), "updateLattice", false));
& Z( I) G/ R& t# s, _8 f' p( ^- S } catch (Exception e) {5 n! b2 N& Y' R& T2 @
System.err.println("Exception updateLattice: " + e.getMessage ());6 l3 R+ b1 E/ I( T: t0 B; q* R6 u# G* M* D
}6 B9 C+ D* Y. A) i B8 H
& K' U7 Y- o; n6 H$ ^ // Then we create a schedule that executes the" k# u% V% w/ q
// modelActions. modelActions is an ActionGroup, by itself it
/ B8 M# v1 S9 j" S) P // has no notion of time. In order to have it executed in4 ^) K8 h f& q% X9 Z
// time, we create a Schedule that says to use the; J& R7 n8 J u7 {8 F9 C; H
// modelActions ActionGroup at particular times. This
, v. k* P! k/ b+ R" R; ~7 x( } @' n // schedule has a repeat interval of 1, it will loop every
& g# r* D4 K" Q# f5 A // time step. The action is executed at time 0 relative to
}& f1 j {) q+ ~" \" A5 j v // the beginning of the loop.
9 G8 X5 N: o! a/ w) H# x N
: `/ z& g. h! S( h9 \: W // This is a simple schedule, with only one action that is
3 z6 n2 i; Z0 D0 Q5 M! X3 d // just repeated every time. See jmousetrap for more
9 H% e6 J) A: E7 ]! H // complicated schedules.
: D( ~0 |8 [# A2 Y9 X
% g5 G+ r2 P: D) C2 n modelSchedule = new ScheduleImpl (getZone (), 1);
l7 c8 Y0 \ }8 B @1 J modelSchedule.at$createAction (0, modelActions);
$ |% v3 z2 p, P
% y3 i) w( Y+ a0 ]6 r' i& ~- e. l return this;8 `; s( h \' o6 w
} |