HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) W' F' z$ M0 W6 i! M7 s
4 b& U; C/ j# j5 T- i9 E public Object buildActions () {
" f: T: {5 F8 o- L3 Z: D: v# V super.buildActions();
; D# }* U7 a9 `9 [# y( R& ]' l
2 `5 }* b" \+ N8 g( L // Create the list of simulation actions. We put these in# c$ D! d6 N$ [% w6 C. G8 X# Q& P z
// an action group, because we want these actions to be M/ p9 \% ^- \
// executed in a specific order, but these steps should
, S+ W$ g3 J* n: J5 R1 x$ ~ // take no (simulated) time. The M(foo) means "The message6 A9 r7 _! x6 J& r- E- ^! g
// called <foo>". You can send a message To a particular. w: q( r9 H3 S
// object, or ForEach object in a collection.' a( \* `. {* J2 B5 ^9 g
! D# k; b9 V8 A- p1 j9 c // Note we update the heatspace in two phases: first run
. k1 B% g \7 W // diffusion, then run "updateWorld" to actually enact the& c t5 y# a! F; D" v$ g
// changes the heatbugs have made. The ordering here is( u; ~2 X8 H5 e4 U
// significant!
" k) n; }6 i. L! m# h ( q" ]- U; S4 U x" I
// Note also, that with the additional8 A- [% S6 \/ Q: R6 N4 w
// `randomizeHeatbugUpdateOrder' Boolean flag we can0 c6 M, D" _7 d$ Y
// randomize the order in which the bugs actually run
" a* {8 Z) h( T7 { // their step rule. This has the effect of removing any
{/ |0 G: O1 J1 `1 @! C // systematic bias in the iteration throught the heatbug
- S+ u0 u3 Y; V6 w' T& k, s // list from timestep to timestep6 o4 i: `1 S* h) S4 o# H! {' t/ T, V
6 r+ [( N/ \2 d( a% d) {2 f# Q1 ~0 H
// By default, all `createActionForEach' modelActions have
9 q) b' e6 A# O) G! A* ~$ C( @ // a default order of `Sequential', which means that the( G" v7 V8 A( A9 l1 q
// order of iteration through the `heatbugList' will be
' t+ J$ ?3 t1 _9 o6 p, O7 ^: M // identical (assuming the list order is not changed& M& y4 J" b& D6 O6 d. H) s
// indirectly by some other process)., P6 T! s m: @, b7 V. v
8 }$ ?( v( C9 \+ J0 T modelActions = new ActionGroupImpl (getZone ());
6 g% Z* {, i$ i' K9 }1 T- Z( q/ i* x9 J( X6 [2 i& K" y
try {0 e! ~( g- N. A
modelActions.createActionTo$message7 q: d6 Q- o) k' ]2 l' _9 o
(heat, new Selector (heat.getClass (), "stepRule", false));1 X- I( z9 D* `: @! @* I f* n! i
} catch (Exception e) {
3 C2 z+ Y+ a9 a$ N: z$ w) _) r System.err.println ("Exception stepRule: " + e.getMessage ());) @# ?0 v: W" }' o3 I2 c. ?% R( ^
}
0 h. _( V2 \' p9 U7 t. Q' U( a& u9 k0 M3 m* M4 v u& x% `( `
try {
/ f3 g! Z c% i% O Heatbug proto = (Heatbug) heatbugList.get (0);# J7 b A* l+ F2 n% \0 E/ `+ L U
Selector sel = 6 }* b% Y# y: p9 e9 q
new Selector (proto.getClass (), "heatbugStep", false);
: D, I' y+ l- o! ~ actionForEach =
+ m$ m( N1 Y! a: O6 K modelActions.createFActionForEachHomogeneous$call
# b( x% ~8 q" K+ j( b/ X (heatbugList,
0 H% A Y3 B# | q7 e5 b new FCallImpl (this, proto, sel,3 P" p7 ^+ a7 u5 c
new FArgumentsImpl (this, sel)));6 Q$ f: R& J- S S+ h
} catch (Exception e) {
) A* b: F2 Q% C! w+ G3 S e.printStackTrace (System.err);1 v) m) e1 x0 C2 U. z1 `3 K& E
}
" m* n4 ^- l& Y6 h0 N( Z4 j ( P7 m! z5 @$ Q1 h) ^% T
syncUpdateOrder ();; I5 k" g4 l0 ]5 z$ ?' E2 B7 y
+ ^4 x6 N. ?4 S1 i* O
try {
+ m7 \% L, ~ d9 g modelActions.createActionTo$message
4 b3 ^# T5 G$ h6 B9 T* R1 x (heat, new Selector (heat.getClass (), "updateLattice", false));
C- n1 p1 P w: t9 u } catch (Exception e) {/ y; F1 r& V# d" G6 ~
System.err.println("Exception updateLattice: " + e.getMessage ());$ n3 }, F0 B6 Y7 i$ Q' h
}
d) S/ N3 q q R , c. p4 X3 i6 V$ P
// Then we create a schedule that executes the
" l( y) g V0 k! ]' h0 z# d t // modelActions. modelActions is an ActionGroup, by itself it6 {" A4 j! @+ J* o! t' x2 L
// has no notion of time. In order to have it executed in
. e9 d/ V, z) x, o+ W$ s // time, we create a Schedule that says to use the
4 L y6 z5 [% {# d% S4 J // modelActions ActionGroup at particular times. This
1 b% p- K5 r3 Q6 V1 L, f' i1 A/ w+ l // schedule has a repeat interval of 1, it will loop every/ ^ q. Y" H2 `. r8 v
// time step. The action is executed at time 0 relative to9 h5 ~2 G- n3 [+ {" k% i, h
// the beginning of the loop.
' U/ W$ T8 ^9 X7 G2 v) h5 @3 m) D, v& \# d F
// This is a simple schedule, with only one action that is* @8 _3 S$ V3 _' j: q1 h& _
// just repeated every time. See jmousetrap for more v6 q2 O5 V+ b+ l2 N
// complicated schedules.
9 w, V' {5 b) `6 D$ o b! h
# l2 s; s: x) j modelSchedule = new ScheduleImpl (getZone (), 1);/ R+ x4 n" U& J5 q# K
modelSchedule.at$createAction (0, modelActions);
: J/ c' d) S. o$ E& t- L 5 f _7 X1 I# T
return this;
! t+ E6 k8 T3 r } |