HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 j; m6 `$ N7 p; G3 u1 D& {& k! S f3 A0 @2 G' s/ R
public Object buildActions () {
; t- b# w1 T0 h8 s7 `$ t8 Y super.buildActions();. w; i% E& L4 U. h$ A. W- Q
3 p. @# [6 c9 v6 v/ `
// Create the list of simulation actions. We put these in9 Z7 Q# ?; r8 F4 R: y
// an action group, because we want these actions to be
( J0 C- _$ l. A' f: j // executed in a specific order, but these steps should# D6 s- u# _! X
// take no (simulated) time. The M(foo) means "The message
W6 O. X9 `/ ?+ ^8 l8 }5 X7 n // called <foo>". You can send a message To a particular
( G% T' ?6 C3 s1 c7 Y" Y$ C2 G' L // object, or ForEach object in a collection.
8 [4 Q9 q U5 W # F( ~, m5 W' v7 @: w( n m* T
// Note we update the heatspace in two phases: first run
; @+ a/ Q4 E' P4 s4 u" v$ I; Q // diffusion, then run "updateWorld" to actually enact the3 v$ \$ L& Q/ B+ N
// changes the heatbugs have made. The ordering here is# h/ R% ?3 R, h/ }6 m5 l. j+ K
// significant!
7 f6 {6 _. i9 M! R 7 U/ }6 I' T! K: N
// Note also, that with the additional: U# m/ A$ J1 c
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 I$ C# V# }- I+ X0 f9 x& s% i
// randomize the order in which the bugs actually run
/ j8 u* G7 M* I/ t // their step rule. This has the effect of removing any
- r/ a+ i* U, b: G // systematic bias in the iteration throught the heatbug& ?9 L" ^$ ~4 b. }' t* K6 m
// list from timestep to timestep3 }- R' D9 I9 L4 a
) n# ]7 r3 e W! L. a
// By default, all `createActionForEach' modelActions have. X! R2 V6 ^( T/ g1 ]
// a default order of `Sequential', which means that the6 X O! F, `& f) ]; n: A
// order of iteration through the `heatbugList' will be
8 \' M9 f; u! N5 v // identical (assuming the list order is not changed2 a; G R+ T% n) F% n* E
// indirectly by some other process).. e9 B0 d. _! n0 J0 I
8 [7 S) T4 |1 { modelActions = new ActionGroupImpl (getZone ());
) K6 l& W- B) [# d' u5 _) O/ t+ ~3 h9 x/ e
try {
: s3 D F* d- P, g0 a0 O, _& d* Q, j modelActions.createActionTo$message
9 }& [/ G. V2 T (heat, new Selector (heat.getClass (), "stepRule", false));3 h1 ]8 S5 k6 z
} catch (Exception e) { G* b! X+ ^# `
System.err.println ("Exception stepRule: " + e.getMessage ());
8 ?/ c# b5 x$ q% o* G) _ }
2 S" y4 Z$ U0 ^( d! X* f( W. M% @) e+ |* L& R: s( ^
try {. F( V* g& e$ k g! Q5 N
Heatbug proto = (Heatbug) heatbugList.get (0);" D- g, G5 U. i$ s9 j4 a6 j! I' ?/ x1 L
Selector sel = 7 b2 _$ n4 W1 j3 a
new Selector (proto.getClass (), "heatbugStep", false);
: p1 q0 I+ w$ J$ g5 k actionForEach =
! i. ^7 a7 U3 C- e modelActions.createFActionForEachHomogeneous$call' I- |0 |1 ]* ~; m
(heatbugList,4 W! r+ ` `3 c
new FCallImpl (this, proto, sel,- M1 M& w1 j# c. ?$ P) G" G! Y
new FArgumentsImpl (this, sel)));
4 i4 M9 o+ t! \3 q } catch (Exception e) {
3 C. z0 I0 W4 B1 J# V e.printStackTrace (System.err);
9 L/ p; g2 j2 I6 P e; \* R }
# p# i) q1 P! I* A6 b( G+ x * W1 @ t1 V7 |: w" q' g3 K
syncUpdateOrder ();
2 m6 T1 x" ~! }( z4 l5 O/ |* L) }3 h+ C- n3 \& [5 A
try {( J* T2 e4 t( H( S: x! p( ]
modelActions.createActionTo$message
( w5 j3 R* a0 {8 T! }9 J1 @% {$ m: y (heat, new Selector (heat.getClass (), "updateLattice", false));
5 r0 q9 N$ J) {1 ? } catch (Exception e) {
& L2 I; T. n# `: W' F System.err.println("Exception updateLattice: " + e.getMessage ());
8 U$ R; d2 d7 B& u1 W }
4 w, Y- ]) P8 b# a9 S* r
; z' N J- O3 a# X- a5 m // Then we create a schedule that executes the7 J! U# a9 K; q$ J4 r
// modelActions. modelActions is an ActionGroup, by itself it
7 M9 L8 D/ J Z9 W2 f // has no notion of time. In order to have it executed in4 ]/ G- q+ O( R; p% O1 W7 [/ \1 Y, a
// time, we create a Schedule that says to use the7 k$ a, z$ K; ~3 p+ t: u
// modelActions ActionGroup at particular times. This2 i9 s0 R' Z; ^6 s0 J( i+ F
// schedule has a repeat interval of 1, it will loop every$ k9 x$ P* `2 U( O$ w; A
// time step. The action is executed at time 0 relative to
' O- F5 u& A# G! R6 Y# `& V // the beginning of the loop.; P6 ?, E a+ f1 G) a
& k0 k" k% H; p& g // This is a simple schedule, with only one action that is- ^ A7 T+ Q1 V* [0 C
// just repeated every time. See jmousetrap for more
4 R4 q( z( Y0 P% Y3 ^ // complicated schedules.
0 e& R1 r# J$ G
* T* [! X- u: ?& D1 E7 F modelSchedule = new ScheduleImpl (getZone (), 1);+ c, W7 M9 q( `! o2 }4 }/ y7 r
modelSchedule.at$createAction (0, modelActions); q, K- i' d& C/ n- [" [+ \9 u$ ]& F+ L* M
3 d# O2 S" [$ B$ a) J return this;" Q+ {5 s& j8 V) ?9 i1 e. Q8 z2 h" l0 N
} |