HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ n4 H _: @2 h2 X+ o/ j3 n3 g" }8 q! \% t
public Object buildActions () {
- I9 C4 R& }" H" m+ g4 k# X super.buildActions();0 `( P5 r8 o2 z5 L; _% B5 Q
U, }: I6 b) o' I5 |. g3 ?: R // Create the list of simulation actions. We put these in# j% \, r; m! a0 E# Y! [
// an action group, because we want these actions to be+ L: p( E6 q4 v* Y3 G; V7 f# p
// executed in a specific order, but these steps should
1 i/ s3 b1 l0 Z // take no (simulated) time. The M(foo) means "The message1 f7 N; \* z5 L3 z' ^
// called <foo>". You can send a message To a particular( j Z7 \. J5 l1 M9 W7 o8 W
// object, or ForEach object in a collection.
. E8 P( H1 K9 x' y4 ~' K
( r: b- Q2 Q |- x4 F // Note we update the heatspace in two phases: first run: G: q8 g$ R3 p# Q* r6 x3 X
// diffusion, then run "updateWorld" to actually enact the
: `! R" q& {: f2 R, M // changes the heatbugs have made. The ordering here is
( G4 n- o2 d4 q // significant!
7 B: I0 v n$ N. m
) {3 j9 F+ j2 ?" X- u0 Y; o3 `6 _ // Note also, that with the additional# R$ \# I" [3 S, \! v3 E8 K8 I) v7 ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can; E# D6 K& Q0 Q* s c2 N, C5 \
// randomize the order in which the bugs actually run. g1 |& h0 r' K: s- Q' n! c
// their step rule. This has the effect of removing any/ l0 {( l8 q( y4 D7 J
// systematic bias in the iteration throught the heatbug
# K4 k) U( H! J/ c: ]2 ` // list from timestep to timestep- N' |# c; g3 I5 `) a* ~5 Y- Z
" @! x H/ X, i: [; z) {
// By default, all `createActionForEach' modelActions have
$ Y% g; A0 M8 Z1 J# p0 m3 S9 [ // a default order of `Sequential', which means that the" V9 l/ Z8 K+ n! d
// order of iteration through the `heatbugList' will be3 n/ f3 t H8 |. ~- {3 K
// identical (assuming the list order is not changed% `% v6 {6 t/ t P8 [. i/ D: h3 t
// indirectly by some other process).
( M. d, U2 W J$ a2 R
. E2 v( o) b( M3 B# ` modelActions = new ActionGroupImpl (getZone ());0 b) x; ~7 @! | r, ^
( e8 l2 r D4 ?. z7 w% u6 O0 {
try {
7 T. K0 s) L0 `" o( }$ w modelActions.createActionTo$message
, \1 S G% I' V! ~4 E7 b3 w (heat, new Selector (heat.getClass (), "stepRule", false));7 O( j+ v, i) a, J* x* h: T5 F
} catch (Exception e) {+ \, J9 \8 s; X. J/ G
System.err.println ("Exception stepRule: " + e.getMessage ());; X- ?1 K: s$ e8 u1 {6 ~2 _
}& \) p( h5 Q! y' q+ b V) x
" `3 W; I' l O9 W
try {
- y9 y6 y6 R) L7 l5 w5 I Heatbug proto = (Heatbug) heatbugList.get (0);# l3 g5 ?9 l' e. ]' d
Selector sel =
! Z7 q- {" O | new Selector (proto.getClass (), "heatbugStep", false);
2 T& f; r7 v6 L2 x1 Y, a- E actionForEach =& {9 O0 H- `( r n" q
modelActions.createFActionForEachHomogeneous$call
/ z% M) O2 s1 T$ Z (heatbugList,
; r3 d0 ?4 N3 v2 ~' r new FCallImpl (this, proto, sel,
" E9 G. f( t2 T z# ^ new FArgumentsImpl (this, sel)));3 M: D6 K- |% W) J4 t
} catch (Exception e) {$ D. V' Y# }, O9 z4 n) i, M
e.printStackTrace (System.err);
$ e5 r$ |4 L% t: ? }& J+ g0 h6 j" P" k/ c: T' x
* \8 ]+ A+ N6 K7 N( Z# z% J7 W( A syncUpdateOrder ();
: N2 E+ w- S. {% r% i7 P" ?8 ?# R3 e4 q8 ^6 O
try {
3 M9 M# H% C) P& z- j# s/ ? modelActions.createActionTo$message ' K/ X" f# ?6 B, B
(heat, new Selector (heat.getClass (), "updateLattice", false));/ Y9 U' n! a }6 \
} catch (Exception e) { _( m: _5 z+ z0 U, X/ H: L' ~: V. Z
System.err.println("Exception updateLattice: " + e.getMessage ());/ h1 ^9 j; X) c$ ?
}
4 {1 u* J( g9 r% n. U5 d
7 h$ K& u7 H! l g& h // Then we create a schedule that executes the% M3 A2 f1 K+ @) ?8 F
// modelActions. modelActions is an ActionGroup, by itself it
% S* k, P7 z" M* D+ Y0 g // has no notion of time. In order to have it executed in
5 i$ t2 a. i' |1 |) r- @ // time, we create a Schedule that says to use the
) O L3 P; s. j5 K- O // modelActions ActionGroup at particular times. This h5 ~5 I0 d H- Y' d9 T
// schedule has a repeat interval of 1, it will loop every
/ J; I. b$ T% B+ [( v! M // time step. The action is executed at time 0 relative to" A5 C/ w5 `; ~& F4 N9 x; t0 h
// the beginning of the loop.0 a k6 {- Z9 c5 C2 f) v
, C9 {7 U% H# d- o0 f // This is a simple schedule, with only one action that is
# ^0 \; l: q# ?: ?9 P6 a H. A: H, w // just repeated every time. See jmousetrap for more% e g+ q1 C4 |3 k* b* ]
// complicated schedules.; j7 i+ ^; X6 X3 H5 |+ r- g( f! h
, O1 {& n/ S e7 { modelSchedule = new ScheduleImpl (getZone (), 1);
0 |2 m* A2 [" @; D' W$ q; F modelSchedule.at$createAction (0, modelActions);
3 h0 e- o4 a! D9 ?! x4 c
* E8 d: I5 ]2 I/ G1 I9 F* g return this;& P; w5 h ~2 R9 r# c& t# G% U3 n
} |