HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! G( x8 C. f" U
+ }6 t' q- V% e+ e' G6 R2 t public Object buildActions () {& @2 C/ j2 f% k7 E3 Z9 S2 z
super.buildActions();7 j9 a& S$ q. V# {
& k# h! S0 h# ^7 S
// Create the list of simulation actions. We put these in. a6 Z- u/ J* i1 [/ x
// an action group, because we want these actions to be
( d$ h, X) ?6 T% w' P // executed in a specific order, but these steps should" H d3 c, G* o M6 a
// take no (simulated) time. The M(foo) means "The message
0 O! M4 o& A0 a9 L; Y // called <foo>". You can send a message To a particular0 T7 }1 i% A; X6 z5 [) d' p
// object, or ForEach object in a collection.
8 [+ }6 ^0 q( c" c* M 1 |2 |. [3 Y8 y: t: W! H' }
// Note we update the heatspace in two phases: first run6 Z/ S; B l" W2 T3 U) ]
// diffusion, then run "updateWorld" to actually enact the% K) G4 O( o* O, T! P
// changes the heatbugs have made. The ordering here is
. T+ m, ~' ?7 p4 @ ~9 N // significant!7 } a& g, L0 ]: G F7 B
9 F7 j: C0 a. Z9 i // Note also, that with the additional
/ z1 `, d) e c5 C' I6 n( ` // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 S; \- ]( c0 D+ R; |8 |; Q // randomize the order in which the bugs actually run- j* L; }6 y4 U! e; p4 g1 n% \' O7 C
// their step rule. This has the effect of removing any
2 r+ ^* q6 o5 K5 B' `& T7 ~ // systematic bias in the iteration throught the heatbug# ~" d! J- u2 q9 r+ \1 k
// list from timestep to timestep
. N( ?4 |$ \- y 9 U" r, O& I# a- f4 v
// By default, all `createActionForEach' modelActions have' W2 m9 B. B1 c' v9 {8 \, f5 h
// a default order of `Sequential', which means that the/ g$ e8 ^) Q( ]5 ~" N8 n
// order of iteration through the `heatbugList' will be
; s/ k% K8 s; Z+ O# m& p( S! q // identical (assuming the list order is not changed
) g p, l- Y, z* X% p // indirectly by some other process)." g# _, t8 A! L1 S& E' u% Q7 l
$ m1 l- Q8 r6 s C& [ modelActions = new ActionGroupImpl (getZone ());4 ^5 p0 p' [5 P' P6 {& O
! p) d% y: t y/ z
try {3 G G$ R* _( Z& P7 j3 d D
modelActions.createActionTo$message u$ r) l5 P0 L0 R7 f3 R
(heat, new Selector (heat.getClass (), "stepRule", false));
6 r( y- c/ O& ~; |- {0 M2 }: d% w } catch (Exception e) {
1 p& P2 M# {1 ?. i' z1 w( @9 O2 [ System.err.println ("Exception stepRule: " + e.getMessage ());; x+ g+ N! Q. ]
}% U9 {8 H. f3 @, u
T) S5 g0 S* @, d x try {
& h9 G$ j1 Z) b8 s; v4 c. [2 B% S! j Heatbug proto = (Heatbug) heatbugList.get (0);- G7 a7 h$ }) |) i, _/ S) X
Selector sel = 8 Q( l6 q4 {" p# k' [ e$ B. T
new Selector (proto.getClass (), "heatbugStep", false);9 ^, P2 Z4 @. L+ M) ~0 \) s3 G
actionForEach =7 A% P1 \& J3 s8 l
modelActions.createFActionForEachHomogeneous$call2 b0 K. D' ~& m a9 h1 p
(heatbugList,
6 F2 Y! t8 l8 ? new FCallImpl (this, proto, sel,( I( ?3 U; n* V5 B8 r; y) J4 B
new FArgumentsImpl (this, sel)));
2 J8 o* h1 ^8 z6 V } catch (Exception e) {
! D' N/ m$ S2 ?; [/ L% {: W, p e.printStackTrace (System.err);' P$ e% [) n7 O
}2 i& \ k; R& I" |+ b
- Z" U6 P: @1 Z! ~9 O/ \
syncUpdateOrder ();0 {3 b# r) M: t' \: i
0 {7 F c! X# D) M
try {
& q7 ~ X, q) C6 X% D2 k! b modelActions.createActionTo$message , C0 a( [' ~( P$ {
(heat, new Selector (heat.getClass (), "updateLattice", false));+ W- }8 \3 S! {0 @6 o5 E
} catch (Exception e) {
3 [( v& _1 K+ N* l7 Z System.err.println("Exception updateLattice: " + e.getMessage ());! } Y1 v7 ^0 ]2 b$ J$ K
}
) Y& b# j# F+ E2 M* H8 w9 B
: {9 f- H, x" W2 _8 \ // Then we create a schedule that executes the1 O5 y: W$ H; j& r0 }
// modelActions. modelActions is an ActionGroup, by itself it3 o: U/ N( U* `
// has no notion of time. In order to have it executed in D# U5 O- l9 l$ h/ i+ s
// time, we create a Schedule that says to use the$ o5 G; ~4 M) W5 a' U. v* m2 Z
// modelActions ActionGroup at particular times. This; T e7 K$ ]+ o) z5 k
// schedule has a repeat interval of 1, it will loop every
8 k+ C- j' m- U: x // time step. The action is executed at time 0 relative to
! T: Y; s8 w# A8 w8 g1 e# [7 q // the beginning of the loop.
( z: M; a- \# H+ Z; i
- B8 F9 Q! j G6 {" F$ P, ~ // This is a simple schedule, with only one action that is
& k \( O8 [) F" U; g) V, D; {, H // just repeated every time. See jmousetrap for more
9 \8 M, @( v2 Q5 f4 d // complicated schedules.
* n% L( Y# i5 a4 k: n! X+ u 6 s% |& Z! Y4 O9 O" V; O) z( r
modelSchedule = new ScheduleImpl (getZone (), 1);
* h% d8 ?0 J4 M: G modelSchedule.at$createAction (0, modelActions);
1 N6 w1 [; A- j" N1 t( Z8 S* N
$ X9 g3 w8 B0 E return this;
( t$ {" ^. D0 W2 v Y } |