HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: B$ Z1 R n0 h% j9 v- p
* O) Q, A: Z, T' p5 ?2 m
public Object buildActions () {
8 n& c% S& R$ h% d8 ~$ Y; A super.buildActions();6 V$ _8 X& A+ Y+ Q
1 L8 h7 ?; B7 J3 h$ X7 X! w
// Create the list of simulation actions. We put these in
4 T. x9 A i8 U0 c4 t // an action group, because we want these actions to be
/ n5 M, v/ L4 M5 p // executed in a specific order, but these steps should
+ g- w( c. }3 Z* K/ R3 L // take no (simulated) time. The M(foo) means "The message/ E7 x& S" G% f ?
// called <foo>". You can send a message To a particular* Y" u0 W4 Q4 O+ H
// object, or ForEach object in a collection.
$ p# |5 @. a3 @* W + P9 s$ B1 G+ O# a4 G3 w! h0 z6 c
// Note we update the heatspace in two phases: first run. I, r# ]" A0 W5 S
// diffusion, then run "updateWorld" to actually enact the" [& p1 P q+ b* \6 I- a7 p: \1 \
// changes the heatbugs have made. The ordering here is
; x: @5 R# t: M N: Q // significant!
2 h; U$ M0 }/ O' z( ^4 L ) N! N* Y8 F9 ]9 l* ~2 Q
// Note also, that with the additional+ H7 c' ]' y; M3 ]
// `randomizeHeatbugUpdateOrder' Boolean flag we can
" k4 `5 Z( X9 z$ G% P2 q // randomize the order in which the bugs actually run3 S& t2 `0 b( U7 A8 W4 [. a
// their step rule. This has the effect of removing any- Z' W! E0 o9 g7 E
// systematic bias in the iteration throught the heatbug
/ \% r5 ~! m& j, \8 Q2 J9 N0 h // list from timestep to timestep( _) H5 m0 F$ d3 j# Z. [+ k
- s* O/ n% H9 u4 m4 p // By default, all `createActionForEach' modelActions have
5 \2 k! S& R2 g( ^) D- k" D, d2 S // a default order of `Sequential', which means that the
1 _) W D, g( M% Z! D* \ // order of iteration through the `heatbugList' will be' Y* x1 S2 [5 D3 T
// identical (assuming the list order is not changed& q6 g: {0 I- b6 d
// indirectly by some other process).
$ L* k- t2 ?, S: \ ( E% x' x- R1 [
modelActions = new ActionGroupImpl (getZone ());
$ A( V- h% z3 d/ {, | \
0 j1 ^' u1 B0 U8 I try {
8 q0 B7 R& F; C! O" e' Y modelActions.createActionTo$message
7 l, x7 L; c* l1 k3 J ? (heat, new Selector (heat.getClass (), "stepRule", false));4 B4 h: x" `0 e+ ^9 k+ k [
} catch (Exception e) {
# \0 F+ k5 h v4 ` System.err.println ("Exception stepRule: " + e.getMessage ());, M- ?/ e1 z- w2 z/ z0 h0 A& \5 }
}
) b& i0 t7 L+ _7 l
! A" ?) p; T1 o try {
0 V) s: j) |( L0 O' S3 X. P0 f Heatbug proto = (Heatbug) heatbugList.get (0);& {2 C5 I H% ?
Selector sel = : \4 F& q! I2 I _5 j- T1 j$ g( L
new Selector (proto.getClass (), "heatbugStep", false);( u4 f' k3 j' T u. s: Y; L4 k8 m
actionForEach =( a( g0 t9 S. R. U D' n
modelActions.createFActionForEachHomogeneous$call1 N; f u& o; Q; A
(heatbugList,
4 e2 v+ P, b& u4 Z# ] new FCallImpl (this, proto, sel,
1 |3 F% K* T& t3 c1 z" Q new FArgumentsImpl (this, sel)));
3 \; W# I. X7 H5 \7 c# k# O. F } catch (Exception e) {0 g: ^+ N$ c a- X8 W, Y
e.printStackTrace (System.err);
* j6 Q5 M% @8 f) T }
7 |" t/ O, m9 z( @% ^/ c7 Z 9 v4 H& Y! p& U @; \
syncUpdateOrder ();
+ ~% H5 t$ C# r9 M( t, B! z& n- E7 \+ W4 T
try {# p3 u3 q( U" G7 \ k
modelActions.createActionTo$message
* g# A# w% b# y% c+ E) x (heat, new Selector (heat.getClass (), "updateLattice", false));+ ]* b3 F* v3 C8 [$ l- i
} catch (Exception e) {* @, Y& U5 r& ^5 c* q7 ^1 H0 J
System.err.println("Exception updateLattice: " + e.getMessage ());( ~+ \$ O! x% `8 ^
}
# E# } |% N A / K: U/ ^4 _; a& N: ]
// Then we create a schedule that executes the5 F1 T8 }5 s, C- _9 T% A
// modelActions. modelActions is an ActionGroup, by itself it3 A& A* p& b( \: Y& A: i" Q
// has no notion of time. In order to have it executed in( `) J* `% A V0 D' N1 c
// time, we create a Schedule that says to use the& j9 `) B0 k0 Z3 K1 f6 S
// modelActions ActionGroup at particular times. This( m% O8 C M L0 x2 B2 \& h
// schedule has a repeat interval of 1, it will loop every
% V: N' U1 p' v2 d5 n% }3 v // time step. The action is executed at time 0 relative to
; {+ n8 p4 j6 } // the beginning of the loop.
* p' T- E+ A6 D4 q) b" ~1 U3 D: J2 C4 r5 s4 C. C: Y1 m D
// This is a simple schedule, with only one action that is' W$ }0 K6 w+ B. r0 R4 x8 }1 ?
// just repeated every time. See jmousetrap for more+ b1 D2 b5 f4 }: F2 S$ \
// complicated schedules.3 a* i# ^" }% x0 ?# w% @
6 K* W& t. Y9 R5 \ modelSchedule = new ScheduleImpl (getZone (), 1);' w9 X. t( o0 ?) c4 T1 R k8 Y
modelSchedule.at$createAction (0, modelActions);+ b- R- M, R7 o8 }$ {9 l9 @& \
% U7 U2 c1 q X4 C( I% @% }
return this;
$ J. C- {) Z+ |- n0 C+ r) P } |