HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% p! |$ f( X9 }% Q( I k+ J$ @
0 t4 b0 [8 _! x0 }) f: x+ \! T public Object buildActions () {: t& A4 p2 w7 ~' d. l3 {! w0 L
super.buildActions();6 L, `9 }% G( Z5 k) I. _8 \
5 U' t% o, q' H; Z. M
// Create the list of simulation actions. We put these in+ {+ g5 f& ], [
// an action group, because we want these actions to be
, C# e3 D. s9 [- l: c3 ~# L* Q% u // executed in a specific order, but these steps should
1 ]- G: K! {. L( E& Q: c5 z& A // take no (simulated) time. The M(foo) means "The message6 b. p" c9 U( U2 B6 X$ T9 b! L! y6 P
// called <foo>". You can send a message To a particular( y7 D1 L7 B) o# E* t- X
// object, or ForEach object in a collection.
9 `/ |! o' l3 `6 Q7 C4 u
) X. Y. ?! ?- Y. d, W9 [6 q& Q // Note we update the heatspace in two phases: first run
: Q! h+ D; a. J1 m( j5 g // diffusion, then run "updateWorld" to actually enact the- [; |7 V4 @1 ]2 S4 C
// changes the heatbugs have made. The ordering here is
2 I6 A2 O; d9 e! h) d0 z: } // significant!
4 I- H& c% K% c3 {1 {, E4 k : z' Y1 Q, ~' @4 k9 o
// Note also, that with the additional9 o* U! j* |) e9 X8 K* f% ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can
7 ]/ P- i3 D/ _4 ~' y // randomize the order in which the bugs actually run
' i y+ k3 t9 t3 ] // their step rule. This has the effect of removing any
* m: {0 W& C2 } // systematic bias in the iteration throught the heatbug
2 T' b* R# Z$ O. Y/ g // list from timestep to timestep7 P. h( L. S+ c q9 J6 h1 B; Y Y
3 l, c4 f8 y' i* Z9 X // By default, all `createActionForEach' modelActions have" \* [- h0 B- E- w/ s
// a default order of `Sequential', which means that the
' Q6 U7 a' b# ]) b# c7 J8 G [ // order of iteration through the `heatbugList' will be& P4 ]$ O8 R- r
// identical (assuming the list order is not changed: M+ `% N- X5 H4 b7 ~# e
// indirectly by some other process).& D; ^* t4 m. J, f
( H% s3 G- S: h, V' I' M
modelActions = new ActionGroupImpl (getZone ());
8 k+ d( E6 t* l4 ?+ x3 x. w- Q, o [: @4 u) m; i2 u4 Y
try {
; C5 K- C) s8 E" F9 \ modelActions.createActionTo$message V S& k$ N6 l. j7 t0 P: k
(heat, new Selector (heat.getClass (), "stepRule", false));
/ u3 u6 U2 v) R7 P' F } catch (Exception e) {
+ B. N: j/ ?2 {! W System.err.println ("Exception stepRule: " + e.getMessage ());; T( x+ P! M+ p
}
, v/ ?" s9 X1 d" t ~$ M. H
5 m3 z9 X; S- \- K- D f try {
. l. ^% m) j! ~3 H0 U Heatbug proto = (Heatbug) heatbugList.get (0);: v$ }( p5 ^! |" ^3 N! A
Selector sel =
0 U& ^: X4 m& g new Selector (proto.getClass (), "heatbugStep", false);
* \% v. d. t2 }( H& t7 t actionForEach =
\1 W' Y" c; f; h4 Y) I modelActions.createFActionForEachHomogeneous$call
. P: A0 x0 V# ?- g (heatbugList,/ f5 b% k% z' W
new FCallImpl (this, proto, sel,
* B" n w8 ]4 T$ i' M new FArgumentsImpl (this, sel)));
2 q& L9 @! h% D+ @/ G+ k J0 V } catch (Exception e) {6 O, Q# w- m0 O" p
e.printStackTrace (System.err);
( Q- l7 f8 M' B }3 m+ E- p' `- w, L7 [0 b% i0 s; O
3 Y T; q1 r$ O" r
syncUpdateOrder ();
0 f$ Q4 e( q' [1 v+ p: `$ Z* {4 }! a
+ W- w3 S- _' X3 _ try {! L# k/ U' Z/ @7 ?/ G% K
modelActions.createActionTo$message
4 D7 P3 q. ]2 k" F0 c4 D) z (heat, new Selector (heat.getClass (), "updateLattice", false));
; P _/ n% M7 p/ C } catch (Exception e) {# T* b; c9 i! N8 `7 B
System.err.println("Exception updateLattice: " + e.getMessage ());
* G3 a' V: }) T& _ }
0 s$ t1 v7 X2 v& o . b, t0 t6 b" \9 L& s
// Then we create a schedule that executes the5 E* b( E: u) K" Z: V" L: j0 N
// modelActions. modelActions is an ActionGroup, by itself it* m# R" y: W, @7 Z/ _. a6 p" P I+ ?: \
// has no notion of time. In order to have it executed in
1 ?* M$ n0 v ^; T( b // time, we create a Schedule that says to use the
+ j- @% z! }& U3 s8 J5 e // modelActions ActionGroup at particular times. This; @. ?! d2 e, V; m, ^
// schedule has a repeat interval of 1, it will loop every
( _' Y ?0 H$ _5 Y e- W# V // time step. The action is executed at time 0 relative to' y- d+ K& F! j- `$ m: V# W
// the beginning of the loop.
6 Q4 O9 B9 S3 N* O ?5 [
& o7 }3 {( Y# T* Y# }8 z // This is a simple schedule, with only one action that is
- Y) ]- n. `1 I4 H& {5 A# T // just repeated every time. See jmousetrap for more. b7 n" P6 B5 @/ Y+ X
// complicated schedules.7 n5 I5 E5 I8 r) m1 i! [, k
; \, ?0 q1 O& R, V# x. k) J5 n k modelSchedule = new ScheduleImpl (getZone (), 1);! W3 o8 {; I T: E
modelSchedule.at$createAction (0, modelActions);
* x8 {% a- t0 X) z+ G3 D8 Q
" Q% U1 Q: y9 ~+ V( W6 G return this;
; l9 k$ d' y( o. d; S8 ` } |