HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# e- _+ p$ ?7 C# }$ S
; w, T2 {9 X$ o# i0 r! j( } L
public Object buildActions () {. y) Y/ W% Q" V' Z
super.buildActions();4 c7 b7 b3 o/ i/ U- _& L" h
% S; E/ u: y+ ^' ^ Z6 a3 y
// Create the list of simulation actions. We put these in4 ]/ y; O! k2 y, E+ @1 y5 s% w
// an action group, because we want these actions to be9 u/ x6 c4 j. G8 N- }
// executed in a specific order, but these steps should/ R# y: O; i+ L; A k: V
// take no (simulated) time. The M(foo) means "The message' F' y9 {! B: n* w) [/ a
// called <foo>". You can send a message To a particular' l9 t2 \9 T& ^6 n; S
// object, or ForEach object in a collection.
, A; @7 {# e" w4 |! ~/ h
- m, f& S M3 Q+ Q // Note we update the heatspace in two phases: first run
2 t0 \4 i3 X: l3 C6 C // diffusion, then run "updateWorld" to actually enact the! m, f; a. |+ o" ^3 z
// changes the heatbugs have made. The ordering here is
' B. x, A; x# c // significant!, S5 D8 p5 I- e ?$ ^1 F
) X4 M) c/ Q# R1 H6 U
// Note also, that with the additional
- n8 T; C2 |. o1 z0 A! z, w9 g( u // `randomizeHeatbugUpdateOrder' Boolean flag we can1 @ B- g) f1 d% Q5 ~1 O/ C& K( h
// randomize the order in which the bugs actually run
7 Y8 P3 S f) n U8 u8 \ // their step rule. This has the effect of removing any' E2 l: }2 C0 C B+ f5 i0 P
// systematic bias in the iteration throught the heatbug
# n! ~+ a+ c/ H ]' ] // list from timestep to timestep0 |' b6 {1 E6 v- a
0 d9 F6 W6 q7 J- l' X2 ]: U9 m // By default, all `createActionForEach' modelActions have* d0 I1 o$ L7 J. }/ s8 J
// a default order of `Sequential', which means that the& K! Q" B' o. n8 ^: K
// order of iteration through the `heatbugList' will be0 V; M; p" `% m# l8 j( b4 ~# ?
// identical (assuming the list order is not changed3 o+ `2 _; _; j9 l: [1 t: E
// indirectly by some other process).4 A) z+ e" w7 O! \( ?
5 V6 p% g6 m0 d
modelActions = new ActionGroupImpl (getZone ());
# m3 t: H2 f# f( F% l) V# j
8 A1 k7 p" A, X try {; |: b/ K5 M9 E( a% K
modelActions.createActionTo$message
) @3 Y* T7 P. a. Y4 A3 J; w- ? (heat, new Selector (heat.getClass (), "stepRule", false));
0 k+ S* C) h: b8 H+ K3 q+ }6 k" ^6 D } catch (Exception e) {
" M' m1 u0 h6 F. C) L9 X System.err.println ("Exception stepRule: " + e.getMessage ());/ r: a. {8 m$ e4 R" e6 k
}2 v" @" R0 G: P' C2 s
# x$ `+ g- \1 a3 o: y try {
& [: y O& a% Z7 i5 T Heatbug proto = (Heatbug) heatbugList.get (0);
9 w3 d+ p8 C% Z3 a; s6 J, } Selector sel =
' [# u3 ?6 \- I3 S! } new Selector (proto.getClass (), "heatbugStep", false);
6 {: N) i0 A# g* _# m actionForEach =
" w7 _" U3 y9 q8 J! x" ?0 s6 u( _ modelActions.createFActionForEachHomogeneous$call
% Y7 C$ }+ F, F8 F: { (heatbugList,/ t q/ v t; A; V$ o
new FCallImpl (this, proto, sel,
, j& r7 {$ _3 Z# ~$ b' u4 D. L new FArgumentsImpl (this, sel)));) Z( g- F$ f/ ?' w1 Q# t- n" o
} catch (Exception e) {
8 H2 F" E" b i- t e.printStackTrace (System.err);
2 ] o( B% F% y; s A0 q* d. G: E }* y( W, h5 n% ~: L/ D0 d
! X8 w% B! b6 A. Q$ {! I1 t- ~3 P syncUpdateOrder ();6 x' ~, J$ x5 M- r o# |% c9 U
5 Y2 e6 G0 J. }; d) ]. E; F* N+ x
try {
& F2 S Z/ u! E2 D1 g" [" f modelActions.createActionTo$message
! B' M, |* F, P (heat, new Selector (heat.getClass (), "updateLattice", false));
7 T' } S+ x5 N9 M& ? } catch (Exception e) {; s" c2 [: S) L3 e, k8 N
System.err.println("Exception updateLattice: " + e.getMessage ());/ Q3 D* h% A8 L6 _: d) E. H- h+ ~; f; j
}
, \; S: c2 ~$ Y" D5 }
; }1 K- V3 J9 X- x // Then we create a schedule that executes the
+ X) F0 l- `: b: Q( x: V8 I // modelActions. modelActions is an ActionGroup, by itself it
+ H6 H/ y4 k5 C$ W3 C# I, C // has no notion of time. In order to have it executed in$ \2 L6 {' W7 z9 O
// time, we create a Schedule that says to use the. P, S7 n2 H! y# A! U$ I
// modelActions ActionGroup at particular times. This" M& v7 |6 e$ [
// schedule has a repeat interval of 1, it will loop every
2 @0 S( E8 ?% F- ^+ j+ n X1 y* Q // time step. The action is executed at time 0 relative to- c1 U. S/ b$ Z; I, {7 d# _
// the beginning of the loop.3 I' p3 ]: t4 s: I' [8 R
, z: _. {8 |- |7 S9 @) H) L
// This is a simple schedule, with only one action that is
- S8 E- P: G+ h8 e: @3 o // just repeated every time. See jmousetrap for more6 q% S5 M* i! G% u2 K; e* V
// complicated schedules.
. x4 q& @: R- x9 B ' W& p( K6 v) q( k+ i* N1 O
modelSchedule = new ScheduleImpl (getZone (), 1);1 c9 Z. b, \! h! l
modelSchedule.at$createAction (0, modelActions);
: i' O: a, h' Z6 o' p* V4 H2 I5 L0 {
, I# V2 B8 n: m& A1 W, U return this;1 t) d& y. ?( ~$ r, G7 j" b
} |