HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 d6 Z' C: ?/ \ O& ~; O
! E$ F/ s: e' P8 l# ]3 S1 X public Object buildActions () {# ]3 c/ `% o" |8 Q7 u
super.buildActions();6 A, L7 [: F) w
7 i" ~3 I L+ G% o- t6 q
// Create the list of simulation actions. We put these in
$ a. G& T9 |* N6 P/ q // an action group, because we want these actions to be
* O4 \, C4 P M$ S$ A3 Q# g // executed in a specific order, but these steps should3 D+ s$ r* S# D. F, r; z1 V" y
// take no (simulated) time. The M(foo) means "The message2 g8 ^( H z9 r+ S1 R
// called <foo>". You can send a message To a particular
7 S B) \; d3 D; w0 m* V/ H* F0 @ D // object, or ForEach object in a collection.* X. r/ V1 }" a0 c: A1 {; ?
: `8 t4 N( L6 T/ k) `3 ~. {
// Note we update the heatspace in two phases: first run
7 c4 f1 }) {- O- ` // diffusion, then run "updateWorld" to actually enact the
- H$ J. {0 J* ]& w4 T+ ~" i! x; A) c // changes the heatbugs have made. The ordering here is
* e7 |# t# X N' O3 W // significant!4 u3 Z. s+ u7 r, l. i
' i8 h$ i/ V3 ^+ g/ ?% x
// Note also, that with the additional
1 x2 `8 H, H! X$ D // `randomizeHeatbugUpdateOrder' Boolean flag we can! A8 H. A& W; i4 d7 e( w; b& d
// randomize the order in which the bugs actually run5 G8 n/ m$ G8 S- P1 s5 C( _. \
// their step rule. This has the effect of removing any
: a, y& [0 y; F: a J# g; R // systematic bias in the iteration throught the heatbug
1 O a# B8 @ g1 F4 {1 c // list from timestep to timestep
; P; J5 t2 Q9 s
8 E9 F& \1 T: X7 Q3 O3 C // By default, all `createActionForEach' modelActions have
! R! S1 u, y0 _% x, J // a default order of `Sequential', which means that the6 O+ \9 {. e! o, g0 Q
// order of iteration through the `heatbugList' will be: d- `" x. g+ i5 ?
// identical (assuming the list order is not changed6 t2 o0 U: _0 u: g1 P; t
// indirectly by some other process).
, t4 ^. P6 o/ _# g5 @- N 5 _" H' [) l& o6 S+ l* L5 u" w2 u
modelActions = new ActionGroupImpl (getZone ());
# p, b. ]; {* k8 i' {. J5 @5 N5 P" F$ S q d/ d" G7 k
try {
8 G) c6 b& u+ F modelActions.createActionTo$message
- P- x( L" Z( R5 @; ^ (heat, new Selector (heat.getClass (), "stepRule", false));
' t3 W% x1 T* M2 x } catch (Exception e) {+ f0 s+ R* B, F, D( S! C8 t
System.err.println ("Exception stepRule: " + e.getMessage ());3 B4 L% b* Y) e+ `$ |
}
7 A1 S+ b( h8 [) q' U8 r9 b' V/ S
: P, V+ [0 G0 b, w h9 j try {+ U% x: ~9 D4 I5 Y0 w; ^2 B
Heatbug proto = (Heatbug) heatbugList.get (0);
, z! p$ U% V y Selector sel = 9 i; R* B V. }$ Q$ f
new Selector (proto.getClass (), "heatbugStep", false);% V0 [8 R8 n: I \; W. p: l
actionForEach =
; {+ {: N+ ]& `; Q! m modelActions.createFActionForEachHomogeneous$call; a6 ~) Z# i* X/ M* i( n
(heatbugList,! L. d2 \. b% K4 [
new FCallImpl (this, proto, sel,4 ?, V& z5 N1 d
new FArgumentsImpl (this, sel)));3 y2 w, _8 a9 Q1 b( U0 R
} catch (Exception e) {
8 L, e# `1 g M" p6 Z- c# n$ L e.printStackTrace (System.err);
* G8 v0 n) w0 y4 K5 o }
$ E# S. Y0 T' v2 a/ s- ] 3 }, E5 A6 w5 {* w# T, v' }4 u% _
syncUpdateOrder ();, ^! V' V* a' S$ m6 m
: l1 q$ o2 o7 I6 K- K6 \ try {* l7 B7 t5 B8 H+ Z, Y* X& Z
modelActions.createActionTo$message 6 h. C' s3 s" w3 x* g7 e' m) ]
(heat, new Selector (heat.getClass (), "updateLattice", false));
7 ]2 \# }: a5 j" j+ E8 n } catch (Exception e) {
4 I; b! S: \3 i- G5 v System.err.println("Exception updateLattice: " + e.getMessage ());( n) H% k( l; P; P
}9 g0 Y, h+ u& {7 C% W
+ j! C* h+ y/ \* h+ i! w" r2 ] // Then we create a schedule that executes the; a9 C" y1 O# j
// modelActions. modelActions is an ActionGroup, by itself it
/ Q. C5 i9 @% z3 z/ `1 v! g) V // has no notion of time. In order to have it executed in3 e5 G) |- J2 L9 b0 ~4 j# a
// time, we create a Schedule that says to use the/ n+ e; f* G7 m2 `. V5 p
// modelActions ActionGroup at particular times. This
, ?9 z* S& B. k- d9 Q) o // schedule has a repeat interval of 1, it will loop every9 u4 D( |% u4 b- n/ v8 S( ?
// time step. The action is executed at time 0 relative to
9 [; X8 O: O9 r2 A, k/ R3 k // the beginning of the loop.8 l# b0 ~8 m9 Y k
j1 N7 u. Z |( W/ p+ \
// This is a simple schedule, with only one action that is( ~- k7 V4 l4 D- f
// just repeated every time. See jmousetrap for more$ K3 P- y) S& W( J4 Q4 a
// complicated schedules.
2 b! q, p& t- ? @ - \1 m7 B# M3 ^8 S, C$ U% i+ J' A6 y! W" U
modelSchedule = new ScheduleImpl (getZone (), 1);. g* n' F/ s. k3 V8 A, Z* |# c$ D) g+ H
modelSchedule.at$createAction (0, modelActions);3 B9 a" n5 V) J, c5 z. W
4 G4 H/ y! a, v& u% j return this;
/ y. h9 k& c1 G) D } |