HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 ]) y" ~) M7 c' O$ V ?- B3 S
5 }: O% o" V- S! u b" l public Object buildActions () {
1 D+ h2 V1 V7 _2 ~) V super.buildActions();* l5 L* } r. S4 t% h' ^
4 h- d( }9 |0 ]
// Create the list of simulation actions. We put these in
% |: V. t4 F& t$ T6 ?8 _ G // an action group, because we want these actions to be
' F! C c4 U7 s# X // executed in a specific order, but these steps should
/ E% p7 k; i. K+ m$ d- _$ v) D) G // take no (simulated) time. The M(foo) means "The message
; ~' q+ t1 T& ]% X // called <foo>". You can send a message To a particular
) \4 R: I1 a8 U! g // object, or ForEach object in a collection.. i5 {9 y* H( {, K' q3 k
9 b# U0 G0 K Q+ H- l% b$ L& K // Note we update the heatspace in two phases: first run
( L' `6 r* l$ P, S, v; x // diffusion, then run "updateWorld" to actually enact the$ z$ l( `" c) U6 i
// changes the heatbugs have made. The ordering here is: x6 w4 d7 d7 v' r2 d$ E
// significant!
S$ u$ u4 S* ]
# I$ S) B: R3 i5 V3 K0 l // Note also, that with the additional D7 ^' i0 u6 K/ Z1 h; N
// `randomizeHeatbugUpdateOrder' Boolean flag we can3 a [0 A A% _6 H* @ ^ V
// randomize the order in which the bugs actually run
' v& i& s! }7 l' w' R3 i1 D // their step rule. This has the effect of removing any7 m9 _- z( K. u) w E1 v- a
// systematic bias in the iteration throught the heatbug2 C) z- L! T9 l& |$ e
// list from timestep to timestep
; s4 i( {9 ~+ s% b# B; k# b
5 i: i. _1 N& C" C" h+ c( J // By default, all `createActionForEach' modelActions have
; N" E+ g) A' Q4 b8 h* u // a default order of `Sequential', which means that the
: T. P7 \. M1 `( N2 t // order of iteration through the `heatbugList' will be
# w. N: z$ y/ { // identical (assuming the list order is not changed
5 h/ \, o0 B j+ x0 } // indirectly by some other process).
' [# b* D K0 F) z4 f
: a! y7 L8 O- B0 _9 N modelActions = new ActionGroupImpl (getZone ());
# f; j( N* Z5 J: |# S o% [3 \
( M& x$ Q( A# ?4 w7 k2 B" w try {) G. Y4 w& X8 _5 i; N
modelActions.createActionTo$message
1 N8 N& x7 O; H8 } (heat, new Selector (heat.getClass (), "stepRule", false));3 ^% H8 B, r) M" s3 H. `
} catch (Exception e) {
b* ?1 A' Q6 k! X System.err.println ("Exception stepRule: " + e.getMessage ());) d' p1 |2 h$ C4 _) d+ B' w
}$ e6 F& ?+ a3 K$ b$ W) K" r
; w# W/ h& \/ W/ k7 B$ r try {
# }0 O" q+ \+ [* M1 o/ \6 z# L Heatbug proto = (Heatbug) heatbugList.get (0);
2 v4 x; a, I. O, q$ p6 e8 N Selector sel = 8 E- V5 I$ l* X
new Selector (proto.getClass (), "heatbugStep", false);! w0 h4 f+ l6 }" |
actionForEach =+ S: D, i( n% @0 y7 J7 u4 b
modelActions.createFActionForEachHomogeneous$call/ \4 i9 s8 D5 s. J4 d' p. E
(heatbugList,
4 T4 v6 K: i2 q& C: z new FCallImpl (this, proto, sel,
. k- G! N1 k$ q new FArgumentsImpl (this, sel)));: A( M7 [( }4 D$ }* O& j, ~% @" }
} catch (Exception e) {1 b7 q, W y7 Q7 p0 n
e.printStackTrace (System.err);) Q" ]5 ?* y H, `( ^0 S
}8 b6 N" Y+ d; f4 w: `4 Z: Z
8 a3 l: h" q4 z8 j K# V
syncUpdateOrder ();1 J( V( Q9 W1 E0 |" X7 `
, u Q" j5 Z, g. T) @. P# Y
try {5 ?. B0 ~8 c+ S1 d4 V3 _
modelActions.createActionTo$message
3 E8 g: E- l n' P- S t6 u (heat, new Selector (heat.getClass (), "updateLattice", false));) A! K+ i& ?2 f. u# Y# S
} catch (Exception e) {
# O" l) M# b1 R; d8 o System.err.println("Exception updateLattice: " + e.getMessage ());
: L) |3 x% y! G7 B1 ^( I }2 j3 j( N$ L; {8 ]6 \
( v# x v) E7 I2 ]8 O$ H. r- ^) ~ // Then we create a schedule that executes the2 \$ Z1 F( S! ?- `
// modelActions. modelActions is an ActionGroup, by itself it
) R1 u h; [5 m4 L8 p& T$ G // has no notion of time. In order to have it executed in8 z# D' p5 }* |3 w
// time, we create a Schedule that says to use the
8 I3 k; L) R( t! i: s- S5 Q: S // modelActions ActionGroup at particular times. This
( l5 \: ^( K+ z9 I* Q // schedule has a repeat interval of 1, it will loop every* P1 Z0 Y+ y" E
// time step. The action is executed at time 0 relative to- ?+ k. y. W. T. ]! p
// the beginning of the loop.; D# F, m; ]0 ~
8 O' i* K. x( }7 Y" q0 n1 w8 w
// This is a simple schedule, with only one action that is# z2 d- {/ i0 ] \
// just repeated every time. See jmousetrap for more& r$ N9 \5 @/ z
// complicated schedules.4 K9 J* U! c, t2 ~! h
, y- D" ~" k# ~ modelSchedule = new ScheduleImpl (getZone (), 1);
" T) k; F. L. ~9 C; ` modelSchedule.at$createAction (0, modelActions);3 h1 d; V- Q* G H
3 E$ N- V5 b. |* a* L
return this;6 Z$ J* u" N, p$ W
} |