HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& Q1 ~* v' |4 C' p# ]4 N4 s
: J% F m" l6 W* b- V+ w public Object buildActions () {
3 u! x0 {+ ]5 s0 B" B super.buildActions();) s. F5 K% l) N( {
; y3 D# i0 I2 n2 b // Create the list of simulation actions. We put these in% b1 N, j4 {3 m3 {) k+ H
// an action group, because we want these actions to be4 [$ W# e& p2 R0 b) ^+ ~4 e; o
// executed in a specific order, but these steps should7 z4 C; G: ?( Q. _
// take no (simulated) time. The M(foo) means "The message! {9 x9 o" T6 s; Z/ y, t. ~/ k
// called <foo>". You can send a message To a particular& g0 f3 H @' X+ Z1 W7 Q
// object, or ForEach object in a collection." x0 F4 z% k/ i0 C( d
6 W7 u5 U( v( Z0 i // Note we update the heatspace in two phases: first run
6 J& i1 h" S& d: _) G& z. [ // diffusion, then run "updateWorld" to actually enact the/ L4 C2 ]4 `, V3 E$ K8 [1 T. s1 Q
// changes the heatbugs have made. The ordering here is) P3 l0 q1 N3 G" x' H
// significant!' X4 T" n4 q+ y
& t2 c; q( U# `! h/ _6 [ // Note also, that with the additional
3 k5 M* h& n" \6 {2 j& c; B // `randomizeHeatbugUpdateOrder' Boolean flag we can
1 w1 T( V5 y9 |" I) G/ @! Q7 X6 G4 R // randomize the order in which the bugs actually run
, V2 ]: H; K% D1 P // their step rule. This has the effect of removing any
$ ]" P1 j7 ?8 }; F2 H/ T* S d // systematic bias in the iteration throught the heatbug# }0 {. S A% N3 b( s6 O5 Z* Y
// list from timestep to timestep, H5 t- [3 o3 Y3 Z# Z A, g- C
# s% f: A. |$ \: z // By default, all `createActionForEach' modelActions have
5 G/ I; T" C1 F6 q+ O // a default order of `Sequential', which means that the% |$ r) Z* x4 Y3 X( d. z
// order of iteration through the `heatbugList' will be" O+ r+ j, f; J! p) [7 p
// identical (assuming the list order is not changed5 S- B. P' s6 V; r3 n" d4 z
// indirectly by some other process).) F! F/ j7 ^% T* k
9 q9 N5 R' A4 X9 S% @0 t modelActions = new ActionGroupImpl (getZone ());
' R7 Z9 x; ?: d
1 l0 `. z3 x- t9 v try {5 `9 c( R& u' x0 H
modelActions.createActionTo$message
7 I2 f: q7 Z2 U6 I! b3 h0 ~ (heat, new Selector (heat.getClass (), "stepRule", false));: A( {7 L ?% d/ G- r% K
} catch (Exception e) {- Y6 @8 F& H, M) [+ N( p
System.err.println ("Exception stepRule: " + e.getMessage ());
% ]9 E* C* O! P! s }
- N/ U0 |2 `; X4 \
. {: g" r7 D# w1 a try {, T9 d+ C% ]8 @8 |+ Y. X
Heatbug proto = (Heatbug) heatbugList.get (0);
. d" w) T( v. t# o7 i Selector sel =
, ~1 r2 V( \ g1 f: p" ? new Selector (proto.getClass (), "heatbugStep", false);
9 v6 f/ ? x: i( Y' _0 \ actionForEach =$ G/ m( l0 s' x/ ?! K7 ~" Z
modelActions.createFActionForEachHomogeneous$call
* `5 R8 B' q$ x% G1 w (heatbugList,2 f2 d# K: h" C u* F
new FCallImpl (this, proto, sel,
9 L- B6 h2 \5 o, `! ~' b new FArgumentsImpl (this, sel)));0 U% J8 k9 j! \" x9 P5 n: P4 x0 ?2 W
} catch (Exception e) {8 u. j* R/ V* _( [- Q# O
e.printStackTrace (System.err);
' A/ p1 O0 _6 t2 C) E }! H7 g2 d. z. n/ ^5 t7 I
# j% [# X1 v7 o' g4 M5 g( k
syncUpdateOrder ();
9 n; d$ G; R8 Z' N' A% c y2 n1 X. J, i- H
try {' t0 C7 N7 C9 Z# g+ b
modelActions.createActionTo$message
0 @' L! T: D- W! q (heat, new Selector (heat.getClass (), "updateLattice", false));
( X$ }+ Y& |& `4 | U& ?( l, s, b } catch (Exception e) {
- A' d" T) A7 r# N" b/ p0 A4 k( m System.err.println("Exception updateLattice: " + e.getMessage ());
2 @$ Q5 V @7 u) @/ l. X }& s1 S2 M- b- N- _$ T3 Z/ {
# z+ Y3 [( m V+ n/ d // Then we create a schedule that executes the
0 g9 k% d. L4 E: V' l2 Z( |3 c // modelActions. modelActions is an ActionGroup, by itself it
2 i3 g7 a% y& H S) ` // has no notion of time. In order to have it executed in4 [& X5 x3 R. q7 v: z3 Z0 H
// time, we create a Schedule that says to use the* d9 m% d& x, E' C! a9 I; W5 T" F+ R1 e
// modelActions ActionGroup at particular times. This
4 U* M3 j# }8 q+ Q0 T) w3 i // schedule has a repeat interval of 1, it will loop every6 N8 T2 O2 C4 q
// time step. The action is executed at time 0 relative to
6 U9 Y' R' r6 U5 L( n // the beginning of the loop.% l8 k3 N9 k- R C U
. V. j* m, E. i' z' U
// This is a simple schedule, with only one action that is
) Y$ Y1 `2 `; p: h // just repeated every time. See jmousetrap for more0 P; `5 L9 g2 |, c
// complicated schedules.( q ~$ m, P0 J. m" \% Y3 b9 @
, ^( O9 [; T j* S) T9 ]3 a
modelSchedule = new ScheduleImpl (getZone (), 1);
6 T" A# `- O# h. Z" H" \ modelSchedule.at$createAction (0, modelActions);0 ^; l! H: Q5 X1 l1 |$ q
' M& f1 r( d7 S' d8 M
return this;
* J k0 o- N- z) ]" y } |