HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* U, |2 T3 {) o0 _6 |$ n
9 i( r8 V5 s1 H1 H public Object buildActions () {4 O+ _+ r% H+ _0 Y% `" |0 [8 Q9 q( Z
super.buildActions();
: G# A7 X2 {8 R 8 j$ K0 i! H1 s6 z/ q6 T
// Create the list of simulation actions. We put these in& E! s1 ?- K. s1 H4 p: H
// an action group, because we want these actions to be7 q% Y2 C& l; \' x% s! I
// executed in a specific order, but these steps should
% _/ N( O5 a7 e$ P/ ^4 |# F: v; r // take no (simulated) time. The M(foo) means "The message
- _$ M( \3 l4 ?/ _ // called <foo>". You can send a message To a particular
8 T/ S' x9 R3 d+ K3 x( T/ e& d // object, or ForEach object in a collection.4 q$ f8 F7 d* o
6 p, A1 K: P" c; M, d
// Note we update the heatspace in two phases: first run s8 j- y1 K% `2 b
// diffusion, then run "updateWorld" to actually enact the
?1 n! ?8 V: ?: z1 \3 F7 ^ // changes the heatbugs have made. The ordering here is
& M& W* H, F3 m // significant!$ S) I3 c( z' M3 ~% p; O
$ x9 D c% E$ U" o; @! U) i
// Note also, that with the additional& M1 Y9 O; T" f8 U1 x
// `randomizeHeatbugUpdateOrder' Boolean flag we can& y( O/ J3 |8 ?$ F9 w! [
// randomize the order in which the bugs actually run
2 o. V6 J. O# G" o3 y B // their step rule. This has the effect of removing any; q! ~2 D2 W+ k' \* r7 z
// systematic bias in the iteration throught the heatbug
, _0 M0 I, Y, W$ }: K // list from timestep to timestep8 [' J: X, Z" }$ n1 E
; Y- R! M2 K2 `3 n0 F& s4 c# E e // By default, all `createActionForEach' modelActions have
# J( _; F8 Y; X/ H& ? // a default order of `Sequential', which means that the
0 u! C. u) z* V& |5 P# X // order of iteration through the `heatbugList' will be
- F/ }2 q) Q6 N A9 e- u `( }7 g; G // identical (assuming the list order is not changed Q8 W' @( y9 D. A& G$ H+ t) `
// indirectly by some other process).
! o# \ ?2 _( T& M 0 D1 j5 U/ B( K, g* E
modelActions = new ActionGroupImpl (getZone ());5 `8 t9 Y* c) B4 W; Z: x7 f
) l3 l+ T- _4 ?8 w7 C% q
try {
- B$ y5 H' R# D2 V3 U; M% c, q R5 f& q modelActions.createActionTo$message
3 F; _1 J( U9 g5 w; r' b (heat, new Selector (heat.getClass (), "stepRule", false));" n4 ^! ]9 Q3 c( _% M, Z; s, o0 a
} catch (Exception e) {
$ h p1 {6 m* }" P System.err.println ("Exception stepRule: " + e.getMessage ());
1 q, Q& ]# @! A" S& Q& S }$ P5 v/ F8 a# k: Z; m/ V) @
7 i I: j. S& q5 j! ]) i5 u
try {
- z/ F( R' J; L% [. E5 g9 y. @7 F' F Heatbug proto = (Heatbug) heatbugList.get (0);) Y/ b, |# C; T
Selector sel =
& g A& s: z$ |' ~! R0 O1 T% x new Selector (proto.getClass (), "heatbugStep", false);& I6 A+ G/ K* @" }
actionForEach =
! Y0 t3 j: ?: A0 d- R" Q) J5 i modelActions.createFActionForEachHomogeneous$call
8 M% y) X2 J, A* _% C# u (heatbugList, o& Z: H- u" t0 f/ ?; e
new FCallImpl (this, proto, sel,
# k9 X2 F) z t; a new FArgumentsImpl (this, sel)));
1 U- b( H. r' O/ t } catch (Exception e) {
! D/ j( E5 u' l5 {9 D8 b e.printStackTrace (System.err);& ?$ |3 ~2 u0 r5 F/ V# m
}
! E5 a: S* r- Q3 A# j 3 C; L0 s4 v, R2 W* U; n8 ]2 |) r
syncUpdateOrder ();% ~' u y3 W+ T# h/ [/ q2 F
$ t* C b) j/ ~9 X! W! _
try {
# j3 }) R u, Q0 \- ? modelActions.createActionTo$message ' `+ k4 J4 l! u; k/ A! c
(heat, new Selector (heat.getClass (), "updateLattice", false));3 z$ W; u) {" e
} catch (Exception e) {, k8 r+ _7 x( l& Y! N
System.err.println("Exception updateLattice: " + e.getMessage ());
8 l2 e+ P b" _( m" T! ` }/ ~1 h3 _( t: s' J4 U& U
) g; b- W' Z; b+ z, c- ?0 H8 W // Then we create a schedule that executes the
! U0 S3 z: c9 @- K1 `; u) }& f1 T& i. s // modelActions. modelActions is an ActionGroup, by itself it
/ q& O( ?! Y/ |# k0 f // has no notion of time. In order to have it executed in' P; {- U( b. m4 ^. ^. y
// time, we create a Schedule that says to use the
b7 b( \% Y) y( T: g, J // modelActions ActionGroup at particular times. This
* u$ ?4 ]2 f5 @( D; O // schedule has a repeat interval of 1, it will loop every
6 p% S: B& @$ v // time step. The action is executed at time 0 relative to
" H1 Q/ n5 \) K) F // the beginning of the loop.
% E9 w, J7 h& h+ l
% ^* @8 U: X. h5 K4 M0 _ K // This is a simple schedule, with only one action that is
( w- n8 y b9 W7 q4 U) H: V' x* _ // just repeated every time. See jmousetrap for more1 T9 V% P5 y% F( N3 f1 u+ y
// complicated schedules.
* V4 _& e- A1 T; K7 Z ! N5 ]9 y3 b! k
modelSchedule = new ScheduleImpl (getZone (), 1); ~" C+ F. b/ }) v; g
modelSchedule.at$createAction (0, modelActions);$ {4 R5 D/ `5 u$ v
3 a4 C1 g: z4 G9 |3 w return this;" Z8 H7 [% S9 ?
} |