HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 J7 ^7 A" M5 A$ q
2 Q5 S+ p6 ]: g1 r6 L; E/ r* n public Object buildActions () {
- [/ @0 ~! M& y" M/ @; Q% E+ { super.buildActions();
9 J9 _5 O) h; S c" B3 X8 t+ @
// Create the list of simulation actions. We put these in/ \ T8 b5 o7 X$ n6 l0 z& d
// an action group, because we want these actions to be$ O e. O& m& w, e; a3 R
// executed in a specific order, but these steps should, a2 i8 ]' n/ R& r7 X
// take no (simulated) time. The M(foo) means "The message$ e% M5 ~; c/ m8 ?9 I$ f/ E9 t" \
// called <foo>". You can send a message To a particular% F9 H# W. }. W* p( W
// object, or ForEach object in a collection.* j' f1 `& j) M, f/ L
$ [0 K+ w* w/ A) } n+ m // Note we update the heatspace in two phases: first run: z6 t) K+ n7 h/ m" C6 V1 w
// diffusion, then run "updateWorld" to actually enact the6 c2 L" G% G( f
// changes the heatbugs have made. The ordering here is
4 m8 z G$ ~6 v // significant!, M! j7 _# Q9 z& j
4 \0 Q1 h, w; D8 [
// Note also, that with the additional+ Z% f0 J+ ^( H& f* G
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ v' ?! r1 ^; k& Q // randomize the order in which the bugs actually run2 [) k9 p+ ^% w& t
// their step rule. This has the effect of removing any
- L+ w k8 H3 z/ m8 Z // systematic bias in the iteration throught the heatbug
( \3 R: o: e9 d4 S1 S1 T0 r5 u, v // list from timestep to timestep
. V. X0 K/ c. ~0 p1 F/ e- Q5 B
' K- ~2 ]$ K; t // By default, all `createActionForEach' modelActions have
4 I5 c" ^; Z. P5 J# g, c/ l/ ?6 v // a default order of `Sequential', which means that the: T6 Y" f% b) C: D
// order of iteration through the `heatbugList' will be
& e1 d" N% v% }$ S# k( o // identical (assuming the list order is not changed
# I$ U" L+ S1 u, |& r! e' [ // indirectly by some other process).6 ?9 d, ]) p% \% {6 P
9 \; p* z F1 w
modelActions = new ActionGroupImpl (getZone ());' l: ^2 ]; T0 `$ h0 I( |
! X! B1 Y/ r6 h3 Z7 g( }
try {
& ^$ o% l1 p% y! q1 ^ modelActions.createActionTo$message9 [% _% B( \$ l4 N4 p
(heat, new Selector (heat.getClass (), "stepRule", false));
2 w- G) i9 W9 J. n1 A" l6 l7 Z ~ } catch (Exception e) {- x; E8 E9 M9 h" A& z
System.err.println ("Exception stepRule: " + e.getMessage ());
% L4 N, h; g, _ m4 J2 u4 h; X+ \ }. b8 A% w$ ^5 }, G! M
% b. M; J- Q) B4 X9 x4 g try {
* O+ k5 f; |9 g9 s. j# N. z" M3 x Heatbug proto = (Heatbug) heatbugList.get (0);
6 B- K3 Y. a$ R0 e7 P Selector sel = ' p/ ]! T/ c. s! @2 [3 h. ~/ W
new Selector (proto.getClass (), "heatbugStep", false);
& d9 u" A' J& z7 _& o, Z actionForEach =/ [# M' g: |4 F0 j- \9 ?7 G8 Q9 l& Z
modelActions.createFActionForEachHomogeneous$call
; x0 m: T8 S! {$ w' \6 F; l (heatbugList,, F* J3 o$ @% o6 m
new FCallImpl (this, proto, sel,
8 N& E! m9 O$ [" _3 h new FArgumentsImpl (this, sel)));2 F$ g1 p9 h) ^
} catch (Exception e) {- D8 q' r* e; v5 R7 X
e.printStackTrace (System.err);" M& c9 m' ]6 C% O% {3 c
}
" f& X. I& [6 I; u
. b/ c; ?' _' [3 @0 C+ b syncUpdateOrder ();& K/ c6 \; S& D
; j( o1 u" \/ d$ t2 a. c try {
x, w% p% G( Z modelActions.createActionTo$message
8 q6 O4 M0 j. ~. Z (heat, new Selector (heat.getClass (), "updateLattice", false));
/ ~1 F* ]+ _5 e1 I } catch (Exception e) {' N3 S( X1 e4 f1 J) D6 @+ \
System.err.println("Exception updateLattice: " + e.getMessage ());
+ D5 N1 r9 d, k9 f% M6 v, n! \ }
6 V' A. x0 [! _" }5 g9 @
0 h8 p0 |; X. U1 e( N8 s8 `3 } // Then we create a schedule that executes the* @ K1 E9 M8 `. w2 l( d
// modelActions. modelActions is an ActionGroup, by itself it
8 O0 e- W9 H% w: h // has no notion of time. In order to have it executed in
6 {5 o5 `; h, _ // time, we create a Schedule that says to use the
* P4 I A9 p: |+ n0 { // modelActions ActionGroup at particular times. This
+ }8 L: c' f5 ] // schedule has a repeat interval of 1, it will loop every: e- @" t$ U: q& I* d$ L3 J% I
// time step. The action is executed at time 0 relative to
1 F! V/ X3 O: f // the beginning of the loop.
8 z# P, F- T# e/ W$ L6 r1 A$ ~" n9 Y! q' k. X! B* b- {" S# ~; ]
// This is a simple schedule, with only one action that is
3 \% ~" R; w, f8 ` // just repeated every time. See jmousetrap for more
$ S/ o4 r8 U& U' e // complicated schedules.% j/ ?( n+ g2 r+ s, r
% {% P9 r8 M S+ v8 j
modelSchedule = new ScheduleImpl (getZone (), 1);& G) v( L5 x1 m# e2 ]# S6 Q
modelSchedule.at$createAction (0, modelActions);
9 n9 C5 ^8 |' z; { 2 H( S. H( P( b, H2 j: W4 E$ w
return this;
+ f) H3 _/ K* D; s P& d# o9 ^' M } |