HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ x0 h0 _0 Z! G6 W0 ~
4 {# I! P+ u7 o1 R0 m* f3 M public Object buildActions () {7 S) v* q% S* [: `$ O
super.buildActions();
' Y2 N' C9 ]" V+ ?! j& O& J3 G1 } 8 r3 a k, A9 H I
// Create the list of simulation actions. We put these in
6 o1 y( b- n2 }3 g; w // an action group, because we want these actions to be/ j) P& H- G: W! k$ Y7 t
// executed in a specific order, but these steps should0 @: p2 R/ E5 _5 }& R
// take no (simulated) time. The M(foo) means "The message/ \; c% h7 s, T3 ~3 E' x0 d* B5 g
// called <foo>". You can send a message To a particular
- E7 p9 [- C E // object, or ForEach object in a collection.# ]+ ]% H% F1 P1 Y! ~
' G8 o1 E5 \7 C# w0 L5 v6 ]
// Note we update the heatspace in two phases: first run$ {( ?6 J+ s$ ?) y! m7 J# ^/ a& x
// diffusion, then run "updateWorld" to actually enact the
( Y7 k! b" g' U o0 x! ^0 h/ p // changes the heatbugs have made. The ordering here is
8 k- o, s5 j7 d8 m: a5 w5 r' h // significant!
+ E5 y) \: P' c. I0 O- `
8 o Z/ }1 E4 s1 b/ r$ t // Note also, that with the additional8 N/ ^1 I/ Q# _
// `randomizeHeatbugUpdateOrder' Boolean flag we can% m. J9 P0 h/ y: D) o, a
// randomize the order in which the bugs actually run) v$ {% @$ \0 u% y: H8 \
// their step rule. This has the effect of removing any
! L8 V. A6 A" F- Q! D // systematic bias in the iteration throught the heatbug
7 [2 ~0 O5 Y; O // list from timestep to timestep
4 n3 [ A- T4 `7 H1 h" o( v5 P 7 }2 @( r; I" E" n
// By default, all `createActionForEach' modelActions have
6 n; x7 p5 ?( E% M& N // a default order of `Sequential', which means that the+ M9 m" p7 u5 S+ o4 n1 t6 U% |( F
// order of iteration through the `heatbugList' will be9 T$ d+ @- N: U! L$ P! o* F" e
// identical (assuming the list order is not changed( X W' O9 b( m" w) d8 o9 @
// indirectly by some other process).
; z, z4 X6 A- i% o
' ?7 w3 s- m3 Q$ ~- t. j modelActions = new ActionGroupImpl (getZone ());
. H% M+ h0 P( }9 _$ h- w
& q$ _2 Z: f& u6 `6 z- o try {3 I: z* W' D+ \+ f" ~1 a* U1 h" H; z
modelActions.createActionTo$message9 I9 M6 C3 v0 S0 j/ r
(heat, new Selector (heat.getClass (), "stepRule", false));
& y. ~* P$ t0 [$ E } catch (Exception e) {
) r2 r0 [5 ` D5 Q( e+ r7 ^8 S6 r System.err.println ("Exception stepRule: " + e.getMessage ());, c- E& A4 H0 S/ j. a. J
}
+ [: t7 X/ b. i" ?5 s* O/ G9 y
, A( t, B1 R+ B% @6 x7 B try {
. T; x5 q) U: a0 k* U4 a# i$ ` Heatbug proto = (Heatbug) heatbugList.get (0);0 ]: h2 p& V1 n U7 E
Selector sel =
9 z, p5 `" b9 P) ? new Selector (proto.getClass (), "heatbugStep", false);
! \# |& N! |0 Q6 f actionForEach =+ t0 l9 E3 t$ K7 q. p; t5 R
modelActions.createFActionForEachHomogeneous$call5 S ` t" ~0 [( X8 j
(heatbugList,. O8 w) h" j B/ C7 S( y9 c* C
new FCallImpl (this, proto, sel,- V) F; J1 `1 p; c
new FArgumentsImpl (this, sel)));$ F- H$ k. y2 p" P* f1 U
} catch (Exception e) {
% E8 T9 |& n0 T5 l/ r d( k e.printStackTrace (System.err);
8 ^! R2 g# g% I ^" \ }
+ Y% s3 l- @8 }% l9 [: ]
, K' v) S/ M' Z3 I syncUpdateOrder ();9 J$ i! y3 N" ]. A( c3 a
% w r; l; T# P( X9 ?% l
try {. G1 G2 M2 C1 [! i( q0 i2 _
modelActions.createActionTo$message
% A* w5 X V) F) G! |1 }8 T! X (heat, new Selector (heat.getClass (), "updateLattice", false));
1 U$ P& K+ |) y } catch (Exception e) {) h( c2 f+ s/ J$ K8 ?
System.err.println("Exception updateLattice: " + e.getMessage ());9 l4 _8 I# c3 T9 Q
}
" g( e1 j/ ?% b$ z ]) U7 G
5 v: B: @" z5 K# b8 x& [9 Y* z // Then we create a schedule that executes the
v1 U4 r4 O9 ]8 c" Y" [ // modelActions. modelActions is an ActionGroup, by itself it
4 I% a4 Z4 k% |2 Q4 L4 k! L // has no notion of time. In order to have it executed in
6 S7 u3 M& V1 l // time, we create a Schedule that says to use the% R$ ^4 V$ w2 d4 ^- ^! Z3 s4 U
// modelActions ActionGroup at particular times. This
! o- X/ Y5 w7 O: f8 L2 W+ K // schedule has a repeat interval of 1, it will loop every
; Q2 A9 T. S8 Z/ N. Y0 f3 q // time step. The action is executed at time 0 relative to! G- ]8 p) ~+ v$ [8 Y! o- p
// the beginning of the loop.
2 v4 [/ _/ u5 j! h9 g, V
% r" |$ `! q' X* g. [6 l // This is a simple schedule, with only one action that is) m* `* Z$ f, ^' W- F# B2 [
// just repeated every time. See jmousetrap for more; X4 c3 s- t1 H! `' o3 T9 _
// complicated schedules.
+ W( H$ J1 u4 Z# W& j# o" b* e & o* K/ f0 _! r' Q1 n8 [8 y
modelSchedule = new ScheduleImpl (getZone (), 1);
7 Q4 O f6 v) L, M8 o modelSchedule.at$createAction (0, modelActions);
/ E c, ^' ^) v$ ?( O
7 R' b1 Z' V+ y return this;! A2 @+ _4 [' R/ d, ~$ g
} |