HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 J& `; X8 j1 z {
: ?9 G5 _4 A) E% L. b public Object buildActions () {8 E: H/ R4 M6 ?& S' f1 Z8 I
super.buildActions();
( t: `- v0 O: G4 [ `, m# G. k4 h& A. ~1 F5 C1 t
// Create the list of simulation actions. We put these in) C6 \! d/ ?# c# w1 o
// an action group, because we want these actions to be
; p5 I' \6 d @* ^3 j+ [ // executed in a specific order, but these steps should. z! v$ e0 w- C9 n$ z! |: s9 Z
// take no (simulated) time. The M(foo) means "The message6 m, Y! P m; j6 m, ?# O
// called <foo>". You can send a message To a particular
' ~7 H& Z |% L4 q$ K' w2 | // object, or ForEach object in a collection.
" E" Q" p5 o: I' n ! x; m7 I( t/ E \ l
// Note we update the heatspace in two phases: first run
3 u- s1 Q# E F, E& Q1 A! ~9 O/ H // diffusion, then run "updateWorld" to actually enact the/ O+ o j/ g2 d: N' Y/ s& r
// changes the heatbugs have made. The ordering here is/ @ e" N- s$ G1 H7 [/ y1 f( I8 h
// significant! X- O @: P: N3 t9 j! y
' A3 d2 B' s4 @/ G: T/ ~: s
// Note also, that with the additional# k2 n; j0 U( o! Y
// `randomizeHeatbugUpdateOrder' Boolean flag we can
) m. |5 D) e# _7 O0 k n; W" P. i // randomize the order in which the bugs actually run" ?6 f- q+ L; [" k
// their step rule. This has the effect of removing any
* a. e7 [4 ^- ]1 I // systematic bias in the iteration throught the heatbug, `+ W k, f) L
// list from timestep to timestep/ S) o, W7 k* m. _( M# _
. v- \5 }0 O4 }7 S
// By default, all `createActionForEach' modelActions have
# l& c6 Z" Y* Y! N) B // a default order of `Sequential', which means that the% o8 x3 ~# i: A( c' e2 ^. D9 n
// order of iteration through the `heatbugList' will be7 w8 ^6 }: Q2 K* F- _2 z- d# j
// identical (assuming the list order is not changed: k3 O! P/ X% f) a! v7 a. _4 e
// indirectly by some other process).$ E, Y* ]& j: p1 q' u* J6 T3 | m' l
/ n5 M; ^2 j7 `( O
modelActions = new ActionGroupImpl (getZone ());
( y6 j5 j6 \) ~1 D9 y; A$ b8 Z2 P' P' d0 V" ]2 m
try {
) Q8 [9 N! X% _" U modelActions.createActionTo$message
" Y; z9 I& K* d; ^, z0 O$ G W4 b, _5 w (heat, new Selector (heat.getClass (), "stepRule", false));
8 x4 o% O) F! X6 F8 @ } catch (Exception e) {
3 D4 \" }1 {$ ?8 r# N! ?2 r" s System.err.println ("Exception stepRule: " + e.getMessage ());' Z+ T2 t/ u8 X+ e4 y' ~" `
}
! a& T p+ k' }7 F" S& v5 W6 m2 o. J. J
try {
- V) w3 s! C- l; i I, [" m Heatbug proto = (Heatbug) heatbugList.get (0);% f: f( V6 P- B$ R" k% B
Selector sel =
! b$ ], x+ k+ ^9 @: R& K new Selector (proto.getClass (), "heatbugStep", false);3 Z! B" `4 g g- c
actionForEach =
- C: c( e) ~" @) C4 g8 p modelActions.createFActionForEachHomogeneous$call' y2 a! _0 O( L$ Q5 [+ v
(heatbugList,. ^: S2 R0 c9 A8 o
new FCallImpl (this, proto, sel,
: O y& N; I' i, e1 u# G new FArgumentsImpl (this, sel)));2 n/ h2 D& Y0 r7 t' Z& v
} catch (Exception e) {
# A1 T5 [5 L. C9 K e.printStackTrace (System.err);
+ ^" j2 G5 H! [' y! [ }; _0 N2 \4 D5 {& b1 ]* [2 O( F- a
7 b3 [" t' ]7 f% L% e# l
syncUpdateOrder ();
: n1 h' d' K4 H, w; p/ D% n3 j* \( O7 _' Y. Y
try {
' f9 j' `: Y% o" E modelActions.createActionTo$message 5 s8 x. v h' w! V; I U' j
(heat, new Selector (heat.getClass (), "updateLattice", false));, B0 N {! Y9 D+ k) U
} catch (Exception e) {* f ^+ r* W/ w1 x$ w7 y9 J
System.err.println("Exception updateLattice: " + e.getMessage ());
/ o. I _6 x' B: A, u0 _ }7 d. Y4 G7 S4 G: N, ]
i2 c9 Q( k( v // Then we create a schedule that executes the
$ T) M' L& f7 i$ j: l* X // modelActions. modelActions is an ActionGroup, by itself it) T6 X5 d% l! ]: d6 _4 y9 D: p
// has no notion of time. In order to have it executed in7 r1 t' @; i2 X- k; k: V6 l
// time, we create a Schedule that says to use the! v3 U7 K9 r# T7 P) v( t
// modelActions ActionGroup at particular times. This' f5 ]& c. ]7 n: x7 @4 P7 z- K" @' H
// schedule has a repeat interval of 1, it will loop every O; ]4 m) x8 M- `- H: I
// time step. The action is executed at time 0 relative to
6 @* @% U6 L" { // the beginning of the loop.
, d7 k, d* |; r# E# T: }6 C U4 Z- d0 a' {. C0 B+ }% U; u$ V
// This is a simple schedule, with only one action that is
. p8 v% @, [6 ~" K // just repeated every time. See jmousetrap for more7 \) i) W6 Q9 S3 \* N- X- n
// complicated schedules.
1 P4 B1 ?1 e5 E; [, [$ Q W, V# n. K: q
modelSchedule = new ScheduleImpl (getZone (), 1);
# a0 ~$ B$ l/ P: F modelSchedule.at$createAction (0, modelActions);8 e) ?; r1 N. D/ F! O1 y
& N% }; [0 v t return this;- R% T7 V* `# H5 V& [' M7 D
} |