HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. D% `4 t: ~) z' K' q4 Q/ ?5 u8 I; y/ p3 L- c2 Q, d; D
public Object buildActions () {
# c) d0 L6 P0 [- D& M super.buildActions();8 b0 b) k! O$ e2 \4 F
% d9 g) i! p p/ E // Create the list of simulation actions. We put these in X5 {5 j2 ^( H
// an action group, because we want these actions to be
$ N5 ` G# c2 k" i, ` // executed in a specific order, but these steps should- l: O- }7 W X: B7 b
// take no (simulated) time. The M(foo) means "The message6 y. L2 G6 b; Q
// called <foo>". You can send a message To a particular$ C, D3 X6 V$ Q6 b \& n
// object, or ForEach object in a collection.
! S7 L7 h, Q- c
+ b8 c& P2 B& u( G1 E" z- n ?6 E3 t // Note we update the heatspace in two phases: first run
# d& N+ M5 T. P0 I, R2 J5 ` // diffusion, then run "updateWorld" to actually enact the
+ Y0 E: l( H z" S2 A5 G7 z // changes the heatbugs have made. The ordering here is! _, [5 u8 p7 A$ a' I8 [; p8 C
// significant!: b: Y! q3 L% u* \' s
4 [( j7 p% I8 b+ c/ { S: |6 @* E1 M // Note also, that with the additional
2 x7 n; g4 S0 O3 N // `randomizeHeatbugUpdateOrder' Boolean flag we can8 {( T2 w. I* j$ J& o
// randomize the order in which the bugs actually run
7 F: ?# q' G1 L% s/ R4 e) f // their step rule. This has the effect of removing any
) ^7 X' m5 [& k# r" p* q; B- t // systematic bias in the iteration throught the heatbug
6 X( B! {+ \# `4 m' t7 x // list from timestep to timestep
2 [ N$ M& d) g, t( w
: I& B9 [ @. N$ \! e) B6 m // By default, all `createActionForEach' modelActions have
* s# `/ Z' ]7 F // a default order of `Sequential', which means that the+ g, e! j# ?6 D
// order of iteration through the `heatbugList' will be% a% e" V* W1 ^, @4 y
// identical (assuming the list order is not changed
; P( D: B \ P- A5 P' j, V // indirectly by some other process).
) b* `9 M# z$ b0 g0 }$ n3 b2 v
2 I! `% }* V. Q modelActions = new ActionGroupImpl (getZone ());3 Z0 E E: |/ G, M
/ U9 U, [) ] Z7 \: J$ T$ k try {
+ O/ g- G$ @- w2 q4 w modelActions.createActionTo$message. R4 N; k/ M* D4 ^
(heat, new Selector (heat.getClass (), "stepRule", false));
7 A+ U+ K" o7 t# D6 [ } catch (Exception e) {6 w6 H. a( P% n8 h' d
System.err.println ("Exception stepRule: " + e.getMessage ()); N; j+ r* k: b& t$ E3 z% f
} x" H4 x" C" u5 l& x
8 f0 v/ @8 b; }" V0 O
try {
, V2 l! P. q4 `) q& t; ~, M Heatbug proto = (Heatbug) heatbugList.get (0);
1 E- K2 e; ^3 C0 r1 r2 c Selector sel =
; D0 m! a- F9 z* D3 d new Selector (proto.getClass (), "heatbugStep", false);' \' V! y5 X9 L2 B' o
actionForEach =
" c* N. u, V. z3 X8 g6 L modelActions.createFActionForEachHomogeneous$call: E( d6 A2 k; p& d
(heatbugList,7 c; j1 |. E K2 N2 U- I: h+ ~" ^8 v
new FCallImpl (this, proto, sel,
s2 ?# s8 |. |6 [0 w new FArgumentsImpl (this, sel)));
1 t1 E6 k( D% h# P+ o } catch (Exception e) {: J4 u2 r: q4 B7 P1 i+ b
e.printStackTrace (System.err);# O. T; C% A; d: V1 O0 j: {
}
9 F/ I+ Y) \2 n* } ( r' K# n7 ^- x$ R, a, L
syncUpdateOrder ();
e& z. [1 e0 W# H( j/ ~& e" \' ]# b, a/ m4 Q6 [- \
try {
7 M: K/ x5 N! U' i' d: c7 [ modelActions.createActionTo$message # \1 p. N/ E" X9 Q' z! _( S
(heat, new Selector (heat.getClass (), "updateLattice", false));0 k9 _( Q# K5 @& l( e, o( k
} catch (Exception e) {
! h0 }3 `+ } z- U System.err.println("Exception updateLattice: " + e.getMessage ());5 H2 Q6 D2 \* x4 S7 {. W& u9 u8 O
}
' i% U& _7 v6 r1 @; c
O u# [) i$ E& V! [ // Then we create a schedule that executes the' J" d/ C+ ^ t5 v$ i
// modelActions. modelActions is an ActionGroup, by itself it* q. x4 `* O5 |9 b2 J3 O
// has no notion of time. In order to have it executed in; y. g" ^3 W* Y$ H3 ~
// time, we create a Schedule that says to use the
8 A0 C7 F d6 P# M+ P) T' ` // modelActions ActionGroup at particular times. This. o! P( w/ M3 e
// schedule has a repeat interval of 1, it will loop every' \; p+ ^9 Z6 P' F% [; |
// time step. The action is executed at time 0 relative to- `+ u% I$ p }% K" Y+ z
// the beginning of the loop.
1 D; q. u6 }& n0 P( m* t
. r- z, I' ~) [$ m$ r' U/ F9 F" q9 C // This is a simple schedule, with only one action that is
/ \6 Z6 C) N2 D7 u' G: _4 Z% w // just repeated every time. See jmousetrap for more
+ V- B' z& v% R // complicated schedules.
- x2 F6 k! E; }2 g5 d
2 c6 {+ }! {, i4 F$ v! L3 d0 f modelSchedule = new ScheduleImpl (getZone (), 1);% F8 H6 S7 L- e; z0 ^2 p) l2 `- V
modelSchedule.at$createAction (0, modelActions);
9 K9 e+ w" _" j+ x; ^4 w+ K % J$ ^ O _9 W! M
return this;
! G j9 ~) r- f8 t } |