HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) Y4 A! r) J* d; R. ?
" H' k7 J& a% w8 O. P+ T
public Object buildActions () {
+ G4 K% Y* `5 h2 l super.buildActions();( _! N9 X U. s, h3 G9 y3 g8 i, i2 Z
8 o1 B9 }+ q, }) k4 F4 ^0 _# W // Create the list of simulation actions. We put these in; L2 `+ D; {" x8 t$ m. |4 u: H
// an action group, because we want these actions to be
9 _# `# J& r( K. S& ~0 { // executed in a specific order, but these steps should9 A" g" S' y T3 j% z7 l3 [1 f1 b: a3 [/ m3 S
// take no (simulated) time. The M(foo) means "The message$ I. j" q/ w5 _' S3 p$ J
// called <foo>". You can send a message To a particular# N$ i# s5 _% p
// object, or ForEach object in a collection.
2 t0 Z4 _2 \+ P
' |3 u9 `& k3 ?: S // Note we update the heatspace in two phases: first run
* c6 u$ L, i( u. n% u // diffusion, then run "updateWorld" to actually enact the
. d- e( m7 e: q- X4 ~3 e! a# | // changes the heatbugs have made. The ordering here is* f& H7 Y7 l3 N% T
// significant!& Z" v- u4 v# A; H! x
[) {' L. B4 j+ e
// Note also, that with the additional7 L# a V9 u9 J1 H6 h+ n; U, q5 d
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ Z+ g0 f. U" ^' Q$ L8 U6 p* i7 H // randomize the order in which the bugs actually run
5 L0 g" x: m+ {! r% c' x // their step rule. This has the effect of removing any% J- z) q! f! `8 F1 z
// systematic bias in the iteration throught the heatbug
( T/ Q: A: c3 @6 s$ ^' E9 m // list from timestep to timestep8 j* V; K# B( A4 A: l
2 A, ]) J- N0 [1 f) q
// By default, all `createActionForEach' modelActions have
) B. [. {. o2 _" m3 Y- v4 m0 ?: p, m // a default order of `Sequential', which means that the) Y: A: D6 d+ G
// order of iteration through the `heatbugList' will be
7 @# i Y6 X J7 W: b8 r // identical (assuming the list order is not changed, }( a6 N6 R' B- C V0 M
// indirectly by some other process).
+ K, C$ _5 X# k6 S
! i/ r* w9 z7 }+ j6 B" K6 P/ W modelActions = new ActionGroupImpl (getZone ());
4 \9 D8 o; |; C1 B' J) \. y8 O; q- ^% K
try {' R# N; M0 R$ k0 x" j( x
modelActions.createActionTo$message
3 O+ r5 \" r* k! W1 f% J7 }4 q (heat, new Selector (heat.getClass (), "stepRule", false));
4 k; M; x4 ? ~7 K: f* [ } catch (Exception e) {
, C! `0 e2 B! H( g$ Y% B! j8 R# V System.err.println ("Exception stepRule: " + e.getMessage ());
& D0 N8 N0 ^# X- a+ k# b }! y) G3 ]1 C2 D
% I: N/ O. f2 k5 f try {
4 J; i/ h9 S( B: v" |" {* A& d Heatbug proto = (Heatbug) heatbugList.get (0);0 `, S k$ I; K" L7 I
Selector sel =
. t9 a, K6 s: Q( g. Y new Selector (proto.getClass (), "heatbugStep", false);* v" m) v( i& ]- F! S& s P
actionForEach =
N+ Y! l4 Z6 A( Q7 \ }. U. k modelActions.createFActionForEachHomogeneous$call; I- ]$ J7 u' p
(heatbugList,0 M6 d5 k$ L8 I; f: K
new FCallImpl (this, proto, sel,6 E; t) O. K2 J+ s2 `
new FArgumentsImpl (this, sel)));2 r1 i( Q/ O1 P. o$ l2 v5 e
} catch (Exception e) {( _/ r6 e; W+ c+ V& Z( y/ M
e.printStackTrace (System.err);' B1 V3 [+ F- D
}, t/ K& o* O. ]( p9 N! [. |* o5 X% I
3 ^1 N) \1 ~5 M) F! n
syncUpdateOrder ();2 l2 G% g" R" f2 ?- x% Q" ~$ H
. z8 }9 W+ V, d) N
try {7 g" Q2 M% S) Z- _/ s
modelActions.createActionTo$message / |1 O W8 W0 ]
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 I* V; C- F" _) C" ~& F* l2 E } catch (Exception e) {
8 \" O& ^' n9 o System.err.println("Exception updateLattice: " + e.getMessage ());
7 v) E" p9 n( x/ d( o ] }* Z' p$ V3 a5 t5 z
% c# g0 U/ @$ B4 E R
// Then we create a schedule that executes the
( }, F5 P" ~$ e0 ?( p // modelActions. modelActions is an ActionGroup, by itself it
3 S7 q% h: a7 X. c8 K/ ] // has no notion of time. In order to have it executed in; }# f e7 ]- M! B0 U, C5 e
// time, we create a Schedule that says to use the
; B! r$ Y* ]2 A' A // modelActions ActionGroup at particular times. This
$ h# d- }8 L0 o. v+ N% \9 {+ f% O // schedule has a repeat interval of 1, it will loop every; X: F& [' M8 T4 y
// time step. The action is executed at time 0 relative to0 k3 h0 z+ D3 T$ a* X
// the beginning of the loop.. x* b$ b% V2 M7 h1 w. _7 E5 {
4 `# D( y2 a {- m9 `" K" y6 [* k1 ? // This is a simple schedule, with only one action that is
7 G' b* Q' q+ x1 I // just repeated every time. See jmousetrap for more% n- I, k7 N4 P0 i0 N
// complicated schedules.
& P; R% q% l: A 0 S% K* d: g3 \& a
modelSchedule = new ScheduleImpl (getZone (), 1);5 m0 |" a3 m% q
modelSchedule.at$createAction (0, modelActions);
6 A) S2 @7 z1 p" o; g
( Q# D9 `( r1 l9 i V2 Y. e return this; p1 p- |: d: w! g# t
} |