HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# A# I) R% j& r4 {. o2 B7 L5 T3 y/ }1 \# }4 v
public Object buildActions () {
) |9 U! p4 E% R s& q super.buildActions();( D, ^4 @8 `, x9 `2 x6 k4 E
) [& x3 _& `4 P. W( O
// Create the list of simulation actions. We put these in0 e1 t( Q% a, A. e$ V, G" `
// an action group, because we want these actions to be: E) D3 h8 a5 W4 [/ Z
// executed in a specific order, but these steps should: o6 l9 W# M, o1 \& r) H
// take no (simulated) time. The M(foo) means "The message# ?5 n$ j6 c+ M2 C& q& w: \3 h# P
// called <foo>". You can send a message To a particular
7 z0 D5 m' M$ D2 R) d0 Q // object, or ForEach object in a collection.
q1 z$ }# v& d: H2 k H 7 Z# b" ^# q- P# P8 [+ F; G- F2 N
// Note we update the heatspace in two phases: first run
) v$ r' ^' N& n+ F$ J4 \2 } // diffusion, then run "updateWorld" to actually enact the
. \% z7 B3 F7 H) W% ? // changes the heatbugs have made. The ordering here is
& a. ]$ W& @+ T // significant!1 g; a2 q, c6 e, H4 n8 B# w+ f g6 y
# }( A; N, Z/ n+ z) H6 x' H // Note also, that with the additional7 T6 [$ I2 U) P3 l7 L4 m5 O. p2 y- C
// `randomizeHeatbugUpdateOrder' Boolean flag we can6 R7 {+ c8 z2 M* J, o1 a
// randomize the order in which the bugs actually run% p+ D5 u8 I7 |* t' ^
// their step rule. This has the effect of removing any
6 Y. D6 j% p# [. m // systematic bias in the iteration throught the heatbug9 S6 d: g6 H1 Y; ]8 g* D: h! c
// list from timestep to timestep2 u! b N7 D: I
- f4 Z& r. R. M6 @8 G3 I // By default, all `createActionForEach' modelActions have( n2 P) @! I& o* G4 x" Y! z3 P" j
// a default order of `Sequential', which means that the& f. Q W) a# R% k4 ^& W8 M
// order of iteration through the `heatbugList' will be
8 d# m Y- W0 D$ k$ i // identical (assuming the list order is not changed$ e: ]0 |; p( N, r9 O
// indirectly by some other process).2 S" n' A3 A9 @0 e$ }# b- q
! D) N9 o, b+ `* m# P9 X* O
modelActions = new ActionGroupImpl (getZone ());
* _0 c( O8 R) L6 [! E7 ]
6 h: s& G( y ~2 |- Z try {7 M" y; y' w1 |
modelActions.createActionTo$message2 h, n$ W8 U9 R7 L) K
(heat, new Selector (heat.getClass (), "stepRule", false));
2 p9 Y2 k }. z8 }, u } catch (Exception e) {( H( v' s9 G1 L0 L6 O% @
System.err.println ("Exception stepRule: " + e.getMessage ());: T" q/ V$ F' U/ Z
}' {9 z; q* v3 G4 u8 W. D$ H$ j
0 z: l6 T" i9 |1 _" M try {+ t- ^1 ^; d& P7 c4 a6 g% o1 K
Heatbug proto = (Heatbug) heatbugList.get (0);
6 u! F$ }" l% i' Z Selector sel =
, W" t+ f- T2 G& b new Selector (proto.getClass (), "heatbugStep", false);
5 _; S5 K; ^, l actionForEach =# F) Z: u6 r" ^
modelActions.createFActionForEachHomogeneous$call
% k \7 J @* H9 J! P1 I (heatbugList,
. _7 p' ]4 Q/ P7 h7 h new FCallImpl (this, proto, sel,0 ^! l N0 L- c' i
new FArgumentsImpl (this, sel)));
8 {- k# E. V- T' ^& L } catch (Exception e) {
+ M9 a8 k5 E {) s! G e.printStackTrace (System.err);8 e. G) I% i, \, \- S
}" t9 T$ f- k& W! i, v
$ j% y" U: |# S6 m4 ^0 z- G syncUpdateOrder ();7 ?* X' S' O8 {" R3 ]
- Q @8 W5 p. R) P2 n" u. y- D9 D
try {6 M1 o) }) h- p- b7 O8 f: u
modelActions.createActionTo$message 0 l5 m( e8 K" C+ H7 F3 J, }- T2 R
(heat, new Selector (heat.getClass (), "updateLattice", false));
) }) m" E/ _; g& f } catch (Exception e) {- R. `" v. V. C. O
System.err.println("Exception updateLattice: " + e.getMessage ());
5 A7 I) D3 l# @5 Q6 L" A8 N" Y3 X }
! d9 |3 `: b# V3 L; O) A3 l 5 [5 ~2 z V: D) s$ R
// Then we create a schedule that executes the
6 e9 X4 f& w) j5 ~$ B& o // modelActions. modelActions is an ActionGroup, by itself it
0 t+ v8 N) L% t A+ o/ Q: L // has no notion of time. In order to have it executed in
: g5 m2 ~0 u) \: x2 q" Y4 U* ?3 k // time, we create a Schedule that says to use the
: \& I! U- m, p# D- P( l4 u; Z4 S4 s // modelActions ActionGroup at particular times. This
( O/ ^$ I( l, Z, K // schedule has a repeat interval of 1, it will loop every
, T1 c2 n( f8 C8 t+ U // time step. The action is executed at time 0 relative to$ Z/ |- w: s5 h h7 V3 _
// the beginning of the loop.
" X+ h. |7 v9 b$ O8 F. B2 I3 l1 \% W
// This is a simple schedule, with only one action that is6 P. C( W8 f! A! @
// just repeated every time. See jmousetrap for more
' F, E" w- N% M // complicated schedules.2 A# F( x& i" f& h9 n8 Y
+ V6 u( J7 \$ y. n! [ modelSchedule = new ScheduleImpl (getZone (), 1);
0 u- U# ^) a5 a/ X+ {( k. y modelSchedule.at$createAction (0, modelActions);
+ u& I) m# b" ?. u9 n; X$ z . V6 ^# S- q! u% w3 ^ w
return this;7 O! h6 J1 e; p, f8 e
} |