HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 R3 r V/ Z0 O! j5 G" S% U& g. `
/ L3 H. N+ o" D2 t6 E P public Object buildActions () {3 Z% \$ y* q' e: L" l
super.buildActions();
1 `1 e- I! Q9 R# N
1 t( ^- z& l' i8 `( v // Create the list of simulation actions. We put these in
6 ]# `! R6 _* d // an action group, because we want these actions to be; Q' ` r) B8 h* X, ]; G
// executed in a specific order, but these steps should
) r+ _$ z1 `+ |9 k+ D: f) b // take no (simulated) time. The M(foo) means "The message
c5 \. u) e4 S& k# r% ~ // called <foo>". You can send a message To a particular' e) y& M1 A* W$ `) ?
// object, or ForEach object in a collection.6 A% m ~1 D7 W7 T
2 o/ G# s2 k L% n // Note we update the heatspace in two phases: first run
# o$ h2 e" p- n; j. T // diffusion, then run "updateWorld" to actually enact the
! R0 P' h% G2 Q7 F5 O) W // changes the heatbugs have made. The ordering here is+ [3 p* g0 E8 L; M
// significant!. o7 g9 N% u& ?6 B
% b/ o0 q" e8 B) B6 v9 z- B
// Note also, that with the additional
; K* O K {) `. \2 ]7 n // `randomizeHeatbugUpdateOrder' Boolean flag we can
) l+ b" i. v6 B9 e% q // randomize the order in which the bugs actually run: J, |# V( i3 ]% ~6 W) M8 t z
// their step rule. This has the effect of removing any8 d6 F' A7 q, @) f6 R9 V7 J7 |3 I
// systematic bias in the iteration throught the heatbug
k. w6 ~. k9 p+ v // list from timestep to timestep/ `7 V7 @' n$ }, n
4 {' {8 N$ W& |6 H: f6 Y+ f l // By default, all `createActionForEach' modelActions have
1 ^& J" J# b6 r0 s1 K+ u' Q // a default order of `Sequential', which means that the. B1 R8 m- q, O" p2 h: r- i) Y
// order of iteration through the `heatbugList' will be& V& t% y7 Q( H# |/ b
// identical (assuming the list order is not changed/ R# A& Q3 m, _7 W7 M
// indirectly by some other process).
' P: O9 |' ~( ^/ v: b+ V; z - \* ^ P9 ^5 i$ ]
modelActions = new ActionGroupImpl (getZone ());$ M% V5 K) m5 o& a
7 V6 c8 |( N6 i" A" \ try {& A1 P# w$ Y5 x3 }
modelActions.createActionTo$message( g5 M. _4 {9 y) R
(heat, new Selector (heat.getClass (), "stepRule", false));
, l# y; a; W5 }3 Y } catch (Exception e) {4 q3 Z( D [) ^
System.err.println ("Exception stepRule: " + e.getMessage ());
# _- Z; f# c6 e. I, q$ Q }
3 q. R' B: a4 {) q4 t9 q Q6 z+ e' w- R) M; D
try {
4 J5 F# p# v3 u4 d$ s Heatbug proto = (Heatbug) heatbugList.get (0);- i. ^+ |2 j$ q. H. e+ [1 R
Selector sel =
( V2 {2 B w8 S8 h1 U; l0 z new Selector (proto.getClass (), "heatbugStep", false);" C. J t6 `9 ]8 t: H4 S
actionForEach =# K, u+ }8 l% N" v; q
modelActions.createFActionForEachHomogeneous$call
: P: Y3 H$ b0 N! e3 a% Q5 e/ C (heatbugList,
; }2 o5 s5 t; `5 _8 ?7 m' \ new FCallImpl (this, proto, sel,
& O; R) M. _- q! _6 S) ^( a+ J new FArgumentsImpl (this, sel)));: Y. ]. [, f# ?4 |" {
} catch (Exception e) {
0 {- e. p' l+ H0 t e.printStackTrace (System.err);2 }( y( m8 T; M1 p
}
' t* z" g* F/ k9 q 9 k {$ a8 ^8 u7 h
syncUpdateOrder ();
: U# c$ B/ a2 Z7 d% M' s: k' z% x r j0 m; u
try {1 x6 ?9 |* y( m" v1 q0 n
modelActions.createActionTo$message $ N) E9 f" X- }
(heat, new Selector (heat.getClass (), "updateLattice", false));
; \, z. \* P8 a8 N' M } catch (Exception e) {) o% ]5 W. I" n# R
System.err.println("Exception updateLattice: " + e.getMessage ()); s4 b2 F: w3 s9 o
}; i+ f' p% y4 w/ c1 i
: P% r* G. e9 \- q // Then we create a schedule that executes the( S& z! A* W; |1 J, N' Y
// modelActions. modelActions is an ActionGroup, by itself it* o3 c' J8 X: I; L, k: K/ b
// has no notion of time. In order to have it executed in/ H& t# ]' c: A( O6 n% ^
// time, we create a Schedule that says to use the9 _0 @- U0 v* g H9 f3 _( X2 |( w
// modelActions ActionGroup at particular times. This
: I+ \' T5 n6 O, _) V( `1 P // schedule has a repeat interval of 1, it will loop every! o0 a: N M' ]- k; P. U2 U
// time step. The action is executed at time 0 relative to
+ H4 n; t6 r ]: ~# R2 U! W+ r2 S // the beginning of the loop., S4 r5 o# q0 ?+ s, K: ~
" |. t' ^ ^3 o S4 h
// This is a simple schedule, with only one action that is
G1 t" T4 V4 O, p( h/ t // just repeated every time. See jmousetrap for more- R/ r; ~ O+ t0 \/ x6 q# F3 N
// complicated schedules.' G: P( e) ?+ v2 \: g, i2 Q
- p4 ]+ }' p) o, E% D, i3 ~ modelSchedule = new ScheduleImpl (getZone (), 1);* R3 b! U$ b* U7 A& B9 a& O
modelSchedule.at$createAction (0, modelActions);% B. K/ c6 z( r: Y% |7 _8 O
. @. V% L% t" r% ~
return this;) T1 \! o( g! D6 c
} |