HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* f0 J& K: @/ \& ]9 C
) c3 F7 q, f" E, c* x, }+ y3 { public Object buildActions () {
# ]( _) \! C) J; k super.buildActions();) U# e2 T+ L1 O% J
9 _* ?/ q- A6 v7 b) U // Create the list of simulation actions. We put these in
, e |* Y0 X2 m // an action group, because we want these actions to be
7 t/ h7 x" L# V* f3 ^- P+ l3 t // executed in a specific order, but these steps should) ~; S9 ^1 ~0 L1 J/ k$ s
// take no (simulated) time. The M(foo) means "The message
& ^4 j2 \) S- C) F* K m' O+ I1 T // called <foo>". You can send a message To a particular
3 q4 b9 P1 b5 _: a9 s4 Q // object, or ForEach object in a collection.
: v( x" j) A2 l2 g, {$ e
1 Q8 v t' E7 N% h5 T# Y // Note we update the heatspace in two phases: first run% v% _" P3 |, i, W
// diffusion, then run "updateWorld" to actually enact the
1 [( W, I; m$ g. x) p5 u, @ // changes the heatbugs have made. The ordering here is
2 h0 B% d6 ?) \' G // significant!+ k; J& O+ R- {0 x1 m- }: C/ a% [6 S
7 M. E {/ W% k3 a2 _: j // Note also, that with the additional# |$ ~, Z1 o7 }8 D
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& k. ~: D- K# e* N1 Q# `6 @- H- e // randomize the order in which the bugs actually run& f1 p+ t7 e6 u, ^3 ^ k5 K
// their step rule. This has the effect of removing any
1 b: Z1 c! e$ \# \5 f // systematic bias in the iteration throught the heatbug$ z* F h/ P* |) s$ T( L6 ]2 k* v) E
// list from timestep to timestep
Z& ]: d; B0 [% J: v. I0 l" g# O8 ?! h
8 s) f4 q# q9 J% q) M: d6 p // By default, all `createActionForEach' modelActions have( v) \* w0 ]4 ?" D4 U
// a default order of `Sequential', which means that the
" j- ?- t' @8 W# {5 H) I // order of iteration through the `heatbugList' will be1 y& C+ K+ E$ ~5 n& d& Y& `5 V" q
// identical (assuming the list order is not changed% ~3 {8 Y6 R' b* k! u: N( m- Z
// indirectly by some other process).
& E& s4 j8 u: W- q5 Z5 r1 {2 X% x 0 G: ]7 n% D/ W
modelActions = new ActionGroupImpl (getZone ()); T) o/ m. P9 }) _6 t( `
, K$ V0 s1 k7 p8 U+ P- t
try {
- d4 r3 J& D5 N5 k$ K2 s6 q modelActions.createActionTo$message
% W- @! w" G8 t( s. Z. G9 z (heat, new Selector (heat.getClass (), "stepRule", false));& {/ A& v8 l1 X
} catch (Exception e) {4 F: A7 ~$ u4 r5 z. n7 M# u8 e5 R0 G
System.err.println ("Exception stepRule: " + e.getMessage ());
. m7 ]& f8 R) } }. Z4 M4 O& i0 y' h& m8 c$ X
/ j" R% D% g/ G. {: I
try {
& M; R1 e5 H* T3 m! ]5 y1 ^% Z Heatbug proto = (Heatbug) heatbugList.get (0);
8 j+ x& Q4 M; _: i Selector sel = 2 y) t# A, s C% z) f
new Selector (proto.getClass (), "heatbugStep", false);7 r7 M1 W$ U1 }' F/ q# ~
actionForEach =
" g) q R; Q, F, X, S modelActions.createFActionForEachHomogeneous$call
8 k0 i8 _- x# W1 Q ^ (heatbugList,
2 C* C8 n) ~# R, x5 [1 H \& X new FCallImpl (this, proto, sel,9 H" N3 h/ O4 N/ ~" W( [; `
new FArgumentsImpl (this, sel)));
% b- w3 w9 y' m5 Z( E4 k1 G } catch (Exception e) {2 ^5 W1 D( A$ h& k z9 F+ J
e.printStackTrace (System.err);8 o) _1 M+ c2 j
}
6 B3 T: c: \- }" j- k! `* X
# W% g* d0 B; Y" e syncUpdateOrder ();, W% i& q- p ^' o3 g/ z
# |9 }' R$ m- o( C) w! W; V try {
$ S1 t0 @6 W5 ^0 ?4 e p8 J modelActions.createActionTo$message
1 l$ e H1 D0 @: M$ E( i (heat, new Selector (heat.getClass (), "updateLattice", false));
3 q* p( B b$ q) k: c3 P } catch (Exception e) {
7 ^) v* a0 d; v4 r7 b1 ]8 F System.err.println("Exception updateLattice: " + e.getMessage ());6 `9 n8 o2 Q. p7 M7 ^
}" `. U! b* @2 c, D, ]0 A
8 k" V) x' r; D5 x. _0 c4 h
// Then we create a schedule that executes the+ S9 o0 A7 F+ ]7 V' Z/ A
// modelActions. modelActions is an ActionGroup, by itself it
" l; h4 N3 [1 H4 V // has no notion of time. In order to have it executed in
1 s8 B$ X L5 y: @% F% p1 | // time, we create a Schedule that says to use the
4 s& ^ f2 q2 S7 L3 p; E8 L. [ // modelActions ActionGroup at particular times. This u5 r1 H+ h' D3 i9 V: r
// schedule has a repeat interval of 1, it will loop every
! _) Q2 O( t/ k // time step. The action is executed at time 0 relative to0 X0 }0 R# |9 E% O+ o/ W" t
// the beginning of the loop.8 [0 Z. N7 z7 _, _+ r/ L% B
3 U$ z) ?7 D7 C* x9 M+ Q
// This is a simple schedule, with only one action that is' K1 \6 t i. X7 O$ n* w
// just repeated every time. See jmousetrap for more
# i) K9 [4 h) f% j // complicated schedules.
( I; n4 n, C7 S) a7 H" q& J 2 _: r0 B- E' ~7 @8 G
modelSchedule = new ScheduleImpl (getZone (), 1);
9 i4 R4 @, e" R. K modelSchedule.at$createAction (0, modelActions);, X/ g( k: [/ {& `( z$ }; w. E9 U" l
9 G1 u" ?$ f; ~* j
return this;' _: X! E3 m; s6 i* f# E$ [
} |