HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 M8 k7 v7 I, M2 H& _
% }' E; M0 g' O9 d public Object buildActions () {
& ]2 f- @. n2 w! o super.buildActions();) B1 @* d# E' k; h
b8 G- l& I. |+ I& r // Create the list of simulation actions. We put these in& e3 C, V! Y& r* M1 {" q8 _7 H
// an action group, because we want these actions to be: ?2 }0 M8 w3 h( G4 L# k) }" ]
// executed in a specific order, but these steps should! h* x. q3 M" p# a. s
// take no (simulated) time. The M(foo) means "The message8 a" E# g4 i# V& T
// called <foo>". You can send a message To a particular
8 t" I4 i: f" }# B( s // object, or ForEach object in a collection.7 g& E: G! h6 T2 N8 t
% j8 ]* p4 T1 C0 U% J
// Note we update the heatspace in two phases: first run: B* }5 W7 ?$ E U( I( j
// diffusion, then run "updateWorld" to actually enact the7 k% k* w S6 A1 {
// changes the heatbugs have made. The ordering here is
4 E! f L. ~- { // significant!
% i& m9 q4 M/ M" d6 |3 \, u $ j2 R! E S$ x* v
// Note also, that with the additional
! F& q Z, j/ k // `randomizeHeatbugUpdateOrder' Boolean flag we can
- ] c3 q2 L& m. `9 z0 z8 ~: f9 e // randomize the order in which the bugs actually run
% W# v" ]0 c4 p+ V // their step rule. This has the effect of removing any
9 f: d! b5 ~5 c# x- X // systematic bias in the iteration throught the heatbug' x& ~% E! ?3 [' p7 P
// list from timestep to timestep
4 B. E1 O$ N- ?1 {; m8 `9 b : o! b( v8 G$ L4 q& V
// By default, all `createActionForEach' modelActions have
- T" @* r( y9 a3 p1 P# ~ // a default order of `Sequential', which means that the
. ^: n) {3 a& v" S7 @ // order of iteration through the `heatbugList' will be
4 j* n$ l, @+ a& u. q // identical (assuming the list order is not changed# u& c' Z6 P! V* B, E# j( @
// indirectly by some other process).% M; D/ [, A. b2 N
# [+ B% J- L3 Z4 u0 O0 v modelActions = new ActionGroupImpl (getZone ());) O& q2 n H0 f# V
3 {, V5 q* K8 y1 u+ \) G
try {; n% F6 i/ f. B6 ~# P$ ^2 u
modelActions.createActionTo$message
) z- u4 v+ g- R; u (heat, new Selector (heat.getClass (), "stepRule", false));
1 K* u% j: {/ i0 J9 i } catch (Exception e) {
2 q; _' @) f5 Z' K; P System.err.println ("Exception stepRule: " + e.getMessage ());
6 l( [! }6 M' R O5 a, F }
4 J$ x0 U" W; l) V& s4 H8 o2 k% X; A# x6 b
try {
5 {& L1 V5 |8 } Heatbug proto = (Heatbug) heatbugList.get (0);
9 C0 t0 t7 [# Y& w2 @ Selector sel = 3 z5 W# y, ~' i% S
new Selector (proto.getClass (), "heatbugStep", false);
7 b: R' v& r6 N* W) F" p4 M- G actionForEach =1 Z; L0 e" Q0 r
modelActions.createFActionForEachHomogeneous$call8 ~5 a% J5 i/ T( _
(heatbugList,! [/ k* h+ ~& F( s; K- k; O
new FCallImpl (this, proto, sel,
) q, K7 `) E. W/ a2 f5 x# {$ z1 L new FArgumentsImpl (this, sel)));6 p( s- S$ `" U9 G# r1 `
} catch (Exception e) {3 s$ g3 T& @; n3 K; m
e.printStackTrace (System.err);
; ]5 A Z6 E7 n6 F( m8 l# I: b+ B }0 r7 Z0 p; m$ N
! S* r: a% I8 x" q4 R
syncUpdateOrder ();' ]' L* g8 ], ]/ v. G1 ~
0 ?% `: m) N* b% a Q1 p) @ try {
U. v* M0 R" R4 ?/ o/ Z; t modelActions.createActionTo$message
. Z- s, i* h5 t C$ ]0 j& i (heat, new Selector (heat.getClass (), "updateLattice", false));, p- r! D1 u' ], X3 h* G
} catch (Exception e) {
5 {% [; {$ n; Q) ?1 [2 _5 ^ System.err.println("Exception updateLattice: " + e.getMessage ());
# ]9 M' ?, \* H+ ?: ?0 U9 h }4 C% Q" T5 l% z0 |" d
) Z3 F- L9 C% B7 S9 r3 f" k2 z
// Then we create a schedule that executes the
; |5 N c m0 X1 [. D // modelActions. modelActions is an ActionGroup, by itself it
1 w0 L, s$ b6 }1 j // has no notion of time. In order to have it executed in/ j) z: g& k" b& q: I3 N; D/ ^8 u
// time, we create a Schedule that says to use the4 x5 |" R2 O4 @+ @4 w" G9 d
// modelActions ActionGroup at particular times. This6 T2 k5 Y0 x5 F, u( f
// schedule has a repeat interval of 1, it will loop every" G+ W; k( I" Z' _' \6 y
// time step. The action is executed at time 0 relative to
4 Z4 Z7 W' r. G- b1 W* Y$ n* B // the beginning of the loop.) T6 y4 t) X8 K
; t1 A$ L% L, @+ `2 Q9 I // This is a simple schedule, with only one action that is. z/ K! Y% M6 N8 }8 i: d
// just repeated every time. See jmousetrap for more
e$ m) t& S, e9 y, N# l // complicated schedules.4 k7 a0 B( c4 `) }+ B% P6 C
. E: o C/ ]+ {2 Q7 e/ \2 d# c modelSchedule = new ScheduleImpl (getZone (), 1);
{0 D- U9 o: `: \! _+ ] modelSchedule.at$createAction (0, modelActions);$ @6 ^2 s/ W M: z
* Q9 u6 g9 X8 j: f! `; y5 Q4 X- A
return this;3 b2 j0 W; t" s5 S! R. [: x' X
} |