HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ o+ a& |% G! P- {9 w! q
3 f1 u7 E7 c) Z
public Object buildActions () {4 r$ i: z( ?$ h3 t: I6 P
super.buildActions();
7 a& w/ y& N$ O, A/ e ; c" ]7 W9 e0 j, B+ t" R
// Create the list of simulation actions. We put these in3 e+ L$ @# p) f# a6 H* P/ z$ M
// an action group, because we want these actions to be1 @/ c' J A, w( Z
// executed in a specific order, but these steps should) g9 q {# s7 ^0 `- V
// take no (simulated) time. The M(foo) means "The message
3 b% W" G, \ L3 y // called <foo>". You can send a message To a particular" o! v. g* L7 |) ~( L9 R
// object, or ForEach object in a collection.
5 j* K3 ~* I4 K* |% c4 J+ D$ Q& K
|) Z' @. f, B5 a // Note we update the heatspace in two phases: first run
+ n. B, p+ [, }6 W // diffusion, then run "updateWorld" to actually enact the
% s6 n0 L" T& b // changes the heatbugs have made. The ordering here is' T# ]8 k$ ?% }8 j6 u
// significant!! L5 k' l$ t0 j% I. A
/ f+ g$ q7 B4 _3 e! t" W( f
// Note also, that with the additional5 \9 k$ r" g& X: M% R, m
// `randomizeHeatbugUpdateOrder' Boolean flag we can. v- X# P; I! }$ g$ M
// randomize the order in which the bugs actually run
: L# w [# t7 p9 G( v$ V' V8 h // their step rule. This has the effect of removing any
* L* F0 S6 {5 W/ N8 r# n. b // systematic bias in the iteration throught the heatbug6 S+ D: D+ S5 |% u n. O
// list from timestep to timestep2 [* u- {2 L5 R6 |8 `6 Z
. Z w% i! P, g+ ^2 V S
// By default, all `createActionForEach' modelActions have
8 K' N' G2 i0 e8 I7 {. |7 }6 o; S // a default order of `Sequential', which means that the1 E, X9 F! `# Y4 Y. {0 z F+ C
// order of iteration through the `heatbugList' will be
, q9 @8 k& S, ~& M! \ // identical (assuming the list order is not changed; S, U; R( N$ Z; Q
// indirectly by some other process).$ U+ h, z6 G9 |* J: G! o% _
/ T7 k2 {) `$ c2 `% ?2 y
modelActions = new ActionGroupImpl (getZone ());
1 R; {, O4 u# C, Z! W( B, {
' A: q* s w$ n6 e7 ~# R0 Y try {
- y1 \) e" G/ d modelActions.createActionTo$message
1 E% H0 }2 F9 ^! \! z0 i (heat, new Selector (heat.getClass (), "stepRule", false));
9 D& }+ X% Q7 l4 O6 h } catch (Exception e) {% M/ K) c" }$ |
System.err.println ("Exception stepRule: " + e.getMessage ());4 D) I& q/ P8 U; F1 M+ Q
}- ]/ `7 a0 B/ S U
: v- a% w' M9 M, R$ s) k
try {
2 X2 \1 V5 F: A; M Heatbug proto = (Heatbug) heatbugList.get (0);
; Z- d Z1 {, H! v6 f Selector sel = 3 E |; S4 ?, L6 o. P7 n/ c9 o% R
new Selector (proto.getClass (), "heatbugStep", false);* W( f: W3 ?2 Y# K/ V. Y+ R
actionForEach =' g. C0 K K" O; _& H4 L: r9 U3 |
modelActions.createFActionForEachHomogeneous$call
* P; g8 P9 a+ u$ |: Q m (heatbugList,
, Q6 |- a8 m2 E( M" n new FCallImpl (this, proto, sel,; c) J6 \; N& ]+ L, G
new FArgumentsImpl (this, sel)));
/ X. x# i9 O/ L/ K' k; I } catch (Exception e) {/ G) a( r+ k6 v$ c* J0 x
e.printStackTrace (System.err);
) Z% a5 r( G' F' d4 Z }
9 ]$ n; N! T& A# f- b' u' p - u% N+ }7 E( h* x2 n& v
syncUpdateOrder ();5 @2 C4 }1 M. s t# k
0 _* A& g2 a+ V try {
5 ]' {4 {/ B: i2 Y; @ q modelActions.createActionTo$message ! A7 L1 }! T! k; |& Q; F
(heat, new Selector (heat.getClass (), "updateLattice", false));, j% L8 Z' L7 f8 K' u1 w
} catch (Exception e) {
9 X0 X( {; V* Q System.err.println("Exception updateLattice: " + e.getMessage ());
- y* ^- F% }1 ^( `/ k( X: C }
7 L% B- K3 g( b, Q2 U8 D
2 O% l+ @( q0 H- K' E // Then we create a schedule that executes the
, ]4 k8 s4 O0 ~! G# Y% I // modelActions. modelActions is an ActionGroup, by itself it
. x* `, t+ o) }' | // has no notion of time. In order to have it executed in
. J7 p% ]" A2 z) B // time, we create a Schedule that says to use the/ ?6 m8 u0 e; ~6 H2 Z& l, A1 Q
// modelActions ActionGroup at particular times. This" ~! a6 B- ?- e4 c7 X% Y: _' u7 Y M# @
// schedule has a repeat interval of 1, it will loop every
. W4 W; q3 z- [* g. n1 m* Q& O5 z // time step. The action is executed at time 0 relative to
* V `2 z- y0 } {+ O% T // the beginning of the loop.6 w" S; b2 I; p9 V( c1 F4 D
/ j+ l7 i, G' K! L' j8 e# I // This is a simple schedule, with only one action that is
: x/ p, a5 G. v; v9 w // just repeated every time. See jmousetrap for more
6 l9 E! F3 t9 a M9 w+ _/ O# o // complicated schedules.
* N& M( Q8 p6 u7 `: I m " P& p C/ j. S( c1 P0 [: n" F
modelSchedule = new ScheduleImpl (getZone (), 1);
1 n1 b7 y/ b' G; y; b modelSchedule.at$createAction (0, modelActions);& y6 g. ?6 q$ O
' h. e( O+ k9 U' m- T. y
return this;% N3 {. l4 w& O& a$ }: ^
} |