HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 M7 E1 i( ~" C* R4 K
, L! Q9 D5 R' { public Object buildActions () {, G6 N1 Q- @( W. _) e
super.buildActions();
" v3 U% w M4 y + j3 d& _9 R- h, _7 H$ q8 U- S
// Create the list of simulation actions. We put these in
U& \( R L! o, _3 F! a // an action group, because we want these actions to be
( K6 d p; w. |5 W$ P // executed in a specific order, but these steps should
* s" {' v& Y( e/ J9 {3 E2 l // take no (simulated) time. The M(foo) means "The message' x) D! f% b1 X9 P3 ^# a
// called <foo>". You can send a message To a particular2 Y3 d: X& `( o& a( E: X: `! h
// object, or ForEach object in a collection., M; x4 [. q# }/ @
1 a# y$ a/ X8 |$ S& v
// Note we update the heatspace in two phases: first run" H2 b/ w x. h. |
// diffusion, then run "updateWorld" to actually enact the1 k5 P; ?0 c' \1 s9 W3 S
// changes the heatbugs have made. The ordering here is
+ D7 ^; C' W( ? Q // significant!8 v8 X* L; N5 V, B& C% @( l/ Z
{( i* u3 o( X& T. ^; n5 G% J' Y // Note also, that with the additional
- \& ]( t' V$ r4 }1 J7 y // `randomizeHeatbugUpdateOrder' Boolean flag we can
, G" I9 L% m" T! g, W // randomize the order in which the bugs actually run& g9 u2 ?' g, z7 `$ z! \
// their step rule. This has the effect of removing any! ^) L- g& Q- D0 I- T' V/ e
// systematic bias in the iteration throught the heatbug0 n' _+ N5 Z9 n* G) e! k
// list from timestep to timestep
8 d2 I7 o* \ k3 ]/ A8 [3 w $ }: H. G& S/ _+ ~. y" F
// By default, all `createActionForEach' modelActions have
9 F$ s% ~. ]6 ]4 q // a default order of `Sequential', which means that the
+ S4 `. S- Q( O! y; K: K // order of iteration through the `heatbugList' will be( `! W& c' y& {, F
// identical (assuming the list order is not changed
J& N- x9 k' W; c // indirectly by some other process).7 o3 r4 F, R/ d- K2 D/ o! w2 C9 g
. G8 f0 c" [: e2 u7 k) c$ B
modelActions = new ActionGroupImpl (getZone ());9 b* ?/ ]3 C; S+ d* c1 v
/ z& x! \9 c7 d) I! W# ?$ b
try {: _6 y, c8 M' [$ F
modelActions.createActionTo$message2 w4 B6 n; D, D( G
(heat, new Selector (heat.getClass (), "stepRule", false));
# ]8 s8 y# Y! ^! g } catch (Exception e) {
2 o7 x f& L3 ?# z3 c* D1 u l System.err.println ("Exception stepRule: " + e.getMessage ());
3 P6 {/ k4 K) z7 n( E2 K }
$ Y. N: z5 i. @8 r( B
) }9 d4 m" g# w7 | try {! o2 V$ L2 a2 h) g; C
Heatbug proto = (Heatbug) heatbugList.get (0);
( h9 m$ h9 A. N: o2 M: | Selector sel =
0 V7 P& r2 n' c! [# m! l new Selector (proto.getClass (), "heatbugStep", false); T- d; v6 I5 f5 h; j% m& i7 h0 V
actionForEach =
/ C. l+ C) Y) [% @ modelActions.createFActionForEachHomogeneous$call: u; f- C% K% ]" t+ p
(heatbugList,; O# P8 J$ R$ B! z! ]8 w& U* S
new FCallImpl (this, proto, sel,
+ ^1 E. X' d, j2 X& P% U f new FArgumentsImpl (this, sel)));2 s9 _: @' O O% ]% l' b( ]
} catch (Exception e) {
3 }% ]! C& d- L" a) x1 V e.printStackTrace (System.err);
* i) A+ E2 E2 J }$ @, ]7 ^* D& R: V0 B
( |! j; W2 ~# A, y' o- t, [- T4 q syncUpdateOrder ();
- n h' A) Z/ s5 |5 [
9 c& P, H* |5 _# A5 t7 H try {; |" j+ j4 s7 V) L# P! ~ D" }
modelActions.createActionTo$message 1 g' Q' p3 ?4 K: B* `8 g
(heat, new Selector (heat.getClass (), "updateLattice", false));* \" ^: [& x1 ]* x
} catch (Exception e) {/ }1 Y* P; }2 K B
System.err.println("Exception updateLattice: " + e.getMessage ());
' A* ]: y/ i8 P( w8 Z }
6 s: C2 s# O$ t, a* F
8 B' u" V+ w# W! B9 W: W // Then we create a schedule that executes the
6 N5 l, j0 A7 _: X; p; G // modelActions. modelActions is an ActionGroup, by itself it
; G' Z2 n) r! x/ Y! p // has no notion of time. In order to have it executed in5 [# O' g, {+ k
// time, we create a Schedule that says to use the
% }: o# X' l" f( P // modelActions ActionGroup at particular times. This! H( L: ?0 M4 v& C8 O* f
// schedule has a repeat interval of 1, it will loop every
1 w0 j. V- ?. c2 Z // time step. The action is executed at time 0 relative to
! s' d6 @. q# r* V z' q, M // the beginning of the loop.
5 g1 k/ x1 u! h# q2 x; i0 y" `* m( ]; Z
// This is a simple schedule, with only one action that is4 y+ e! Q# W, [; A4 j0 `$ }
// just repeated every time. See jmousetrap for more
/ X+ j- P# I3 S! x- N6 Q4 B // complicated schedules.2 k# o4 }2 v1 [; a) Z
- q! H+ ]3 X5 W1 m- ]0 i modelSchedule = new ScheduleImpl (getZone (), 1);
1 `, M' v& y2 P" u" u/ W% m modelSchedule.at$createAction (0, modelActions);
3 ~2 U8 L% }1 _, T- f; H
, C4 E$ M- i; \( O& d' N/ @0 k( T return this;
0 k% k" {! ], Q+ ?3 l: ] } |