HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 m! G$ X. E3 E/ ^! J" X6 p) c e; K" n
public Object buildActions () {
1 g4 I l0 q' D7 r super.buildActions();# f- x. j( B( {" e; j
" p7 a6 e$ l! |' W* D' v1 T& _( d // Create the list of simulation actions. We put these in/ B+ Y* i/ W) H
// an action group, because we want these actions to be% W5 n9 T/ X5 Y3 @9 |
// executed in a specific order, but these steps should
+ j/ x3 F7 _) y# Z. C# k // take no (simulated) time. The M(foo) means "The message
& T& ^+ b4 \1 J) S1 S // called <foo>". You can send a message To a particular
% X% O! x. G3 n2 Q1 {5 L3 a0 i // object, or ForEach object in a collection.# j+ W2 Q( L& y4 u. n
" n" ~: w" Y5 J) V7 h
// Note we update the heatspace in two phases: first run
6 Y O; v6 m' S+ S // diffusion, then run "updateWorld" to actually enact the
' N; D* E& E6 h# z // changes the heatbugs have made. The ordering here is
+ B: B" f- d$ R // significant!
9 ~6 }& s! p4 m2 i/ s; }$ W
3 F5 P4 G( q" k // Note also, that with the additional
2 ]" g3 h1 z7 \: i4 V! `9 e* X. d // `randomizeHeatbugUpdateOrder' Boolean flag we can
: M3 f0 t. d3 T, _5 ~ // randomize the order in which the bugs actually run
$ R7 T+ l3 [( |1 o // their step rule. This has the effect of removing any4 o) K( H; N* X$ }+ L2 X! x: r# u
// systematic bias in the iteration throught the heatbug
; F$ t4 @0 E6 v // list from timestep to timestep" u8 a9 L9 i0 R; ?4 w8 X( @1 m6 O
" R3 R% k0 Q4 n* r0 c% E2 P2 P
// By default, all `createActionForEach' modelActions have
9 w2 x! U. `% }( ]7 o4 J; b // a default order of `Sequential', which means that the. T2 |4 J* Q) d- s k' t' U/ B
// order of iteration through the `heatbugList' will be- r( V) m" ], ]1 R8 C4 G7 L* ^
// identical (assuming the list order is not changed, a. v5 }. R O( Y
// indirectly by some other process).
6 B+ U; [9 D- `- I# v! N, W p: B# I( L* P k C) ^# m7 l1 r6 N0 X, Q
modelActions = new ActionGroupImpl (getZone ());
. ~' ~7 X+ ?7 L8 j# S$ b
$ A# J0 s% Z% v. s, V3 p a try {+ V, F* K: N3 [# Z" z4 _' V" U9 P# L
modelActions.createActionTo$message) T; U1 ^' {: c7 Y% T
(heat, new Selector (heat.getClass (), "stepRule", false));
" y! M! E- s4 C9 v1 ?$ M' U( n } catch (Exception e) {! ^/ I: _: n Y4 g! D: E
System.err.println ("Exception stepRule: " + e.getMessage ());" K L! i- q+ w# o6 Z" h; w" p
}
$ l @' |7 o# `" E. Q; C8 Z7 ~1 e1 i& s# ~2 u6 o! w- B: U- A* X
try {4 W, I/ q% p; S+ m$ K
Heatbug proto = (Heatbug) heatbugList.get (0);) i, u: n: f+ x8 g* B1 D0 y
Selector sel =
9 ~$ z2 G& p7 b! \' M$ N new Selector (proto.getClass (), "heatbugStep", false);0 `/ _; c, E& o3 f/ U0 s5 ?
actionForEach =/ J3 f- l6 C, P& G% ?: {
modelActions.createFActionForEachHomogeneous$call# J8 f/ j/ Z/ k0 v& }3 z) Q
(heatbugList,# N; I7 _; i0 t' A2 O9 Y
new FCallImpl (this, proto, sel,# _' A0 x$ @9 i3 R5 x' O% n* [
new FArgumentsImpl (this, sel)));& T: q/ s9 W3 W4 {
} catch (Exception e) {
, ^! r( ~0 U1 \4 T* O8 k; O# } e.printStackTrace (System.err);
9 ~# k( ^8 f; B { r/ o4 E5 e* l }
; E4 p6 ?9 Q" z) B5 n( f9 r4 B
: r8 w2 e7 f/ M. r$ s* n syncUpdateOrder (); Q+ Y$ m3 u. e
. p! G! p* |, \2 L/ j- c) z try {8 M. c* @& m6 T' X! X! e3 a
modelActions.createActionTo$message 4 b5 s6 Y, d; l5 g6 M5 z$ i
(heat, new Selector (heat.getClass (), "updateLattice", false));
$ Z0 w- X5 {9 y+ \, P5 ~: h& P } catch (Exception e) {
) d% B! ^4 h6 d" u System.err.println("Exception updateLattice: " + e.getMessage ());
" G2 f% Y" K8 D+ x N- }3 T, O }
: d0 O8 q# c" Q" j+ B" M, G0 G
) N( z4 |$ x5 l: @. a/ F0 A // Then we create a schedule that executes the; r/ G" n! A/ L- }+ `
// modelActions. modelActions is an ActionGroup, by itself it8 H0 _# |- ~/ {& y0 L/ x$ H
// has no notion of time. In order to have it executed in2 A6 h- I- \8 I8 f
// time, we create a Schedule that says to use the
, R! p2 `- \: J' ^$ V9 ] // modelActions ActionGroup at particular times. This
0 |) g$ w4 r7 t) Z' b- H // schedule has a repeat interval of 1, it will loop every
5 r0 c2 i) ]1 H# y5 f0 P9 t // time step. The action is executed at time 0 relative to
5 o6 O/ D+ {8 o# `0 V7 t! z/ } // the beginning of the loop.
5 A4 }/ V- F" G- L
8 E* ^3 q1 `# J0 f' K/ g, e // This is a simple schedule, with only one action that is e2 s9 g& h8 e% _ `
// just repeated every time. See jmousetrap for more
: T+ Y: n* H) Y4 z+ i* Z& P' z // complicated schedules.
7 Q- O5 d' @8 s+ F2 h P
& S a# I9 S7 f4 d; p7 A: L, x modelSchedule = new ScheduleImpl (getZone (), 1);
- @6 @2 |- ?& I modelSchedule.at$createAction (0, modelActions);# J& c7 b% i# [/ _
" C* V5 c2 o/ a. m
return this;' W: l) l: t3 J. O6 v/ ~
} |