HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" a; y9 \5 O3 R/ V* Q5 c
" a5 \. c- A; F! v9 z& D public Object buildActions () {
; R' a, ?2 o& S, U8 y- ] super.buildActions();- b% G9 Q! u' Q
8 f0 _8 s8 d. B1 X1 i; ^# R2 j // Create the list of simulation actions. We put these in9 y) d4 F# x/ m: x2 ^7 b
// an action group, because we want these actions to be
' r- ]8 l/ T2 ^6 T1 q$ B // executed in a specific order, but these steps should* A: A3 n, h1 U: V( \
// take no (simulated) time. The M(foo) means "The message
* `2 {6 {1 V+ } // called <foo>". You can send a message To a particular5 p/ E. t! }0 E' ~7 j9 a
// object, or ForEach object in a collection., a6 l8 n8 ]9 s! q* \; G" a$ \1 ~9 A
" z% S/ ^0 Y1 _5 }
// Note we update the heatspace in two phases: first run
3 ?7 T( _8 z6 M8 I! p // diffusion, then run "updateWorld" to actually enact the
1 \0 {; Q( e' v" C5 j // changes the heatbugs have made. The ordering here is
; @$ }' ~& p1 B+ d) g // significant!5 B3 i" _' [7 ?" L( A& f d6 w
' u0 W( j# ~3 h9 F U // Note also, that with the additional* m. E1 Z- E% C
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- q, C m9 d, k0 t // randomize the order in which the bugs actually run: k2 n% a6 F. T3 a, N3 G) t# C
// their step rule. This has the effect of removing any
8 C& o J0 }$ S6 [* p // systematic bias in the iteration throught the heatbug
; E3 s$ m# V+ T // list from timestep to timestep! y! ~7 I* _: q* B' h! E) F& ~+ S
, J' y9 S: E3 v6 z // By default, all `createActionForEach' modelActions have7 H w/ o- Y4 w+ u7 y, t
// a default order of `Sequential', which means that the
* }" D) H* v. p, ^8 H0 \' e4 ~6 X // order of iteration through the `heatbugList' will be' m4 D, e C- Z' m
// identical (assuming the list order is not changed
/ E0 x5 r" a& D3 P+ L // indirectly by some other process)., y* Q( t; r5 O1 z
0 K% M' n0 b7 S: J2 j5 s. u
modelActions = new ActionGroupImpl (getZone ());
! G' ~: p4 _* j0 j( {, M, `9 F. y5 [( d, T/ q& `# f" ]
try {7 c# R4 a$ L1 J5 C6 ]
modelActions.createActionTo$message
1 B, z* @" m- x (heat, new Selector (heat.getClass (), "stepRule", false));
! R& S. s( y6 T1 ? } catch (Exception e) {
9 a6 A& K1 f% D4 h1 I/ N System.err.println ("Exception stepRule: " + e.getMessage ());4 y: ], j; K: o0 f
}1 @+ T* k2 k5 T! X# d# c
7 O0 ~: e, I4 c9 O! T
try {
, t& |- Q/ _2 p# x Heatbug proto = (Heatbug) heatbugList.get (0);) T3 ]4 b% O, \% i( M- w, C3 ]
Selector sel =
) B9 `! w1 A4 a5 A" K new Selector (proto.getClass (), "heatbugStep", false);+ G R5 [5 A! {$ Y: m% n" \
actionForEach =
, N+ g/ L3 A, S8 n modelActions.createFActionForEachHomogeneous$call
1 S L. S0 t( X" g" K1 { (heatbugList,
1 r5 f( s* i e: T4 K) D new FCallImpl (this, proto, sel,
6 Y& E/ v! F! T7 D. m' M new FArgumentsImpl (this, sel)));
% _$ W- v' c6 a q5 B: B } catch (Exception e) {! F. o) k& f7 b1 J0 j: [
e.printStackTrace (System.err);
! ?" E" s( s0 V* Q8 | }' C3 z) H. S- W" r) s* f y
+ Q+ |- l4 b2 u' T" ` syncUpdateOrder ();+ W6 {' U, b a! S2 h( g
" D$ i5 L* r/ D, E8 j) \, A; V
try {' o8 C' [: F5 p( P# Z0 t+ ^# l
modelActions.createActionTo$message $ j: M( v4 ~8 z9 V4 a2 E
(heat, new Selector (heat.getClass (), "updateLattice", false));
( C7 m1 N) b$ z( ^ ~5 G& o } catch (Exception e) {
F, n2 H0 H* I3 Y, h System.err.println("Exception updateLattice: " + e.getMessage ());" B1 [8 g7 U/ N& i4 m
}/ a8 t, T* `" @; f1 x
4 g. A) Q3 w+ I! v: P, L! E" g2 ^ // Then we create a schedule that executes the* F- K @& M) q
// modelActions. modelActions is an ActionGroup, by itself it
7 f. O6 F. U1 \+ y // has no notion of time. In order to have it executed in" s2 ^7 O' G# e. c. ?- A
// time, we create a Schedule that says to use the
& C4 {& r, \* Q9 ~+ ` // modelActions ActionGroup at particular times. This
: B4 X0 h: B h" G // schedule has a repeat interval of 1, it will loop every
5 {4 M4 j0 r$ R) z( k // time step. The action is executed at time 0 relative to$ y) J$ k, M0 U% Q
// the beginning of the loop.
& p& H/ ^9 C' |' F5 u' ^
1 K' } T6 Y) k- C, s // This is a simple schedule, with only one action that is
/ L! W- | G8 G; N5 b" B. D; ^ // just repeated every time. See jmousetrap for more( O6 J( f4 j4 `, p4 T9 X* w
// complicated schedules.2 T2 g- ^; f; g6 R9 _
* Y6 k. C# \ d6 w modelSchedule = new ScheduleImpl (getZone (), 1);+ R. N/ Z. J" \5 [8 D1 a2 X$ ~7 O% |4 a: O
modelSchedule.at$createAction (0, modelActions);) I5 `7 B8 s( d8 `& ~2 Y W
; R& V/ C5 C# `' w0 X9 L! L6 b0 T
return this;3 _- t" F* e) l4 \) A% j8 I7 `
} |