HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 b1 s. @, w; Q' E: s: x
* A/ ]7 V8 u9 I0 }3 e2 ?; ` public Object buildActions () {& v8 T$ ` L- n& U+ g! |8 E5 `
super.buildActions();) U e0 p8 {9 X7 K# j# P. N7 t
! P3 B6 \) b7 C5 L; P. I6 {& A // Create the list of simulation actions. We put these in
1 L& l2 c2 p; M7 R) l // an action group, because we want these actions to be: G N% _* b8 M* u5 t, w
// executed in a specific order, but these steps should
' ?! y; k6 h( ^5 s% p // take no (simulated) time. The M(foo) means "The message
% n H- X* k1 s" a K$ [3 D" \ // called <foo>". You can send a message To a particular; Y( ~+ J9 p+ z2 `$ E5 g; [: u8 ?
// object, or ForEach object in a collection.
; D- j( z1 q. T$ v4 }$ L" x% h
8 P$ P$ Z' P7 g4 n- O, c // Note we update the heatspace in two phases: first run
3 t' |7 ]: f; r+ w) b5 I // diffusion, then run "updateWorld" to actually enact the
0 T+ l, s# g. k // changes the heatbugs have made. The ordering here is
: W. C0 e' O9 L4 Q% N5 { // significant!
5 ~% Q. }/ N+ t9 g' l / m/ F8 n2 E' G
// Note also, that with the additional
! \% b6 T& Q( n: O4 W/ I // `randomizeHeatbugUpdateOrder' Boolean flag we can
" `2 F' S2 s- K6 W2 f // randomize the order in which the bugs actually run2 j6 u% S7 w+ T
// their step rule. This has the effect of removing any2 a; |( I$ w6 j* X
// systematic bias in the iteration throught the heatbug
; ~/ {2 M% F2 m4 n8 ? // list from timestep to timestep) F! @+ [. M$ k% O* c% T
# U- I7 d. G% I
// By default, all `createActionForEach' modelActions have+ V5 F* n& ]" t8 v
// a default order of `Sequential', which means that the/ O2 L- L9 f3 q; { m: F5 o
// order of iteration through the `heatbugList' will be- ~( H% Q' F, ?: r' A3 {
// identical (assuming the list order is not changed5 F5 z. \* r, ?+ ?
// indirectly by some other process).) x2 q( i6 F- _
- |8 @+ I) y% t3 [' h/ r2 e( t modelActions = new ActionGroupImpl (getZone ());
( R% x; k# l' n( K$ y% x& L9 ^' M. ~7 U) @7 c1 P' ^
try {
9 s, c! G5 I4 R" L. Q modelActions.createActionTo$message4 o# h3 t6 p) Y+ _4 p7 J
(heat, new Selector (heat.getClass (), "stepRule", false));
0 v2 z: ~; ?$ P9 K" ^1 A } catch (Exception e) {
|8 b, _% x0 k System.err.println ("Exception stepRule: " + e.getMessage ());
" C9 r' g7 K. H0 K/ `0 o6 m }
9 d0 Y: ~ @2 p- T6 k6 V; a$ I5 T$ a- k. B+ U4 |4 l$ e
try {
- J. e- Y, X$ x5 | Heatbug proto = (Heatbug) heatbugList.get (0);
: @* G6 {+ @0 ~2 j6 B/ Y$ r: A Selector sel = + ~# C2 O/ J: y
new Selector (proto.getClass (), "heatbugStep", false);( [" c5 D: C* d# z5 G2 T7 Z& o
actionForEach =4 v2 _3 l: s0 x4 O2 p" z
modelActions.createFActionForEachHomogeneous$call! w+ D% l5 V6 |4 e
(heatbugList,2 M; P; L7 T3 Y/ P: ^
new FCallImpl (this, proto, sel,4 |& C: K" b ` X$ ]
new FArgumentsImpl (this, sel)));( a7 ?" o) s4 O6 R8 j$ \
} catch (Exception e) {% i% [( g6 V3 j- v9 v3 R/ m4 a
e.printStackTrace (System.err);
; u/ U3 ?' u y8 t" {/ E z }
7 c: k; I* e+ C; U" Z9 ~ 3 l4 \# V4 t$ Y; H4 r* r
syncUpdateOrder (); U$ T% F6 s& o9 `
7 c- v3 D5 ` n" B: A# d try {
1 G- Y% Z9 Z) L: M+ Z! ~ modelActions.createActionTo$message 9 N( w' U9 k, r' z0 V
(heat, new Selector (heat.getClass (), "updateLattice", false));/ V5 n, p7 x* {+ N% X& L9 x
} catch (Exception e) {
% E( A& @; ~9 z( S8 b" b System.err.println("Exception updateLattice: " + e.getMessage ());! B4 D- J/ K! q4 w: j
}# h4 l1 c; Q1 p3 H8 ?+ {! `
% M4 H; W8 [% _( @# F0 M // Then we create a schedule that executes the, X9 U1 [" e. y1 |# n6 s- G/ b
// modelActions. modelActions is an ActionGroup, by itself it
5 p" q& V) y9 i% h; H4 f; t% [ // has no notion of time. In order to have it executed in% z6 H! N- D) {1 b z. p9 X
// time, we create a Schedule that says to use the
# h/ d/ @$ i+ P% I" [/ }( c- h // modelActions ActionGroup at particular times. This
$ I! G6 A9 h# J. C* J% Q7 _9 z // schedule has a repeat interval of 1, it will loop every( j8 n8 ]" H" a |; j/ D8 ^' v
// time step. The action is executed at time 0 relative to+ z. p/ N# H1 b* o0 t6 ^; x( Z
// the beginning of the loop.
3 G: }( z. M9 e @6 h! ~9 V6 N9 M3 z' z0 ^
// This is a simple schedule, with only one action that is
- z: Z; P p k/ ?1 v& N; D // just repeated every time. See jmousetrap for more) i$ Q' m: _' P3 Q4 j, L
// complicated schedules.3 s! E& v, O, A' l& l ~; Z2 ?
* _1 k7 e5 T+ c$ b
modelSchedule = new ScheduleImpl (getZone (), 1);" s2 u) c& c" |/ `& v2 P( d u8 Z
modelSchedule.at$createAction (0, modelActions);# J+ v7 ^- Y, o& r3 R/ s
9 C+ ]1 E* t* J5 W9 \9 h4 D return this;
$ l# L4 J7 _4 N8 h; Y0 F } |