HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( x; } p! A0 k* T9 H& z) w% g# Z6 r- K3 j3 c/ H9 F5 Z6 F! y1 p
public Object buildActions () {
; q) _+ L( e l super.buildActions();
2 @9 g, }, o7 J
- n2 r" S$ I" \$ } // Create the list of simulation actions. We put these in
' k- X4 k0 N9 E+ y, H R+ h // an action group, because we want these actions to be
3 w' g( g! h( `4 f1 K, n // executed in a specific order, but these steps should7 `: d+ `4 t' T5 D4 e- Z+ k
// take no (simulated) time. The M(foo) means "The message+ [4 y& w5 Q& s. f+ p
// called <foo>". You can send a message To a particular
8 B/ m; i- ^1 T' Y* d# }7 M8 i // object, or ForEach object in a collection.6 t# j5 J0 ]! Q3 w
1 L: y# M! C W0 b0 Y2 K2 ]2 q // Note we update the heatspace in two phases: first run4 }0 D9 x- \, {9 J' L
// diffusion, then run "updateWorld" to actually enact the8 e5 P$ E: E5 L0 x4 B* `0 |
// changes the heatbugs have made. The ordering here is
; k# R' H6 J9 h' C( e // significant!. g2 i! e1 D9 h
# u- ~) W6 S* O4 E7 e7 U
// Note also, that with the additional
0 k5 e ?; s: Z% Z0 v7 I // `randomizeHeatbugUpdateOrder' Boolean flag we can
8 F' Q3 [# L* n e! c0 |/ t4 n // randomize the order in which the bugs actually run9 ^# S( w2 @" v% i1 P% @
// their step rule. This has the effect of removing any
- b1 L% `# t) y' Q // systematic bias in the iteration throught the heatbug; A: ]! t* z# z. R, A: G8 g
// list from timestep to timestep
% X8 G) S$ D" a; ] - q3 D! m; ?1 N) ?% p
// By default, all `createActionForEach' modelActions have, @, X- q" h1 s( \8 f
// a default order of `Sequential', which means that the
" C9 b; V6 N# {+ [ // order of iteration through the `heatbugList' will be. A/ i& Q, d2 B5 X
// identical (assuming the list order is not changed5 ]/ A9 J" {8 z
// indirectly by some other process).
: X' Q( h4 C7 x" ^: J! Y L ) z* N2 b$ V" C+ @1 `( K7 B. }5 g
modelActions = new ActionGroupImpl (getZone ());+ l" s% F3 A- N8 h% p
. A% u* X; W c4 a8 ~% H
try {
7 X M8 j4 d9 B- g1 p2 s9 n9 j) p5 j modelActions.createActionTo$message8 d# ^ _: l* Y
(heat, new Selector (heat.getClass (), "stepRule", false));4 k1 _5 }! K. j! \1 _7 ]" M4 @
} catch (Exception e) {
2 q0 s9 ]3 `5 i2 u System.err.println ("Exception stepRule: " + e.getMessage ());( d1 @; f' F( Z; ?; T# |% t; u
}
& Y4 |$ L& \% e/ n& S5 g% K; N$ u
4 H* m5 p1 p, b+ V: R* h try {( b4 K+ ~ ^7 ~. u
Heatbug proto = (Heatbug) heatbugList.get (0);
$ u. t# A- B6 Y: P( S7 D1 N- P Selector sel = ) K; Y, J" K/ k, W: Y8 r3 h
new Selector (proto.getClass (), "heatbugStep", false);
) X, Q. @* Z0 A% S0 ?6 ~ actionForEach =, P; h' } Q' a
modelActions.createFActionForEachHomogeneous$call
7 Z8 T% H9 j8 T# D (heatbugList,1 c& q* k3 Y1 I7 A1 n. C4 R
new FCallImpl (this, proto, sel,
' O( r( I1 \: n new FArgumentsImpl (this, sel)));
~3 }1 D+ N6 A7 ~ } catch (Exception e) {* E% \* j9 `9 @: P7 k- p( P
e.printStackTrace (System.err);
) d3 z" d! S' q }
+ P6 @+ ?! U% Z: J& d& [ % D+ {% s2 K \7 b6 k
syncUpdateOrder ();: i; b- B" p. w) a! s, ~3 a& b9 M
8 U% t% n9 G) p9 l2 ~
try {
) U; H0 ^# f3 _. b modelActions.createActionTo$message / l0 o9 l( P( P
(heat, new Selector (heat.getClass (), "updateLattice", false));$ q0 U7 M1 H$ ]0 S6 i1 `0 H
} catch (Exception e) {
. _; O8 b U$ s& j System.err.println("Exception updateLattice: " + e.getMessage ());- A2 T7 P ~+ Y% |* _9 _
}
" J" T: r) }! F- L | ; v$ a. ~ S& h7 v7 F
// Then we create a schedule that executes the
' D) @1 I% k+ r8 {* D: M( t8 z // modelActions. modelActions is an ActionGroup, by itself it
' a: m! A U+ s9 C1 D // has no notion of time. In order to have it executed in
1 d {6 f0 B% G6 ^/ Y: V. u // time, we create a Schedule that says to use the
% ^9 H5 V/ `) I7 C // modelActions ActionGroup at particular times. This. E$ O+ L6 Y4 U* Y$ P, q" \
// schedule has a repeat interval of 1, it will loop every
p% s# e& j8 K: T5 l% J( ^1 i // time step. The action is executed at time 0 relative to
% [6 z+ T9 H5 Z* ^$ \: u // the beginning of the loop.
! C" z/ ?# u+ V1 C- C
, M% S9 z2 Z# Q7 S // This is a simple schedule, with only one action that is
: v# h5 L. e! S5 K! t( f& P# `5 s // just repeated every time. See jmousetrap for more) Q# C& c9 v4 k" ^* u( }4 T0 e
// complicated schedules.
7 w/ K+ d% m3 C2 I5 D
9 @5 ?/ \& x2 r( x/ G modelSchedule = new ScheduleImpl (getZone (), 1);0 D& p8 r& S7 I! z) B; r3 C
modelSchedule.at$createAction (0, modelActions);
$ I- w- p, i5 z8 s
' X) M0 W' O) N* m8 j% g" Q$ { return this;; i+ F3 V- c- w
} |