HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: X+ _ J( }1 O$ K9 E* w4 r6 K
' u$ e7 y) P: Q2 u9 O; ^
public Object buildActions () { a; O& E% Y$ B& y& A" q
super.buildActions();
* D2 L1 X0 x, S- C 8 y- _) u. O& Y* U9 a6 ]6 m) ^
// Create the list of simulation actions. We put these in
: I/ g1 Y+ m% f; G) T // an action group, because we want these actions to be
$ O* X/ O- j2 K- j // executed in a specific order, but these steps should
/ G- l' N; d! H* Y8 T1 |5 B, }) H // take no (simulated) time. The M(foo) means "The message
1 O$ r8 O; f8 j! s5 v. W5 ^ // called <foo>". You can send a message To a particular
1 z+ C7 L, q! b; W# y- Z // object, or ForEach object in a collection.0 R4 _% Q' m7 ]& `: U+ C! J) \$ M
; ?' @6 o2 @( y0 {( Q
// Note we update the heatspace in two phases: first run( i2 _# J0 W$ n! p2 v
// diffusion, then run "updateWorld" to actually enact the
( s9 q0 N; P2 V( y // changes the heatbugs have made. The ordering here is; P4 T, Y/ N" _* ^( M, m, r
// significant!
. t' c7 ]6 _. y" O+ L0 O
7 G! U; E+ v2 w* k3 L // Note also, that with the additional' w; s1 N8 X) R1 _5 I5 g2 M
// `randomizeHeatbugUpdateOrder' Boolean flag we can) p5 i& t, z$ \& S: O" T0 H6 J
// randomize the order in which the bugs actually run0 m( d6 g* L' s' X
// their step rule. This has the effect of removing any8 \5 Q6 ]% t5 e
// systematic bias in the iteration throught the heatbug
: j5 @2 s+ v! b // list from timestep to timestep
5 J' W+ S) \- h: g3 {2 S
6 U+ s* K4 y& L. p V // By default, all `createActionForEach' modelActions have/ k: |5 [2 L$ c2 O0 w( n2 W8 }9 i( R' j
// a default order of `Sequential', which means that the1 D7 D# E) q4 O- P. X8 s
// order of iteration through the `heatbugList' will be0 K J% v, t2 |0 E1 }) A* b$ x6 J. {
// identical (assuming the list order is not changed
1 Z: I& Z, a. S+ p, Z! l# Z // indirectly by some other process).9 J1 m) h% e4 Q8 e- c5 x" n' w
; N2 K1 A( I6 A1 h) ^+ F& t5 [
modelActions = new ActionGroupImpl (getZone ());; F* ~5 I6 W }: o
) M! o' I& b! n9 l) W try {
( ~; G: U; R$ M$ K) E modelActions.createActionTo$message4 H8 x2 m" m( Z/ D' l* ?
(heat, new Selector (heat.getClass (), "stepRule", false));4 C3 l5 w# M1 ~
} catch (Exception e) {
( h) ?1 F0 i$ r, @' R! e System.err.println ("Exception stepRule: " + e.getMessage ());
8 r6 [" C3 C8 q! |. d. A: l; _8 [ }4 q2 e& q3 j3 b4 q& Y, }" M( L
( B' u0 K6 r8 a s! @3 A try {
0 c2 v, v" e% F! x. e1 Z Heatbug proto = (Heatbug) heatbugList.get (0);) y4 Z& G, f/ |3 \, V2 R! t1 z. W
Selector sel = 5 W! `. b$ J3 X! n
new Selector (proto.getClass (), "heatbugStep", false);" d: a$ w- q; j1 p) t u
actionForEach =
' p1 Y9 O: z" E2 R* r. D- A' Y* F# r modelActions.createFActionForEachHomogeneous$call: _! W/ _! q& e* [( i
(heatbugList,' Q) E8 u/ U) y9 C; U2 }
new FCallImpl (this, proto, sel,
+ z* v) H* H* g' ~ new FArgumentsImpl (this, sel)));; f* F. v( K( U9 C) v: ~
} catch (Exception e) {
9 D/ n- v a+ n/ x8 l e.printStackTrace (System.err);
) i' G3 J9 K1 F" {( O$ L. V; J+ u }9 G, n% F1 _3 \% H
. h9 T5 E1 B" t$ R- O7 M4 r8 I9 \
syncUpdateOrder ();$ ]' t- [( H a2 ^# v
; @1 A0 O) Q7 S9 }/ p
try {" m4 _8 _2 g0 O' i0 p( O
modelActions.createActionTo$message . s* V+ n6 Q- C; ~ v
(heat, new Selector (heat.getClass (), "updateLattice", false));" u7 c& f6 @, Z! V1 k9 A
} catch (Exception e) {; I& [8 a8 h0 f. [+ }8 i. t
System.err.println("Exception updateLattice: " + e.getMessage ());. c) H+ s2 R6 f0 a+ K
}
* k+ _6 \' C! }" ?$ m8 i. w4 m4 b $ {6 ^4 a: x+ J: P
// Then we create a schedule that executes the
1 n: M1 W) {4 r# g! g+ P8 r // modelActions. modelActions is an ActionGroup, by itself it
6 N. z, O# h0 Z* W // has no notion of time. In order to have it executed in
3 ^# g8 R: V# n. W: i: D) t$ } // time, we create a Schedule that says to use the
) d' m V' \1 I! O0 `, ]6 ?! s // modelActions ActionGroup at particular times. This! x- X+ O* _: V4 F- i2 F
// schedule has a repeat interval of 1, it will loop every, t6 B: n2 i/ n0 y+ y+ b
// time step. The action is executed at time 0 relative to
2 `2 H5 h$ o1 B4 p. j: e4 @ // the beginning of the loop.
4 Y. `" b' \: G( s
. B2 z6 Z% d {6 w, }1 G' z // This is a simple schedule, with only one action that is
8 A: a6 s/ d3 z) o J) h( [/ h // just repeated every time. See jmousetrap for more
1 y+ U4 f+ p% e" U9 L1 R# a3 b // complicated schedules.
, L1 H2 ^5 D7 L+ } |. O# o
2 H: I' I: f+ _7 i modelSchedule = new ScheduleImpl (getZone (), 1);$ e; ?9 A% w9 `# X
modelSchedule.at$createAction (0, modelActions);
% m5 D5 @2 l& L2 g8 G2 g# W / Y5 r) w6 C" A, ^$ i- X
return this;
9 X+ E: E3 w0 p* K0 U/ j } |