HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- _2 v- o, r, j# K H' S$ F# D+ Y5 }: W1 I! @: Z+ ?* J' @, @
public Object buildActions () {& D# g$ ?- {, X& u k
super.buildActions(); B3 @- S' \6 G) D/ {* G
1 e" r/ t( _! D; p' l& d) \2 Y2 B
// Create the list of simulation actions. We put these in) R4 ~, N4 R% m$ C! H0 s' v
// an action group, because we want these actions to be
" ?4 L; @* ~4 [! ]; S" `% c // executed in a specific order, but these steps should
0 X k9 b, b P8 X' Z$ K // take no (simulated) time. The M(foo) means "The message
6 E% H; r8 q. y( W // called <foo>". You can send a message To a particular" J$ g4 j# K7 w6 a; k9 J+ o8 `
// object, or ForEach object in a collection.$ q- b, ]6 l. N- r3 z. Z- S E
' ^; E3 q1 f: q8 L
// Note we update the heatspace in two phases: first run, z9 m. W) ?- B' i
// diffusion, then run "updateWorld" to actually enact the i3 r! F9 R6 G% Y, j
// changes the heatbugs have made. The ordering here is B3 F+ l( D# z& z: ?# D7 }% r8 r
// significant!
4 y/ q. K# ]5 \/ m" L8 @ . i8 K) @6 U. L) v x' |8 Z. M. `- A
// Note also, that with the additional+ P O3 l0 S) B; A* W2 M" X( [: d4 h$ F
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# R' H. k; P4 Z6 ^+ l // randomize the order in which the bugs actually run* m; O0 o" t; h* t9 }% n
// their step rule. This has the effect of removing any
' w% ^" }7 R& k | // systematic bias in the iteration throught the heatbug
+ q6 O8 s& X2 ?8 {0 b# Y) d // list from timestep to timestep/ h: \; B. o8 q
* D5 _- G/ X3 Q: @2 p- n
// By default, all `createActionForEach' modelActions have
" V3 k- u- _2 }9 s // a default order of `Sequential', which means that the
, G8 M e' p3 V/ U7 U$ r // order of iteration through the `heatbugList' will be
- x+ a4 ^) D4 w5 T, u) l, b8 ^ X0 M // identical (assuming the list order is not changed3 c) \6 }9 D/ _: c: D
// indirectly by some other process)./ E' Q9 L/ G$ o# f, h
: q; i) a8 V- B9 R
modelActions = new ActionGroupImpl (getZone ());9 O3 b" F/ b: G4 s& {
$ i9 z0 j; d+ f3 t try {
6 u% K) Y4 N! j3 M+ Y& H modelActions.createActionTo$message3 Z4 j9 U$ y! I- h
(heat, new Selector (heat.getClass (), "stepRule", false));! D: g" w4 F- x p
} catch (Exception e) {9 P( {: A) @( b: {
System.err.println ("Exception stepRule: " + e.getMessage ());
# X1 h2 Y) X% R# m& e3 k5 w }6 E) `2 G) u F2 J) r/ z
2 a( Q+ s/ v* [. i. Y* Z/ z' {
try {! G9 e6 S5 m0 A$ q) b- G3 B
Heatbug proto = (Heatbug) heatbugList.get (0);
0 } v8 t9 h- T" b4 \% D8 [) J Selector sel = " Y, T; A- l' Z4 u
new Selector (proto.getClass (), "heatbugStep", false);
4 A% p3 v4 S' u' s% U% ]7 ~0 Q! e actionForEach =5 }0 F* V# ^9 o! G4 w
modelActions.createFActionForEachHomogeneous$call
/ h: i5 D+ `' c2 x) ` (heatbugList,
& g# o |9 d% H" S- M8 `- b/ D new FCallImpl (this, proto, sel,' {9 Z! w% {- O) D% _/ S
new FArgumentsImpl (this, sel)));
3 x7 F$ z4 I( P, ~! @ } catch (Exception e) {
6 T y' R+ M% k+ b& i; o( S$ f e.printStackTrace (System.err);9 z, ?: |# g9 v) p3 M. B
}7 N; l5 V \% ]$ V0 T& Z3 L
# f8 _. `8 D( k8 |- O syncUpdateOrder ();* l4 y- M" q. j
" Y1 P% J8 ]% U9 ^
try {6 y! u; F/ y# q9 g8 r# Q$ Z% L# g
modelActions.createActionTo$message 0 X" ?/ V1 A1 y/ q) b" `
(heat, new Selector (heat.getClass (), "updateLattice", false));
& E+ i- s( U6 l7 x } catch (Exception e) {& k3 { W/ t2 T& n4 ]
System.err.println("Exception updateLattice: " + e.getMessage ());# y. A' q6 W( j1 X6 z
}
2 l' w6 q4 R3 B2 Q
; N0 I0 F ~8 `3 p; B // Then we create a schedule that executes the
* ^8 v' K5 N% [3 Y // modelActions. modelActions is an ActionGroup, by itself it0 c+ X3 Q n. M3 j# c$ I5 U
// has no notion of time. In order to have it executed in
3 r- H( `3 f- q! O+ n8 n( X // time, we create a Schedule that says to use the
) U; R- L9 q0 `2 H& u+ O; ~ // modelActions ActionGroup at particular times. This* }( |! c! y. s
// schedule has a repeat interval of 1, it will loop every( q R, D& C, h" Z: b7 n3 a" \+ R
// time step. The action is executed at time 0 relative to) |; r! T& i9 }7 V
// the beginning of the loop.0 ?/ ?9 x+ A* F6 e; o% `4 f+ z
, u' b& D7 o$ G // This is a simple schedule, with only one action that is+ k# G* }. j( j* r* ~% w8 E
// just repeated every time. See jmousetrap for more6 c: U9 O/ g: }& m+ g
// complicated schedules.
$ L# _; D" \ K a+ |- J% {! z& n
4 j! u4 b; I8 |0 P, O" I# J7 I0 s3 ? modelSchedule = new ScheduleImpl (getZone (), 1);
: p8 E: J9 f8 G+ I modelSchedule.at$createAction (0, modelActions);5 i# p* }* ~' u/ @6 X; B
7 ]- B" j! ?: S7 M0 B5 N9 ~) _
return this;8 E# [/ N) V" X5 `
} |