HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) A+ z, o1 f8 W
6 r; V- i& Z- W3 Q public Object buildActions () {2 } Y" w( ] @
super.buildActions();9 P' k" [" l0 ?1 J+ M4 M9 H, N. u
& q' T! o, Y8 ~- H
// Create the list of simulation actions. We put these in
1 d# W! A4 s2 n. v // an action group, because we want these actions to be) `; F7 H3 g) k2 D
// executed in a specific order, but these steps should# @6 z0 M3 S0 a) V8 T! f
// take no (simulated) time. The M(foo) means "The message
- G7 V# E" P! k+ @/ x k1 U // called <foo>". You can send a message To a particular+ C* U3 y% T1 \5 @; D& K
// object, or ForEach object in a collection.
6 h0 Q/ p' E. M$ e+ ? 7 O* z- E. p. V# Z
// Note we update the heatspace in two phases: first run5 X& ?% ?1 k8 L# ?) K
// diffusion, then run "updateWorld" to actually enact the1 c: ?* L. j- m9 C/ B3 }
// changes the heatbugs have made. The ordering here is0 l' [: I/ q% D
// significant!
% c9 h' Q& L* W7 y% @0 r7 Q
2 s$ h. J W3 m/ a, V; C$ @1 o // Note also, that with the additional
5 ]3 O. E1 p/ S y/ j // `randomizeHeatbugUpdateOrder' Boolean flag we can8 ]2 o0 u5 Z2 b* V# I
// randomize the order in which the bugs actually run. N( Z" y! z( [, Z& \; }. J# d
// their step rule. This has the effect of removing any* R! L) P+ m6 Q! j
// systematic bias in the iteration throught the heatbug
) }1 K- t$ w# R0 n. K5 g // list from timestep to timestep
+ [/ M! ^ W- {! e" O9 q' M . n0 F% \4 m( P/ F b& J# J3 ^$ r
// By default, all `createActionForEach' modelActions have% f' f2 w- J6 \) E: @) @/ r
// a default order of `Sequential', which means that the8 _6 X, A# W7 y' r
// order of iteration through the `heatbugList' will be
3 x; C/ i: e$ h1 z+ f: Y; C // identical (assuming the list order is not changed
6 T: A+ p/ t" s // indirectly by some other process).0 z4 B! L' N4 ^) _/ P5 W! C
& q9 ` h; ]0 d3 s* h8 Y; G( Z modelActions = new ActionGroupImpl (getZone ());( W+ o! f9 Z# F) f% B
) k8 H. o! Z* y9 G) d- J try {$ o( q1 a0 R% Y; g( P& R( i7 Q3 R
modelActions.createActionTo$message
2 @* t3 ?* j6 ~. v/ y, ?/ [8 d (heat, new Selector (heat.getClass (), "stepRule", false));
; T x% e0 Q/ `9 s- i& @6 I0 K } catch (Exception e) {
- d% y% _4 K, Q- ]# Q; T System.err.println ("Exception stepRule: " + e.getMessage ());1 O5 H* Q- J a! J+ @
}
: k: F- g* r( ^8 {- H
. {7 X9 H7 ], f" L! ~ try {8 P8 Y3 ~" e. L) H+ b! w" R
Heatbug proto = (Heatbug) heatbugList.get (0);4 q- _ {7 v; I* |; V9 G
Selector sel =
. H: t6 F+ Q5 p new Selector (proto.getClass (), "heatbugStep", false);# _ @! v- y2 T$ z+ v
actionForEach =: ]6 f9 i0 x" N+ A; G S
modelActions.createFActionForEachHomogeneous$call4 g$ J* {( \& ~. G) g# c5 L) A$ {$ z
(heatbugList,& Q5 S3 w+ H$ H* h6 k% i1 a
new FCallImpl (this, proto, sel,
% W U7 `# C8 v' [: d7 L. i new FArgumentsImpl (this, sel)));
! D/ \2 f4 X/ z" Y } catch (Exception e) {: Z7 X% [; X# t# v
e.printStackTrace (System.err);7 f2 a) A3 Z5 V7 A1 K5 B
}; f: U0 e: d9 A: z0 M* w
) Z8 G$ c/ \( m+ U syncUpdateOrder ();6 M- @# }- W2 V% _
* M) L0 u) H% i1 k& A' ^ try {2 K4 |0 z5 e9 |6 l
modelActions.createActionTo$message
2 D4 S: _# L: N/ u (heat, new Selector (heat.getClass (), "updateLattice", false));
" ?) S: i& X; A' Q) X } catch (Exception e) {
8 t* L, e3 u# O( M0 H' W System.err.println("Exception updateLattice: " + e.getMessage ());3 T2 \- p+ c5 g) L G' |2 \' H. d V" m
}' x2 J; |8 @7 h0 C/ i6 S4 `: T, l2 N
1 ^* ^/ I) V4 b/ F" q, i
// Then we create a schedule that executes the
6 r* i$ X g/ t% s# E // modelActions. modelActions is an ActionGroup, by itself it
' C9 l' ~8 l5 [% ]: u // has no notion of time. In order to have it executed in- n% s3 J# F7 L3 |: J
// time, we create a Schedule that says to use the
/ R; b W2 q) _; _ // modelActions ActionGroup at particular times. This% [) }+ a' Q; v. n8 d" ]
// schedule has a repeat interval of 1, it will loop every6 }0 d A0 C9 }) |- B
// time step. The action is executed at time 0 relative to
4 T+ W2 p8 t3 J' ^7 ~ // the beginning of the loop.
6 z; g/ ?. a: X. J
) _ V% V8 U' m // This is a simple schedule, with only one action that is) W7 V" E) I7 [+ F% e( l9 s4 v
// just repeated every time. See jmousetrap for more
- e5 u( x( T" v% i. A8 S // complicated schedules.! Z# J6 d( M+ ~2 h$ V/ r
; C3 b+ D7 F5 g- I; o( ~ modelSchedule = new ScheduleImpl (getZone (), 1);4 q0 x( {' W& s: ~0 V
modelSchedule.at$createAction (0, modelActions);
5 J3 @! j% d6 j5 k9 I) s W( t0 e
3 a3 M$ E& K+ K- Z return this;" g* a9 v. Z8 _+ w
} |