HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 N# b/ g7 L3 X/ J A1 ]; e9 x) U4 p! T4 H& s3 O2 u
public Object buildActions () {0 I3 I* `0 E: H
super.buildActions();
, S/ T5 V! Z1 Y8 L; |/ ~ - }; c p4 `8 m/ v# L! m7 |
// Create the list of simulation actions. We put these in3 l! J3 t( f. e% a/ J/ d# ]; H
// an action group, because we want these actions to be
; r# {* N, H- N6 E8 m; a1 M // executed in a specific order, but these steps should; F' S0 C, S- O. z" ^
// take no (simulated) time. The M(foo) means "The message
& K* r0 ]; u6 j1 ^# a // called <foo>". You can send a message To a particular
( l# D9 } K- \8 k+ s' m // object, or ForEach object in a collection.2 H5 F' r1 ^# e
" j# Q2 I' e1 U$ z2 ^
// Note we update the heatspace in two phases: first run4 `. ~& I' |, l8 r
// diffusion, then run "updateWorld" to actually enact the
' Y' _3 |! U' s // changes the heatbugs have made. The ordering here is- K6 o" W& o: N0 `7 m g; H
// significant!
! ]1 f( H/ j0 i8 l. N5 \5 ] & y, [' z" y1 [3 n( [+ _9 D0 w! x$ F+ x
// Note also, that with the additional, i. p% Z% r6 e5 o9 b
// `randomizeHeatbugUpdateOrder' Boolean flag we can
) G6 m0 d1 O1 l' u0 D4 q/ s8 l; m7 L // randomize the order in which the bugs actually run8 N& L( O; ?- R. m
// their step rule. This has the effect of removing any
2 J' R* y5 M- K: t. C // systematic bias in the iteration throught the heatbug
9 J1 K) h3 Q J! b* ]- \5 Y# t // list from timestep to timestep
7 ~! w( v" ]( S: k6 e- \7 P & z5 Y1 F7 a; n l9 h8 _* I
// By default, all `createActionForEach' modelActions have
X6 L% b4 }; M1 h' x4 T' i& R // a default order of `Sequential', which means that the9 C8 ?3 h x8 }9 L. k
// order of iteration through the `heatbugList' will be) F- \7 K$ ^& e
// identical (assuming the list order is not changed
% I( v+ z, b7 V // indirectly by some other process).
( `! z& z/ f' r- g( l! m7 v* }. L . h( y R* d5 [" l- S
modelActions = new ActionGroupImpl (getZone ());5 z9 o2 v6 d1 h
$ O0 w8 B* ]- U8 f- ^
try {7 m0 b8 r& t. |4 O. _1 ~
modelActions.createActionTo$message
0 |. T7 {8 x6 B5 ]8 R (heat, new Selector (heat.getClass (), "stepRule", false));
5 v9 k7 z& }, ?, w2 ?5 E% } } catch (Exception e) {
7 [4 x7 t3 T3 X System.err.println ("Exception stepRule: " + e.getMessage ());4 t, l4 y t! l @
}
8 b$ ?5 F- `8 W- z
7 y7 \. q$ {3 X5 k) I try {8 T" y- M2 S2 p' a& j3 b& n8 X% w3 R
Heatbug proto = (Heatbug) heatbugList.get (0);* {: h$ i3 u; M
Selector sel =
& z8 F; t& ^3 s6 s/ H) S1 g% J new Selector (proto.getClass (), "heatbugStep", false);
e" H o1 |: L4 t( P. Y actionForEach =
0 S) u7 c2 n5 {( |+ j modelActions.createFActionForEachHomogeneous$call* P8 W5 m+ ]9 _' w
(heatbugList,
0 `' B* W6 h( o- a* ^6 s7 u new FCallImpl (this, proto, sel,3 N& Q6 E& C; ]) y0 \% i! z+ B) }
new FArgumentsImpl (this, sel)));
3 c6 [: P9 C, z, [ Y* A( K6 c } catch (Exception e) {9 e( i2 V; U0 @& m0 ~2 P
e.printStackTrace (System.err);8 K+ I7 V: Q( Q0 q. D) x+ ^9 o* ?
}6 S* ]2 U% l) V! P& v
7 E4 u4 o( D, j; D7 Y2 P& a
syncUpdateOrder ();
j* k- Q- n/ n$ z
+ E8 \* k8 Z. a) u1 u, V! j9 ^. Y try {
' r8 ^2 S& |! n' w modelActions.createActionTo$message 6 P4 q/ w8 {3 C: w
(heat, new Selector (heat.getClass (), "updateLattice", false));/ Z8 ?# w7 w+ n/ W* W
} catch (Exception e) {
' u' S: f9 T; z8 a System.err.println("Exception updateLattice: " + e.getMessage ());2 I, B9 A( A2 Z# R- x
}
- ^) O7 ] `8 m ! p! Z8 V& |+ T2 K% k
// Then we create a schedule that executes the' d" n* B( S- a5 s
// modelActions. modelActions is an ActionGroup, by itself it
/ v8 q) `/ Q7 N0 v+ z4 Q // has no notion of time. In order to have it executed in
6 ?+ E6 k* d' y9 |) @: m6 G3 B // time, we create a Schedule that says to use the* P/ ~% J; T! }+ v
// modelActions ActionGroup at particular times. This8 S; m* i, y. ^' q0 a' `# F2 Q
// schedule has a repeat interval of 1, it will loop every" E% v t2 R9 ~" o) V) `' v
// time step. The action is executed at time 0 relative to n. q" m, a4 L
// the beginning of the loop.
4 m$ m" z+ p3 ^ D" d$ t& E0 L# z5 O. K3 i( E3 M0 v: v
// This is a simple schedule, with only one action that is
& b. L/ x( x Y5 w( ]+ a' R // just repeated every time. See jmousetrap for more2 K& K! G- o8 a
// complicated schedules.
m: k1 s- r, Y- |8 l
, K5 t5 [8 r9 H8 R, L1 H+ J E modelSchedule = new ScheduleImpl (getZone (), 1);
K1 J( b6 s( E- z. @ modelSchedule.at$createAction (0, modelActions);3 c) u6 p# Z% n- b: _
2 o: ?! {! h5 `9 T
return this;1 V8 f2 { S# c) x6 g a; h
} |