HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( S. ?9 Q: J6 p2 ?+ I
0 u: O" H, ~0 L* k F9 P: H& G public Object buildActions () {
3 x: G' J& E2 K super.buildActions();
$ m6 s% K- }/ I9 L5 y' x. {" D + T' `$ U/ N9 X: ~$ F: X$ \7 W
// Create the list of simulation actions. We put these in; E* ~8 ?& e7 @
// an action group, because we want these actions to be
; z8 O9 v) l% E4 ~ // executed in a specific order, but these steps should8 \9 |3 U# k' \! b1 `
// take no (simulated) time. The M(foo) means "The message5 D, i: I( L, G0 g
// called <foo>". You can send a message To a particular% F f! ~) _3 ~) L3 ^8 r( n
// object, or ForEach object in a collection.. x. \8 I2 }9 I: G
7 P W: p' S$ X* ~ // Note we update the heatspace in two phases: first run
7 o* T( `, U4 `9 n% e+ w // diffusion, then run "updateWorld" to actually enact the4 }9 M: [2 p" t' N
// changes the heatbugs have made. The ordering here is7 x3 j: @) P" _4 K5 q$ M
// significant!
0 N, o, s; v4 |9 o
# P2 K& j. b+ S3 @9 ~ // Note also, that with the additional
& ~- D7 D& Y. J' D5 q# p6 u) I0 c( t, C // `randomizeHeatbugUpdateOrder' Boolean flag we can }( b1 Q! C- {! r9 D3 L9 A/ ?
// randomize the order in which the bugs actually run
9 a; {8 t/ p) y9 ?' E# m. ? // their step rule. This has the effect of removing any6 R6 |% s$ |& U6 f+ S! D
// systematic bias in the iteration throught the heatbug1 b, ?2 C E2 }; x
// list from timestep to timestep
" S( d8 l, k; k) m+ j % J/ q( `! s# E. a, f8 F* m
// By default, all `createActionForEach' modelActions have" W/ K8 j6 I* V4 @
// a default order of `Sequential', which means that the
3 ]3 s& R& q- W' x // order of iteration through the `heatbugList' will be
2 [& B6 n I. _- |( f // identical (assuming the list order is not changed
1 t" O# y5 F, w5 f( ^% E* m( d // indirectly by some other process).
6 y+ q2 t H# h# W ) R+ ]/ m5 ~, T0 V; W' Q4 j
modelActions = new ActionGroupImpl (getZone ());
% X/ O1 i# p) _' g. n: U- Z5 Y7 A- C1 Q2 u5 D& g+ Q
try {
9 ^; G5 r6 {7 e1 @ modelActions.createActionTo$message; r- ]1 c5 f0 i' Z1 r
(heat, new Selector (heat.getClass (), "stepRule", false));. ]3 V! M6 Z' p3 F' g4 ^
} catch (Exception e) {
* `- S5 I5 d/ A# n System.err.println ("Exception stepRule: " + e.getMessage ());
+ q/ u, @* {5 n8 ?9 X; V }
# r- ]7 B0 ~. [" Z' y0 M' h
- Z4 C6 |3 b5 c% ~1 @ try {
% r9 `" K$ e3 M5 {) { Heatbug proto = (Heatbug) heatbugList.get (0);
# e( G: _8 X) X; \3 @0 X Selector sel = ! o( L7 R+ M" I( E9 o3 Z% ^& d
new Selector (proto.getClass (), "heatbugStep", false);
" h+ r. i$ p. D3 f actionForEach =$ E5 a/ V2 M, c& \2 X A
modelActions.createFActionForEachHomogeneous$call
) i% E* m( T+ d- l4 |$ J1 R (heatbugList,
% b: b3 y7 ^7 P2 Z" w4 }4 i9 S( b5 j new FCallImpl (this, proto, sel,
3 N$ p7 I. t8 T9 l) M" p. o' s new FArgumentsImpl (this, sel)));
& ~, C; Y; N2 X' y3 A* T7 T } catch (Exception e) {1 U& A6 V! l5 H) T; J6 j0 L% Z
e.printStackTrace (System.err);; N3 n0 m" y' S
}4 Y' n+ u6 t! } q* {; t: g" j
k) F4 q/ Y" S* }
syncUpdateOrder ();
. z* s4 T7 f0 n7 N( | `
! r+ ~, y3 }, C" B1 A8 g try { k- w0 }9 E4 [8 `2 l, I& d! L
modelActions.createActionTo$message
4 i& J& H& r$ B5 G* V3 W9 R4 w$ v (heat, new Selector (heat.getClass (), "updateLattice", false));; H, K. g/ k- x C9 K
} catch (Exception e) {& f) } g2 _. k; b3 t$ Q( J
System.err.println("Exception updateLattice: " + e.getMessage ());
3 I; R9 A# @5 S$ Y( @8 T$ s" o, B }
: `1 K; q1 W( I7 f# s
Q' m# d8 c* W U // Then we create a schedule that executes the% n. g( N: }2 ~: F2 q5 R2 e
// modelActions. modelActions is an ActionGroup, by itself it) I9 s; X9 y3 o/ E) v) n' Y0 d
// has no notion of time. In order to have it executed in
: i+ t/ _' S1 f // time, we create a Schedule that says to use the
( K4 O: a# y% U9 W& U // modelActions ActionGroup at particular times. This( ]7 J9 P+ j1 s9 O
// schedule has a repeat interval of 1, it will loop every
# b! [) e2 p# D: s! M4 H // time step. The action is executed at time 0 relative to& L+ l+ y0 c! v* H; w( Q
// the beginning of the loop.0 Z& Z$ h$ X% e; T8 ~1 S0 |
0 _! Z' X- ^* g- O
// This is a simple schedule, with only one action that is d" v$ y: ~. F z; c: q7 X
// just repeated every time. See jmousetrap for more
- v6 ^5 r# x% P: n( H) N // complicated schedules.
; w+ n( k, T" d' B, J 7 E/ `/ b! c* C, |
modelSchedule = new ScheduleImpl (getZone (), 1);7 m) s* p" E& c3 J5 u
modelSchedule.at$createAction (0, modelActions); E. }. m! j$ V8 d
* d& c/ e7 Q, I0 h6 W8 t0 t+ @ return this;7 I5 A% G7 E1 j8 J! E+ t
} |