HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ D6 h1 | [. ^. U) s( l# Z6 J" a% B( y9 e+ _9 O
public Object buildActions () {" R& ~5 a7 X8 E- \6 H! ]
super.buildActions();
5 V! [ N8 r1 x+ ~6 y
! j+ V2 ], Z- b g // Create the list of simulation actions. We put these in
$ ]1 P5 B, b c1 J9 A // an action group, because we want these actions to be
! T" }: L/ W8 l3 G# R& e8 N // executed in a specific order, but these steps should2 l4 }. p+ I. p: \
// take no (simulated) time. The M(foo) means "The message( T: [# R* x( \6 C, e; |: d
// called <foo>". You can send a message To a particular# j P" P$ P- _
// object, or ForEach object in a collection.
+ ~/ M- {* w& ^ b+ r ) g) N6 l: F, x* e D2 b* L
// Note we update the heatspace in two phases: first run2 s9 x+ E5 J1 E2 ]+ B# R
// diffusion, then run "updateWorld" to actually enact the
) h5 f7 u" ]: v* N: D* } // changes the heatbugs have made. The ordering here is
. U u; d# N d) d // significant!6 b. [6 B0 K6 K- \- ^$ N1 t: b
, |0 _5 [' @2 w% ?; s2 S' l( T // Note also, that with the additional2 q" m- w8 W: E
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 q- N6 X c/ @' Q( A
// randomize the order in which the bugs actually run9 \; p- f1 m" t5 P6 j2 a4 e: k1 T# o
// their step rule. This has the effect of removing any
& f5 k' Q# v& W3 h# P // systematic bias in the iteration throught the heatbug9 I( r0 c" m9 \, @. T- {
// list from timestep to timestep( J/ Z/ G* p6 O, k2 }) L7 x! G- V+ u
9 C- k% Z# k L( b4 ]% T# ~
// By default, all `createActionForEach' modelActions have
4 b/ e% \) h6 j0 n, O$ W0 J // a default order of `Sequential', which means that the
0 J) I& F! Y' F9 b, p // order of iteration through the `heatbugList' will be
5 K( V6 u+ z6 U: v, k& H# s# { Q" A // identical (assuming the list order is not changed% e8 w, X: b% a
// indirectly by some other process).
5 E' `9 }9 s9 E4 a0 B0 P
0 y9 c# e. a( e- ^ {7 g modelActions = new ActionGroupImpl (getZone ());" Q2 m! Q- ^5 |4 ~' ]) t
' [: h( E! Y0 F# I Y( R) a try {- y( u- \" D! [% O
modelActions.createActionTo$message' C8 P0 w) V4 F: k5 ^* T
(heat, new Selector (heat.getClass (), "stepRule", false));! }$ t j7 c, ~' F' x6 S8 h, L
} catch (Exception e) {6 V& W6 ~% F: a( A
System.err.println ("Exception stepRule: " + e.getMessage ());' V* c: W4 _9 u/ g3 w( [6 Z4 ~8 ?
}
0 C& h2 w- [0 Z7 G/ Q3 k& o1 r# w1 j5 u8 n* e, {- j
try {
0 x/ V, i% ?) Y# c3 Q+ K: t2 E8 q Heatbug proto = (Heatbug) heatbugList.get (0);
4 z% p/ H @( | Selector sel = ) x. F( z3 r. E \) W7 [; D+ a
new Selector (proto.getClass (), "heatbugStep", false);
/ X4 }1 Z/ ]& l5 _" H actionForEach =
+ j+ M3 t7 I" o, `# Z3 N/ f modelActions.createFActionForEachHomogeneous$call
( U6 ?# w3 v# G' m2 ?1 ` (heatbugList,
. G& r1 D: |# `: {% P& g new FCallImpl (this, proto, sel,4 z" _3 g Z3 W: Q/ v
new FArgumentsImpl (this, sel)));) F Q& ~: q0 [& G
} catch (Exception e) {
. `, M% c, W$ S7 y0 u e.printStackTrace (System.err);
" t( C2 j; G/ o" k( c" D# g }: h7 A7 T3 a' n/ @2 U0 F# g
" B0 b/ E f0 T7 L5 U
syncUpdateOrder ();# x& T6 R+ C" g9 V
% b4 i& f$ a; x1 K- s$ P- D try { S/ x1 b; ~$ f0 _9 L% D: i( ]
modelActions.createActionTo$message , Z% R q# D2 v% Z" |
(heat, new Selector (heat.getClass (), "updateLattice", false));
% l6 C _8 R% n8 e } catch (Exception e) {( G5 q& e. I* f
System.err.println("Exception updateLattice: " + e.getMessage ());* O) J1 ?. f. G/ ?3 v7 W/ ^& `
}% w/ K Q0 {0 l) F
% S8 i1 g3 R& `+ V* t2 f
// Then we create a schedule that executes the m4 q) Z9 f2 y8 g! K% j
// modelActions. modelActions is an ActionGroup, by itself it
! T. o, f+ D- t // has no notion of time. In order to have it executed in
# t1 k' L# [. h* F8 ^ // time, we create a Schedule that says to use the
( f" ?8 v0 L. M% y& S) z // modelActions ActionGroup at particular times. This6 [$ i( v# v! j. N9 _ N' M
// schedule has a repeat interval of 1, it will loop every7 D; `0 I5 c; {) q% I
// time step. The action is executed at time 0 relative to1 ~% Y, ^7 W1 N+ ^
// the beginning of the loop.
- s0 F9 l. U' R- l$ R Q& f. t4 q8 U& t/ @ {! @
// This is a simple schedule, with only one action that is. [. S F" V. k6 ^ |7 t: x
// just repeated every time. See jmousetrap for more
6 ~& u) i* v: b // complicated schedules.
; @( o) @" c4 Y( A& v) o2 J
7 f+ K: f0 r" f* Y9 R* P# } modelSchedule = new ScheduleImpl (getZone (), 1);
0 V6 K3 Q& L- N modelSchedule.at$createAction (0, modelActions);
1 p! Q- B2 J K / d) k# B% ^$ e$ Z3 H% a
return this;, P0 Y S$ K: q+ [4 y
} |