HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 O3 K6 S( ?' x5 P2 D2 c: ~
. ?' j& ?" [3 s7 K public Object buildActions () {
8 y6 b* N5 q" m$ R super.buildActions();
- M3 i4 A* J; r# _- z: O ( e) `5 ^" }1 E6 L* p- {7 W! Q
// Create the list of simulation actions. We put these in
$ L4 ]" M& R! W: _5 Q // an action group, because we want these actions to be& U' |5 `4 _, z$ X# z! F
// executed in a specific order, but these steps should) {9 U- {1 V @
// take no (simulated) time. The M(foo) means "The message
d- e. O& K* v& `' g // called <foo>". You can send a message To a particular. O/ V F' [% Y& y7 X F
// object, or ForEach object in a collection.
2 D+ t+ q) S. j) ?: k
: d9 L; F: y; {" @7 o // Note we update the heatspace in two phases: first run
6 s$ t! j( o8 F( y- O2 n6 l( t8 e // diffusion, then run "updateWorld" to actually enact the
$ s3 ^ q+ B2 {: A' s4 u4 n- e // changes the heatbugs have made. The ordering here is2 R/ E/ k$ v7 c# ?, H5 S3 s
// significant!( e. {5 b O6 L2 K3 a- T
, b' {& u: I+ u0 a c; a4 x2 C" c
// Note also, that with the additional1 [. q! L9 w) s; ?) u: O
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ U4 o) |! q: J
// randomize the order in which the bugs actually run
4 ]* n; m |" g! I( F+ ~- D // their step rule. This has the effect of removing any6 `/ l/ ~ _, B8 k3 O
// systematic bias in the iteration throught the heatbug4 Y4 `( j+ ?9 _" | |
// list from timestep to timestep! e' l4 Y/ d$ N
, W6 R) D/ H% U- m7 S' K // By default, all `createActionForEach' modelActions have
1 ` ? z8 g2 x, E ]4 Z' Y // a default order of `Sequential', which means that the
* v& T2 v1 D0 d9 a. Z8 ^ A // order of iteration through the `heatbugList' will be
% t$ [$ ~! P( e: ]" ?4 [" E // identical (assuming the list order is not changed
: y! b- @/ N6 }! c( u# v2 n$ h // indirectly by some other process).9 s$ U" n4 U5 m- |' j: B' r0 s
" }8 U7 L/ y. G B) [* b! [
modelActions = new ActionGroupImpl (getZone ());
+ T6 f/ G. F# P6 k3 N
; h K/ }* E0 \6 r try {! N4 v( |. g5 ~
modelActions.createActionTo$message
/ Q6 d4 @7 ], s7 V/ ?1 B. ~: E (heat, new Selector (heat.getClass (), "stepRule", false));
7 Q3 m7 D* q. v% [ O! a2 F } catch (Exception e) {6 g. i9 F1 Z/ h& G" l5 N8 Q4 E
System.err.println ("Exception stepRule: " + e.getMessage ());) u# ?3 o1 ?" r
}3 D" d& N# E4 m! ?- A2 R' d
/ S* @5 k5 N. U- _
try {
E8 E G/ V( R Heatbug proto = (Heatbug) heatbugList.get (0);
! z' {' k9 a% L Selector sel = . W' U. c6 Q# G8 Y5 J" g% Y9 c
new Selector (proto.getClass (), "heatbugStep", false);
7 J+ c2 K9 N2 ^/ i. U! g actionForEach =
& O6 f5 D; B: s/ U4 q y3 I modelActions.createFActionForEachHomogeneous$call
0 E5 v g g6 a2 w0 P4 S$ p2 ]: ? (heatbugList,
/ W$ l5 M$ U5 y3 Z# R) h, E) V0 a new FCallImpl (this, proto, sel,9 O7 B7 y$ {6 L
new FArgumentsImpl (this, sel)));; Z1 X0 ^& O. x4 ~- E( W
} catch (Exception e) {
7 m" }7 f, \5 U* L Z" d% g e.printStackTrace (System.err);+ z7 W3 ~) n% Q, J, ^1 J3 p6 K: F
}5 ?" ^) B( N2 R4 u1 J, ^& M
; f3 U0 E( e7 _% {. w
syncUpdateOrder ();6 N' O2 m; h, H5 z* H$ @
3 L0 H) `. ~5 r7 L' f. b
try {
8 t8 S2 Y, D3 \7 y1 E% P modelActions.createActionTo$message + S9 n# U- ~: o* v
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 D. {- v1 [7 q! Y& n } catch (Exception e) {2 V2 i# Q+ M, Z
System.err.println("Exception updateLattice: " + e.getMessage ());2 {& M2 {4 b' w$ @
}
% o8 P) ~9 g- K0 @
# b" g V4 W7 e' y$ y) t0 ^9 Y // Then we create a schedule that executes the
( T) M, A& ~' c/ C! L. t // modelActions. modelActions is an ActionGroup, by itself it" a; H% H: j- l' d- S2 G
// has no notion of time. In order to have it executed in, _1 `( Z( d1 J
// time, we create a Schedule that says to use the! z# z6 F+ y+ g+ V
// modelActions ActionGroup at particular times. This
& {- W9 ~9 p$ n( d* o2 W // schedule has a repeat interval of 1, it will loop every% T$ T1 u; x' h- A- R C3 x
// time step. The action is executed at time 0 relative to. e: ]8 C. N) w# j# k6 L
// the beginning of the loop.$ a. `: o+ d: x- M
# K2 e' G" s: ~! n7 A+ g // This is a simple schedule, with only one action that is
( f4 V4 o/ ]1 S // just repeated every time. See jmousetrap for more
) {# }, }4 x/ n, n% D // complicated schedules.
0 A$ A/ v4 U. ~3 }, |* f 8 X H- m+ y+ R) u4 A
modelSchedule = new ScheduleImpl (getZone (), 1);
) J3 U6 F) K3 [0 r# S4 _, _7 B" {: B modelSchedule.at$createAction (0, modelActions);' ]+ ^2 y+ W: |; B% U) J
8 v2 O1 q! ~1 \( T3 D( v0 G' g0 Y0 P
return this;$ l6 o8 B! i7 m4 u& Q
} |