HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; w. Q3 f! `9 r+ M4 y6 Q/ x6 K! P. j3 E6 {# r5 }2 }
public Object buildActions () {
3 J) t; p; y0 X Q super.buildActions();
7 u4 o1 p! \* i X, L
7 h! Z% r9 X1 `3 h/ s // Create the list of simulation actions. We put these in
; h3 g0 V8 [% z$ J // an action group, because we want these actions to be7 Q* [& v- E/ s% s2 l$ T
// executed in a specific order, but these steps should( ^/ m- i0 k9 v
// take no (simulated) time. The M(foo) means "The message
& m% ~4 A1 f; z // called <foo>". You can send a message To a particular
8 [7 U/ Q7 d% P( ]% G // object, or ForEach object in a collection.
: T! }* M3 K' K- t( ]" K % U- V. q' w. M+ W
// Note we update the heatspace in two phases: first run
; w6 I9 i5 [" ~; M1 g // diffusion, then run "updateWorld" to actually enact the! S7 n, z' k5 O3 C3 L
// changes the heatbugs have made. The ordering here is
5 Z3 D& Z( o ?$ n+ } // significant!$ e& |! c( a G3 n
! X: g# E$ k3 I2 R8 u* B // Note also, that with the additional; [- h2 k/ l" J/ k$ O& q" _
// `randomizeHeatbugUpdateOrder' Boolean flag we can
+ y6 z4 P* X/ w // randomize the order in which the bugs actually run! A; m0 y" O" h: [0 t! k& ~, P
// their step rule. This has the effect of removing any% n8 J" ?3 f7 H% c
// systematic bias in the iteration throught the heatbug
B$ Q/ [$ O+ b$ j+ i // list from timestep to timestep
1 w7 L. L8 k! e, e9 o7 y 0 h2 e2 r' T. R5 J. R9 R; E
// By default, all `createActionForEach' modelActions have
- e1 b7 P: ^- s4 P- a9 Q // a default order of `Sequential', which means that the0 K( ^8 \' y( ~' @1 [+ R
// order of iteration through the `heatbugList' will be5 c2 y3 S {! Y; Z G
// identical (assuming the list order is not changed
6 f% y' J4 J' d k5 L) y // indirectly by some other process).
( ^' U6 d% B* e6 V
4 S5 }7 Q" ]: C2 @3 f9 D" B modelActions = new ActionGroupImpl (getZone ());
9 P. X+ Z6 d6 Z4 M
) S' Z6 Z. }) s try {7 N; l, \1 R! A
modelActions.createActionTo$message
6 u" D6 j6 H# w$ F: z7 i4 Z (heat, new Selector (heat.getClass (), "stepRule", false));
H9 H% H' b$ e3 g& @! d } catch (Exception e) {
, ?2 r0 Q4 J% N- O6 k& @& p System.err.println ("Exception stepRule: " + e.getMessage ());
7 _6 _$ F8 n0 e. x" A7 ^7 { }
9 Y( `& k# ?6 b* r8 Q7 P! n H+ R1 \3 ^9 @
try {3 d" q6 f% ?# f* C2 M+ r6 l" Y. Y
Heatbug proto = (Heatbug) heatbugList.get (0);: y3 i5 ]7 B6 r# @: q$ G
Selector sel =
- s- i2 M* C8 N$ ?* L new Selector (proto.getClass (), "heatbugStep", false);
- s# o, R5 Y& s* X) ^ actionForEach =
2 e- c: G; O$ s/ I- G |9 X3 K modelActions.createFActionForEachHomogeneous$call
8 x2 U. y2 N) `* F0 l+ D( Z (heatbugList,
% Q9 s. ?4 c r new FCallImpl (this, proto, sel,
" ]4 N' y9 e+ W* G H new FArgumentsImpl (this, sel)));
5 r6 B7 c& L7 m" p* p, w } catch (Exception e) {
8 \$ @4 L8 N8 p e.printStackTrace (System.err); k0 z( M- j9 ?# W- P3 g" i/ v: k! e
}
) S1 g5 I {! U; e 2 |5 i( _( o1 `
syncUpdateOrder ();8 [) @; A$ S; p& M
6 X" q& P+ T$ l9 V try {
) B1 ^/ H5 D: a% _8 w! A A. p modelActions.createActionTo$message $ E& B( t& U- f* B! Y5 Q: u- o
(heat, new Selector (heat.getClass (), "updateLattice", false));
6 h& n: ?9 l3 o6 o6 \$ r- `) P } catch (Exception e) {
6 H6 ]$ K+ |% x4 q: E2 C2 E2 V \ System.err.println("Exception updateLattice: " + e.getMessage ());, L9 x7 M% g" C" U# m
}
" o% O5 t, S1 k- k7 V! v, r/ @
$ M7 L7 N: k% p' F0 F5 ` // Then we create a schedule that executes the
% m/ ~' ^5 n9 M+ S // modelActions. modelActions is an ActionGroup, by itself it5 j8 G" h) P, Z0 B- r( p
// has no notion of time. In order to have it executed in
$ T+ w% f4 x: H$ X" ~ // time, we create a Schedule that says to use the
. ]' q( u, w9 A+ O' Y3 e7 \ // modelActions ActionGroup at particular times. This+ r. C: ?4 p( D( d' D9 d5 B6 @1 l
// schedule has a repeat interval of 1, it will loop every
0 r) [7 N+ j5 Z$ ?+ c // time step. The action is executed at time 0 relative to
( A- v: H$ ~2 W& l! h( w9 K7 C // the beginning of the loop.
1 O) U+ O8 v, \5 N' a
# t2 l$ }: w: b8 ~ // This is a simple schedule, with only one action that is3 N3 ?8 }# ~% C X- |3 W5 ~
// just repeated every time. See jmousetrap for more/ ]0 V" G9 L/ Q* C O: d4 a
// complicated schedules.7 a2 i6 B# b" L; U
- e" T8 t9 O1 D& [/ G, x modelSchedule = new ScheduleImpl (getZone (), 1);) k4 J* K; d6 H3 ~* f b2 L7 K I
modelSchedule.at$createAction (0, modelActions);( \+ C0 z$ N$ t7 }0 L: t) w" ]' D
. N. z- T6 w$ I$ N
return this;. q6 R$ H3 U0 f# K! b8 `9 e) Q
} |