HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( V% o4 K% ^& t6 b
$ x$ s6 L/ q) p! D6 v; o0 Z
public Object buildActions () {7 G5 \( N" |$ H2 J! h, G8 g- [
super.buildActions();" `# Y8 x/ N) U* H$ a; {$ j
1 O7 R; J" o7 b, U
// Create the list of simulation actions. We put these in
" s3 \; m8 o( O( G, W // an action group, because we want these actions to be* ]! [ O% ~& q- G J, U7 i2 L. _
// executed in a specific order, but these steps should
& f: a6 Z. K' }! N! K // take no (simulated) time. The M(foo) means "The message
, W f& n& V5 b) l2 M- Z) g // called <foo>". You can send a message To a particular& w5 k+ b6 x9 Z
// object, or ForEach object in a collection.; q8 V4 p1 U6 j2 j
' G, X' f* V' Q3 Z7 t- K4 E // Note we update the heatspace in two phases: first run1 b5 U. K1 e) m. I9 j7 @( x3 J$ P- ~
// diffusion, then run "updateWorld" to actually enact the# d' I& L7 U; p+ {1 Z" _
// changes the heatbugs have made. The ordering here is
3 l& N, B8 I. r3 H1 M6 O' o7 l // significant!
3 X H( x% R2 o. S' o! C 5 w6 J$ z4 _" Y9 n. i
// Note also, that with the additional3 u2 g7 N* d* S9 g% u$ q2 R
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ t. [; T4 r+ }' e; S3 `, e
// randomize the order in which the bugs actually run
) Q/ O8 N3 x ~9 K" T6 G0 T+ Z& A // their step rule. This has the effect of removing any1 r7 `9 k. v1 C$ O
// systematic bias in the iteration throught the heatbug
$ V/ v2 h9 Z: O; e // list from timestep to timestep
( l2 N* X+ p2 O+ u9 S
5 v0 z1 [$ Y% z# c5 q; V // By default, all `createActionForEach' modelActions have' R5 O5 n, _ M) @1 T
// a default order of `Sequential', which means that the$ }$ h! C$ g |! L5 z* K
// order of iteration through the `heatbugList' will be
6 e1 K0 @! j2 F! [9 B // identical (assuming the list order is not changed" z' X) q4 `( |# n$ B' U
// indirectly by some other process).
* ]! K- r( L/ [6 J- t/ L8 N
' R, M* a/ g) a+ q modelActions = new ActionGroupImpl (getZone ());
/ g8 k& H' X- X# M+ n
" o- [5 r1 K; ^; Y: s" M9 r5 \ try {2 W& U& K5 |8 Z' T* A s& X
modelActions.createActionTo$message9 [9 a1 t3 u$ g: a1 `6 G% b" m/ {
(heat, new Selector (heat.getClass (), "stepRule", false));/ m8 A) s- R& i% x+ B
} catch (Exception e) {
$ L! |' L' R e System.err.println ("Exception stepRule: " + e.getMessage ());& ]* t9 H7 j$ s! \& E1 s5 [; c
}
8 x c5 L- ?/ U4 S3 A, j% G& @1 F- p `2 M4 \4 v& g
try {
/ J# o. G( n! E8 v) q Heatbug proto = (Heatbug) heatbugList.get (0);
) K; G* Y) f9 _% Y5 \7 v4 t' K Selector sel =
+ O, J2 s; Y/ c6 n new Selector (proto.getClass (), "heatbugStep", false);0 x4 m! l! m; t D3 M. S
actionForEach =
, Q8 B% u* o$ |8 W modelActions.createFActionForEachHomogeneous$call
$ J/ N+ B$ e, b/ ~ (heatbugList,# W/ M; C# h x- J* T3 c8 c
new FCallImpl (this, proto, sel,
" h/ R) o/ w8 {/ J9 v- M new FArgumentsImpl (this, sel)));
. S9 y& }4 g! G } catch (Exception e) {! p a# e+ W1 A9 @" V
e.printStackTrace (System.err);/ _( w- o) _1 D$ U
}3 S$ r% [$ i, [( Q
% P! ]; r& ?: Y: r: C1 a
syncUpdateOrder ();
: d: y& R: A" O! T: l; v$ \* M
" v! e' W. l0 D# `6 s try {+ L8 N7 p) t# }" l. m T+ o9 D
modelActions.createActionTo$message
( l! a& n5 H) z- x; l7 ~ (heat, new Selector (heat.getClass (), "updateLattice", false)); j% r7 @/ T0 ~2 ~( x) @
} catch (Exception e) { x# I7 [/ F* x. U9 U9 z8 f
System.err.println("Exception updateLattice: " + e.getMessage ());
3 n: J" H- L0 x) s( S# D }8 j6 s4 j, D" Z( W6 V) q
! c: c4 {0 \/ P% ?" q
// Then we create a schedule that executes the3 l/ n \# S$ f; Z M3 \0 b# T
// modelActions. modelActions is an ActionGroup, by itself it; |5 g6 h, c8 }! U/ k5 s: b
// has no notion of time. In order to have it executed in6 w9 V0 p, l8 s0 v
// time, we create a Schedule that says to use the
/ J1 E# o: s3 I$ G$ p: @, n // modelActions ActionGroup at particular times. This
% ?- K) ?3 R5 M! t2 ^/ t // schedule has a repeat interval of 1, it will loop every3 H$ j5 G2 m. t# M
// time step. The action is executed at time 0 relative to
1 T; G* `2 E4 B/ ^/ _* E( B // the beginning of the loop. K( u; x9 x# [' @- p1 J+ [6 W4 Y
, g7 M9 t; i5 z7 q
// This is a simple schedule, with only one action that is6 [! x# i) \( k- M9 m2 e6 W
// just repeated every time. See jmousetrap for more* x# }4 ?$ M& o! d( ]6 F+ j
// complicated schedules." } `3 @& E# a4 f% k
" H1 T9 i, y$ S5 p3 D" E modelSchedule = new ScheduleImpl (getZone (), 1);
. c6 T7 c; H# z4 I8 I& t3 P modelSchedule.at$createAction (0, modelActions);& n0 c8 d) |6 F4 Y! Z0 J: f0 x, w
9 Q5 x: |" F$ A, z, u! J
return this;" {8 i; e: G( N: e( A
} |