HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, v0 l, o8 q! ]3 S. |: D5 ]
8 [# S" F4 i8 K0 J+ V public Object buildActions () {% d( L! D3 o7 ?& u8 d l2 U8 p
super.buildActions();
5 V+ \3 S$ z5 u/ |1 _9 E * \; q1 g- x6 N- g% H+ D0 G$ _5 u
// Create the list of simulation actions. We put these in
5 y6 k2 G [. \; ^$ N* T$ a // an action group, because we want these actions to be
: C# @% d. Z i# q, [: K, b- k // executed in a specific order, but these steps should( }# s/ ?' ^7 d) S4 [/ G8 G
// take no (simulated) time. The M(foo) means "The message
- x( N: {( U& y0 X5 y& o( e // called <foo>". You can send a message To a particular8 T9 P, D: y2 t+ [
// object, or ForEach object in a collection.
6 F+ ?8 e& G8 M" [- m1 _; R2 n* L0 z5 V
' y1 _/ W$ ]. n& d9 @ // Note we update the heatspace in two phases: first run I. Q" S' b, R( q7 g& @
// diffusion, then run "updateWorld" to actually enact the, I* {7 e/ P0 X7 a* I. a
// changes the heatbugs have made. The ordering here is
2 p3 c7 [+ x% i% A9 Y // significant!
# W R) b1 X, f$ X) ^& f0 @: Z
; T# |/ N3 {; v8 h // Note also, that with the additional
& x0 L3 E5 \1 |5 g b1 Q1 o // `randomizeHeatbugUpdateOrder' Boolean flag we can( `) j @+ {. m- `
// randomize the order in which the bugs actually run+ H( a( [/ [0 b
// their step rule. This has the effect of removing any" x4 c5 O' ]- J+ E& r
// systematic bias in the iteration throught the heatbug
7 u7 f0 g% v/ q' ]9 }0 q1 f! F // list from timestep to timestep
& D. t& t8 d& S( b( d7 w + U% M/ S; I( x; Q& E# u
// By default, all `createActionForEach' modelActions have
& h! `8 n" L; M // a default order of `Sequential', which means that the7 P8 H. b5 q$ d' ^
// order of iteration through the `heatbugList' will be# m) o( |8 ]: n
// identical (assuming the list order is not changed# b+ j* t, E. n0 w' p& ^
// indirectly by some other process).+ o7 J) k& V% ~% j( i/ S/ \
# |1 z. l. I8 J4 v5 \ modelActions = new ActionGroupImpl (getZone ());( J& j" p9 h2 _7 h' Q! n! c
- C" h& {' @/ `4 y8 h% V try {
6 P- P$ i7 C, V+ W8 s modelActions.createActionTo$message7 P! m/ t% |+ d, o. \5 G2 }/ ^2 Y
(heat, new Selector (heat.getClass (), "stepRule", false));' d& B2 ]9 B) ^! L# c5 q, E( j9 t
} catch (Exception e) {8 P' o6 G/ ~9 T* u6 D% }2 N% _
System.err.println ("Exception stepRule: " + e.getMessage ());
) n8 D8 f, A; O) Y/ T }
" a4 m2 X$ | c8 I
d I' ^1 [- @2 V. x+ t try {- U; U. s2 N& D" Y3 @& S
Heatbug proto = (Heatbug) heatbugList.get (0);* y6 X$ R% O- M5 N d8 Q# K
Selector sel = 6 F* S0 T. v3 O9 b! |3 `/ x! c- e9 G
new Selector (proto.getClass (), "heatbugStep", false);1 R5 y/ J. B! Q- b" R* Q
actionForEach =
; P5 Z$ w% @# M/ }% {, |, ^# g& J modelActions.createFActionForEachHomogeneous$call
( _# b8 F% ]3 p' a" n5 y" T (heatbugList,
3 o8 ~( s: G7 q" w: J9 Q new FCallImpl (this, proto, sel,
- B, v- J; c; ]7 r' Y# q3 c" [ new FArgumentsImpl (this, sel)));
4 c4 |( [, }" d, Z& }; a } catch (Exception e) {& C& u0 g7 l/ V4 v5 F
e.printStackTrace (System.err);5 w% o% K% d8 R; n
}+ t9 ^! R2 f: T
& e+ j# Z0 o3 i: G+ n syncUpdateOrder ();
* U$ e# _) D: ?' {+ I% {( d/ }- q& S0 v
try {
4 T8 V' } |$ K1 K modelActions.createActionTo$message 2 p4 S: r, m' c9 W8 a/ @* r
(heat, new Selector (heat.getClass (), "updateLattice", false));
3 f+ |' b( l. v9 u8 L } catch (Exception e) {
1 O5 v. B% Q+ O6 _! Z+ B System.err.println("Exception updateLattice: " + e.getMessage ());8 B/ I5 o) z' ?# h; @0 h
}
+ ]" {' w, a, N7 ~, s % Y$ y# @; i/ E& }- H
// Then we create a schedule that executes the0 L' F9 k% F3 R: u' e: w
// modelActions. modelActions is an ActionGroup, by itself it3 ?9 h6 K5 U/ D) D
// has no notion of time. In order to have it executed in
3 a! s/ ?+ Z7 \+ `$ m$ d // time, we create a Schedule that says to use the
) N5 \; B* U# s6 K6 {! Q" ~, h // modelActions ActionGroup at particular times. This
. s: X7 `0 w# e8 f3 K, _. R$ T/ q // schedule has a repeat interval of 1, it will loop every2 X; x+ _/ a3 o7 B* H# f- z
// time step. The action is executed at time 0 relative to
% v, M3 b% {2 r" n // the beginning of the loop.
, `) `! z0 r& {$ }: U, M# ?" G% E
0 n( O% H. j% d" i9 j* ]* r+ p // This is a simple schedule, with only one action that is7 N0 L5 F0 ]9 a5 s
// just repeated every time. See jmousetrap for more
- J5 H* j: u+ G3 r6 r( Y* f // complicated schedules.' b4 Y' u3 ~# M, @: S' [
2 x/ O8 i" q1 z# S7 [3 Z! z( P5 P) h, Y
modelSchedule = new ScheduleImpl (getZone (), 1);
6 v( S x# ?1 b* B6 K modelSchedule.at$createAction (0, modelActions);7 |7 H7 I& E+ Q, m) S6 F+ l- n
# {5 `) ~5 E0 J" D% g return this;
% u( [ ~% X% m+ v& n; s: u/ d/ G } |