HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# l' }( f9 s1 \! ~/ j
H8 K7 I$ }- v% Z" s' ^ public Object buildActions () {
3 E) s: j. ?9 W: l4 i8 c8 S super.buildActions();. D) n* E) h+ K; U3 R( _. S _
7 I7 [ S, y# m // Create the list of simulation actions. We put these in, ^% P* S6 N& P3 s1 O- g! v' J9 C
// an action group, because we want these actions to be' f- G1 Q6 f- F0 P
// executed in a specific order, but these steps should3 b0 N0 B! y6 t' Q* @( M' E
// take no (simulated) time. The M(foo) means "The message
5 I% I# m; F2 P- p$ z8 x! b- @ // called <foo>". You can send a message To a particular1 X5 F; j$ A$ _5 ?* ^3 ~# R# ]
// object, or ForEach object in a collection." F& B* t5 }1 L1 B
0 C( I+ V; |" s& e* {' G
// Note we update the heatspace in two phases: first run( M4 v/ u1 L* ^( p& D
// diffusion, then run "updateWorld" to actually enact the) n# F& ]' U+ f. L; h
// changes the heatbugs have made. The ordering here is6 Y( H8 ]4 `& x3 G" R+ z. V
// significant!- a4 ] \- L5 o& V, n& \
$ \1 P7 X1 u7 C% U/ y- D Z // Note also, that with the additional, X7 q9 k7 p: Y( r: `
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- d0 n3 u; {3 C, n // randomize the order in which the bugs actually run8 m. U( z4 l( ^* A4 j
// their step rule. This has the effect of removing any
$ e9 E# G" q; o1 r* C- i# G // systematic bias in the iteration throught the heatbug
0 u" L9 Z( h6 C# n+ o, b5 R // list from timestep to timestep
7 `4 A5 P: z8 ~8 b& y' G3 k+ |0 m
P) O6 x9 X; s4 B) y, S6 g // By default, all `createActionForEach' modelActions have
x7 y4 x' s% q s // a default order of `Sequential', which means that the
8 j. t r: ]0 k% P0 f0 L& U6 G // order of iteration through the `heatbugList' will be- s+ [: X* ] t8 L
// identical (assuming the list order is not changed
! B7 T# K0 t2 {0 U; q& r // indirectly by some other process).' Z/ U; b1 W0 @4 d* I
; M( }0 z4 u u! ]# V: ]6 @* t/ ~8 \ modelActions = new ActionGroupImpl (getZone ());+ U! X$ R- ~& M! Z7 \0 K
& t0 ~' T T( g% O/ {, ] try {
- s( S; M. h1 p6 }6 y3 R modelActions.createActionTo$message' A2 F3 E9 X& d, X+ `
(heat, new Selector (heat.getClass (), "stepRule", false));; q8 o( |" v. n8 N6 {! M" |* {4 T
} catch (Exception e) {; Y3 |, @; [2 A" ~; I
System.err.println ("Exception stepRule: " + e.getMessage ());0 b' F* [4 J0 z* i' ?
}
" M. W9 D7 w; q+ ^+ }/ g2 a: B5 ]/ x. h/ Q" ~- f3 F
try {
$ m: X$ s" v# l1 L6 c Heatbug proto = (Heatbug) heatbugList.get (0);! D0 A/ J2 p# t; B; I' G5 H
Selector sel =
6 V" f3 |5 Y4 N1 H6 o M. J. e new Selector (proto.getClass (), "heatbugStep", false);4 v% O* q) _' h+ ~. t
actionForEach =/ E+ ?0 Z. B, H
modelActions.createFActionForEachHomogeneous$call
' h. d E Y$ s6 W5 r6 x) ` (heatbugList,* B/ ~8 W2 s! q
new FCallImpl (this, proto, sel,* s' C" R2 H0 M& C' U( V
new FArgumentsImpl (this, sel)));4 x4 I. f% M$ _. `% d7 V
} catch (Exception e) {
5 Z0 @& U2 I2 M+ K3 A e.printStackTrace (System.err);
+ h" g% @$ I( I( ^ }
4 u$ x" g/ f) c& g! N
, J& M$ f2 V' p syncUpdateOrder ();0 u6 }- m! w" ]# w6 W, n/ Q6 l# B
/ U% ^3 y6 s& U' ~
try {
; F; |+ \0 h' @! z modelActions.createActionTo$message
$ \* P3 ]8 ^: m, E) t+ @. g (heat, new Selector (heat.getClass (), "updateLattice", false));: s0 b6 w! S. }' B6 `
} catch (Exception e) {1 c$ ? y7 T, T
System.err.println("Exception updateLattice: " + e.getMessage ());6 s- |0 N, ]; Q
}
$ h" m, W. \& v/ K M ' }' Z( [$ k' T9 o. h, V
// Then we create a schedule that executes the
8 c, I1 s# f. u // modelActions. modelActions is an ActionGroup, by itself it
4 V( g+ l7 ~& q6 W" m* x# q // has no notion of time. In order to have it executed in
& t# d o+ ]' T4 V @2 Q5 l // time, we create a Schedule that says to use the
" G' k( N) A% }' w9 p' C // modelActions ActionGroup at particular times. This/ n( o0 j) n5 n ]- {7 M
// schedule has a repeat interval of 1, it will loop every" p3 `7 H& P; d6 E$ g* I8 R
// time step. The action is executed at time 0 relative to$ W7 E: t; z Q1 G5 x' ?9 c1 l% E
// the beginning of the loop.
' ]4 V" X6 T+ x1 B) R. k, e/ ~2 o% P) r# o) r7 d
// This is a simple schedule, with only one action that is
& |- ^" p' n& g // just repeated every time. See jmousetrap for more
' e8 j: I& S! D2 p% B2 ]* k // complicated schedules. d: ?; ^8 T6 F1 [& N* q" V
, N, i; I- T! s, f
modelSchedule = new ScheduleImpl (getZone (), 1);
7 D0 h4 m% e8 r! Z3 f! M modelSchedule.at$createAction (0, modelActions);
2 o9 [; Y& P/ G& d: k7 R # m5 H& r8 T4 m) T$ P I
return this;1 L' c/ b+ f+ Y9 b' f: R
} |