HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 G. t0 q; Z3 w6 P+ H p% ]( g% X) e3 b' S4 ?+ o
public Object buildActions () {; w, A+ j' H4 i1 Z0 ]4 |% g( Q
super.buildActions();3 D+ ^. [7 T; q: T* U4 P
( F& Y1 _7 |3 ]: s' t% _: Z7 ^$ j // Create the list of simulation actions. We put these in% V* y( ]3 m: V D
// an action group, because we want these actions to be
- B- z# T$ p, k! j7 H) ~) N // executed in a specific order, but these steps should; s2 g+ ~& j# X
// take no (simulated) time. The M(foo) means "The message; {; X4 A. E- u+ m" `: n F) u
// called <foo>". You can send a message To a particular
, _( ~6 K; y1 X% S5 n // object, or ForEach object in a collection.# B+ r$ e4 _* f" w
# K ~! V) [: |0 F6 O
// Note we update the heatspace in two phases: first run
) o5 O. J% p- s0 C: w4 p$ Q2 ~9 e // diffusion, then run "updateWorld" to actually enact the
; F; b( O+ A0 V9 X: K8 g // changes the heatbugs have made. The ordering here is
* k' Q# H6 ~3 q/ \) w& Q // significant!
7 p2 d( O% P* v( d; n& A6 X5 Z$ h
8 K1 _; o6 V; ~! x0 {8 P. A // Note also, that with the additional) N0 m' b+ G' q- |2 |0 P
// `randomizeHeatbugUpdateOrder' Boolean flag we can
9 p! H9 N# ^* e // randomize the order in which the bugs actually run
' e2 D2 f$ p) Q# B5 L5 ]1 x0 \ // their step rule. This has the effect of removing any
# h9 ^2 a3 {: s // systematic bias in the iteration throught the heatbug
2 Y5 y; H9 v0 q9 \! Z2 Z% g8 Q/ r // list from timestep to timestep7 |! z- E4 x, f( I0 i- M4 }! F
) G2 s/ R* G/ ^. m1 z2 ~# B7 }" o
// By default, all `createActionForEach' modelActions have8 p; O7 _- Y8 `. _1 Q
// a default order of `Sequential', which means that the* |/ w4 o: V. O0 ]/ j- f6 ^4 L; P
// order of iteration through the `heatbugList' will be4 Y* c# |1 S! i# ^( b; d& f6 d
// identical (assuming the list order is not changed8 V: m1 D7 ?9 n6 U; M4 ]
// indirectly by some other process).: t# R; Q% g+ [
9 k2 |4 I9 t) c H: {
modelActions = new ActionGroupImpl (getZone ());
+ L2 d" h3 p1 m2 u3 {% r' N
j7 z3 l! @0 {7 Z& m try {
; H% e( ?( d0 B! R* l R ] modelActions.createActionTo$message
9 U5 ~! l( o) v. D( L (heat, new Selector (heat.getClass (), "stepRule", false));
! G! {) _( l$ G$ N/ I. X+ C9 X } catch (Exception e) {$ i3 ` t% b: O& e, P
System.err.println ("Exception stepRule: " + e.getMessage ());
2 B* Y1 l5 v# R8 p& Q }1 t- C. w7 F, R" b9 n' X
; B$ }5 s j9 G2 C, V1 V2 f
try {3 i& W( G4 U! T: D& [. o
Heatbug proto = (Heatbug) heatbugList.get (0);
8 G' f% S6 {/ D$ @6 p Selector sel =
% x: I! A, O& f' v7 n+ K new Selector (proto.getClass (), "heatbugStep", false);
8 J3 Q" m% P3 k K, V/ | actionForEach =
/ R/ D, m8 a. G& a R8 N- S modelActions.createFActionForEachHomogeneous$call
0 `% r) H* O* O+ f- G- b6 a (heatbugList,
) h( i4 x* B1 _ S1 M( M$ ] new FCallImpl (this, proto, sel,. a+ e7 L2 `2 y7 G, _8 e
new FArgumentsImpl (this, sel)));
# Q7 f$ v. a: ?/ n } catch (Exception e) {
0 t7 M; @" y. L8 a# h2 b e.printStackTrace (System.err);
' m& C$ o; y" I+ F7 @; w } j8 I6 }$ l- g/ e$ p1 @
. m7 T8 u. l3 d" f% O- M3 H a7 v
syncUpdateOrder ();
' [+ h. P v, h4 s& L# ?9 M, X, y( X: C ?6 W5 d* d
try {6 P: _; M4 }" @$ p# U1 J* @
modelActions.createActionTo$message
: p$ w2 I' S4 K+ z7 p* i9 o% N$ D( t d (heat, new Selector (heat.getClass (), "updateLattice", false));
! r9 f' c4 X; J% P8 T" e' q+ A$ i } catch (Exception e) {9 `' B+ ^) Q" N: Y. i4 ]+ K
System.err.println("Exception updateLattice: " + e.getMessage ());7 O( r6 Q( X y% K5 D3 m
}9 T# d( ^* p0 p% x
' [& U% {5 `, b+ a9 L8 s1 S" V1 n Z // Then we create a schedule that executes the; W* M, O! Z. t; m8 D, J3 a( G
// modelActions. modelActions is an ActionGroup, by itself it
6 h) f) ~( S$ D; d: `5 G) M8 _ Z // has no notion of time. In order to have it executed in
" X9 x" M; A& y( S! f' g* U // time, we create a Schedule that says to use the
$ D: u: T) O2 B; v* ^5 O // modelActions ActionGroup at particular times. This
: _- h4 u6 L% a% w& x // schedule has a repeat interval of 1, it will loop every3 y, s8 M2 F2 m" ?- p6 I* K
// time step. The action is executed at time 0 relative to. q8 E2 ~- p! R5 b+ W; l
// the beginning of the loop.; V k) f" x/ V( W ~
6 s! E) L: @3 Q! q' b5 i* F/ Y // This is a simple schedule, with only one action that is
0 @* K- C' O8 m# i // just repeated every time. See jmousetrap for more
. T) `# T$ _# c4 A1 `, k4 K' E // complicated schedules.
6 L5 V9 @: N& r4 K
) p6 _7 z" T& H$ O0 Q; ^: G; I' O modelSchedule = new ScheduleImpl (getZone (), 1);
( A+ n V6 y; ^9 S, V) ?! ?+ J modelSchedule.at$createAction (0, modelActions);
' m7 ^; p+ m l" V/ j
' Y8 b0 d6 S! ~5 R N2 f return this;
' {5 r1 a. i2 b- Q- g } |