HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% {) c+ m x1 u# J4 d2 F
( x x3 P8 L2 N, y2 f5 w4 v
public Object buildActions () {# J# m" x- c0 A% @$ r3 l
super.buildActions();! x! I! f8 c$ S. e
* i: ^6 o8 L; _8 ?: v
// Create the list of simulation actions. We put these in- w; }5 q5 E7 M+ L
// an action group, because we want these actions to be
3 x$ ]9 g" {* }' L3 \- ~ // executed in a specific order, but these steps should
7 j7 H/ r: u1 A& X0 m // take no (simulated) time. The M(foo) means "The message1 l% b7 V( i P6 y9 n
// called <foo>". You can send a message To a particular1 u; C; k2 o* g$ l/ s" B& Z
// object, or ForEach object in a collection.+ j) j) e+ i0 T
( B" e6 F/ h! S3 [& c* v // Note we update the heatspace in two phases: first run: _# v% |1 c5 }) M, `
// diffusion, then run "updateWorld" to actually enact the+ _% C4 U) Y6 w. ?1 j5 K
// changes the heatbugs have made. The ordering here is
, C7 X9 K, a& ] S" c; W; r // significant!9 r* f$ D& z; G Y" `1 V5 E
2 v% m9 G) b) R* P2 ? // Note also, that with the additional$ P. _. Q" t: F B3 ?9 ]+ s- e
// `randomizeHeatbugUpdateOrder' Boolean flag we can( Y; _3 {' T+ x
// randomize the order in which the bugs actually run9 e: G1 [, F" N! i
// their step rule. This has the effect of removing any* k( H$ g! N a" w; }6 P0 `
// systematic bias in the iteration throught the heatbug
6 I! t* X7 U% I5 `: c // list from timestep to timestep- p1 V6 E- j" j) r1 f
?9 L0 A. G' s& q% h5 A: p
// By default, all `createActionForEach' modelActions have
9 u0 C2 a) ]! e! B // a default order of `Sequential', which means that the
' ?3 Y) ? P3 w# j0 x // order of iteration through the `heatbugList' will be
3 @ O- Y3 v6 d; W4 h // identical (assuming the list order is not changed$ T' \( _& s: Q1 [
// indirectly by some other process).
5 ?& w' v+ _6 i: T2 d& g( P' W
1 \0 {0 Y \( ?; ] modelActions = new ActionGroupImpl (getZone ());
+ ?! k3 o- A4 l4 u0 c1 X, W
7 j6 y3 @9 ]: Z$ [0 N2 t try {# q: G/ ?. B9 ~8 j$ B6 Q' H
modelActions.createActionTo$message
5 H- l# h5 A( h A (heat, new Selector (heat.getClass (), "stepRule", false));
" P5 {. ^6 t l4 I+ b$ t6 ~' u } catch (Exception e) {( C! w! {9 b) m' _
System.err.println ("Exception stepRule: " + e.getMessage ());! w; l( @) S. E5 m" a( ]
}) h. Q& A. i6 d# w$ [. U
8 Z- y5 j. n6 X, A# w0 v try {
% [" w1 ^- U9 {: C1 D0 G1 c: N# N/ O4 { Heatbug proto = (Heatbug) heatbugList.get (0);/ l. s4 [5 ?6 b: J
Selector sel = 4 _, o" r: b7 W' j- T
new Selector (proto.getClass (), "heatbugStep", false);/ p k; i* ?, R! k
actionForEach =
+ F0 J% x7 Q: h5 N modelActions.createFActionForEachHomogeneous$call
0 \" ~) Y( n6 H (heatbugList,
5 m8 [" T4 G t ]8 Y% a3 @+ }+ w new FCallImpl (this, proto, sel,
- u. C; Z1 Q7 ~ new FArgumentsImpl (this, sel)));0 t% Y: y; E/ |4 U) F
} catch (Exception e) {
" K( K% N7 Q6 Z" V+ `9 V" C0 ~ e.printStackTrace (System.err);7 N, ?& @* r# d6 s
}
U4 c- I {2 n
: g6 d: z+ D' K; p syncUpdateOrder ();
& n2 s) t: e; s! u# P; M8 K U4 A# o" F1 f' B2 u
try {& f0 L, n6 C& R. l1 I6 E$ E
modelActions.createActionTo$message 7 G3 L/ g# Y. T- R' U3 I
(heat, new Selector (heat.getClass (), "updateLattice", false));( S- ?; B5 L# z t0 I3 Y
} catch (Exception e) {
9 h6 P0 z/ e! ^+ e. J System.err.println("Exception updateLattice: " + e.getMessage ());, ]* @3 d& H) D% e
}4 Z: h. E2 s6 h6 {$ F9 y
4 Y. L. |& N' R5 b; Z# G
// Then we create a schedule that executes the
+ e2 m% N* _0 ?, P% K // modelActions. modelActions is an ActionGroup, by itself it
1 f; T. M" @& X/ e // has no notion of time. In order to have it executed in0 _1 k" h; s/ S/ ]5 j0 Q$ {! T
// time, we create a Schedule that says to use the: Y8 K1 X; @8 F0 c* Z8 T0 D' d ]2 }
// modelActions ActionGroup at particular times. This
" i q3 n* M9 N* L: l/ b) n8 C- W; U // schedule has a repeat interval of 1, it will loop every
) H' u% b; Q8 m7 K* R7 j7 W( W // time step. The action is executed at time 0 relative to
' ~: o8 m$ @& b% d& K7 B; g // the beginning of the loop.' E6 m2 ?, \ l( v" F8 _8 \
" L" d8 M$ Z" t3 u // This is a simple schedule, with only one action that is
: t/ B' J: W+ K; ] // just repeated every time. See jmousetrap for more
9 D9 _, s1 Y; ` X4 E3 l( g // complicated schedules.
! ]' b. H6 G6 }& R5 i + ^5 ~3 _: a& G7 w7 g: ^
modelSchedule = new ScheduleImpl (getZone (), 1);
; k# P( C1 K; x modelSchedule.at$createAction (0, modelActions);# `8 b8 _5 @" i2 @6 W: O
9 u7 {; {* P* Z! a return this;' c8 o; l: H9 T; U+ t$ D n
} |