HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# U& |1 q0 m/ a6 t* U( R7 [. B4 W
' G! p) w2 Z! }) F. G2 B
public Object buildActions () {* [+ e! D$ A7 f5 W% f" N0 |
super.buildActions();- s3 p, I7 m; O* d3 ]8 \; d
3 R, M; X4 D7 |7 ~9 A // Create the list of simulation actions. We put these in( L0 r% Q8 I9 X- Y7 O
// an action group, because we want these actions to be
; l Z1 \. r" s5 E- M' D; U/ Q7 E // executed in a specific order, but these steps should- m3 ?; }% {% [1 D( V- G" ^# f
// take no (simulated) time. The M(foo) means "The message
$ y2 ^ h) x5 B W- z // called <foo>". You can send a message To a particular7 N: f( {4 G3 u
// object, or ForEach object in a collection.
9 c% H, F+ t& @: ?8 v! C2 ]; k 2 m" [& |; g2 g: y1 {3 z
// Note we update the heatspace in two phases: first run' s' Q1 p! U* q. l
// diffusion, then run "updateWorld" to actually enact the
7 `5 a, K) L; `- Y // changes the heatbugs have made. The ordering here is
% M4 B, R+ W' H( w // significant!$ A. d: e2 t1 c: h) c$ `9 d! G
; P- O, {! _/ H3 P. Y9 M
// Note also, that with the additional
% [7 ~- S3 [- V3 X! X3 i, f3 I // `randomizeHeatbugUpdateOrder' Boolean flag we can' Q0 M9 m! B: p9 W
// randomize the order in which the bugs actually run
$ B+ P8 K) p( F x // their step rule. This has the effect of removing any. m/ I& p! o+ ~- F( j2 C8 I
// systematic bias in the iteration throught the heatbug
7 f1 M7 C6 a4 K: c2 B // list from timestep to timestep5 i. B, `' q+ A7 X5 @/ t& w3 x& g: n4 w
- C* k9 n. B6 o5 | // By default, all `createActionForEach' modelActions have
4 _& D1 t, n* Y0 w, u // a default order of `Sequential', which means that the
8 |& J6 l' R) y5 s$ ` // order of iteration through the `heatbugList' will be) M: h8 T1 ?! R l% x5 e8 V, l7 P
// identical (assuming the list order is not changed
3 u8 E* }* w3 Y // indirectly by some other process).
4 H- }6 p* ]% a3 j% D 5 I6 R* z, `: D: m& L
modelActions = new ActionGroupImpl (getZone ());4 N7 M6 p# x9 p3 a4 v0 y
, O& y2 n9 P, I3 J1 K) S* j6 R: v
try {% T. N$ X; o2 I, @: L" A
modelActions.createActionTo$message- V6 x o5 ^+ V: _+ N
(heat, new Selector (heat.getClass (), "stepRule", false));3 r8 |# S, m" q/ ~& _2 |$ f
} catch (Exception e) {
8 c3 x9 v4 G. s1 B7 [ System.err.println ("Exception stepRule: " + e.getMessage ());
+ `! j v! h, d/ D3 w* u% Q2 Q }, Z6 J, u% G' @; g* z( H) n' M
% F- ]9 E- G: _# S* @ try {6 v: I9 C% D4 |4 l. }
Heatbug proto = (Heatbug) heatbugList.get (0);# F; n. Q# m. B0 k
Selector sel =
9 r( @% c/ q9 p" d, ~3 @% D$ L new Selector (proto.getClass (), "heatbugStep", false);: d( [3 e8 Z' v# G+ I- n
actionForEach =
/ R/ v) j! A5 u) i- Z8 u modelActions.createFActionForEachHomogeneous$call* W1 I$ Q6 N# b0 v& H$ n
(heatbugList,* S4 V& v5 ?6 _' |
new FCallImpl (this, proto, sel,3 f4 \- b! R) u! m0 Y" R0 `: T
new FArgumentsImpl (this, sel)));
. u! p, Y2 ]2 D7 ^- q+ u* q" f# o0 v! H } catch (Exception e) {% y0 {5 x( B% Q
e.printStackTrace (System.err);
, B4 w/ D6 q" c: r& y* f" u2 G, } }. B" ~1 q/ [, a$ _; |4 B
% v- `+ M `7 K. a4 z8 h- d
syncUpdateOrder ();
`; c n1 F3 M/ Y2 q% _
) ~* I6 t4 j& |1 l" E try {
5 l, K3 [ F) K/ o modelActions.createActionTo$message
3 F' Q1 e6 Q" c W2 |# _ (heat, new Selector (heat.getClass (), "updateLattice", false));& U' A5 h) m; \5 m4 @
} catch (Exception e) {$ [9 @) P% P) x4 u8 q/ c2 y" j
System.err.println("Exception updateLattice: " + e.getMessage ());6 _; P4 A% S. U- N
}
. h1 G* M: v! ` ( w$ _4 S7 L% n! z6 Q2 O
// Then we create a schedule that executes the
! K% J: M" _5 n4 p // modelActions. modelActions is an ActionGroup, by itself it2 c& p' S; U) I+ d/ p9 E
// has no notion of time. In order to have it executed in9 @4 A5 {3 k7 | l1 D
// time, we create a Schedule that says to use the& Z/ `" }- J, }& d# S, V, h
// modelActions ActionGroup at particular times. This
) \* Y! }$ o2 A, R* _9 V7 ^6 V. @ // schedule has a repeat interval of 1, it will loop every
9 f( X! g# w) |* n$ g // time step. The action is executed at time 0 relative to
: j0 q1 o/ K: W3 c7 Y$ ?! I6 k2 E; V // the beginning of the loop.' `8 t( Y" u# \( E( g. p# E
' _0 F! Y: ~% e3 A' I# }% I2 S! P // This is a simple schedule, with only one action that is: K5 k/ f0 W5 h
// just repeated every time. See jmousetrap for more2 ^; c. ]; e* J
// complicated schedules.
( g0 t$ \( ~/ H& S
) H: x' i3 r8 X4 e+ g. I modelSchedule = new ScheduleImpl (getZone (), 1);
: p! W# K9 z, @; m- X5 @9 z7 A7 Q modelSchedule.at$createAction (0, modelActions);
$ N! G2 k6 U& m- B . Q& k3 f7 }% j
return this;8 `6 [- {3 P; \- J8 K; K
} |