HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: A! W5 a! N* v4 Z& t$ C1 c2 t$ Z* V$ _# ~
public Object buildActions () {
$ }& K4 W5 [0 w7 f+ u super.buildActions();" D9 G+ z" B( _; G
O: K5 i4 U Z1 P$ V4 F- @5 O // Create the list of simulation actions. We put these in
. w1 y2 D$ F+ q // an action group, because we want these actions to be0 [+ D0 Q" P3 P# h! M
// executed in a specific order, but these steps should- J+ D5 b6 {/ v* P, j0 n2 [9 b
// take no (simulated) time. The M(foo) means "The message
+ \% ~8 O9 l- ^+ { // called <foo>". You can send a message To a particular' j# O7 t" z2 y6 Q% j/ u
// object, or ForEach object in a collection.* j- p7 O/ U: u% Q7 Y3 o
( w( ^% Y$ F z
// Note we update the heatspace in two phases: first run
) p6 D- R, i- ^3 z" @/ j // diffusion, then run "updateWorld" to actually enact the
) P- O2 x, t& U6 t& V6 \ // changes the heatbugs have made. The ordering here is& E3 t( t. H1 [4 v3 O
// significant!
( o$ }3 Q2 f6 P1 R, ~
2 z) O! e. [5 ]5 T- ]; N3 T // Note also, that with the additional' C* N) u y A" N5 R. X! t
// `randomizeHeatbugUpdateOrder' Boolean flag we can7 L: G* I& } Y v7 P
// randomize the order in which the bugs actually run ?+ G) ?5 R* B: B5 J/ P2 L
// their step rule. This has the effect of removing any
$ H' ]$ A! { i* }0 y1 ?) w* y L // systematic bias in the iteration throught the heatbug
% I* I/ a( J( X# B: a // list from timestep to timestep7 X+ i' c/ r$ X" F" {
# `+ B3 F7 ?$ u2 T* n
// By default, all `createActionForEach' modelActions have& |: S* e; U- e0 I- i# q
// a default order of `Sequential', which means that the
G' }/ z- a" M. e // order of iteration through the `heatbugList' will be
7 _6 z% N, N, }) k% h# i // identical (assuming the list order is not changed0 [ z8 z0 Y- @9 l+ Q8 ^
// indirectly by some other process).* P9 B9 W; y9 C: p& B/ d1 o
& K2 Q: ]" h& ]4 }& q
modelActions = new ActionGroupImpl (getZone ());
- w* c6 a7 k& F# l# b( q4 g7 c2 j/ G( Q) }& E4 @0 H6 f! s6 Q" o* |7 Q2 n7 \
try {, O4 y. v( P# \& Y
modelActions.createActionTo$message
. O$ p4 Z. D# \; u8 `( y (heat, new Selector (heat.getClass (), "stepRule", false));4 m0 E$ {! t [) W- d: |5 U
} catch (Exception e) { b) ~/ k& V B3 O7 K! K9 C" W
System.err.println ("Exception stepRule: " + e.getMessage ());
0 {5 ?- Q$ O7 c7 p- y5 H9 n }
$ F/ B/ _% Y0 p/ I! Z2 d( v1 c* U' r8 H/ w$ c8 j0 ~ p8 ?
try {3 |0 ~8 ]7 W1 Y* o. F
Heatbug proto = (Heatbug) heatbugList.get (0);3 Q% b g: V) u0 V
Selector sel = 7 u6 S/ z, B5 S O+ B- h# x9 Z2 a
new Selector (proto.getClass (), "heatbugStep", false);
2 ]" y% r! u4 X; |; |: t# ~5 C actionForEach = n' ]1 a2 g# }8 b0 @
modelActions.createFActionForEachHomogeneous$call
- m( d* j: N$ T) }& A/ @ (heatbugList,
3 Q/ [6 U1 b% w8 m4 |& ?! [; Q new FCallImpl (this, proto, sel, [9 q9 `( B7 z2 V9 \& F4 C/ h
new FArgumentsImpl (this, sel)));
1 p! R: g `: \/ w } catch (Exception e) {
7 O3 r. h; Z) `$ |: q e.printStackTrace (System.err);
* ]1 F& g# o3 [7 k& @ }
* p6 k3 ?; H* \$ y
U* T5 P \7 n5 T5 w0 l/ m# f& O syncUpdateOrder ();. h6 y7 O/ {" q4 L9 C
' v2 ^$ O2 ?4 {4 {) n7 u try {
( b8 {# {; C! j. b+ \0 F4 v% V! ~ modelActions.createActionTo$message
; o2 u( }7 R& {6 q; P& f (heat, new Selector (heat.getClass (), "updateLattice", false));/ F2 b% a) Q9 _( X9 k ^+ }
} catch (Exception e) {
0 N' c8 h0 i) [- k System.err.println("Exception updateLattice: " + e.getMessage ());
" j, E3 G: I+ H }1 i% u+ N' ?3 S% i/ P H- R6 G% ]) d
c2 e$ m2 j$ X$ R% _+ S2 O8 ]
// Then we create a schedule that executes the/ `# @7 w9 [ P
// modelActions. modelActions is an ActionGroup, by itself it- O8 ?8 T" K1 D& Y; j; q
// has no notion of time. In order to have it executed in* x% h8 l8 Q k1 q
// time, we create a Schedule that says to use the
) r5 q) Y Z2 R/ i // modelActions ActionGroup at particular times. This
* S5 |3 ^" c* Q$ F // schedule has a repeat interval of 1, it will loop every
1 n) }5 l! H" n# Q) C8 c f // time step. The action is executed at time 0 relative to
. Q; h+ _; a* g) Z // the beginning of the loop.! I# U$ T1 x$ f" d
5 P ^2 n. w0 H: f // This is a simple schedule, with only one action that is
+ A+ l- S7 y6 D7 m // just repeated every time. See jmousetrap for more
: T: w1 }+ ]* H5 r' k; F! v. F' e // complicated schedules.
! b0 V" H% A0 v ( ?6 C# f/ a3 ?& W" X# f' A9 o: J
modelSchedule = new ScheduleImpl (getZone (), 1);9 d" L4 _9 |: L5 Q
modelSchedule.at$createAction (0, modelActions);
, l% w" W, r& i9 M J+ N9 A; | , _) |! `2 X: X+ m# ~
return this;
" ^; ^: A& B9 l1 p } |