HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 {% T, l B1 F1 C" X) q# w4 C
) l, J+ ?. \+ [/ a" z
public Object buildActions () {6 Q! G+ Y0 G/ v& A4 c2 H+ v8 `
super.buildActions();8 I8 m9 Q7 W4 a6 |5 U1 Q! P
7 H0 L. g) j/ C& A" D$ S2 X
// Create the list of simulation actions. We put these in8 L% R) O# _! ^" {: E5 Q9 E5 W
// an action group, because we want these actions to be( U( `( g# Z' o
// executed in a specific order, but these steps should. e& B, y/ h& N* P+ X* r) J
// take no (simulated) time. The M(foo) means "The message- h7 o( D8 Y+ t) Z- j2 k1 x' l, S, `
// called <foo>". You can send a message To a particular
' H# _! h5 i6 g6 e. ` // object, or ForEach object in a collection. k8 \+ _. a- l) @6 o n5 F
. P X) j) c9 ?
// Note we update the heatspace in two phases: first run; r- A8 i* a& Y+ G+ J
// diffusion, then run "updateWorld" to actually enact the
5 `+ g; Z/ R7 s# i // changes the heatbugs have made. The ordering here is% ^8 R, [* k. c
// significant!1 ?: X; J' O) W, e& u* c
3 @1 _) b) N# ^0 X' n: A
// Note also, that with the additional; m6 D- C9 ^* w; v; [" e1 H! q1 w8 g+ @
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 f$ _' ?- s# q I
// randomize the order in which the bugs actually run
( ], s9 x; j+ {& C // their step rule. This has the effect of removing any
3 W2 W- S8 ]* w0 h8 x // systematic bias in the iteration throught the heatbug
& C" \' L# S2 A: \* z // list from timestep to timestep9 y$ J7 o6 k7 y f9 K
. m% q; C8 H$ e. k( M4 y, K* \* I$ v/ {
// By default, all `createActionForEach' modelActions have
, Y$ R: y6 c/ K. ` // a default order of `Sequential', which means that the# x/ E/ R8 S9 y& T6 G
// order of iteration through the `heatbugList' will be
) y. V4 W F7 ~# w! E // identical (assuming the list order is not changed
* [, a) C4 `1 C" t // indirectly by some other process).( g" z9 a9 x8 l" t% t: a2 F) E
4 }" G# y* U4 q \6 a. ]) K0 j
modelActions = new ActionGroupImpl (getZone ());
e t, w: l0 j
3 x# L5 I$ c' k" f: j p- ^4 Q try {/ S; z. j8 O$ v6 H9 Y. }0 N5 e
modelActions.createActionTo$message
) E( ^ u$ z2 o- D+ q$ I (heat, new Selector (heat.getClass (), "stepRule", false));( T% i; N9 @& q: [+ }
} catch (Exception e) {
. s# G9 m) X$ s$ g3 N& U$ ] System.err.println ("Exception stepRule: " + e.getMessage ());1 |, i e! L* [4 A; m; t3 i
}2 n1 h/ E0 Z0 ?4 E* ?
/ O+ \7 \0 W& S8 f* K1 M try {, H7 N+ k3 f- q' C: Z# b6 a ~
Heatbug proto = (Heatbug) heatbugList.get (0);) Z) r4 ]/ Q; R4 N( J1 y! V
Selector sel =
$ `# }; d1 E9 F$ p new Selector (proto.getClass (), "heatbugStep", false);8 p1 j1 R2 E" A5 x/ z# I
actionForEach =
- r u3 {8 p: E$ ?" |% [) g modelActions.createFActionForEachHomogeneous$call" g7 y" T0 F) t8 n& Q
(heatbugList,
# h: M/ V( D4 d+ h$ S& R! h new FCallImpl (this, proto, sel,
# g5 ^" W2 x- ~* c8 d, V* ]" l new FArgumentsImpl (this, sel)));
" j' m# S% O7 f, n( |2 ` } catch (Exception e) {
# l. ?$ K2 t, N8 B! t e.printStackTrace (System.err);
8 H* Z- v1 H$ G2 S" X8 B }
( @8 D; a6 i: A$ l% P* i/ K5 @
' _+ f: `0 }# ~& L# r syncUpdateOrder ();
L. D8 a$ v) c1 h
: K3 k0 E5 ] K try {
; `4 A+ w' q7 H modelActions.createActionTo$message 4 P7 `6 o0 v7 e/ e% o8 v/ I, Z, n
(heat, new Selector (heat.getClass (), "updateLattice", false));; F7 Q% F- Z, Z6 P4 C
} catch (Exception e) {
( S& u! q, }; a5 w) y* E8 Q System.err.println("Exception updateLattice: " + e.getMessage ());
2 I7 R- f& J1 Q7 _" }# `+ f- r. O }& k0 [' H' u( R8 g2 H5 g
; [8 [: N" [8 c2 W; J% N+ q' g // Then we create a schedule that executes the/ j$ s; A9 j, t4 q$ U
// modelActions. modelActions is an ActionGroup, by itself it
$ }3 y$ D8 j9 Y# e7 ^3 [1 x // has no notion of time. In order to have it executed in
, O. s$ ?6 r( ^: h9 o // time, we create a Schedule that says to use the
: D2 p7 _) w, E1 ~6 X // modelActions ActionGroup at particular times. This, u6 b. j4 p; G% I) R1 ]
// schedule has a repeat interval of 1, it will loop every
$ ?/ `4 E/ J$ d // time step. The action is executed at time 0 relative to
' P; F9 E6 [& \) \8 g! O) C1 {- Y // the beginning of the loop.* Q6 B- i3 b5 J) C9 G( }6 ]% o
3 W7 u7 G) f# y2 v# S4 ~: d // This is a simple schedule, with only one action that is
5 q5 b8 F$ ^. x* M5 b' M1 C // just repeated every time. See jmousetrap for more4 [2 t$ X* _" p1 Q1 V& M
// complicated schedules.
( }3 T" B" ]2 y# ~ : d- c/ J% ~- k0 u& e
modelSchedule = new ScheduleImpl (getZone (), 1);+ A W4 z/ E4 U
modelSchedule.at$createAction (0, modelActions);
, a% O9 w+ k, g # v8 x! C) U" p+ B* H
return this;- @$ M* A( v1 y& j
} |