HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; q$ j5 B3 o3 [: T
8 V; O% S/ _: v1 n public Object buildActions () {
7 \7 G. W2 \( T1 F! }: J super.buildActions();3 C# V/ u1 |; W
/ @6 l7 X: ]& V' E9 J' G$ b: b3 |
// Create the list of simulation actions. We put these in
$ P/ L) s! o- D: C* @9 u f // an action group, because we want these actions to be
; K8 z: Q" S+ R8 d" k$ r/ r // executed in a specific order, but these steps should. G2 C3 A2 B9 N) S- d3 M. K9 O
// take no (simulated) time. The M(foo) means "The message
% S4 k* x0 Y3 E6 z% o // called <foo>". You can send a message To a particular9 Z* f0 d* o4 _+ ]+ x3 Z
// object, or ForEach object in a collection.
! H" k: s ]2 z
# ^; x8 f. z9 o // Note we update the heatspace in two phases: first run
$ M% p1 c. {+ s2 p0 p( H5 k // diffusion, then run "updateWorld" to actually enact the
) U( X/ v' m) n8 b" ^/ [ // changes the heatbugs have made. The ordering here is: E( Y* _+ K. g
// significant!
0 j* N- u) y) q. `3 S+ r" g, X & A& P' A G0 n+ S0 p9 d4 a% ^
// Note also, that with the additional0 X: J. e5 X1 b* d9 I+ f
// `randomizeHeatbugUpdateOrder' Boolean flag we can. H4 u. S5 t& L) t
// randomize the order in which the bugs actually run( n2 }$ x/ ?/ @# m
// their step rule. This has the effect of removing any
- e' J3 d& a- B% [* j // systematic bias in the iteration throught the heatbug2 m% z6 v) Y9 S
// list from timestep to timestep
6 j5 _) T( }2 ]2 a 2 c/ V$ c2 b9 o. @, Y. l
// By default, all `createActionForEach' modelActions have& s" L. i& U% X5 f
// a default order of `Sequential', which means that the& Y* z$ P( s; }' o# o' Y
// order of iteration through the `heatbugList' will be
/ r# y( V! N7 } // identical (assuming the list order is not changed
2 N' u3 ~1 d& J // indirectly by some other process).% T/ Q4 q" V, d+ ^; M: U
+ z2 O3 N$ Q( z8 ]" P# d4 N modelActions = new ActionGroupImpl (getZone ());
t2 u, N& A9 B" R( d5 J/ W' v5 v* X5 F3 z9 u: V
try {
p1 \( R" n/ z( @: x modelActions.createActionTo$message
/ g( I1 r4 }2 h! l1 M$ E8 { (heat, new Selector (heat.getClass (), "stepRule", false));
/ A" C2 @! a5 }. h5 }2 f: L0 a& Z; s4 q } catch (Exception e) {* E1 k: F9 m' K5 L
System.err.println ("Exception stepRule: " + e.getMessage ());
6 B1 v$ g9 {7 C. t6 v, d }2 r# j; t+ Z! ^- Q. W, D- ]
) Y) \; r. D% D; t5 L7 z% O# G
try {
- G2 q T$ l7 w3 ^( I \/ @$ O+ T0 R/ G Heatbug proto = (Heatbug) heatbugList.get (0);# E4 d5 x7 j0 ?: ?0 x E" ]' Z
Selector sel =
8 C2 U/ S6 y5 M) ]$ o( i. q new Selector (proto.getClass (), "heatbugStep", false);
& X0 ^8 Y# ?6 L ]9 z& I actionForEach =
1 o/ |/ {2 G% I* i9 Q& M8 } modelActions.createFActionForEachHomogeneous$call! U, { u0 z+ I! Y5 M0 N
(heatbugList,# a$ d' w, L1 j
new FCallImpl (this, proto, sel,
l1 T9 t& w5 E3 N. l5 V" w/ K new FArgumentsImpl (this, sel)));- R i$ Z, J, N7 t( d$ n2 Z6 ~
} catch (Exception e) { w" T) y s7 u4 d% {3 G
e.printStackTrace (System.err);$ @2 t3 ]0 P* r. B) F% N6 R
}8 Z9 A3 F, m" j2 v6 ~1 {
8 o) F" D: c4 c1 ] syncUpdateOrder ();
- S$ }1 d0 m. b! M4 u2 B
( U1 K' e6 B# H7 s% U( v$ X try {! C# @ n: g( u( ], [1 ]" U
modelActions.createActionTo$message
) C% y8 y' f y (heat, new Selector (heat.getClass (), "updateLattice", false));3 T7 h. e% s( i; N+ I0 y8 {- K$ v+ S
} catch (Exception e) {9 s/ f# d: z m# k( ^# P8 q4 Z
System.err.println("Exception updateLattice: " + e.getMessage ());
! e) H) y- {2 a& v }* _: O4 h6 D$ w' B) a- j
3 t$ j+ _# y, h& }" M* a6 E
// Then we create a schedule that executes the
" x. o4 B. o. S# F& O( N% X; E // modelActions. modelActions is an ActionGroup, by itself it
5 `, z9 _% w# g; T0 \3 B // has no notion of time. In order to have it executed in- m) H& v' c( n, V$ P6 @
// time, we create a Schedule that says to use the
' k+ ]- A; V: v // modelActions ActionGroup at particular times. This- m- l4 I0 f: `% d" f/ H) `
// schedule has a repeat interval of 1, it will loop every
. N2 y8 h& P8 A- l5 ]: s // time step. The action is executed at time 0 relative to
* H4 S" T) a' ?8 b9 |2 n) v+ ^0 E // the beginning of the loop.3 S6 g9 Q; ], u" |. a& N/ _8 t3 C
" Q3 w: J; r/ ?- J
// This is a simple schedule, with only one action that is
, l5 A- I* ^2 e9 k) \: {) n! D // just repeated every time. See jmousetrap for more; X1 l A4 b$ Y) J. i: V( E
// complicated schedules.
: ?% a" n% A4 V* {% {, V- X2 J0 x5 E ' M( [4 ]2 C. q9 y; h
modelSchedule = new ScheduleImpl (getZone (), 1);
3 Z/ {/ a5 f. A& F3 I modelSchedule.at$createAction (0, modelActions);0 }' A4 x( Y( P7 z
. ]! [( P: s |* A/ }6 V/ d: q return this;
6 E. b3 |0 b. E/ d& ]8 | } |