HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ T+ V# ]0 u$ }* }; T; L( P
& I3 D) @9 Y& w4 U7 y0 \8 l
public Object buildActions () {% @( m' N9 L/ @5 T+ A. X% {
super.buildActions();
& N, [# z$ }# l. g1 f& H4 P; D
5 b2 q, f9 l. P7 Q4 a* z$ H // Create the list of simulation actions. We put these in
) t2 O/ A# U: r# }2 t3 ~$ } // an action group, because we want these actions to be
$ I R3 \7 P& `' e9 N+ }$ G/ S+ B // executed in a specific order, but these steps should( b( H% K2 k( \
// take no (simulated) time. The M(foo) means "The message! a7 J; x* U% A& f
// called <foo>". You can send a message To a particular
5 E2 Q3 m9 D. F6 H2 ^) ~+ ]0 @' E // object, or ForEach object in a collection.
3 n1 D! Q0 h# I" ^: O + |# n I7 d; W7 U \
// Note we update the heatspace in two phases: first run" t4 w8 E% P4 e* Y
// diffusion, then run "updateWorld" to actually enact the1 O) C g' u7 v" r' F. [' m! [
// changes the heatbugs have made. The ordering here is
% w1 f% {0 J: g4 n- d // significant!: p6 d/ z0 M6 y) P2 U+ a( v
) F6 Y" |: n8 `& z' b
// Note also, that with the additional
- s3 k' U, m" [) M // `randomizeHeatbugUpdateOrder' Boolean flag we can* x" U, m' y" j
// randomize the order in which the bugs actually run
6 C, y- l3 U7 ?& W V- t& s1 x // their step rule. This has the effect of removing any# E* W1 s D$ H
// systematic bias in the iteration throught the heatbug6 H% z5 j: @% m: i* E+ I& d
// list from timestep to timestep
+ M9 @& e o9 A8 u: {* A + M2 D. m" J7 i7 i: ]
// By default, all `createActionForEach' modelActions have7 O- d1 m7 K& F5 v* T' i4 d
// a default order of `Sequential', which means that the+ g; X; S7 K m5 G: N
// order of iteration through the `heatbugList' will be$ Z$ ?, Q5 }$ z' }$ J# v5 D
// identical (assuming the list order is not changed) C* _' H7 T$ m+ C. i0 `
// indirectly by some other process).
) l( r' \5 S# J ) \% `+ k. a, S' C
modelActions = new ActionGroupImpl (getZone ());
9 U& d( P$ \3 J+ d6 j7 Z# P+ _& }. T; A* l- @) L" p1 b
try {7 u& x9 O, g5 ~6 I
modelActions.createActionTo$message( d, x/ h1 f8 m9 X- w' o
(heat, new Selector (heat.getClass (), "stepRule", false));2 x$ P5 [: D2 O; A) Z' F& N! C
} catch (Exception e) {/ G6 R: L" a; x6 K& z0 |: B
System.err.println ("Exception stepRule: " + e.getMessage ());. F4 p0 D* s$ }7 X% f+ ?) \
}
1 C3 b- A4 n" Y" L0 k+ s
+ K& `& {/ C+ K! z( _0 r, Q Q4 J try {) M W/ f8 z; X& X9 E' r7 W
Heatbug proto = (Heatbug) heatbugList.get (0);
0 k' |6 W& _" I" N4 f7 A$ ~: p Selector sel =
! V8 e% V$ ^3 ^' Z, h4 x O new Selector (proto.getClass (), "heatbugStep", false);4 b% \9 Q% x4 F j1 A# @% K
actionForEach =2 H; P7 K( z U7 {: R# r" s7 i
modelActions.createFActionForEachHomogeneous$call
4 M8 b9 X, ~5 i' `5 y$ d+ w: u2 C (heatbugList," h% C% c1 Z m* `$ e& K i$ e B
new FCallImpl (this, proto, sel,
8 f- k t* V5 S! A/ q( ?( \6 C0 d new FArgumentsImpl (this, sel)));. N0 K% l4 L/ E
} catch (Exception e) {+ q* ] T* R, d1 l) m
e.printStackTrace (System.err);
* f2 m' B) c- E0 `4 c2 u }
# Z% z$ I9 Z5 [& q$ g
' V7 y' y. X6 @9 a( p syncUpdateOrder ();
4 k' f3 A: |: m% D2 g9 m
+ J9 e, y9 c9 S try {
9 X8 A3 U4 a4 _! @* T, E modelActions.createActionTo$message 4 ?1 P# F" J& K. Q, ], C
(heat, new Selector (heat.getClass (), "updateLattice", false));( ]" u- W, J: I5 d+ z, Y( ?& A
} catch (Exception e) {, d9 N+ ]. q$ v! _" z* w
System.err.println("Exception updateLattice: " + e.getMessage ());( d, \7 u# p& b$ k+ Y) l0 U3 U
}
3 |6 E% N; n( F4 D- y( W1 y
% b5 |# `3 D+ K) b; b% f // Then we create a schedule that executes the0 @! x2 |5 |# N- Q
// modelActions. modelActions is an ActionGroup, by itself it
# s/ J; L$ T3 N; b0 h // has no notion of time. In order to have it executed in
\7 d5 L" o$ l, ^& } // time, we create a Schedule that says to use the! T/ m) K& u& \ `: O
// modelActions ActionGroup at particular times. This
9 P2 U% X( i6 |/ V // schedule has a repeat interval of 1, it will loop every
! e% N, R+ E1 s: X- g D4 q // time step. The action is executed at time 0 relative to
3 t& I% _% k* P0 W4 H // the beginning of the loop.% x# z* S) _+ a" w% w2 U" h& y
6 ~( e& n% n0 C0 q: k
// This is a simple schedule, with only one action that is+ E3 l0 K: i' u7 p; ~- _8 R1 w U
// just repeated every time. See jmousetrap for more" V6 v5 y9 R5 s2 ` F% O. q$ u+ [
// complicated schedules." H& a" [" `) c
3 g8 U1 d6 q% E5 {8 c5 z* x& y modelSchedule = new ScheduleImpl (getZone (), 1);
" e( {: P7 e, o. } modelSchedule.at$createAction (0, modelActions);
7 o$ n8 ~8 F; @ |- ] 9 g, C: q! O1 l
return this;
/ L9 l6 C/ [' g- i/ \ _6 Z, A; m } |