HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ i* S! y7 i" h: z. {, T6 j( J
0 I! ^7 S- L* [* M! G6 D# f: n public Object buildActions () {
A' K* g/ v$ E" l% T" G+ X super.buildActions();* ?/ ^+ v2 U% S+ l- d! o
! _) Z5 S6 k! a3 ?# t: L // Create the list of simulation actions. We put these in& N$ U; s( P9 R- W
// an action group, because we want these actions to be- y) u" r8 c S- m$ P
// executed in a specific order, but these steps should( V# \& [7 P6 R. p) d9 T1 s+ Q$ h5 Q
// take no (simulated) time. The M(foo) means "The message4 ]6 K+ ]; q- \+ q6 O. P8 U0 k
// called <foo>". You can send a message To a particular; j4 a: Z: \' _% b. {
// object, or ForEach object in a collection.
6 J# b8 h0 F8 ? 5 x) o! m8 |- b
// Note we update the heatspace in two phases: first run/ b8 u! R8 w" C0 z3 Z T) a
// diffusion, then run "updateWorld" to actually enact the
: d4 j& I2 K7 k5 L) G) { // changes the heatbugs have made. The ordering here is
* Y8 ]3 S- W+ `, V" Q7 O4 { // significant!
+ [( q; O' E- i% \' M `7 z
$ N6 N8 G8 |& O. ~" h* g- J) o! c // Note also, that with the additional
) s: R7 D, z3 M // `randomizeHeatbugUpdateOrder' Boolean flag we can
% A1 p7 ]* k8 N" ] // randomize the order in which the bugs actually run
9 ]: p( [5 w! e5 F M$ ]7 R, O% u // their step rule. This has the effect of removing any
9 {" I5 y% F! U% ]$ t% m% h, ] // systematic bias in the iteration throught the heatbug
, M: r- p# f) E& t8 R$ o // list from timestep to timestep9 N! N! Y" o0 b( |# r- A: X
9 t0 z/ b1 z2 N
// By default, all `createActionForEach' modelActions have
, X9 D. H8 k% E // a default order of `Sequential', which means that the) c4 |* q6 [& l* v7 m& {
// order of iteration through the `heatbugList' will be
: w# G4 B, q: F8 Z2 m. M9 ` // identical (assuming the list order is not changed
+ C7 W' d) l }9 D' D% r' B // indirectly by some other process).# \3 o: z& t9 @( M( H
: [- e1 S: L' G- }3 n2 P5 b
modelActions = new ActionGroupImpl (getZone ());- h+ r4 M+ I: Z/ c
4 P7 c( n# N* H5 |) P; S P
try {7 U8 N2 i. d1 h9 e1 _3 B q1 c
modelActions.createActionTo$message# i' a5 d0 }) ^ {$ I
(heat, new Selector (heat.getClass (), "stepRule", false));
& S2 v$ i* p7 f p+ L3 f% f+ ~ } catch (Exception e) {
) ^) @4 |- Q8 j7 m! \ System.err.println ("Exception stepRule: " + e.getMessage ());2 \* K) s1 l6 l- z
}
, C5 k+ l4 o/ j$ d' F
* t3 ^) x( r7 J" A+ { try {- \: o5 c. d* p0 n* ]: e7 G) f
Heatbug proto = (Heatbug) heatbugList.get (0);
, \+ Q; T6 D4 x, @5 |8 ? Selector sel = ! M. k H* z: R6 D* c
new Selector (proto.getClass (), "heatbugStep", false);
- }) C1 a3 ^* m- H actionForEach =' Q/ \' V. p6 l, l% k
modelActions.createFActionForEachHomogeneous$call' N" h5 N& r7 p. c1 r
(heatbugList,
/ Q A6 d0 S6 |. I0 B! p new FCallImpl (this, proto, sel,! o! e# M X9 h7 O2 Y) _: i$ a! X
new FArgumentsImpl (this, sel)));
; w0 o K O# H d } catch (Exception e) {# } S' e5 C) o" U1 {
e.printStackTrace (System.err);# U+ T7 G. R. O& r5 D' d
}
) c2 p0 I% M5 N# D
1 I" B$ G! |8 s, \! j syncUpdateOrder ();: f+ d2 T; m; H. w9 v, I
7 _) I* `, H( b
try {' o$ Z; ]. t/ Z
modelActions.createActionTo$message
' u3 t" Q3 _' a: U& P: W0 h (heat, new Selector (heat.getClass (), "updateLattice", false));$ V- C- z# V2 d3 k- \$ [: i
} catch (Exception e) {
- X" t7 k: K3 q5 k System.err.println("Exception updateLattice: " + e.getMessage ());
( i' Y7 _) _8 P% u* V/ T! U }
) i" q. q9 m5 ^, P. a& g( U+ a) \
: ~: O2 ^% J# t& W // Then we create a schedule that executes the
; x# N5 N9 Q$ G. B, `: T // modelActions. modelActions is an ActionGroup, by itself it, b$ n W: K6 @; j! k" [
// has no notion of time. In order to have it executed in7 z5 Y4 @* u) \- {& v7 L- G
// time, we create a Schedule that says to use the
9 K( N4 ~" v, E // modelActions ActionGroup at particular times. This
0 `9 T P8 {4 t! \ // schedule has a repeat interval of 1, it will loop every
, e7 D( h l6 K // time step. The action is executed at time 0 relative to
7 Z' J5 ?( n8 W5 S) g2 v // the beginning of the loop.8 i8 B' _- g1 `1 [/ @& d$ n
8 \* s; y# ~( F5 S // This is a simple schedule, with only one action that is; r* }, W, [ p( P
// just repeated every time. See jmousetrap for more6 O7 J7 t5 u B' W& B
// complicated schedules.
8 L) l) A \7 L9 @) c$ p9 m, p) N ! ^' |2 y9 ~# |- X m
modelSchedule = new ScheduleImpl (getZone (), 1);
8 d4 l4 I& g9 p: | modelSchedule.at$createAction (0, modelActions);
$ @9 ^: J. b$ V ' A: n, N7 s! O8 I( B8 z/ |
return this;- G0 x* o: E& T% V
} |