HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 A4 j- v8 R9 a( J# \1 D
" z# ^ d1 m5 V3 V/ z4 ~ public Object buildActions () {
* ]" d1 \7 T% F- z super.buildActions();" w! e5 w3 Z! I5 j8 R
: E+ M/ p! X! w$ Q4 g& O& |
// Create the list of simulation actions. We put these in
. C) K* x" a& y& j" ^+ s2 T$ g // an action group, because we want these actions to be
8 U" L; n/ r: ^ Z' Y* a, J; \2 ^% ] // executed in a specific order, but these steps should& x& o% R! e% Y2 P
// take no (simulated) time. The M(foo) means "The message
" x. z+ B! M* _ // called <foo>". You can send a message To a particular
- k, c$ d/ E1 O d9 h- S' ` // object, or ForEach object in a collection.
( V; k! i: ]1 I" v9 Z6 u
0 N, Y% Q2 x& d& H7 v // Note we update the heatspace in two phases: first run' g1 k) _& ^ H8 ^" R- f* `, t9 {, K
// diffusion, then run "updateWorld" to actually enact the
3 w' }4 A$ E: k* K. p3 C0 y // changes the heatbugs have made. The ordering here is. b- G, m7 ]# y2 p/ i! I
// significant!
# K9 r) s2 M) P4 G: y$ z
2 p3 X4 d% ]3 f // Note also, that with the additional
3 a$ z, P/ ~1 l v# s- d // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 J( ]' z" _' ]6 x# @/ X# C' Y // randomize the order in which the bugs actually run$ G H. q& |7 `7 I2 f0 e
// their step rule. This has the effect of removing any9 E5 U5 [! A4 ]. F# v |, `/ O
// systematic bias in the iteration throught the heatbug
* l n; v8 s% y) \3 m // list from timestep to timestep
! X6 K% ?6 Q$ P( [1 | 7 z1 s y+ F4 _3 t7 r
// By default, all `createActionForEach' modelActions have
* r& \! r7 ]! B( u( q& L // a default order of `Sequential', which means that the. z; u6 X: v/ q( H7 E" |& y
// order of iteration through the `heatbugList' will be
) v" U( U1 m1 T // identical (assuming the list order is not changed- G8 @+ b! d/ Q2 N6 }6 s' V8 k4 `
// indirectly by some other process).
# C$ c: f+ U j, f5 ? . d" K3 d" n+ o, |) B
modelActions = new ActionGroupImpl (getZone ());
4 J, h" U- q& r* r. c* X" H# ^+ L: E% E
try {7 l8 m' P, d6 [1 z8 R- ^% S
modelActions.createActionTo$message
4 i4 d8 I4 G A- Z (heat, new Selector (heat.getClass (), "stepRule", false));$ h. i. e6 A3 x! G m6 a
} catch (Exception e) {
; ? {, }) V9 _* K# Z' J5 S System.err.println ("Exception stepRule: " + e.getMessage ());$ J; g5 B$ F1 q$ B5 g$ w
}! U2 Z& j7 I7 s& P
3 {8 A" x8 W6 r1 p6 L1 Q try {
- [6 v4 t" t' J& }+ p% q Heatbug proto = (Heatbug) heatbugList.get (0);4 @ O2 _: `. j% |
Selector sel = ( w- e8 a$ B1 ]+ \# r
new Selector (proto.getClass (), "heatbugStep", false);
; T, M9 m- P5 ^. S) y actionForEach =* L" A7 [ p; \9 m: ~& _" i
modelActions.createFActionForEachHomogeneous$call9 ~2 i5 t1 V6 W" {4 {3 R
(heatbugList,9 J; B5 O9 W) p+ v
new FCallImpl (this, proto, sel,: d& _. A( v/ \7 [$ p
new FArgumentsImpl (this, sel)));/ ^6 Q+ }8 \+ {2 _
} catch (Exception e) {
7 d4 t& m5 ~4 d0 x e.printStackTrace (System.err);/ W1 s3 M& w" Y/ J
}6 @- ~9 p4 o9 _$ B
) E! S, C7 A$ R e8 @ syncUpdateOrder ();
( f- d, M9 g5 i( F9 A( m. J* C/ P1 W" ?& q# | v( r' g
try {' _/ t3 p! q4 D
modelActions.createActionTo$message 8 V/ [& v9 c g
(heat, new Selector (heat.getClass (), "updateLattice", false));
$ H( z" x5 D3 [4 S# {$ @; d } catch (Exception e) {
3 {2 T+ ^6 D ? V0 `+ g System.err.println("Exception updateLattice: " + e.getMessage ());7 a/ |$ B" x6 P1 [
}
. V y/ r! {9 z5 f " h; p2 e4 @) s. f- p$ Q# A
// Then we create a schedule that executes the1 Z0 C8 [5 _( p
// modelActions. modelActions is an ActionGroup, by itself it
" O: ` y+ }4 E9 ^, B // has no notion of time. In order to have it executed in
# [8 U# h2 U- j) {1 Q1 E: N+ Y, V p // time, we create a Schedule that says to use the5 z% m" A3 O0 ?6 [9 m1 W/ x+ U
// modelActions ActionGroup at particular times. This
; M, b! H/ h" ]+ V2 w // schedule has a repeat interval of 1, it will loop every. ~( t' ^9 x0 _) q; O) u# D
// time step. The action is executed at time 0 relative to: `! f$ q' m, q: {0 D
// the beginning of the loop.
+ W$ q* C' u0 J- i' z2 ?/ E# {$ P( F( w3 t: U# }5 q* i' Z$ ]
// This is a simple schedule, with only one action that is
3 }4 H8 j3 K4 | // just repeated every time. See jmousetrap for more
, g; M- e+ Y1 H: C+ y- k. x // complicated schedules.
" ^( I' m+ j* L3 D6 X+ s( F
6 M* D( _: c, i1 e/ _6 m+ L modelSchedule = new ScheduleImpl (getZone (), 1);
% x2 c9 X L/ N/ ^' F modelSchedule.at$createAction (0, modelActions);, l; b" @6 ?+ b9 M3 ?) `/ p
# i2 q; G2 ]4 D3 A# h) D; S return this;
# H( p1 s8 e' }6 D; ~' J } |