HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. y/ g; U2 N- B# {( h
~$ B* _! N J& U/ ? public Object buildActions () {* n9 _; e; X2 M" l6 d8 J
super.buildActions();
( O+ Z; P+ m8 f" M8 M5 x + Z5 c) d/ a7 o! Q- `- Y" L$ p
// Create the list of simulation actions. We put these in
) n! s7 P8 u7 t0 Q; e, X3 W // an action group, because we want these actions to be
7 `2 D* G) r( f# \" o6 @' w, l% P // executed in a specific order, but these steps should
+ C4 a! S( N8 a% M; B$ w // take no (simulated) time. The M(foo) means "The message/ H7 t* D* s# g; h2 a/ I
// called <foo>". You can send a message To a particular
/ z0 f/ `) U' u7 E3 F& N // object, or ForEach object in a collection.
: n) ^' y2 T6 l 0 \1 E/ j# W0 I( ]2 R% C
// Note we update the heatspace in two phases: first run
3 Q1 p Q( p" f$ u // diffusion, then run "updateWorld" to actually enact the+ ?# g- D+ d! G% N X. z6 {
// changes the heatbugs have made. The ordering here is
0 p. Z. J0 s1 [% @& m, b3 B // significant!* d3 a) n/ x0 T, M# x
1 R" v+ i% J$ I. ~ // Note also, that with the additional
" j2 d) q7 h! C- i // `randomizeHeatbugUpdateOrder' Boolean flag we can5 d3 ?9 k, s. k
// randomize the order in which the bugs actually run
, Y; P: B7 h$ W // their step rule. This has the effect of removing any1 R. R9 e6 i# P _# c* o
// systematic bias in the iteration throught the heatbug* \: u3 `4 ]% O
// list from timestep to timestep
% D, I* q3 L: }+ n: l3 u 0 W0 o' S4 \+ R- C
// By default, all `createActionForEach' modelActions have
5 `8 \" s8 q: a // a default order of `Sequential', which means that the& ~) a& H- x) Q) x
// order of iteration through the `heatbugList' will be1 j" h$ l' N7 @. a* k
// identical (assuming the list order is not changed
2 {# T9 @% @+ |0 N // indirectly by some other process).
@' ~- j; {8 D4 s: J Q o% y$ W7 [; W
modelActions = new ActionGroupImpl (getZone ());
- d1 r# H. v+ O) ?- U- @
- {0 v/ _) s& `# N5 E try {
5 O" e( L2 P9 w$ S5 ^* }7 d modelActions.createActionTo$message
0 B/ c% j5 A: Q1 C3 X5 P (heat, new Selector (heat.getClass (), "stepRule", false));
1 q+ g! w" `2 R B2 C& q2 G! a } catch (Exception e) {5 a8 L; t n. q8 L
System.err.println ("Exception stepRule: " + e.getMessage ());
( ]) Q$ i0 j0 v7 ]% {2 y }2 s4 ~1 [1 E ^/ K; j0 b
* j1 F$ `4 o$ _/ A, w" q try {9 q9 R/ E/ J! Y3 a' j
Heatbug proto = (Heatbug) heatbugList.get (0);8 J5 W5 Q" R1 f, u( D# H
Selector sel =
' v/ c z& H* l8 v new Selector (proto.getClass (), "heatbugStep", false);
5 q8 G3 u7 ?) ]8 B3 {+ h actionForEach =
$ {$ f& k" t" s5 p; @% S) N, e modelActions.createFActionForEachHomogeneous$call
& o7 h" n- r* x& W, z (heatbugList,7 P9 ?, u7 s2 ]/ p2 O
new FCallImpl (this, proto, sel,
: R* l& v( [8 R- _4 w: z& f new FArgumentsImpl (this, sel)));8 ~5 l: P, O$ R, @# N
} catch (Exception e) {
$ Z9 I) I0 I- T9 Y/ V; a4 w& c e.printStackTrace (System.err);
5 J. r* r9 I0 O( `) K3 a" H }
+ q; K: A% H% ?) y8 k) [: t 4 T. ]( p( @ M. {
syncUpdateOrder ();
' @/ a( l2 j" ^; n# p% t& w- `; O0 c2 x) M6 ?
try {- g; Z+ b% I- W
modelActions.createActionTo$message # ?# j0 h3 E- l+ W) p1 P
(heat, new Selector (heat.getClass (), "updateLattice", false));* x/ u- C& L) Z5 r: U' B
} catch (Exception e) {
5 w. e# c0 N1 P3 | System.err.println("Exception updateLattice: " + e.getMessage ());% F$ f6 |3 }( e% o1 B
}
& T9 q1 C- T. r9 ~1 B) `* j ) q2 v- u, @% w
// Then we create a schedule that executes the* h$ G+ L; A! ^! {5 j
// modelActions. modelActions is an ActionGroup, by itself it
5 n& e: L( ^6 G T: N W // has no notion of time. In order to have it executed in# O9 l: c+ U' O. R/ n3 G) G
// time, we create a Schedule that says to use the
* y! K$ M+ @' ?' y3 b. v // modelActions ActionGroup at particular times. This
2 m. c% C8 k7 D8 _! X // schedule has a repeat interval of 1, it will loop every
. n2 ^4 R' i- d4 P // time step. The action is executed at time 0 relative to$ Y) \' w7 W% g" R4 |
// the beginning of the loop.) v) i. i2 Y# O/ p( U V/ \& @1 [
" h1 H5 L: u% O0 E // This is a simple schedule, with only one action that is
; |$ m8 z2 e* P2 H" J // just repeated every time. See jmousetrap for more
9 h6 [) T s8 K2 v$ g/ m3 M // complicated schedules.! C& k: U0 s- G; |! c& a" z
3 r$ p9 k/ }# N: Z$ S
modelSchedule = new ScheduleImpl (getZone (), 1);
2 ~3 Q9 i5 y9 p' d" p7 U) D modelSchedule.at$createAction (0, modelActions);# H1 I8 S* O, M2 [2 o- z9 {7 ^
, |; E5 f `- [4 C return this;: ?7 G# ~3 J e
} |