HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 ^1 n% j: Q* H0 U f3 c; i
; B* \/ T3 i4 ^ public Object buildActions () {
; K2 Q9 X* G7 r super.buildActions();# t# ~# `6 B$ ?$ K, b
4 g4 y4 N f, `1 v // Create the list of simulation actions. We put these in3 N9 C, \9 E( A p/ T1 Z9 A
// an action group, because we want these actions to be4 I( o8 \/ }; J! k1 p
// executed in a specific order, but these steps should
* q; d$ R9 I9 }5 ^% ` // take no (simulated) time. The M(foo) means "The message7 N, t4 @, B* B3 Y# U; ]
// called <foo>". You can send a message To a particular
! |! p5 v+ ? R) z // object, or ForEach object in a collection.2 @% G: s, |2 b/ ~' Q7 R
* ]' F* p/ s3 J% _
// Note we update the heatspace in two phases: first run, q3 y7 }+ f' _7 ]
// diffusion, then run "updateWorld" to actually enact the4 F" R* [# G3 {4 \: F# \3 f
// changes the heatbugs have made. The ordering here is
9 L5 M+ ^! H' @/ R // significant!. n. u' ]2 l6 c) S. ^
; I' K* h' H- D. m // Note also, that with the additional
) D. E& q* n( K* W) |& B // `randomizeHeatbugUpdateOrder' Boolean flag we can5 Q7 O+ [- u B8 C* ~7 G! A
// randomize the order in which the bugs actually run
& Y' D& G' E! i. {( n // their step rule. This has the effect of removing any: m% K& ^& Z9 o' A7 a
// systematic bias in the iteration throught the heatbug
# _, z4 S) |/ H( ?1 z3 \( l3 | // list from timestep to timestep
1 Z9 ~/ N( J9 f4 p 9 }7 y7 l2 Z, j1 y
// By default, all `createActionForEach' modelActions have
8 L c) S3 H/ x( h H // a default order of `Sequential', which means that the* d# z8 f Z7 d# C/ S$ c
// order of iteration through the `heatbugList' will be
8 E$ ?' B( d3 k' E7 m2 a // identical (assuming the list order is not changed. S6 Y% g( Y# G1 o3 o* L/ V- I, b
// indirectly by some other process).* C3 f+ g+ M( G: z% U
# \; w) x7 b6 e) x$ p+ @5 v modelActions = new ActionGroupImpl (getZone ());. q! z8 a; U, w
# V$ c* s3 o( {& V, m4 v try {6 I7 I* c: h/ d
modelActions.createActionTo$message
& c8 {- G3 A9 i N7 S# ~ (heat, new Selector (heat.getClass (), "stepRule", false));
s8 |' h# t; M" M* T } catch (Exception e) {3 [2 y( J0 l( _5 g
System.err.println ("Exception stepRule: " + e.getMessage ());4 V* j8 u; b: n1 ]* ]0 }
}
( {* o4 t6 f0 u. H% |
/ `3 i+ s, K9 L: e% t r try {7 M1 ]; {* L6 ^+ C
Heatbug proto = (Heatbug) heatbugList.get (0);
! m k) }; Y6 w- N Selector sel = 6 z; G' z* e( c7 ]0 a0 j; `
new Selector (proto.getClass (), "heatbugStep", false);
% Z$ ?0 e% |$ r0 _% [+ w& t actionForEach =" [1 L% Y- |& V) Z' X( _
modelActions.createFActionForEachHomogeneous$call1 ], K7 N' r7 K' r, x8 {. [ `0 o3 a
(heatbugList,5 G# Q: ^" h O1 Z& f ~ {
new FCallImpl (this, proto, sel,
6 p7 ~1 H2 {% _! X( |( N1 Z new FArgumentsImpl (this, sel)));* ^7 C9 y' M6 |5 y( O
} catch (Exception e) {- k6 j3 t2 ?" m
e.printStackTrace (System.err);7 ]4 {9 E9 }3 ~1 t# J& j) f6 J9 ?
}% m- P4 q' G: z+ F! w" E! C
! V9 b' Y/ W1 A0 E2 E syncUpdateOrder ();2 f- [7 y+ F5 h( a7 J
2 m8 m' N6 j& V6 w( ^8 L, i& s
try {; K8 W9 c+ P. k
modelActions.createActionTo$message
' | ?; ?% _ S( z7 i% t (heat, new Selector (heat.getClass (), "updateLattice", false));
, l& Q' f. C) S! j% A } catch (Exception e) {
1 J) ]; J T7 u5 T4 R; X, H% z System.err.println("Exception updateLattice: " + e.getMessage ());
) `* z Y# z( e0 ?; O0 n1 N }
& Z7 X7 D) X/ @* ~3 T$ @
- v7 Q9 w' y$ N) t; | // Then we create a schedule that executes the5 v0 ?5 d+ {. m2 Z: _
// modelActions. modelActions is an ActionGroup, by itself it
7 A6 h1 }" @& `9 y; F/ x // has no notion of time. In order to have it executed in4 x* B, L5 `1 u! V7 y1 d" \
// time, we create a Schedule that says to use the
, @! p, y! r2 R2 K // modelActions ActionGroup at particular times. This
% j6 {1 q+ O. v- o4 \' `/ ?6 ]0 F // schedule has a repeat interval of 1, it will loop every
1 I( ?+ J9 T9 Y" A3 T // time step. The action is executed at time 0 relative to- O1 S& \2 k) W
// the beginning of the loop.
' [, G+ x/ @ ~/ J. _( o
, S3 s a. L$ q2 l. i) s' ^ // This is a simple schedule, with only one action that is6 {. x! w" s; g. I" h& i8 x
// just repeated every time. See jmousetrap for more
2 t3 ~% R# \+ R: D1 h // complicated schedules. X" @: Y! L- S+ H# N+ y( S. D
% w9 m, h9 G2 X1 \) s6 C
modelSchedule = new ScheduleImpl (getZone (), 1);
h$ V$ q# F. [, ?. N3 i modelSchedule.at$createAction (0, modelActions);9 k+ _2 h6 o- _0 a5 B
# f" p9 Q% S: |4 N# h; U
return this;
( C% K, a* o1 K; |( [8 b3 } } |