HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 b7 c2 y/ y* }0 a+ N
. f. {' [ U) @& Q- O- @/ c public Object buildActions () {4 i$ O, {' P6 c& u
super.buildActions();/ {: q9 S4 W7 i5 n: K
5 A' i3 u. |) D7 b // Create the list of simulation actions. We put these in
1 T: g, j, g% ^& h6 S // an action group, because we want these actions to be9 ?) i2 @% E6 _% N& d
// executed in a specific order, but these steps should
) `* h! Z0 W" w# |0 ]: {' V // take no (simulated) time. The M(foo) means "The message
X' y) g: e& j; d3 p4 b // called <foo>". You can send a message To a particular
: e8 E' Z+ K) |9 ]1 k. X0 z; N4 \ // object, or ForEach object in a collection./ ^ {$ F" G5 c8 f6 L1 a
' l" \- e, {- H4 l // Note we update the heatspace in two phases: first run
' x" w9 v/ X& @/ B3 F3 K // diffusion, then run "updateWorld" to actually enact the
8 q$ i7 m3 M2 s // changes the heatbugs have made. The ordering here is
/ C$ v2 }( f k // significant!7 p3 v6 D) O8 i( k% s
; R& K4 I& D4 E6 i // Note also, that with the additional
: r4 O# {* F& q) n5 v // `randomizeHeatbugUpdateOrder' Boolean flag we can8 ?& _9 Z& {% K# c6 E# X+ D5 D+ @
// randomize the order in which the bugs actually run
7 {! z( r& V( O2 M" J) w- Z // their step rule. This has the effect of removing any. \* [3 a6 c5 }5 Q( @6 P' a; ?6 z
// systematic bias in the iteration throught the heatbug
/ o" X9 ^2 T+ p6 X6 Z // list from timestep to timestep
/ K( n" Z- |% f0 r% c
1 j- \* n$ k* x // By default, all `createActionForEach' modelActions have$ @: Z! U# p' u
// a default order of `Sequential', which means that the
" V, h3 M/ V1 W) q // order of iteration through the `heatbugList' will be
9 a% Y7 | ?! d+ R; I // identical (assuming the list order is not changed- M+ f8 w& `1 p j5 j) [. w
// indirectly by some other process).& v5 `. W/ [) E6 S$ z1 K
- E0 Y* n; y; F2 C2 U( {
modelActions = new ActionGroupImpl (getZone ());* @) W! S7 U+ {- q+ E- w u* j# S% p
% j* A8 t5 b5 i4 t. S; @; q try {" M& Y% V0 _6 y, D* u
modelActions.createActionTo$message
f+ y' l4 [6 @. [& P. A' x9 O (heat, new Selector (heat.getClass (), "stepRule", false));/ L5 T% [6 h- N$ b+ e
} catch (Exception e) {5 `. v1 n- M5 y8 h+ c) r8 q/ k3 G
System.err.println ("Exception stepRule: " + e.getMessage ());
8 c, _& i; V# X1 V# ^6 c, c. | }# V5 C! ^; L3 g. x$ G1 j+ k. o5 L
9 @* z8 ^ o8 d2 I6 S- Y
try {
. ~9 c2 I5 S) S( Y" q Heatbug proto = (Heatbug) heatbugList.get (0);7 Y$ v: [5 B+ D' ?) K" ^. [
Selector sel = & o" r( U _3 O1 q: u9 E
new Selector (proto.getClass (), "heatbugStep", false);; O, ^3 g$ [4 j# G9 I/ t
actionForEach =
" K% d; r u# r# I! h* ^ modelActions.createFActionForEachHomogeneous$call" E. Z* E! _) u) N2 J6 I
(heatbugList,
: X/ t7 w9 N- L2 r% g new FCallImpl (this, proto, sel,) n# G" `$ r; q8 h, @. a
new FArgumentsImpl (this, sel)));$ Q4 ~$ R: s/ j" l$ G
} catch (Exception e) {
; m. l2 ~5 v1 L$ I: s9 N) K e.printStackTrace (System.err);# O, U5 `& Z- y7 `5 A1 o
}: n! t' `+ ]- K% q4 c1 r
% v; x0 e% m1 e8 ~! k5 r, @ syncUpdateOrder ();$ {+ R/ I2 b8 z- A( x# f* @
, y9 C8 e! I' K7 _$ b8 @. E
try {5 s- f3 b( |$ r* h$ `
modelActions.createActionTo$message ; J7 B: O* k3 r* R1 O( d! x
(heat, new Selector (heat.getClass (), "updateLattice", false));( W1 G- j4 A' O, ?( H2 H d' o
} catch (Exception e) {
1 O3 h* k4 a, S+ ?" R7 b, Q System.err.println("Exception updateLattice: " + e.getMessage ());9 ~5 q3 ^) \% h! K
}' |- K* d4 Q8 R7 u3 l( t
2 G2 v- v( Q- N. n- i* p: U8 h: {( ^ // Then we create a schedule that executes the" z+ e* M/ K" o! Z% V/ D! G
// modelActions. modelActions is an ActionGroup, by itself it/ i& \6 L1 B% z& `4 q
// has no notion of time. In order to have it executed in0 |$ T# {% c8 c9 _& T
// time, we create a Schedule that says to use the
; X& [# }: ^+ Q* ]5 A: S) I // modelActions ActionGroup at particular times. This
: \; t T4 K$ {4 B/ X: S: L // schedule has a repeat interval of 1, it will loop every% k- V0 `: h* V) t- d8 I
// time step. The action is executed at time 0 relative to& {' f, |) i) c2 Q, w9 r5 v5 O
// the beginning of the loop.
5 _) x5 X" \. P0 ]6 T& H9 P v1 M5 `' L
// This is a simple schedule, with only one action that is
9 d: b* Z* j2 J$ ~ m$ W9 s0 s% D6 b // just repeated every time. See jmousetrap for more- W6 k( F. |2 V- E
// complicated schedules.
+ q9 o, g3 L& z- t2 \. V" L 0 `. C/ ^+ I0 b8 k7 L
modelSchedule = new ScheduleImpl (getZone (), 1);
. x8 r% W0 H) q" V4 g# \ modelSchedule.at$createAction (0, modelActions);
- F. |* A4 d; s+ g ' @% K! g! L/ j* D/ f; f
return this;
6 D3 q1 L" a" R- z } |