HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( q( h! r3 j! @- U' i5 d0 p, u
( b; s" d' e, X; h) k public Object buildActions () {' ^6 D; B- X: C/ F
super.buildActions();) G" R9 c. O: M1 c8 E
$ Z4 d5 ]3 y1 ]. ], X' F
// Create the list of simulation actions. We put these in2 Q, C5 C1 I. {3 j; C6 T2 \3 c9 K- u% W
// an action group, because we want these actions to be6 G. ^& s+ h0 X- m- A: a( [
// executed in a specific order, but these steps should
7 D1 L+ y& @" {$ s/ N+ ` // take no (simulated) time. The M(foo) means "The message
) O0 p* o6 Q5 P" t4 h // called <foo>". You can send a message To a particular5 z% Q: j H, e2 y) p6 j- J# k
// object, or ForEach object in a collection.6 r9 Z& V1 M& o/ J; h' u4 c
0 z+ |4 z% c* R. c1 S // Note we update the heatspace in two phases: first run! J7 m5 f, Q5 v: A
// diffusion, then run "updateWorld" to actually enact the3 L/ \* Q0 h5 Q& D0 V/ w
// changes the heatbugs have made. The ordering here is
5 L1 o% y# w l1 b4 a7 a2 Y# z // significant!
& K) o5 B, f6 ^2 K
; u# t! c! n1 w( ^2 k // Note also, that with the additional2 p1 i1 _9 B8 ^/ S0 l
// `randomizeHeatbugUpdateOrder' Boolean flag we can
0 ~2 _( x' ?2 Q5 O // randomize the order in which the bugs actually run: w X* i4 f5 ?. e; g# ~# B% Q
// their step rule. This has the effect of removing any, L# ]* ^8 z0 k' U& x
// systematic bias in the iteration throught the heatbug6 h' v- _3 I( A0 g2 b5 O( f8 J
// list from timestep to timestep
+ c* P, d r6 m" u4 t& E, k
2 _5 {+ Z$ ]& W( D, T, ] // By default, all `createActionForEach' modelActions have
% G4 I/ s. Z9 p* J // a default order of `Sequential', which means that the# D- f: Z9 a9 G C
// order of iteration through the `heatbugList' will be
' R, u6 r8 H3 J4 Q. ?, q( l8 P // identical (assuming the list order is not changed; d4 k1 P5 c! g3 D. {* d& P
// indirectly by some other process).: T e/ n; ?: t9 b
( H2 d5 @: o8 k. l. G
modelActions = new ActionGroupImpl (getZone ());
6 h# D; [9 X$ N: H4 I
. h, d; Z* z8 b. U7 f try {. M- r! {/ x: K; c
modelActions.createActionTo$message: O0 S: v2 x: l7 x1 E
(heat, new Selector (heat.getClass (), "stepRule", false));
- f5 K! r0 | Z9 @6 l4 ?; V } catch (Exception e) {! Z* @3 u) b( G. u
System.err.println ("Exception stepRule: " + e.getMessage ());/ r4 D2 k& Z4 v' q- m. s% h
}
- l0 X% {2 Q8 ]% H4 K
; t# }) A0 ~! S* k. ^' g* L, ~ try {
" F# k) F% n! z Heatbug proto = (Heatbug) heatbugList.get (0);! s: u6 K2 u+ x8 |1 E' P4 V4 |3 M
Selector sel = 1 d' e- c5 {7 u* }+ l2 S+ y
new Selector (proto.getClass (), "heatbugStep", false);
/ d Z& I2 L6 z6 r: V: g P, g actionForEach =5 q4 P, X9 P0 F; U0 b; t* K2 n7 L
modelActions.createFActionForEachHomogeneous$call- \( [: f# x8 O v: M( M4 K% t
(heatbugList,
) n3 P6 _' W( J" f# { new FCallImpl (this, proto, sel,
6 { S- o9 ~: s- ^2 L new FArgumentsImpl (this, sel)));% K7 T' S7 f9 \. ], v9 a
} catch (Exception e) {
1 }" e/ _9 |7 m+ P e.printStackTrace (System.err);
' z; c, r- |2 C" b }
6 U+ M/ i5 N2 n+ r9 D) s+ Q 7 }1 b( i4 g; U
syncUpdateOrder ();
# \' u+ Y, U, \5 @5 @
; r2 \2 d- h/ u try {- [, J' Q4 _# _; p q/ O( C: q
modelActions.createActionTo$message
* U) m0 h% s* O' o (heat, new Selector (heat.getClass (), "updateLattice", false));: L, E% r; O# h% F9 G
} catch (Exception e) {, }" X! [5 B" ]) A5 S- u. M/ C! ?
System.err.println("Exception updateLattice: " + e.getMessage ());
, p0 Y1 R4 x, d. u; u) c }9 J+ p/ k' I( R' Z
+ v4 e0 I' W6 p q7 y: R# ? P
// Then we create a schedule that executes the
* o2 Y- e2 z5 I% ` // modelActions. modelActions is an ActionGroup, by itself it
! [) S! f7 t, O; _ // has no notion of time. In order to have it executed in" t7 H) L' h4 R8 }# Q9 I3 K
// time, we create a Schedule that says to use the9 `( ?) \( g/ }- o& w
// modelActions ActionGroup at particular times. This) n+ z! D& }" r# o$ _: V* C
// schedule has a repeat interval of 1, it will loop every
0 X( k4 J, O5 v5 L; U5 g5 @ // time step. The action is executed at time 0 relative to
. k/ g8 g j4 U% E' T9 W // the beginning of the loop.
3 a3 M, ]0 i, X$ Q; g9 n, Q; p' u: C. P1 V' y" e0 E* f2 d9 b0 X( f% T
// This is a simple schedule, with only one action that is
8 m7 @: Q4 T H0 X // just repeated every time. See jmousetrap for more
' f- E. J" S; x8 E1 }$ G$ F; q8 C2 F // complicated schedules., u. p9 a' \, c
1 W6 n4 e9 |4 t; G% e
modelSchedule = new ScheduleImpl (getZone (), 1);: S' O' k0 {6 f- K# F7 a
modelSchedule.at$createAction (0, modelActions);% {0 d: K+ Y" h% X2 g
' _# a" Q- ]# @( c return this;7 H# K; y9 p1 E
} |