HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# Y3 K7 ?0 y8 L; b
: }3 [' G$ J/ t, x; o public Object buildActions () {
: e& ~2 w8 o6 ^; J- c0 Q5 L super.buildActions();
2 X) q( m7 G; m0 w' u2 u; ~2 k) f
$ j# x I4 `3 l6 } // Create the list of simulation actions. We put these in
) ?- q! u( v+ L/ b // an action group, because we want these actions to be
- D' }6 q1 C% O5 \; u, T // executed in a specific order, but these steps should
. a8 w) m3 i; J( g w5 k // take no (simulated) time. The M(foo) means "The message- @/ N x g& o% {/ W
// called <foo>". You can send a message To a particular/ E6 V8 d0 t% f% S- E) K
// object, or ForEach object in a collection.
3 T/ t% s8 z; D 9 j! V. |/ a2 G
// Note we update the heatspace in two phases: first run. b" l& B6 f! _) b
// diffusion, then run "updateWorld" to actually enact the
' N# K" ]3 B+ v' J+ n0 I1 k% R7 N // changes the heatbugs have made. The ordering here is5 q+ @% C1 W% ]8 l% W5 W
// significant!
/ }$ a: k/ a: ?8 W & G) }- u* ?* @/ U p5 u: F" f
// Note also, that with the additional; I7 `8 f. s" M; j5 O4 i. }
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 }; u+ }) R4 K+ R, o
// randomize the order in which the bugs actually run
( `+ r2 ^4 Q0 e // their step rule. This has the effect of removing any
2 ?" y: {. k& l1 T* r2 j // systematic bias in the iteration throught the heatbug' X* I' o% ?3 U9 q2 J5 a2 T
// list from timestep to timestep
% f0 t, K# @% ^ R- u6 d
; h+ a9 W3 B/ g1 j! \ // By default, all `createActionForEach' modelActions have
, P* }) r$ y5 ?- G3 b // a default order of `Sequential', which means that the
# I! t$ x4 S8 Y // order of iteration through the `heatbugList' will be% b% w* S4 X D- m3 Y
// identical (assuming the list order is not changed
/ r1 d k1 k7 p/ v' m. B // indirectly by some other process)./ k w4 T* }1 Z2 l3 k; w' D6 N: C q& ]
% |5 q3 G9 ^! D- {
modelActions = new ActionGroupImpl (getZone ());* g6 O2 t- b: r; A: j
/ m6 ]9 N/ r) I& ?5 {9 R. Q' {
try {
/ b0 F( X4 h4 \( U* L" J modelActions.createActionTo$message }8 [. V- q2 w4 u. i; C( X! s) j; R/ Q
(heat, new Selector (heat.getClass (), "stepRule", false));
* ]* N- Y3 y# s% n0 `8 f1 d } catch (Exception e) {5 O4 \ o7 x. D# v4 a& m* v1 d
System.err.println ("Exception stepRule: " + e.getMessage ());
+ U* E4 z4 u2 M# M, G* F }+ f" }% d2 b* c8 z& i
3 f/ i( Z) n1 {8 q9 E: Z' v" V try {# ?$ ]5 I" x: x" P7 T7 B0 Z
Heatbug proto = (Heatbug) heatbugList.get (0);% H( t, A H# T/ f$ `' q
Selector sel =
7 _; Y' R( f! i5 q3 Q9 q( Y new Selector (proto.getClass (), "heatbugStep", false);
* G5 R6 M( o: H& M5 j actionForEach =$ g( D& \$ S6 v/ l$ H
modelActions.createFActionForEachHomogeneous$call p* Q, d) Z. O; \
(heatbugList,
1 Z% }/ \; o I7 j. ]" k new FCallImpl (this, proto, sel,
; J! w& k; Z$ R" f new FArgumentsImpl (this, sel)));
" @" D( v; ~4 n# ?6 G: e } catch (Exception e) {7 ?& b4 ^# h! K9 {) z9 X
e.printStackTrace (System.err);/ h; N9 l; B: _+ ~
}4 B* {. D, ~7 k4 w
* r. @" c# Z0 A ?0 V3 u: a, D
syncUpdateOrder ();2 s) F r& h8 ~* p, o' }
" U7 K# Z; u" i) l
try {$ v- M; v1 T0 A* |. e6 E$ d3 R( ~
modelActions.createActionTo$message ) [. h4 e4 j. P% o" g5 {4 b
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 P9 E: ^4 Q8 R4 l( b } catch (Exception e) {
7 v" J" V; \ e2 w. O0 S5 D$ Z System.err.println("Exception updateLattice: " + e.getMessage ());
! n( @/ h& _0 y5 l, |# D/ w }& ~0 b. D3 o6 k, x6 u1 C0 P
4 ~: `( Y- D* |8 ^) `* C
// Then we create a schedule that executes the7 [5 v& Q/ d- R, K: ^& d
// modelActions. modelActions is an ActionGroup, by itself it0 ]. w; X9 n2 I4 x3 b3 t: z
// has no notion of time. In order to have it executed in
; j S( {8 j4 Z. b% w0 Y8 G // time, we create a Schedule that says to use the6 g7 d' h: q! d4 |. w4 e3 d
// modelActions ActionGroup at particular times. This. ` j* O2 U6 t* |' R/ ^# q
// schedule has a repeat interval of 1, it will loop every( z/ c" H6 s) Z
// time step. The action is executed at time 0 relative to$ D; R# {, k# @' O0 }% A
// the beginning of the loop.
3 E' b q: d# B
, {" v O/ z) _: [ // This is a simple schedule, with only one action that is
8 ~, x5 L0 J# J: X5 V) ~% @7 l // just repeated every time. See jmousetrap for more8 }( g C8 S# j- U8 F+ ?
// complicated schedules.
- h- A4 m7 O+ c9 _ Z
3 I% ?* Y# A" p modelSchedule = new ScheduleImpl (getZone (), 1);% L. a1 j! |. P+ K- l
modelSchedule.at$createAction (0, modelActions);
6 K( O) d- \4 k' ]: j5 B: B
. @* m' f* h& w return this;/ K! U6 T0 \5 _% ^' \4 r
} |