HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ V7 t" [7 L: V$ h4 ]
; ~/ {4 G/ c. S" @- o public Object buildActions () {6 \9 X) X% h7 _9 v
super.buildActions();
. ~# j! Y- x" a% P! K3 W6 d" b5 z7 p
$ P7 K3 M Z; F" P // Create the list of simulation actions. We put these in4 X" F; x% K. N' U* ?% _. l* v J5 o
// an action group, because we want these actions to be$ o( |5 i% Z3 r: _+ G
// executed in a specific order, but these steps should
r$ ~+ Q5 ]) Y( _! M! f // take no (simulated) time. The M(foo) means "The message
5 r7 T8 n7 m' a // called <foo>". You can send a message To a particular
! p% ?6 o6 E | // object, or ForEach object in a collection.
7 h/ d8 L- K; l# e' H8 P* [ , i6 o" G; D9 e
// Note we update the heatspace in two phases: first run
; H# D# D. C. r; _& [ // diffusion, then run "updateWorld" to actually enact the
8 E7 t8 T8 r! k5 m4 m // changes the heatbugs have made. The ordering here is F; ]8 M; Y! k- M' y* j1 R
// significant!
1 m- w$ w/ [1 W# q! w
. O- A+ X- l1 e; t // Note also, that with the additional1 |8 b) T. H$ h3 O4 g9 j' i
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 G3 Z8 u8 S5 o4 n$ o
// randomize the order in which the bugs actually run
- F# E- v V0 ?3 ^* @# M0 L // their step rule. This has the effect of removing any- r$ B( a8 b- @/ x8 h2 n
// systematic bias in the iteration throught the heatbug
: C6 A# k" ] a8 ~% J // list from timestep to timestep
; i$ c+ j3 o# }5 n
( B2 N+ T* ~, c0 X" g+ [ // By default, all `createActionForEach' modelActions have
; `, G M& z& x2 b0 O* I4 d // a default order of `Sequential', which means that the& W- U; _+ @( ?; I
// order of iteration through the `heatbugList' will be
: Q' L0 J/ n+ T& w+ O7 Q // identical (assuming the list order is not changed
) |$ ~ [7 p- p0 W // indirectly by some other process).) G5 r" \% y4 [2 A
0 I* s& r1 j: u: o8 V4 T modelActions = new ActionGroupImpl (getZone ());% z8 Q% U" }6 B) H1 C
$ i& E/ }+ P( S; h3 | q
try {6 m5 X% s. Z; [0 [( r K. |
modelActions.createActionTo$message
6 j; _ x. d0 {0 g" [! M (heat, new Selector (heat.getClass (), "stepRule", false));
: G; o: T2 w9 r1 ^3 s! H } catch (Exception e) {7 K+ i0 S, h2 j/ Q: n7 ?; V
System.err.println ("Exception stepRule: " + e.getMessage ());; Z0 N! H1 Y( |% u+ j& G
}
* b* z. t/ }8 W( a% C4 G& e
3 n, q; Q4 ~: {, f4 O try {/ [; ?0 e9 x5 v9 @
Heatbug proto = (Heatbug) heatbugList.get (0);, e- M5 `/ Q* r1 a2 K- m
Selector sel = 5 b/ ?" z% Z: _0 ]# r
new Selector (proto.getClass (), "heatbugStep", false);/ d7 ?9 P5 _* w4 r5 T3 }
actionForEach =
9 q9 X( X/ Z3 w8 o6 T modelActions.createFActionForEachHomogeneous$call
0 h5 Z( d7 O% I7 T9 l* {1 Z (heatbugList,
9 I6 ]/ s# I3 ` h+ e new FCallImpl (this, proto, sel,
, @ ^4 }$ p6 I new FArgumentsImpl (this, sel)));
1 n; x$ ]& z/ ?9 ?$ z7 c } catch (Exception e) {+ y. a: i* T) U4 \* P5 W' i$ j9 W5 L
e.printStackTrace (System.err);. j$ K" p- G* `4 V& ]
}1 Y: L5 a$ }# B
5 I9 u/ S- _$ F4 c$ A0 i7 `1 u0 E8 G syncUpdateOrder ();* j$ N! ]; d% N: n
3 @9 c1 J$ b K* w6 O' b try {
# s( _# B! S' _3 e9 F! P modelActions.createActionTo$message
0 b& j6 r5 i7 h" _) l# j; S( p (heat, new Selector (heat.getClass (), "updateLattice", false));+ l/ _! q w4 n+ T6 B' r p1 R8 t
} catch (Exception e) {
/ I1 ?1 K0 n; \0 @7 v) h5 c, ?5 l System.err.println("Exception updateLattice: " + e.getMessage ());
: i' \2 x& ~) y/ a }; A$ L' Y4 p1 x2 X" H! W
* k: `) u$ J7 X' x4 o4 D
// Then we create a schedule that executes the
* L$ D k L/ ^/ G+ e% k // modelActions. modelActions is an ActionGroup, by itself it
/ j% T/ _5 ]" K9 L8 B' M- }9 M* X // has no notion of time. In order to have it executed in! s* E& T @# H5 R" m! t
// time, we create a Schedule that says to use the
1 [4 ^( U& F3 Q. x+ v" z // modelActions ActionGroup at particular times. This' ~) V- g9 U: _
// schedule has a repeat interval of 1, it will loop every( I' M7 F- F* L% [
// time step. The action is executed at time 0 relative to
, `& w+ m& `' i. Q // the beginning of the loop.
: ?4 e; @6 {0 ^! {! Q# g( S6 U" \
; ~) U7 Z9 g3 |- _# Q2 v2 M: K // This is a simple schedule, with only one action that is+ E$ m& z9 d9 I' t" ~2 R& B
// just repeated every time. See jmousetrap for more1 a3 g+ L) Q/ d0 u) s
// complicated schedules.9 I- V# q1 G" h+ i$ h3 I/ y4 J
; ~8 p2 I' i8 r% v3 o; d9 b modelSchedule = new ScheduleImpl (getZone (), 1);
# `6 ?! Z1 b; ~7 l# z4 q. f modelSchedule.at$createAction (0, modelActions);
- Y$ u! c3 m, D$ H2 e* r - {7 ^/ f. {" i4 V& E: x5 t" W$ r
return this;5 F2 b+ g( K& c7 l+ P, |
} |