HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 X% ^" |( ~" X2 {$ u' W* n G& X1 H- }5 Y# }5 m- z
public Object buildActions () {6 U9 M+ |$ z; p' g+ H
super.buildActions();. u3 R4 A5 }/ F2 ?# q. @5 I
' Y1 [* H( Y3 H3 X7 j
// Create the list of simulation actions. We put these in" G s8 U7 Q+ Q8 C
// an action group, because we want these actions to be
5 j# u" N/ _& u2 v7 Y: `1 N // executed in a specific order, but these steps should1 @- x7 z4 o/ ^) w' \( ^2 y
// take no (simulated) time. The M(foo) means "The message/ C6 F: d* p5 k% _/ G/ W- \
// called <foo>". You can send a message To a particular/ K! @( m& a X/ W. n# }
// object, or ForEach object in a collection. `0 t! V6 e8 F) y2 m0 T
5 N/ A4 ]4 k- l
// Note we update the heatspace in two phases: first run
e7 [2 o9 ^3 k7 M // diffusion, then run "updateWorld" to actually enact the2 Y0 q5 m/ C$ B3 b
// changes the heatbugs have made. The ordering here is
. Z7 R' V& m$ s // significant!) ~9 a" P3 |3 E
" x# Q \! j8 @+ g+ G' T: Q
// Note also, that with the additional
) K( i: z8 B/ x' c // `randomizeHeatbugUpdateOrder' Boolean flag we can
' K' K3 T, r5 B7 n // randomize the order in which the bugs actually run
6 K6 x' A, c2 x: R7 ^% C( x // their step rule. This has the effect of removing any
[6 r1 g+ q) b, u" b* Z5 N. d% v // systematic bias in the iteration throught the heatbug
$ i0 z, q# `" N // list from timestep to timestep
: q( u" S% L' x, n( N; d 7 Z# Z2 f9 d! i
// By default, all `createActionForEach' modelActions have
R& g1 v+ X" i# |, T4 U+ U // a default order of `Sequential', which means that the
9 }+ }2 z! q* b) N0 l- @ // order of iteration through the `heatbugList' will be( v8 u: Q9 S/ x5 H
// identical (assuming the list order is not changed
9 Y" S8 j) F) n- O" z" O // indirectly by some other process).
) G9 F& E" O4 y' p; I
5 s" C9 C u) d9 n& m2 r modelActions = new ActionGroupImpl (getZone ());) Q- t6 P. B. U9 B# X" I9 J9 C
6 @% o7 F# r9 T5 |
try {* G2 z) V8 n3 N) d
modelActions.createActionTo$message* K/ K7 _7 b( k: N3 o' [7 F3 o: k
(heat, new Selector (heat.getClass (), "stepRule", false));" `; b, L- G% o2 p0 e: f
} catch (Exception e) {
+ M% v7 E! K/ _. m System.err.println ("Exception stepRule: " + e.getMessage ());
/ d9 Y2 C5 k, z0 C# W5 O; {! q# Z/ T }
2 D# O. E( ~* D8 y8 ^; l$ L, w0 o$ A% g! ]' z+ _4 P: q
try {% u( n! @4 y& B$ x$ W- c: g
Heatbug proto = (Heatbug) heatbugList.get (0);- h& Y5 a2 U; V5 \& r8 B
Selector sel = 1 o4 ^) G6 [% {5 e; r1 j& m3 U
new Selector (proto.getClass (), "heatbugStep", false);
& m! ~7 P& }4 I" c1 e9 E$ G actionForEach =
# i8 J* ^4 k/ u" J7 j6 v modelActions.createFActionForEachHomogeneous$call
% h ~6 C. \" {$ i' @0 X; u. E+ Z (heatbugList,; x# r; |8 p9 d
new FCallImpl (this, proto, sel," S/ I1 v: e, `" O3 ]
new FArgumentsImpl (this, sel)));
4 C# s9 ]8 U0 X% Q5 b$ k } catch (Exception e) {
( ~0 B$ }6 b- Y' v N; R' \+ _2 f e.printStackTrace (System.err);
6 X+ J x" _1 ~3 O$ I5 D( a }# Q/ Z7 r9 M% O* C
! B$ s3 u7 U9 b/ J
syncUpdateOrder ();$ R! p3 s2 V/ G0 Z7 e( \6 t
: l8 d2 x: G& u& X6 }
try {
% P1 H2 b$ N- t" M5 V modelActions.createActionTo$message 2 G0 S7 n4 q! S s1 W
(heat, new Selector (heat.getClass (), "updateLattice", false));" X% H' q/ \$ O0 u
} catch (Exception e) {
5 n0 b8 R& E1 m) Y. _) p% U; A! ? System.err.println("Exception updateLattice: " + e.getMessage ());1 H5 ?7 @6 v# y! J- N/ |7 V
}
) e# T" w2 }; ~& B9 G7 `
% B4 F+ l/ B% ? // Then we create a schedule that executes the/ ]$ r* W0 |2 {8 o0 a
// modelActions. modelActions is an ActionGroup, by itself it
- [2 m( f% j4 O // has no notion of time. In order to have it executed in
8 n, N; v- H$ J8 Y3 N // time, we create a Schedule that says to use the9 {9 a% ?' O' J$ R" P
// modelActions ActionGroup at particular times. This
) n# Y2 o* N7 c1 `* X0 f // schedule has a repeat interval of 1, it will loop every) D% s& @/ c, }0 ]8 ?3 C; S
// time step. The action is executed at time 0 relative to: i& p/ f1 z7 G
// the beginning of the loop.4 ]5 ?+ U) I! w
+ ]+ v: z P3 k( q0 N; o' T1 U // This is a simple schedule, with only one action that is c" y9 ?8 g. |' `. k1 v
// just repeated every time. See jmousetrap for more
" e) f( _6 t3 e // complicated schedules.
* Y: M3 W- ]+ q2 U+ i C
- X# v+ s. |& K5 U modelSchedule = new ScheduleImpl (getZone (), 1);
9 j1 i5 ?) t1 L modelSchedule.at$createAction (0, modelActions);
% i$ b4 ^1 X6 S6 S8 H
$ ]) @% |# T7 P7 r. u return this;+ M7 W2 v: s6 v7 G
} |