HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" N9 a3 W" {% n1 M# j+ G6 I1 j
0 [" T8 N' p- r/ n: E public Object buildActions () {
; i& ?2 p) k# `6 } super.buildActions();" Y3 }5 [% V4 v8 M% `) I- S, h
4 C0 i4 c9 E9 @9 n. |4 s // Create the list of simulation actions. We put these in9 @) U9 q7 [ o( [* O, ]3 I
// an action group, because we want these actions to be
7 `& O! M, y! Q5 S- P // executed in a specific order, but these steps should7 X! B& |/ _2 @' W$ l
// take no (simulated) time. The M(foo) means "The message
+ n h U, E! [' A- W: J! N; C _4 U // called <foo>". You can send a message To a particular
3 \) V0 I, n$ n+ Z) E/ { // object, or ForEach object in a collection.
$ ]/ F; H3 g( g$ i( N6 u % G! r0 J* R' Z1 \; K$ \
// Note we update the heatspace in two phases: first run
) K0 |; N1 {. G0 B; j/ J- p // diffusion, then run "updateWorld" to actually enact the
2 K1 \* V; U0 b& t* p& n5 V // changes the heatbugs have made. The ordering here is* h7 h& v; E- z9 j' p6 q
// significant!8 d) b0 n' u8 |" e4 p L$ Z( a
7 W/ J4 z% p0 Q0 q) l! l // Note also, that with the additional
1 |: w- m5 d$ z) [$ j$ ?! } // `randomizeHeatbugUpdateOrder' Boolean flag we can8 w$ l; }& q, E7 e% ]
// randomize the order in which the bugs actually run
$ E7 {$ |1 i% k8 u# H G // their step rule. This has the effect of removing any
0 X2 c, S# ^0 A3 I( T, e // systematic bias in the iteration throught the heatbug+ H- O1 z8 S1 r# v
// list from timestep to timestep
6 D0 ?9 y+ @" m/ v5 f, y 4 \( j# n7 r: t; O
// By default, all `createActionForEach' modelActions have
( d0 O: Z2 C) m0 K# l! J // a default order of `Sequential', which means that the
& t3 q4 \* T+ K A6 z // order of iteration through the `heatbugList' will be# \* z4 M- y/ ^; c( `: H! }' @( C
// identical (assuming the list order is not changed6 N. |( F0 i j0 G# J' {
// indirectly by some other process).' g& m; N( K. i$ s
# m. U1 F* o0 d modelActions = new ActionGroupImpl (getZone ());: o4 F$ J4 U5 j. r; N
+ b. T( ~; \) h8 f. @- q* w
try {! J5 V' f7 H% w4 h a
modelActions.createActionTo$message
( r0 \6 k, z8 ^7 Y% Z# ` (heat, new Selector (heat.getClass (), "stepRule", false));
7 E6 p6 u( o4 Z5 s$ x } catch (Exception e) {3 ?% T8 t# Z. O/ `
System.err.println ("Exception stepRule: " + e.getMessage ());- |- M; {* ?# o" N
}$ W b; ^+ Q7 K
0 i; j1 l8 d2 [2 ?* a
try {
1 _) \3 C% q L; A, M" d0 m0 ^: k Heatbug proto = (Heatbug) heatbugList.get (0);
& P- E8 C; I& t* |. v6 _5 q: s Selector sel = : ?) f! Y3 k& c" Z
new Selector (proto.getClass (), "heatbugStep", false);8 x7 o- `. C* J- D3 D7 g
actionForEach =
& ?4 f) ^. V$ f modelActions.createFActionForEachHomogeneous$call
, K/ F0 o9 p1 M* L$ Y7 M (heatbugList,) G7 i2 `7 d! N" J- z' }
new FCallImpl (this, proto, sel,
& J- d. Y4 }' B! X+ I- K new FArgumentsImpl (this, sel)));/ f$ K: U. r& ]* ?/ f% q% Z
} catch (Exception e) {
4 L! \: A8 k7 _- R- N e.printStackTrace (System.err);2 F6 V- o% r8 i
}
% O" W1 I; w" ^5 m # H2 E) @2 w$ Y8 }9 a9 s
syncUpdateOrder ();
( V. r) o5 m' t9 I( O8 I
! C( O1 g! e7 s9 P% G" F try {
8 e3 e- u4 u' h8 W9 a+ e7 Y modelActions.createActionTo$message 1 Y, ^" P4 z0 `3 Z9 H+ I! X: ?# e
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 ^. l+ }* Y# L4 Z5 ]& @* C } catch (Exception e) {4 p$ D' q! }; J! b: X o5 O% p
System.err.println("Exception updateLattice: " + e.getMessage ());+ `& s+ K) K! }1 h% y
}- d4 Q* }/ M/ p
, ~% Q2 V, {( d0 I1 | // Then we create a schedule that executes the" E$ l1 M' A0 k
// modelActions. modelActions is an ActionGroup, by itself it- D4 A$ \" p7 |/ \8 j$ A
// has no notion of time. In order to have it executed in9 Q7 `, X0 [ F& j; d1 Q
// time, we create a Schedule that says to use the4 T1 K( j2 H9 u8 h8 @4 m
// modelActions ActionGroup at particular times. This
0 W* h6 ?' G9 Z // schedule has a repeat interval of 1, it will loop every# j L# f6 O6 j9 m& h
// time step. The action is executed at time 0 relative to. z5 o9 e5 q8 p8 Y+ K, O
// the beginning of the loop.
- O( i, N; \6 z# T) [# z! ]0 w& N/ T% ]) v' ?$ |
// This is a simple schedule, with only one action that is ], R$ t) k! k4 w% G K' T0 c1 H8 O2 X
// just repeated every time. See jmousetrap for more0 E3 a* t1 B ^4 z4 @( l1 T2 J
// complicated schedules.
4 X& w$ n- I, q w
6 a4 ]( K9 m, d7 F& D9 U modelSchedule = new ScheduleImpl (getZone (), 1);2 n5 A) o8 w/ t' C& a
modelSchedule.at$createAction (0, modelActions);
/ N9 K6 C# W: o4 x# { ) F' X* C( T3 Y4 P0 O
return this;
1 K k' \9 j1 A) Z: a* d' c } |