HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: I. Q7 l6 S, L/ V; ~
" T" ?3 N( w# G public Object buildActions () {
( P; g8 `% Y/ q" } super.buildActions();$ [# X7 b7 Z1 ~ i; I
9 q( Q( Q4 L( h9 ]4 s" r // Create the list of simulation actions. We put these in
# r0 D, t( a0 Q" Y0 |& ~" K- N // an action group, because we want these actions to be
* ]6 ?$ y1 r6 B // executed in a specific order, but these steps should
6 T' R8 U* ]: h7 m' b6 c* Z2 W // take no (simulated) time. The M(foo) means "The message$ {3 A9 _/ L: N# m
// called <foo>". You can send a message To a particular
$ |& Z/ L& q" n! M5 X1 K3 Y // object, or ForEach object in a collection.0 l* w( D6 h) B" v5 U, q0 @9 n
- j& P$ \9 u1 e$ E$ n( w2 d; |
// Note we update the heatspace in two phases: first run
9 |) [7 {9 I$ ?6 f0 ]: c // diffusion, then run "updateWorld" to actually enact the) t3 x; H5 J0 ^" r5 R( P& a$ [% n
// changes the heatbugs have made. The ordering here is
# o! f4 u9 V) L" ?" c% j8 n1 O // significant!' u7 P+ B2 R. r8 S6 N# R8 e0 A0 i
7 s% O$ Z7 u- B. m c5 n6 ^ // Note also, that with the additional
1 R' o, K6 @$ U1 X1 G // `randomizeHeatbugUpdateOrder' Boolean flag we can
* n! H) V, y9 I2 | _ // randomize the order in which the bugs actually run7 z+ t$ t8 E) \2 f, L; c/ d
// their step rule. This has the effect of removing any% N, B* Q% I, J7 O- a& o
// systematic bias in the iteration throught the heatbug
0 j7 h; E( h* d- |' P8 G // list from timestep to timestep
4 ^4 g& I% a- F/ F- G6 m
. B+ H/ t7 x4 O7 T2 n% s // By default, all `createActionForEach' modelActions have/ x1 B. `5 h9 }+ j$ i u2 O
// a default order of `Sequential', which means that the- O! o6 Y* a& K1 [
// order of iteration through the `heatbugList' will be
" |) o. {) A. C2 L0 R // identical (assuming the list order is not changed
. V# G, \8 r# T2 L, [( h // indirectly by some other process).
7 p$ M) d X6 b* {2 O( ~ 5 @& N' A, q, M3 n3 G
modelActions = new ActionGroupImpl (getZone ());
9 V) \! `; E- `$ c! t+ b2 s# o E1 U2 N& F* g; O1 A6 Y) ~$ ]
try {
9 M. }& W0 s" N) n4 | modelActions.createActionTo$message
: I1 U% O' {1 J$ x (heat, new Selector (heat.getClass (), "stepRule", false));) M- p9 n4 ^( a X+ b, w" N% y
} catch (Exception e) {$ }+ }/ B# |8 H p) f2 V
System.err.println ("Exception stepRule: " + e.getMessage ());
# E9 _& [- z. g } ~$ ^: C/ z0 p. x0 Z
7 g; A4 _ H3 i: D try {) F+ c+ d7 V! F' f ?8 N$ ?
Heatbug proto = (Heatbug) heatbugList.get (0);
6 H) p- B: [; N' u* \; O4 L Selector sel = + S% U* q) S( k5 C! Z( M
new Selector (proto.getClass (), "heatbugStep", false);4 N) h) e: `: S% }+ H( G
actionForEach =% ?" _8 b4 P) L3 c& K
modelActions.createFActionForEachHomogeneous$call5 v; V( P1 W/ p' ]3 W/ B
(heatbugList,0 B, N3 ?/ C* v+ @! Y# k
new FCallImpl (this, proto, sel,
# P& d% q4 n: \8 m2 l new FArgumentsImpl (this, sel)));# w ^ @ B+ y! ^
} catch (Exception e) {, Q4 r4 w' Y' i( Y0 R! w4 b# ?
e.printStackTrace (System.err);! ~! H! e0 S( v p. X1 l) c
}
4 D8 {9 K( ~' F' q. v9 j0 p- Y ! n4 }" P- @9 N, [; U ~ T- A1 G
syncUpdateOrder ();. b( p. o7 [$ _8 ~# b% S, v8 `
1 M" t% [, y0 ~; A) c7 X
try {" |# X$ G( P4 D! V( Y( w
modelActions.createActionTo$message
4 Y, a+ J& E6 C0 B0 ^ (heat, new Selector (heat.getClass (), "updateLattice", false));) T& S, x$ Q% V) ?" _
} catch (Exception e) {: n0 k( v) O' R! H, n3 z0 T: W, }
System.err.println("Exception updateLattice: " + e.getMessage ());% j' T# Z/ f- E! F' Z8 U4 ~: [
}' S Y/ ]9 r* G, V8 S' S
% z3 M, H( n4 Z1 [) | // Then we create a schedule that executes the
% u" K0 G; m9 y! i; I0 f8 I // modelActions. modelActions is an ActionGroup, by itself it2 ~% k2 T/ F% q; ~3 J- `
// has no notion of time. In order to have it executed in
9 L/ [4 n; P& L3 R. y; v // time, we create a Schedule that says to use the9 K6 f9 r# h1 Q
// modelActions ActionGroup at particular times. This. [$ t2 a& [/ y# J1 t8 r9 S8 J7 a7 k
// schedule has a repeat interval of 1, it will loop every8 ~9 |/ n, A8 o6 g
// time step. The action is executed at time 0 relative to4 k4 { v2 A& d) v4 A: Y/ s
// the beginning of the loop.
n' e7 c" w: w e4 x3 r1 D( Z. Q9 o' A( l' H- r
// This is a simple schedule, with only one action that is6 c4 T }8 j$ J
// just repeated every time. See jmousetrap for more4 e3 z N+ d' W6 a/ t' h
// complicated schedules.
6 ]- }4 O4 s+ D; A0 a1 ^- `0 A ' R5 P- Q5 z9 p f
modelSchedule = new ScheduleImpl (getZone (), 1);
+ F4 P8 @9 s. X" F8 f5 a modelSchedule.at$createAction (0, modelActions);
/ Z; m `0 ]/ I9 `) M5 [
2 T, I f' O+ ]9 }: H return this;
( @" ~7 S S+ R W+ W: ]/ m% i } |