HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ T( b v! G- `; `5 F7 c, B
; U) L( [* d: p, f) b public Object buildActions () {
3 A' s/ p8 P# |9 E, \ super.buildActions();+ U2 h+ W5 ]& m0 x7 D7 W
. o V" g: a' Y5 Y/ E
// Create the list of simulation actions. We put these in! {6 \2 f. s, ^# ]' g
// an action group, because we want these actions to be, k1 W" ` w7 Q# x9 h/ t) Y
// executed in a specific order, but these steps should
6 N0 a1 F6 v! U9 b7 I! r5 R // take no (simulated) time. The M(foo) means "The message5 h8 ^7 M: S6 R' r! u
// called <foo>". You can send a message To a particular5 f% m Z* f. H4 |; `# S3 t
// object, or ForEach object in a collection.
! a+ l' K/ v0 H* H0 S $ C& c. v8 K* s9 h
// Note we update the heatspace in two phases: first run1 G4 ~) ~* ~9 L9 M
// diffusion, then run "updateWorld" to actually enact the
7 r: Z. @% [0 M/ V% \# A& t9 b5 H // changes the heatbugs have made. The ordering here is$ }2 x' U! G( C
// significant!
, K$ H8 r/ u& O4 ]9 [# S/ Y 3 L2 B- S6 ~0 f! l( d/ z
// Note also, that with the additional6 L7 z( X B; h3 E% \) C: Y9 e
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& A! x$ f! b% {! ^ // randomize the order in which the bugs actually run
3 b) q4 P6 |; x // their step rule. This has the effect of removing any
8 I, Q" o" L2 y0 s1 [( |1 k$ ` // systematic bias in the iteration throught the heatbug
$ G4 k4 w( E, l% k6 A* W L) } // list from timestep to timestep
# Q7 |" j: ]7 S2 U+ |! t$ g/ m/ U # R' c- T( M* A* W& J+ h
// By default, all `createActionForEach' modelActions have
: l8 S1 |' K3 ? // a default order of `Sequential', which means that the6 k2 Q+ _# c5 n' L
// order of iteration through the `heatbugList' will be
. ]6 a0 Q4 h8 u2 V7 ~; _3 \% D // identical (assuming the list order is not changed
' s% I5 M+ H. `' d // indirectly by some other process).3 b0 K& z) g0 y! R1 {" T
+ d9 A) {/ N9 x9 J7 n6 q
modelActions = new ActionGroupImpl (getZone ());& |+ K; e1 M7 L8 [) w! K
: p2 U2 m+ p. Q$ V7 d7 S
try {
: i& ?+ @- q3 X+ J. a modelActions.createActionTo$message
* O; L7 M& r5 B g8 O (heat, new Selector (heat.getClass (), "stepRule", false));: x0 \6 a9 m" {& x4 w0 W
} catch (Exception e) {
7 E% M8 H4 j3 K6 m" B% X" Q System.err.println ("Exception stepRule: " + e.getMessage ()); }0 r& s& s9 W
}
0 b, x7 ^ t3 K! l; r- P2 ]9 w$ f+ p! S& q
try {
$ ^1 A) }' P, Y Heatbug proto = (Heatbug) heatbugList.get (0);2 O4 z4 l& p) N5 }! t5 b2 z
Selector sel = , V7 @& ^5 P; N# _
new Selector (proto.getClass (), "heatbugStep", false);3 c5 e- V6 m6 K I5 h
actionForEach =
% b, s& P9 e5 ^; l modelActions.createFActionForEachHomogeneous$call
1 y- @# Y8 Y3 j( d (heatbugList,( _6 E) _& @: @2 ^7 z) ]4 o
new FCallImpl (this, proto, sel,: L/ n" S! [7 Y/ h
new FArgumentsImpl (this, sel)));+ t a' l3 O( n- E# j
} catch (Exception e) {
: ?$ B, B4 `2 Z0 ?3 w& h7 @! } e.printStackTrace (System.err);# H n' q2 \* K
}! G) e3 Z5 Q" Z0 Z
4 d" K n7 X/ e
syncUpdateOrder ();6 D) G5 I. W6 f1 g
4 q/ t) z3 _* {. \7 p7 V, f
try {8 z) M2 h- B4 F0 V6 L4 D
modelActions.createActionTo$message 6 a" l. H* m* c; K2 S
(heat, new Selector (heat.getClass (), "updateLattice", false));5 G& j6 d) |# m# R }% |7 }6 j
} catch (Exception e) {8 e+ f A( @8 e5 y, o
System.err.println("Exception updateLattice: " + e.getMessage ());. u7 w# o8 s9 U5 w
}
# [& ~% K# Y( f6 ~9 x* z
% ~, J/ L$ G }/ v# w( T // Then we create a schedule that executes the h; ~7 v1 h5 q# v% b& L6 k
// modelActions. modelActions is an ActionGroup, by itself it0 E& p$ Q/ h0 ?/ }; i# |8 S
// has no notion of time. In order to have it executed in
n5 Z; l2 D% e- j0 I // time, we create a Schedule that says to use the- E, u3 t, Z% K+ S) W/ p
// modelActions ActionGroup at particular times. This
7 t' Q e w6 [. O1 ~6 r // schedule has a repeat interval of 1, it will loop every* A9 D- I" X, e8 S5 R
// time step. The action is executed at time 0 relative to
7 Z* W' A9 a8 ?, N" q9 |$ F // the beginning of the loop.3 p$ h- C4 g% p! E% ~ f8 p+ {3 G) N' e
2 j r; u3 r( I3 C* y$ l
// This is a simple schedule, with only one action that is J) k$ g* y4 D! c) m6 P( D; T, G
// just repeated every time. See jmousetrap for more
1 D* ~+ R$ r' S // complicated schedules.& Z( Z( Z& O. Z( @ j
; g7 Z4 A: @8 x& C g
modelSchedule = new ScheduleImpl (getZone (), 1);; c( H/ Y- J. ?& G5 T( o
modelSchedule.at$createAction (0, modelActions);
. e* e- P& ]( B4 `2 A* I! @ ; V& {: _" o2 t, l- r' y2 K+ m- \
return this;# ^& Q( O$ q+ D9 J
} |