HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 q* F- g1 V$ } I% L
5 J7 E V- L7 _% U/ K
public Object buildActions () {& W! {: J# ~ p t8 N
super.buildActions();
# L* N2 W3 H( p( h: d4 f, S# I
+ l3 G$ o3 d `$ j // Create the list of simulation actions. We put these in# _6 v4 e6 m' x8 j
// an action group, because we want these actions to be
3 P. p7 n+ \0 m! s // executed in a specific order, but these steps should
; I( F9 i. y! J- l( A" D // take no (simulated) time. The M(foo) means "The message
# W% P+ M5 ]6 T! ` // called <foo>". You can send a message To a particular
4 q5 H: c% d0 ]. B3 ]( W // object, or ForEach object in a collection.
8 H0 |0 R7 d9 ` 0 Y/ t% v: b1 n) y( {# Z
// Note we update the heatspace in two phases: first run
0 A3 ]% d# b8 G' R5 w0 o0 m8 O: P- } // diffusion, then run "updateWorld" to actually enact the7 Q+ J p3 B1 q3 v# a5 T2 f+ [
// changes the heatbugs have made. The ordering here is
7 O3 ~( J" b6 p. {8 J // significant!& ^3 J$ X# O5 G$ I* m& Q3 z) ?
+ X& S1 D/ C1 g* v
// Note also, that with the additional
$ `9 w2 T* w% y7 _! o6 K // `randomizeHeatbugUpdateOrder' Boolean flag we can. {0 m0 |/ r; Q3 M) ?' e4 u8 D5 C
// randomize the order in which the bugs actually run
/ g( u' ]( _* }8 ^1 U K. \ // their step rule. This has the effect of removing any
7 S5 ^3 H: W- o7 e; y( f# ` // systematic bias in the iteration throught the heatbug9 s/ Q; [% T2 l. C2 [
// list from timestep to timestep7 L/ v, p4 T* m7 A
. X# ]5 c- i8 q/ i8 \ R // By default, all `createActionForEach' modelActions have
: [7 X( F( `1 C' a% s2 v // a default order of `Sequential', which means that the
5 ^! Z: z+ g. |" L1 k4 x // order of iteration through the `heatbugList' will be9 ]5 h4 r2 e7 G' P S
// identical (assuming the list order is not changed
1 P( |7 E( q0 X: W // indirectly by some other process). j' |9 T5 \$ s% @' g
8 V- ]9 _8 s5 u; P; P | modelActions = new ActionGroupImpl (getZone ());
f# }- v% o- G
0 j7 q+ I4 O( ^$ I try {5 x3 r2 I) w4 I7 q
modelActions.createActionTo$message; r! u+ o! j) u8 B3 Y' l, q
(heat, new Selector (heat.getClass (), "stepRule", false));% `/ L( Q/ j, z4 B: d4 F& V. o4 U8 g
} catch (Exception e) {
" d) W, v) U" y3 W System.err.println ("Exception stepRule: " + e.getMessage ());6 Y @; k4 \: B% D
}
; j! k6 j, g; K9 }! Y+ s! T+ a3 Y; Y5 X$ Z
try {. v- E) ?- x" w! j. K! x
Heatbug proto = (Heatbug) heatbugList.get (0);$ o0 r# b" |: d1 A, B. x I
Selector sel = 5 B) k+ y9 }( j# Q6 x( l, J* Q. k: |
new Selector (proto.getClass (), "heatbugStep", false);
% S+ ^; A" x# V& }- ?! ~2 I actionForEach =3 V' _5 p' \3 m0 L
modelActions.createFActionForEachHomogeneous$call) e, {% r8 L% x+ m' o }* k( \
(heatbugList,; m4 R+ K+ `3 m+ E$ y6 Z# ~' y
new FCallImpl (this, proto, sel,. W7 ~7 u5 I0 r) k5 P
new FArgumentsImpl (this, sel)));7 `& }0 ]5 O+ n: F+ k& O
} catch (Exception e) {# }3 f% e: P# ?0 m
e.printStackTrace (System.err);' ^- L8 l4 v7 u+ ~# l" ]
}
0 F8 k* ~. }8 A* k4 p. F
, |8 m2 V9 Y2 q2 J2 R; U: M3 f syncUpdateOrder ();; f5 r' \7 D( X' J; n }, s
" M& t4 e' L, J8 r try {
. I( F0 d7 j Q% |- _/ J3 }# _ modelActions.createActionTo$message * b/ o9 F6 |) }2 \
(heat, new Selector (heat.getClass (), "updateLattice", false));
( g4 P5 ~( |9 r" g# X& j } catch (Exception e) {
. O' O0 b; |" y) x) a3 c System.err.println("Exception updateLattice: " + e.getMessage ());4 ] X1 ]8 Z. }1 x
}
1 s- k# ^8 z* ~4 [
" q4 s5 P& k: T b // Then we create a schedule that executes the
, j$ r1 ~& q- s# V$ p3 P6 ? // modelActions. modelActions is an ActionGroup, by itself it% m$ C: d6 E0 [) ~. `& k
// has no notion of time. In order to have it executed in
j5 w) P. }' o5 { // time, we create a Schedule that says to use the4 K/ e/ U; f! G/ b$ ~5 ?
// modelActions ActionGroup at particular times. This _. Z4 q" v7 E+ ^- ]4 z K0 P
// schedule has a repeat interval of 1, it will loop every+ q4 t2 ^6 x# X$ P& e( @9 \1 V4 }& L
// time step. The action is executed at time 0 relative to1 n' t9 f7 b3 \# Q
// the beginning of the loop.
( g3 a6 h, i- I
. f3 R+ q$ `: k0 S- B // This is a simple schedule, with only one action that is- b$ u( @- \ l
// just repeated every time. See jmousetrap for more
" H% i0 f1 o3 \3 m6 @ // complicated schedules.
: I# c5 b( l$ ^8 F- K/ E: B4 W% {% E ; j% w$ A" n. G
modelSchedule = new ScheduleImpl (getZone (), 1);
( V% }, F# m ^* i modelSchedule.at$createAction (0, modelActions);! ]' S6 U+ r, d3 _
3 q. ?2 l& W9 s# \ return this;
# U+ t9 [7 J- s, `5 j5 n; T } |