HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% J! t+ n: o- _& C' V; U" w# a
& i* \& ]6 p- u: s/ ?; _' z public Object buildActions () {
- u* i" f0 C0 x9 P. H super.buildActions();
' z' M, o& Y+ b# f! r8 m. F3 O
B. M! J- b* i1 F // Create the list of simulation actions. We put these in
, \, e* R7 t7 o! T) y3 P // an action group, because we want these actions to be
" A4 ?% s: r% f1 Q' _ // executed in a specific order, but these steps should, e2 q0 h/ Y. M
// take no (simulated) time. The M(foo) means "The message
3 r! ^; f& Y$ E% X8 v% g: S1 ~9 r // called <foo>". You can send a message To a particular5 J* U4 `. C+ f/ g& Y& N3 v
// object, or ForEach object in a collection.
8 `8 H5 v' U) l+ X. ?/ I) A " L8 ~$ h- v3 k9 s" \* D/ i# g w& {
// Note we update the heatspace in two phases: first run
) \. g) `5 ~, v V // diffusion, then run "updateWorld" to actually enact the
# _: ]2 A5 j# |, q! D" F2 x# k3 B8 J // changes the heatbugs have made. The ordering here is
& s) u, C" [* y7 f/ b) V3 A+ Q // significant!
5 u* b* Z e2 x) r; Z* Z # [! o; K9 ?8 c9 g" v
// Note also, that with the additional) a4 Y1 M( s3 D8 l3 e
// `randomizeHeatbugUpdateOrder' Boolean flag we can
+ X. w6 \( {! G- A // randomize the order in which the bugs actually run
! Y# {" T, m5 v3 e& _5 a0 n // their step rule. This has the effect of removing any: J% D! J: p B
// systematic bias in the iteration throught the heatbug' ?3 `# S K% w$ _" D: I3 `
// list from timestep to timestep
- B3 c8 @$ C: H9 K8 E2 S 4 L" R. m8 ^' g6 K% [8 j
// By default, all `createActionForEach' modelActions have, e3 M. v* ?' D5 y
// a default order of `Sequential', which means that the% V" Q6 }; D1 L+ N0 U
// order of iteration through the `heatbugList' will be
?" V3 f$ s( @ z* n4 P+ }( j // identical (assuming the list order is not changed
. T; d$ X" G) @' f$ \ // indirectly by some other process).
3 P0 D; l& E+ w/ b2 B& v
: Y3 d" C# a4 `$ R& T& R3 f& X modelActions = new ActionGroupImpl (getZone ());
6 \% G! Y2 Z. U7 |2 K9 ?8 n; E& E5 a9 r/ v) g; M3 ~
try {
" b% |6 q% a3 ?+ F& f- f( j$ \- n modelActions.createActionTo$message
* y. G7 z% O/ p( O (heat, new Selector (heat.getClass (), "stepRule", false));
+ g0 a! D1 y1 n G6 C } catch (Exception e) {
& g7 }+ S- r+ w2 c; m2 _0 W3 a* n System.err.println ("Exception stepRule: " + e.getMessage ()); \9 E2 N* b0 w* S: A4 `! B& `
}
2 \$ C: C% e) W; Q8 D$ ?( I1 P$ i* k) r
try {
& K* i$ g' s0 Q6 W Heatbug proto = (Heatbug) heatbugList.get (0);
( f; x$ Y: s. d! Q7 r+ G Selector sel = / y4 p4 j& H$ i3 H" u
new Selector (proto.getClass (), "heatbugStep", false);: Y$ u+ V' C* ], }& C! s6 Y
actionForEach =8 G9 v# `- H, }/ Z0 | F
modelActions.createFActionForEachHomogeneous$call) f" p* E$ O9 s9 n" Z7 }
(heatbugList,
% a* @1 E) r3 \2 X* ?. e" R Y new FCallImpl (this, proto, sel,
) U9 s1 s" j( `$ G" `6 ?; q new FArgumentsImpl (this, sel)));4 C2 C; ?# \6 o( n, I
} catch (Exception e) {6 M* {, P" O3 ^! v9 a) r
e.printStackTrace (System.err);
' y9 A; N8 p$ \" ` }1 K( B' X8 f6 A2 I5 `! a
; Q& j9 `% t( x) |
syncUpdateOrder ();
; |; f5 A' v' _8 X1 `" y1 g, t8 R# Y. H# u# g
try {
6 ?, s" ]' N) G" j% v modelActions.createActionTo$message
1 W6 k, f/ N! q" B t& G% b# n (heat, new Selector (heat.getClass (), "updateLattice", false));( A+ n' B8 ^ {' H
} catch (Exception e) {
1 e; V* F' } b9 L2 v0 c System.err.println("Exception updateLattice: " + e.getMessage ());4 M6 v: z i8 o+ _; T) f
}
4 m8 z& }6 s1 F' A: k' { $ Z" c# Z9 S6 T3 V( T
// Then we create a schedule that executes the
- i; f/ t: S7 W1 `" V; H& @" ~ // modelActions. modelActions is an ActionGroup, by itself it$ f8 c0 u1 [( @
// has no notion of time. In order to have it executed in5 z% `( G9 Y# n0 t4 S
// time, we create a Schedule that says to use the; x K( Z* p0 ]1 U& k
// modelActions ActionGroup at particular times. This$ G5 m5 z1 c; j" S$ e$ _' Z
// schedule has a repeat interval of 1, it will loop every
. s) N# ?3 v) T+ d4 K) ~ // time step. The action is executed at time 0 relative to
' ~- D6 G2 q/ S& k) H x9 m // the beginning of the loop.5 g" h9 ?8 u" E n& J2 b
7 ^+ Y' V9 D' n% U/ Y // This is a simple schedule, with only one action that is' T1 f) U; P0 N, ~; ^; o% S
// just repeated every time. See jmousetrap for more
1 P+ v3 `$ q% x. |, } j7 l // complicated schedules.# x+ d" o/ z5 L- \6 i, Z/ P [
* M% w# c! T- v& x
modelSchedule = new ScheduleImpl (getZone (), 1);
7 {$ B& r) I$ p2 s- }3 Q modelSchedule.at$createAction (0, modelActions);5 c5 a% }; O9 b: K& ?
5 A& h9 O1 D$ ^" k( `# C: D. g
return this;8 J9 t" S* Y8 G# [2 I& r9 r( X- R
} |