HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' M" _4 t s, ]' x; a9 o$ F! J8 i3 \" x" a
public Object buildActions () {0 L5 D5 c8 B7 l/ N
super.buildActions(); T$ J9 d9 I: p) A6 j; D6 j
3 |! o1 f' ]! Q7 ~: ]3 D
// Create the list of simulation actions. We put these in0 m/ Q# s: b% V; P0 e
// an action group, because we want these actions to be' ?6 J- W* s) } M4 E
// executed in a specific order, but these steps should
/ d/ Q+ u4 v% j T- M+ o9 B, k6 K // take no (simulated) time. The M(foo) means "The message
# ~- n; K/ n2 t* X) @ _/ } // called <foo>". You can send a message To a particular0 P5 d$ S% d8 f. L0 C
// object, or ForEach object in a collection.- U5 e6 p+ o$ l3 K3 A+ O5 Q
! j3 H2 J+ J: b7 W& y: }
// Note we update the heatspace in two phases: first run
$ m2 {+ i* r2 O* F6 M: ` // diffusion, then run "updateWorld" to actually enact the
7 k7 G. B h( s+ ] // changes the heatbugs have made. The ordering here is( T5 n r+ `3 d+ G) e
// significant!2 X' f! o2 l! V2 A' v
2 I z! f' t. o& n
// Note also, that with the additional
7 T& @3 F/ o- f3 @3 I7 O* m2 k // `randomizeHeatbugUpdateOrder' Boolean flag we can
) o! Q6 Y) _# Z- W // randomize the order in which the bugs actually run
2 G- H0 ^ z% l; K; i // their step rule. This has the effect of removing any6 u$ o+ J& a7 r8 c4 `
// systematic bias in the iteration throught the heatbug1 C& @7 m9 o( U. a1 i* z" c7 Y
// list from timestep to timestep: ]1 A% v3 Q* N3 H2 ~9 \
) K% t6 t: S0 l // By default, all `createActionForEach' modelActions have
' W. W7 W! _% e- d0 u- {# S // a default order of `Sequential', which means that the
2 e6 F) `1 N/ D7 Q // order of iteration through the `heatbugList' will be
" s. V8 U" F& l6 x // identical (assuming the list order is not changed2 o# c5 }7 J" j9 A* t# ?( {
// indirectly by some other process). u* `7 S' n; D2 O
* Y; T& u% B0 k/ ~- _ modelActions = new ActionGroupImpl (getZone ());$ B) l" s* Q+ X+ [. g# }
/ ]# @7 ]' m; l( B try {* U, _* M2 o" O+ j- E1 N
modelActions.createActionTo$message/ W7 U1 [* z+ p6 Z
(heat, new Selector (heat.getClass (), "stepRule", false));
/ Z( e$ Z3 r$ D2 r F8 t } catch (Exception e) { |& f" B% k \0 l# {
System.err.println ("Exception stepRule: " + e.getMessage ());7 C \& F" o, O% M y! x
}
9 V" |2 H2 S0 }9 S& F) H' r5 M
/ l/ \; r! P/ T6 I! x7 \ try { S% |' W) P8 y; K, O6 ~, y& \; P
Heatbug proto = (Heatbug) heatbugList.get (0);
& d1 t/ ^3 s6 x- B3 Z( L Selector sel =
0 e- G! F9 R- F/ U new Selector (proto.getClass (), "heatbugStep", false);
3 f8 W8 ~! |+ o7 A# x actionForEach =
8 A. e3 g! |3 `; s9 T2 j' E; B modelActions.createFActionForEachHomogeneous$call
! B+ @ a- _, `) |( a5 U7 u1 j, r (heatbugList,; R! m+ d" ^& z- |
new FCallImpl (this, proto, sel,
# a1 t5 k$ M: g, S! M new FArgumentsImpl (this, sel)));
( r( g: G1 q% M1 G" R8 w" l } catch (Exception e) {
( Q7 _* H' f) D, {- l3 z e.printStackTrace (System.err);" R$ Y. p5 M- q, ? t( |- b) d% ?
}0 a7 n6 K8 @: ^" A, P3 g9 u
' _, z0 T b, A' i) n syncUpdateOrder ();6 z3 Y7 R/ l8 s' v& ?1 @5 i, P7 w
* [4 e! M9 F6 m" @, A9 V
try {& Y( ~, R( P) @- g1 Y+ s
modelActions.createActionTo$message
& C0 }- q+ u$ _1 b5 n3 ^ (heat, new Selector (heat.getClass (), "updateLattice", false));
2 r+ ~" B/ V6 O" Q+ ~* D } catch (Exception e) {' N& _# {! j8 k
System.err.println("Exception updateLattice: " + e.getMessage ());- R/ F# x. h5 Q, K
}# z6 e& L9 | }9 J
# z2 z! I; k& R# | // Then we create a schedule that executes the: p# a0 C `& Q& k1 [ i! Q
// modelActions. modelActions is an ActionGroup, by itself it
( N+ O' ?" N; a$ t, O6 d // has no notion of time. In order to have it executed in
6 |: a% i2 ]. }. L: z- n4 y // time, we create a Schedule that says to use the
8 ^4 c9 j& a- q% v8 `. c5 y // modelActions ActionGroup at particular times. This
$ ~0 W$ C, ^0 q: u // schedule has a repeat interval of 1, it will loop every' R1 S) K5 I: Q( h$ t* g
// time step. The action is executed at time 0 relative to
7 K3 O( U7 B$ q& M3 d1 U* U$ n' v // the beginning of the loop.
, h3 e) s; b/ ^1 |7 _
7 }4 S0 L+ e& i0 ]* i" ~+ ^ T // This is a simple schedule, with only one action that is+ h* \& C8 o, D5 `; E
// just repeated every time. See jmousetrap for more
: K' y0 o; p2 O' _$ b // complicated schedules.
, Z5 U, E9 N# u, j! u y- @" W( i
- q& [, n3 M( [ modelSchedule = new ScheduleImpl (getZone (), 1);+ }0 H* [5 ]8 J2 m, p& Z; k% c
modelSchedule.at$createAction (0, modelActions);+ W% J" L0 p0 z5 O; U6 v& f. j
/ o4 D. j, ?# E0 j- { o/ h return this;3 d1 j) ^8 x& w# W: o9 {7 B! x4 D
} |