HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 S! E" [9 V Q; A' e+ U9 N* W( C1 y2 y, ~$ Z) i, r* R
public Object buildActions () {, `8 N2 B* x! v- _( q9 L
super.buildActions();. G! Y, K) t1 ~" F/ i" q* c) j
+ R3 }5 o6 |2 l. l' {4 p6 w9 { // Create the list of simulation actions. We put these in
& L" b2 p% y0 @) j# V! g: c // an action group, because we want these actions to be( D9 `- g: T- U7 y6 Q, g- N
// executed in a specific order, but these steps should
7 A- i2 J" B o9 T, @2 |: O! {5 F // take no (simulated) time. The M(foo) means "The message
: S9 X/ x0 j7 C% Q9 l/ ^% R // called <foo>". You can send a message To a particular
2 [# o4 [$ L: P // object, or ForEach object in a collection.( y7 f# Z% K x' ?& x
" Q( l7 b% Q6 K9 t% I& _ l // Note we update the heatspace in two phases: first run
; c6 ?2 y# a& ^5 ~% j // diffusion, then run "updateWorld" to actually enact the
]* ]+ i/ D7 R! l" J' N // changes the heatbugs have made. The ordering here is
. y0 s) m& V) L // significant!# S# O4 @3 Y/ p E
v# ]: @5 I; c7 M7 r/ m3 ?
// Note also, that with the additional2 A8 C% }, Y, J
// `randomizeHeatbugUpdateOrder' Boolean flag we can8 S5 n/ M Y ^* n M
// randomize the order in which the bugs actually run
- a: y" W9 X( H C, h3 c; S+ k8 T // their step rule. This has the effect of removing any
1 H) z! y! j+ w6 F, ^2 | // systematic bias in the iteration throught the heatbug) y- b# K( r5 I& r
// list from timestep to timestep' G* ]& n7 e) G; i/ y8 C, t ]) z1 @
" |* U4 I# w& e
// By default, all `createActionForEach' modelActions have
3 |/ i8 D0 b, ?1 Y // a default order of `Sequential', which means that the
1 R, I: N3 `4 r) G4 A // order of iteration through the `heatbugList' will be
- `: l- A. ~$ o- J8 _ // identical (assuming the list order is not changed' H3 P. Q9 M& \4 `) T
// indirectly by some other process).
& c3 @: X4 v6 H7 d, Q& k
2 p( W* ?0 b9 a+ C# B* h) J modelActions = new ActionGroupImpl (getZone ());
( q( @) l& W9 J/ ]; A4 k
" b/ h- I$ i0 ~+ i4 y try {
; p) s& r2 F& R. S modelActions.createActionTo$message6 D6 K0 h. b( o+ V3 Z, i3 [6 b( ^
(heat, new Selector (heat.getClass (), "stepRule", false));# N+ @: q+ s6 a X* C" @0 f5 D8 ?
} catch (Exception e) {5 O; k+ k. m; K: J/ `5 P$ Q
System.err.println ("Exception stepRule: " + e.getMessage ()); h2 [- u2 x4 G' s5 k" V
}; L$ i h0 Z2 Z0 h i0 V' Q' O6 u
+ a# G7 l2 B& f9 F try {
7 ?) a2 A# k% }$ _/ F Heatbug proto = (Heatbug) heatbugList.get (0);; H' Y6 u3 v0 j0 R: b
Selector sel =
1 ~1 x U, H) E" u- A new Selector (proto.getClass (), "heatbugStep", false);" G$ O$ q# b- }# v/ l
actionForEach =+ H: e# s$ L6 X7 F
modelActions.createFActionForEachHomogeneous$call( o4 Z" ~2 M* {. y. W
(heatbugList,
) v0 S/ x0 `* ]) Y2 {( S+ C new FCallImpl (this, proto, sel,
/ F1 [4 N; s% x% I: q5 f new FArgumentsImpl (this, sel)));
8 M8 p% E+ G( f7 U! @9 ]; j* n [ } catch (Exception e) {' w: c9 c4 ~/ D7 a
e.printStackTrace (System.err);8 G9 t- L1 \$ x2 \9 }$ o1 R5 D$ C- a6 f
}2 U: | s! w4 _3 `: @% O
. [: j9 b, f6 u; l S9 T. ?: y
syncUpdateOrder ();
' N- i7 _; j9 O$ p0 Y
- H! e8 c5 G- a/ R4 ` W try {3 }% x. z1 q% l, y+ s
modelActions.createActionTo$message
t/ B! p- W5 [ (heat, new Selector (heat.getClass (), "updateLattice", false));
V% m& X3 S- {# H } catch (Exception e) {
% m- \2 x* W a6 L' R. W5 v( G System.err.println("Exception updateLattice: " + e.getMessage ());: T- u% Q9 L3 t$ S5 {6 A# R. H
}
$ @0 w3 E7 L( g d 5 T2 Z6 u8 r9 M4 j
// Then we create a schedule that executes the- u7 l. t6 `3 S( u% e7 r
// modelActions. modelActions is an ActionGroup, by itself it
8 }3 m/ m% |' D // has no notion of time. In order to have it executed in' } [/ A1 f: u) z& ^6 m: g1 a7 s
// time, we create a Schedule that says to use the
9 K+ p- Y: O6 ~. ?6 U7 F9 K // modelActions ActionGroup at particular times. This
" u* m" f9 s/ T& L2 I4 [4 I // schedule has a repeat interval of 1, it will loop every( X) o. [0 g e7 E; m, u$ l) z; d
// time step. The action is executed at time 0 relative to
* `; @# S. `& o' z+ b& u) a' { // the beginning of the loop.
2 [: o$ Z$ l+ i v/ @% ?6 X& c9 }7 v
// This is a simple schedule, with only one action that is+ w6 K: f! z* j
// just repeated every time. See jmousetrap for more
: _; D$ \5 b' Q# U // complicated schedules.
Q9 x0 d2 i4 [8 O" p 8 Q5 k# G. j+ W& b
modelSchedule = new ScheduleImpl (getZone (), 1);8 V' j# C5 I, }3 P c
modelSchedule.at$createAction (0, modelActions);
0 F& M0 L+ j4 \* M) l 0 N! S/ p5 f7 R3 d
return this;
. Z/ E/ r) Z9 p! F- @+ |* t: _: K) z% k% v } |