HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 |+ x3 P Z1 V! C
" k8 ^. R# m- q* w public Object buildActions () {
& J2 n( g& p6 @9 K: s$ S0 f super.buildActions();
. H. {( ~ C0 O. I# s. d
5 w' D6 ]9 \$ G8 t // Create the list of simulation actions. We put these in
* r0 R2 N" e4 N // an action group, because we want these actions to be) ?( Q* f0 @2 U1 E t0 }9 h* N
// executed in a specific order, but these steps should
7 a1 q' j/ I% P5 u; C // take no (simulated) time. The M(foo) means "The message
, i/ V6 ]! m% K& Q // called <foo>". You can send a message To a particular
) f3 C- ~' d1 h- s // object, or ForEach object in a collection.
- P- b+ V( u+ z2 v; m$ m 1 d6 F; |# y% ]. b; E8 J% S
// Note we update the heatspace in two phases: first run
8 s# e8 C! W6 W: t- I: n // diffusion, then run "updateWorld" to actually enact the
" \ _8 ^: A! w0 _6 C+ t // changes the heatbugs have made. The ordering here is+ p# y) i3 \3 H$ @' {
// significant!: c- ~1 P/ [ W" [
3 [1 R# K# O) h, D' i) X( W6 V // Note also, that with the additional3 g$ t& V: g, B9 D3 b
// `randomizeHeatbugUpdateOrder' Boolean flag we can
, n7 V0 Q6 w% Z/ L5 A$ Y l // randomize the order in which the bugs actually run
7 O+ ~8 J" h5 i7 c5 \1 @ // their step rule. This has the effect of removing any
4 f' U O3 b9 Q6 p6 i8 P // systematic bias in the iteration throught the heatbug
( ~% Q: Y9 v% o8 t9 ?2 ?6 n. W // list from timestep to timestep
" r" q, Q3 \) |4 c 7 A/ J+ p/ S. \, y0 F* D6 a
// By default, all `createActionForEach' modelActions have2 b% Y7 K7 i9 @7 z# _
// a default order of `Sequential', which means that the ~8 E6 }" E" ^1 T" x
// order of iteration through the `heatbugList' will be
$ I: W7 C9 K" C& i+ W4 B5 X5 Q // identical (assuming the list order is not changed) T1 S/ p5 N) R" J7 \; d0 A+ u
// indirectly by some other process)." `3 s+ r' Q. Y T0 L" K* }
3 h3 [ ?4 N' V& k+ Q+ {
modelActions = new ActionGroupImpl (getZone ());
3 J' d4 h4 }2 l3 i* O4 o3 U; O
E- @; P$ c5 v$ G, P try {, Z+ I& y1 N9 A! V1 L0 H
modelActions.createActionTo$message
" Z6 R6 b8 S; _ (heat, new Selector (heat.getClass (), "stepRule", false));" I, v, i n5 |( N3 E5 r0 Z9 Y* G
} catch (Exception e) {4 {9 Q+ f! _3 y! p
System.err.println ("Exception stepRule: " + e.getMessage ());
" r% H5 B# Q% i$ q9 N9 L& O }
; I, I# r7 r @8 m+ V+ W* w" X% ?- z( r6 A' w# t: w, e
try {
( h/ j3 D- ~4 W Heatbug proto = (Heatbug) heatbugList.get (0);
: D. ?4 J/ O/ I$ P( t" ?" }0 _9 a7 ~ Selector sel = $ d/ e, k& i8 I! M8 C% K# R r
new Selector (proto.getClass (), "heatbugStep", false);& ?3 \) f" X% L' {% R/ u3 w& Z9 P% I
actionForEach =2 d9 d5 k- Z* X9 O- [" C4 U. c% q
modelActions.createFActionForEachHomogeneous$call
4 H4 N9 U; i" x (heatbugList,- U* a5 t9 w8 R1 Q8 c) q
new FCallImpl (this, proto, sel,
1 |8 v# w% [" i2 ^% K5 ^$ H new FArgumentsImpl (this, sel)));
( F4 a- J/ e% I# i/ `$ \" Y } catch (Exception e) {
7 w) [0 r: a3 \! `" ? e.printStackTrace (System.err);1 |) q2 P* F4 N. i! A
}& P. z O( ]7 a1 i; p) q
|/ L! ^( a3 s syncUpdateOrder ();; D5 E; d' m9 n7 @& m
" [1 M3 y% e8 b) L! q ^ try { E" ]1 v) O& s- j
modelActions.createActionTo$message # d; Y+ _/ y2 z; t/ z" |
(heat, new Selector (heat.getClass (), "updateLattice", false));; c7 i: H; |5 E/ Z* m
} catch (Exception e) {
9 N. t8 ~0 ]4 d, @) B! A System.err.println("Exception updateLattice: " + e.getMessage ());
2 G* L& Q* j8 I, r( @9 z0 F; M }1 v% ` s0 O( R$ |1 @
0 ~' Q) e7 B- P% r$ X5 b6 S; e1 ^
// Then we create a schedule that executes the- X. F3 ^7 ` z
// modelActions. modelActions is an ActionGroup, by itself it
7 v' D/ {: U7 }% `8 l // has no notion of time. In order to have it executed in6 ?3 t! w a8 \7 p1 G8 Y2 }; }8 E
// time, we create a Schedule that says to use the, {5 D9 f5 J2 i+ b: e
// modelActions ActionGroup at particular times. This
C: V8 M1 G, z" ^' M+ q4 s6 ] // schedule has a repeat interval of 1, it will loop every9 y: `9 s4 D7 L7 `$ d. {& o
// time step. The action is executed at time 0 relative to, m' V: d, y" Z, k1 s
// the beginning of the loop.* V& L, D2 V4 s( `2 G8 k
/ N1 T2 T; J! w l& b9 Q // This is a simple schedule, with only one action that is
7 l- E$ M$ D/ y& j" s8 x // just repeated every time. See jmousetrap for more
1 Z; v4 {" S; j& N* T // complicated schedules.
# O" J m, f2 ` & }+ q' {( E+ {4 b& f
modelSchedule = new ScheduleImpl (getZone (), 1);
3 g: ^% O* o' U o modelSchedule.at$createAction (0, modelActions);
4 E9 z" b ?5 K3 { - e/ Q. [2 p/ a/ c& a6 y3 g) B
return this;
" Z6 G; V5 j# C$ M" n0 v } |