HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: ?) Z) G9 d- X1 h7 [. c
2 ?0 b/ X I. w8 W public Object buildActions () {$ b1 `3 p" _# l) l% }( K
super.buildActions();
" Y* c4 y( O( h0 `2 ?! e5 W8 V 0 c$ o, H( @/ j- H* H" X9 w' ]
// Create the list of simulation actions. We put these in
5 y: o% W7 c8 W; ?' g9 b // an action group, because we want these actions to be
2 Y/ @1 O* W0 c1 ~5 l // executed in a specific order, but these steps should, k. t2 w3 g+ U7 N8 r
// take no (simulated) time. The M(foo) means "The message. q2 B3 T. i p, r
// called <foo>". You can send a message To a particular
, y4 k; i2 P( _7 V% k B; _( |: V // object, or ForEach object in a collection.: n" w, I0 ^% p7 J# n( g) p3 K
" g6 E- s" d: ?: i8 [, j // Note we update the heatspace in two phases: first run& ^! @) h+ Q# V1 n( p
// diffusion, then run "updateWorld" to actually enact the
" f& q2 L$ E# _ // changes the heatbugs have made. The ordering here is5 q% S5 Z( N; ~. \( q. }
// significant!2 }9 b% r( W/ _' {: r
' a4 O: |& H& G5 E" c7 Q // Note also, that with the additional
8 J1 z7 F8 L& x7 f& t // `randomizeHeatbugUpdateOrder' Boolean flag we can/ a3 {' t3 l' } l5 L: p& B
// randomize the order in which the bugs actually run/ _! _ P# @* \3 b% F
// their step rule. This has the effect of removing any
5 j8 e/ q1 j+ j6 O1 O! x // systematic bias in the iteration throught the heatbug
5 m# {5 b. y3 E" i0 K2 q // list from timestep to timestep1 H1 y, D7 H6 n6 @+ R
9 q j, b1 Z3 t: l2 S5 V c // By default, all `createActionForEach' modelActions have
- u; k3 ^ s- h' m; k2 M; w // a default order of `Sequential', which means that the! L7 `1 T+ j9 n6 x0 m! k
// order of iteration through the `heatbugList' will be V, C3 x/ M% S" }1 _
// identical (assuming the list order is not changed
4 e8 O. J: m: P2 U4 b- p1 n" K // indirectly by some other process).4 t% {6 `5 b/ t& o& t1 e2 S
- I& { c7 ?! \, N modelActions = new ActionGroupImpl (getZone ());
% C3 s a. h: x9 i% ?* R2 _" g& y$ c
try {) x1 q3 i- }* O. M C
modelActions.createActionTo$message
0 r7 ?; ?0 Y$ l; G& s: g (heat, new Selector (heat.getClass (), "stepRule", false));
& z3 Z' Y6 P% o/ G \ } catch (Exception e) { k4 }; Y6 G n# ^1 a* Y9 R7 C
System.err.println ("Exception stepRule: " + e.getMessage ());& p3 m ~$ p" G
} O2 E9 i1 ]' k% q; C! i
5 L! q; O! \4 E5 c try {7 K- W, E! y2 {8 d/ k
Heatbug proto = (Heatbug) heatbugList.get (0);
1 E, _1 k( o( ?2 V* R% H0 ~9 g' |, o Selector sel =
. z$ w9 `: l9 y5 U new Selector (proto.getClass (), "heatbugStep", false);
# \6 t& o% Z/ m3 [- t* a+ b @ actionForEach =
}2 L6 }! P" ~7 _, p5 h- t0 r modelActions.createFActionForEachHomogeneous$call
' V- B: J# k1 K$ v (heatbugList,- v$ g& U# l E$ J) d
new FCallImpl (this, proto, sel,! k" a. H0 L& w
new FArgumentsImpl (this, sel)));& v( t/ s8 D9 c6 x& o
} catch (Exception e) {3 p P1 m9 T* ^1 J$ _
e.printStackTrace (System.err);* u+ }1 ^3 |9 w \5 h& j
}; F2 O4 g: P& Q- q* `1 }
# z3 Q# ~. f& e' F+ M) \ syncUpdateOrder ();3 E0 F+ e9 D; A- b! w
- i- B: z% C N/ @* b- Y try {4 k4 f$ \: C% { n4 a& r$ \
modelActions.createActionTo$message
5 q9 u- J& N" O (heat, new Selector (heat.getClass (), "updateLattice", false));
. ?% ]! n: ?5 [4 P/ x0 M1 r. i# W% V } catch (Exception e) {
|! n8 E2 B! ~. z, o# a System.err.println("Exception updateLattice: " + e.getMessage ());
- G. _" F3 s) q7 v }+ |" ^/ v Q$ x+ o( {- c$ l
, `) v# J6 K" P. | // Then we create a schedule that executes the
6 S7 S2 P, b& U# j$ E) v% n9 x // modelActions. modelActions is an ActionGroup, by itself it6 S3 t9 S9 U5 U# j
// has no notion of time. In order to have it executed in1 v* U7 N& k2 c9 x# b, d/ b5 F
// time, we create a Schedule that says to use the
A, A. C7 y/ q // modelActions ActionGroup at particular times. This5 X" C% j4 V1 J9 _" s# B; i
// schedule has a repeat interval of 1, it will loop every$ q$ l% z5 j+ n% i# T& j
// time step. The action is executed at time 0 relative to
2 r7 o1 h; z2 T, L4 l // the beginning of the loop.
! x' d8 F3 N! E$ J, }+ ^
% {" f4 F; n1 R2 C // This is a simple schedule, with only one action that is
; F- f* A0 t( Q+ p# c) `. g // just repeated every time. See jmousetrap for more/ R; |$ t" b. P' ]$ |7 g
// complicated schedules.
. [3 `+ p5 D' Y4 b' L5 `; U / c. E/ d$ V, q, D
modelSchedule = new ScheduleImpl (getZone (), 1);) P5 ]6 t9 \+ C, y8 n. K, M" O
modelSchedule.at$createAction (0, modelActions);
7 b$ Q% T1 s+ g+ s" e' V1 o
* i% R1 n7 M& q7 ~ return this;1 Z( j; h5 ?/ j7 E o! w% E; |
} |