HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& H& Y# M3 U2 D6 N, [5 J
; z5 M: U7 Z$ Q) B2 V/ H
public Object buildActions () {
, C+ O2 s/ h- P super.buildActions();
5 a* n3 u% K4 R
8 N1 ]4 @. \1 t$ g) K+ z" d" X3 m // Create the list of simulation actions. We put these in
8 r ^, l- X5 S) j* ?5 n/ z // an action group, because we want these actions to be
. B; ]0 B" p: t7 i // executed in a specific order, but these steps should! K; |7 y% ?8 I& K- @' \6 V. Q
// take no (simulated) time. The M(foo) means "The message
- V1 |4 F& |& @7 X4 W: T // called <foo>". You can send a message To a particular9 u( S- }0 z/ }% V, s2 _
// object, or ForEach object in a collection.. x6 \9 Q% U4 b, Y; {8 y
0 J1 S$ A: N7 S0 C8 M$ f0 k0 n# D
// Note we update the heatspace in two phases: first run' k1 M* j r. E: h" A
// diffusion, then run "updateWorld" to actually enact the9 v( M3 M6 l* H) {
// changes the heatbugs have made. The ordering here is
5 r2 H9 t5 l6 x0 m; B+ [ // significant!
- m. m1 W6 S9 ]4 Y/ y4 m ) e. _& h0 D) R7 h, b, h
// Note also, that with the additional
; E$ F: D4 M3 b* F% m6 r, ? // `randomizeHeatbugUpdateOrder' Boolean flag we can
' `- g$ \. [5 J. f // randomize the order in which the bugs actually run
5 V9 O! ~8 {' t5 f. R // their step rule. This has the effect of removing any
x* |: p0 k& }+ x D8 [) ` // systematic bias in the iteration throught the heatbug
6 V' z( m+ q/ B* s, D // list from timestep to timestep! e6 q# U) W0 x4 p# c! x
2 o) B% ~ r3 F( C0 L" F" j M* U // By default, all `createActionForEach' modelActions have/ G% T K0 L' Q2 |( T0 v6 W
// a default order of `Sequential', which means that the
, }9 m$ |, @( d: g. S5 D // order of iteration through the `heatbugList' will be
" |0 @% n6 L8 E // identical (assuming the list order is not changed2 S: C0 x3 a0 J% k5 `- w
// indirectly by some other process).
+ y" s u5 G1 k! k, S2 V! d . }5 D% ~( h. N- B
modelActions = new ActionGroupImpl (getZone ());
& Y$ }! E" d: p$ k- ~
& m* p n# |% m5 w8 t. W0 m% [ try {
. I4 ?9 F& I$ W7 X% U2 B: e2 g modelActions.createActionTo$message6 V1 L$ ]/ U2 [& ~1 x/ K- q- G7 z
(heat, new Selector (heat.getClass (), "stepRule", false));( h9 j* C' h+ |7 b
} catch (Exception e) {9 F9 y% v# M8 Y" t6 C
System.err.println ("Exception stepRule: " + e.getMessage ());
n c1 `# Z/ D# N$ p }
3 @7 _1 L& r2 f/ l) m% B& Z7 Q+ C8 q6 I- @) O2 y
try {
8 _2 w3 O6 D) t Heatbug proto = (Heatbug) heatbugList.get (0);% [6 g; G9 A& ]" o: [
Selector sel =
1 q, \! C1 G: ? new Selector (proto.getClass (), "heatbugStep", false);
; x. J2 d' U& a: V. P actionForEach =; U$ ]9 X8 Q5 H, M
modelActions.createFActionForEachHomogeneous$call5 J! y U% X* ]7 i$ c
(heatbugList,
# [+ w+ b8 N& h& B new FCallImpl (this, proto, sel,- R' r3 r7 f1 K8 {9 P! z, v% G2 R
new FArgumentsImpl (this, sel)));( ~4 l9 g2 z m; f. _6 X. c, ]5 e3 O
} catch (Exception e) {" R _7 @4 s7 w8 D$ c
e.printStackTrace (System.err);
; R* R+ n( R/ l p+ r }
0 c( Z, z/ J- m8 H" i # W/ a& J1 K, D! Q
syncUpdateOrder ();, @7 X3 K: D1 \
, j. F; Q5 j% O$ u
try {- N1 E6 s; G$ _# i. i" d0 k
modelActions.createActionTo$message
2 v- a* m: R& S% v (heat, new Selector (heat.getClass (), "updateLattice", false));
( o0 I0 R) L# [4 H } catch (Exception e) {* b+ Z3 X; B. u, p$ ^
System.err.println("Exception updateLattice: " + e.getMessage ());9 I6 a9 o/ A+ I; _" s; n
}
/ O* s% ^% Y7 ?* m5 ~7 ]
9 C e1 ^% |& |0 B2 ^- ^: }5 ]1 A // Then we create a schedule that executes the
+ |% E4 }- p9 i // modelActions. modelActions is an ActionGroup, by itself it
# o+ A& y, ]( \6 k7 M; C // has no notion of time. In order to have it executed in
6 U5 f- C& Q$ x5 w // time, we create a Schedule that says to use the
* x% K( \* \# c, c% R3 ^2 s // modelActions ActionGroup at particular times. This
. k4 P$ R) x4 ~3 D0 q- ^ // schedule has a repeat interval of 1, it will loop every
1 W- g4 ~2 ]$ J( l // time step. The action is executed at time 0 relative to
c2 m" b A: F$ q( s0 k1 o // the beginning of the loop.
2 F# {6 a! B. d; W g9 ?- A8 ?( @, q0 n5 D1 P5 A3 P, r
// This is a simple schedule, with only one action that is
3 R# N' u8 k4 [/ K/ l* j // just repeated every time. See jmousetrap for more
8 }! B! ^2 @+ g9 H& P2 | // complicated schedules.
: n. V: @- q4 y: {6 S# J5 ^$ ?3 |
. b' O9 o+ p+ q$ h8 C modelSchedule = new ScheduleImpl (getZone (), 1);
' a3 Z3 j/ N( {0 O modelSchedule.at$createAction (0, modelActions);
- U9 y$ p( T) i7 r
& {/ E1 z3 q, y' b+ z6 U- M return this;; Z3 o" F) r$ \6 `
} |