HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ W7 Y( z0 Q6 S+ N5 U3 f9 q: f; G
2 _; @% R# E( q! u" z, q public Object buildActions () {
& F3 a/ S- h+ R/ B super.buildActions();; X- h4 I, H, r. r+ l( ~: I4 }
8 `, x$ z( e" v$ G1 X) B' U7 r
// Create the list of simulation actions. We put these in" \; s, U! R/ o7 M: A$ B, ?6 r# ]# N
// an action group, because we want these actions to be0 e$ d0 H3 b) V* }
// executed in a specific order, but these steps should, H! v1 `3 U" B
// take no (simulated) time. The M(foo) means "The message! n4 S6 j( i4 M1 @
// called <foo>". You can send a message To a particular
5 C7 [- ]1 H/ t$ n" T- Z# M4 m, v: L // object, or ForEach object in a collection.& |/ L5 f! q0 v2 G0 b' _0 R$ z2 B
3 e) c& x: J* @, b3 T. q9 M // Note we update the heatspace in two phases: first run. F4 M0 l. P, G' u: r
// diffusion, then run "updateWorld" to actually enact the
3 n# z! o# _* z) p // changes the heatbugs have made. The ordering here is
" A! D' k. E1 n( \* l7 Z // significant!
7 u. \- ?5 W$ V* X5 f
, z) Q! S: }8 U% a" m // Note also, that with the additional
0 A& a" ]2 @8 q5 Q+ U // `randomizeHeatbugUpdateOrder' Boolean flag we can& Y6 Z9 p6 k$ s/ M; m
// randomize the order in which the bugs actually run
7 e. ]3 F/ g" r2 R6 o- t6 q5 p) T // their step rule. This has the effect of removing any
% }) S# l' @. q( L- x // systematic bias in the iteration throught the heatbug
/ w, N- ]( P% R) Y& k7 v // list from timestep to timestep4 Z: Q) d) @6 f4 Z4 i) G: n/ l) f
" y, a+ A( q; }
// By default, all `createActionForEach' modelActions have
" y- W1 u! _8 Y" x" u/ r0 y& x5 M& G // a default order of `Sequential', which means that the" Z" P: N2 l, V3 k% N
// order of iteration through the `heatbugList' will be
% z1 s" Z/ J; n/ l" } // identical (assuming the list order is not changed% T" c* p" v3 n0 w$ H
// indirectly by some other process).1 p _* T2 y5 `0 C
# u3 k1 t$ C1 Z7 _% \4 T% U
modelActions = new ActionGroupImpl (getZone ());
3 X5 b& b `( F# i
) M% x: W" P2 J V try {
% {. }/ X- i2 c# l3 h) s5 s: b modelActions.createActionTo$message
! T8 ^/ t/ d" j* P (heat, new Selector (heat.getClass (), "stepRule", false));
5 D( m0 X4 N8 H% S# C! \ } catch (Exception e) {) B# D. {: k$ M& T; g
System.err.println ("Exception stepRule: " + e.getMessage ());( W# X* g$ a" C$ q0 w& W, [
}5 w9 G M! ?9 X$ T% u
" P0 ~3 b$ }- r+ T# E+ A* U, e8 Y2 Y try {
; q1 |& T) C! t) v4 b' i0 y Heatbug proto = (Heatbug) heatbugList.get (0);: T ?( T+ B! F
Selector sel = & H7 E) x0 X. R8 g, K# W
new Selector (proto.getClass (), "heatbugStep", false);
' X& x; c5 j! w; S& o# h actionForEach =: m& ]& V5 w$ j P' B
modelActions.createFActionForEachHomogeneous$call
# _) ]1 ]" n- h- S/ @ (heatbugList,
2 c2 X# m3 O( S) t new FCallImpl (this, proto, sel,
/ e; R' O4 `9 i: o: f6 s& F2 c% j new FArgumentsImpl (this, sel)));
0 g: f6 w1 l: Q# w v1 Z } catch (Exception e) {
1 t& N' z4 M+ } V" G. [! b e.printStackTrace (System.err);' d2 {. }% R$ h8 _. u% `0 A' l2 }
}
1 }# j" C/ _, H$ a' V) q5 A1 f/ G' }
* w% x4 q2 d" f% Q/ X2 D' a syncUpdateOrder ();
6 W) p5 q3 J( Q& j" X$ m k% \3 f
( n; H# E, |7 n try {
$ s! B# _" H8 p8 A+ a1 R modelActions.createActionTo$message
' h# @. t$ Z" q! C8 v (heat, new Selector (heat.getClass (), "updateLattice", false));# L* j7 |% v6 C
} catch (Exception e) {
3 P- r! v' h Z6 X# o& P System.err.println("Exception updateLattice: " + e.getMessage ());
* i" Z4 L- X" B- L8 @ }
; j9 K6 y9 [: L+ ?& r& ?6 q 8 w/ U7 A/ N6 w: Q# H4 z! G" a
// Then we create a schedule that executes the
# }, V! \# w* c+ {5 ^ // modelActions. modelActions is an ActionGroup, by itself it) S; ] E$ ^. N: h. g- o3 Q2 M) Y
// has no notion of time. In order to have it executed in2 ]5 Z9 ]' K0 K K; P* g2 X
// time, we create a Schedule that says to use the* G( A+ {1 k# j) c, n. Z, t6 H
// modelActions ActionGroup at particular times. This
5 Y* p4 a" W0 H) l5 o$ r$ H; e // schedule has a repeat interval of 1, it will loop every& K4 K( G+ `+ Y! b8 `, p
// time step. The action is executed at time 0 relative to
* c) r6 z* J/ D3 [, k // the beginning of the loop.
! y# m1 P+ F2 X6 Y( p- i1 k+ H
6 `. H. Q- H# m7 g3 F3 x, Q$ O // This is a simple schedule, with only one action that is
6 B4 E1 q; x8 | // just repeated every time. See jmousetrap for more
! V; i3 K, ^) N" o1 S // complicated schedules.5 M$ E( G; f( c9 S5 G$ n
. Z, _$ y1 S w7 ?
modelSchedule = new ScheduleImpl (getZone (), 1);/ \' z! i$ Y h6 ~- x
modelSchedule.at$createAction (0, modelActions);
S, g$ @8 O0 g1 |$ K% F! a1 P
0 s* B% p$ D* H1 X6 N return this;
( I! i0 \7 I$ N$ x3 Y } |