HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* e) G# e* z$ F2 ^2 k" _& {
) z8 }: t, n( j9 B- \' E public Object buildActions () {
7 w, F* i) I) E' o5 q" L' W super.buildActions();
+ J+ l% s c# D" |4 v% A
" X# ^$ ]; F+ ~( @2 k c5 H // Create the list of simulation actions. We put these in
% B! {6 o0 X n; n9 | // an action group, because we want these actions to be: g. @6 V' i: I
// executed in a specific order, but these steps should
6 U5 j% d, S) d) z) J) |7 B* C // take no (simulated) time. The M(foo) means "The message
. m4 W4 Y3 C& T7 ^ // called <foo>". You can send a message To a particular
" U$ d: ~& S |6 J) f // object, or ForEach object in a collection.
s7 I9 ^" s( ]* ~. R8 u
* \4 H$ p) _3 e# I0 @1 t ?7 E, I // Note we update the heatspace in two phases: first run, M4 ~" V* Q' M/ U
// diffusion, then run "updateWorld" to actually enact the% N; B ?( i4 \3 I- y0 d- U
// changes the heatbugs have made. The ordering here is2 @+ Q& c- {7 O% Y$ J) t" g+ q7 _
// significant!
1 F# Z5 ?; a/ M2 l , [7 u" C; i+ \) m) Z% p
// Note also, that with the additional8 k3 l# c' k+ b2 \+ Z1 g1 U
// `randomizeHeatbugUpdateOrder' Boolean flag we can
2 ^3 v" g _9 T2 l0 E! i // randomize the order in which the bugs actually run
+ E' y1 R' i& q // their step rule. This has the effect of removing any
. X& e8 w+ ~0 S // systematic bias in the iteration throught the heatbug
3 d! A [( r3 t7 ?) x // list from timestep to timestep( U, z! }0 A) T
, I& n! O, u7 v' z$ j4 {4 b' U // By default, all `createActionForEach' modelActions have
1 I' Y$ i; l5 x. e8 B/ T& g // a default order of `Sequential', which means that the
# ^" @( O4 }; ]4 z& i4 ]+ I // order of iteration through the `heatbugList' will be8 W: K: G1 {/ a3 y
// identical (assuming the list order is not changed5 Y0 I L. F* n) B0 I# s
// indirectly by some other process).
% f) }" p% s- B7 q
2 J9 |9 N. L# Q3 T6 I modelActions = new ActionGroupImpl (getZone ());
5 A+ ? Q! j/ v
Z u; D/ Q0 A) e! _ try {3 S: a+ a3 x7 ^
modelActions.createActionTo$message" L. G. ~% |6 {7 N/ O# i' h7 J- U6 g# E
(heat, new Selector (heat.getClass (), "stepRule", false));4 }& ^ U8 t9 e. j9 n: U. u
} catch (Exception e) {# @$ \; ~" ~/ ?% l
System.err.println ("Exception stepRule: " + e.getMessage ());# _- R# _5 B9 k% M8 Y5 x
}2 ^1 K! \4 n% D& O; H% b' C: o( d! ]
4 E8 @( o* M* {9 d/ x, X, R4 K
try {, d" `3 A1 n3 d+ ^- j* p2 t
Heatbug proto = (Heatbug) heatbugList.get (0);, {& T: n; ^) s' T. @
Selector sel =
$ P5 t N9 S" b$ H" w0 z new Selector (proto.getClass (), "heatbugStep", false);, ^, X' y3 V* O: \
actionForEach =
- `/ T! }4 ]6 A. R7 V- y7 c- r modelActions.createFActionForEachHomogeneous$call
1 M% |9 A& f* X; Y* S* C5 I (heatbugList,
! s( ~' s9 R+ w" O7 e( d8 V new FCallImpl (this, proto, sel,
& g- [9 h1 W \. D* [ new FArgumentsImpl (this, sel)));: @: t5 Y$ e( T: U5 L# ~0 O$ B
} catch (Exception e) {! M Y1 v; f9 L9 m4 n: c, W; ~8 I
e.printStackTrace (System.err);
) Z. [) G, U# `8 @# } }
& D r r) W( L k {% h" h& p2 m* v) B$ @
syncUpdateOrder ();
) e1 u; a$ I7 X5 B# s2 A9 j f/ B+ g, \- |# a$ W) a# Z
try {
2 t* }9 G& J) q( ~$ j5 c modelActions.createActionTo$message 3 N9 u9 N2 U/ B. G0 w- z: N3 U3 G
(heat, new Selector (heat.getClass (), "updateLattice", false));
0 e7 X( |9 X; H: ^- \: k8 e: ^" w } catch (Exception e) {: U+ X0 R' H% _6 v6 N
System.err.println("Exception updateLattice: " + e.getMessage ());
" i1 [6 j7 e! N' ^- }9 A" w, j }
* J2 p% D9 a+ D
3 }% S y# L P: } // Then we create a schedule that executes the4 z" v# R1 i r2 N# X
// modelActions. modelActions is an ActionGroup, by itself it* P+ E! Q& x! d$ k0 E. L; e9 `1 Z
// has no notion of time. In order to have it executed in2 n$ k0 [- ^/ I9 R" a/ R `
// time, we create a Schedule that says to use the: t6 x$ w. S; k- [" q. z* l- r
// modelActions ActionGroup at particular times. This
( L" M+ o) l4 u7 I$ o // schedule has a repeat interval of 1, it will loop every
: N' q0 M" o) V# W/ O% ~ // time step. The action is executed at time 0 relative to
/ l O3 q2 A5 U // the beginning of the loop.
7 v" _0 p- f, X
% d, _% z! z& Z // This is a simple schedule, with only one action that is
7 p5 m3 w) H) V+ K. i5 S // just repeated every time. See jmousetrap for more
; i. A- p3 ~, g( ~1 U9 L // complicated schedules.6 V, O4 b7 X7 A6 \" }& B7 f% l# h
8 S0 I0 s, \9 @ F
modelSchedule = new ScheduleImpl (getZone (), 1);" `9 ]: ]" y1 d
modelSchedule.at$createAction (0, modelActions);
$ G7 |# I3 D2 o+ \9 p v: o & E# I5 d/ _$ t& {$ B" Z$ M2 L! p
return this;
+ C' f& ]$ y; u+ U" f$ t2 s1 T0 U } |