HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 T( o8 n x9 y; h
2 c" l/ ^: {( X# O public Object buildActions () {
, x1 [- S* x4 }+ l) R3 f4 u super.buildActions();
: e+ |# I: f% \% D3 R 8 z. J: _9 W6 s# M
// Create the list of simulation actions. We put these in# |4 v& r$ Q, ]/ k: e
// an action group, because we want these actions to be0 F r, c$ Z0 l8 U
// executed in a specific order, but these steps should0 C' W1 a' ~# I H/ |5 X; A* d5 A
// take no (simulated) time. The M(foo) means "The message+ u P, r. b8 e y5 K4 e: H
// called <foo>". You can send a message To a particular. x' M8 `& W& A. F
// object, or ForEach object in a collection.
' h* P4 R" W: x- s! r
/ @& u# C" V+ \( ]* z% `5 T8 O' s // Note we update the heatspace in two phases: first run H& n% Y4 W( \
// diffusion, then run "updateWorld" to actually enact the* L# o; r7 i3 x* N
// changes the heatbugs have made. The ordering here is
% J' p, E6 {# L7 f6 ]; l // significant!
- w Q$ ^3 I$ n0 K! f# B( }( Q : t3 h; `- u! {- S8 V: |. R
// Note also, that with the additional& r1 j1 s; L7 t$ j8 z6 {/ Y
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* @* [% Q- b6 v; [" w5 J // randomize the order in which the bugs actually run1 a$ }7 I' X2 k
// their step rule. This has the effect of removing any. A. t) H' B" ?" T: y( J
// systematic bias in the iteration throught the heatbug
" ^9 e. K/ | s& _- K o& W // list from timestep to timestep* S) ?8 C8 K; q. X$ f. Z
7 O' O% E! \; ~# e, a. z
// By default, all `createActionForEach' modelActions have
- m, O% x9 E: Z. H) n // a default order of `Sequential', which means that the
. m1 W7 z& z6 h, D& w% n) ]/ `6 R, ? // order of iteration through the `heatbugList' will be
* {0 s* ?9 y2 c6 z // identical (assuming the list order is not changed
3 l2 V. e1 t9 U/ ?+ V // indirectly by some other process).
* f9 W7 A$ x0 S4 A& p/ d
9 A9 y. ?! n9 X9 Q modelActions = new ActionGroupImpl (getZone ());& ]0 r; L8 o1 B1 Q0 i: K8 q6 |
% j7 C% {: n2 w6 @3 t0 ?, L5 A try {
0 u5 Z7 D# I8 Q7 f+ J modelActions.createActionTo$message
' ?9 F4 s* C; G& R (heat, new Selector (heat.getClass (), "stepRule", false));
8 g# H' {: a$ C8 x+ K' O } catch (Exception e) {, a/ d! c1 `, C' x& Z" f& n# ?
System.err.println ("Exception stepRule: " + e.getMessage ());
- b9 e$ l* ~3 ]3 r }4 D9 Y) g* P8 s' X9 F
* M E! E8 c! m1 ]+ x) y try {
' M3 A2 \. o" n' q1 D Heatbug proto = (Heatbug) heatbugList.get (0);
: v5 i9 C+ @5 r2 ] Selector sel = ' x* | t3 Q1 H2 {5 v5 k
new Selector (proto.getClass (), "heatbugStep", false);' b; J: f2 J: q% s5 _) q( u8 R
actionForEach = k* ~+ p2 D; Z1 a* L. t6 c
modelActions.createFActionForEachHomogeneous$call
' j: I! q) a9 e0 ^5 T8 @' i2 ^- E (heatbugList,
9 V: U" N4 }" v& E new FCallImpl (this, proto, sel,
! D) M, n+ t0 W/ [( Z new FArgumentsImpl (this, sel)));
: i7 V# C. o( v7 P0 z, g } catch (Exception e) {% m! {3 Q& x. z. T: K$ Z( B
e.printStackTrace (System.err);
6 B) L) i- b: g- t% E }5 g7 z4 Q& G$ F0 z3 `8 v
, h& I7 F0 w8 i: V( f" ~7 R
syncUpdateOrder (); _* C6 A! L0 r, Y' A2 u
( @- f, Y: t N' v try {
6 d# j+ T1 Y+ W {; d0 ]' y modelActions.createActionTo$message
# G- l" K. ]6 j, ]2 J' h) s/ q9 g (heat, new Selector (heat.getClass (), "updateLattice", false));) b# G! _* M- O9 j. W
} catch (Exception e) {
: |$ O( n6 N3 G2 I" z System.err.println("Exception updateLattice: " + e.getMessage ());9 X( J0 {0 ?/ G) g# h1 s
}
1 {1 A3 o3 e) @; j2 M" h 4 j; N7 k7 }( N+ Q2 @" V& w
// Then we create a schedule that executes the
7 Y% N+ {' K0 A3 T& a1 f // modelActions. modelActions is an ActionGroup, by itself it
' R, r8 w+ E5 c9 A' O // has no notion of time. In order to have it executed in
" i# g* ?1 R+ Y+ G# w2 S // time, we create a Schedule that says to use the
1 P. s% ?# ^& n$ w0 v* \) \1 F6 x // modelActions ActionGroup at particular times. This; I8 w3 i0 [. v6 w/ K: Q/ n
// schedule has a repeat interval of 1, it will loop every
, Z6 T% f: d8 H' x9 N // time step. The action is executed at time 0 relative to
5 q! T: j& \" T" D2 B* l) R, w // the beginning of the loop. E2 {6 h+ \) I2 ^. P
. f7 v/ _' X% X6 R5 J$ u! u% O1 L
// This is a simple schedule, with only one action that is
) i; f% ~9 ~2 [5 X- w) m // just repeated every time. See jmousetrap for more
# ~. g8 o4 ^' ?! D$ a/ q6 S // complicated schedules.
* z- j @9 z# f7 v9 f$ b! Q* f9 f
7 o% r* f# U5 Q) ]4 } modelSchedule = new ScheduleImpl (getZone (), 1);
- N7 b2 z* d: J) C, n modelSchedule.at$createAction (0, modelActions);! A! j& o: B2 e5 d/ O/ v
- R0 \: _& O$ J, C0 d" C
return this;
4 H1 Z- A# m1 l; Y1 _- k5 J5 c } |