HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% ^ T9 N: b" e! c# K4 s
2 [; l7 |4 }" b6 n public Object buildActions () {8 p. O1 ~) w, ` T: E D5 ?; Y
super.buildActions();
_/ Q& d6 h- V3 D0 @5 W
/ G/ r1 q _( h8 N$ s& g5 p: W // Create the list of simulation actions. We put these in* s" M7 `4 l* s5 D5 }
// an action group, because we want these actions to be& W5 s. c: F$ y
// executed in a specific order, but these steps should
$ h6 v0 V' l+ n0 [0 g9 J // take no (simulated) time. The M(foo) means "The message" { t: k" S4 a
// called <foo>". You can send a message To a particular d9 y- I7 e/ t8 ^4 G% _' f1 A
// object, or ForEach object in a collection.4 F" J/ S6 H) [1 p- |' Z1 u
% L E0 `' e# s0 u" F. l8 s1 R // Note we update the heatspace in two phases: first run
) C+ n& ]2 G5 v3 x* ] // diffusion, then run "updateWorld" to actually enact the) {8 E+ U3 _* g( r1 m8 q
// changes the heatbugs have made. The ordering here is& j' \' v0 [' H
// significant!
8 P' c9 F: L1 v
3 B# B! ^4 W, e1 z& ] // Note also, that with the additional
% j) i4 F2 s+ @' Y // `randomizeHeatbugUpdateOrder' Boolean flag we can
; Y* \0 v d; g // randomize the order in which the bugs actually run' S! ?3 R; \* V1 K
// their step rule. This has the effect of removing any
9 I5 X# t7 j" ?& R+ T) Z // systematic bias in the iteration throught the heatbug f; o' ~/ ^9 F3 A0 n# U
// list from timestep to timestep; ^; l }2 a! s7 I6 d$ y2 B
* N$ h! _( C( l9 M // By default, all `createActionForEach' modelActions have
$ t2 Y$ X6 b1 H/ f6 X // a default order of `Sequential', which means that the- c. w( h. T3 q1 W
// order of iteration through the `heatbugList' will be
/ O/ I" `* v7 W4 W6 O& Q7 w9 e# w- O // identical (assuming the list order is not changed3 j3 E) L4 M9 L, S* N
// indirectly by some other process).
" D9 _) P2 t' l( z) G- \% v* L
1 a; D5 e4 V. [" j9 R8 l modelActions = new ActionGroupImpl (getZone ());/ I3 S# ?1 P; Y- b
; ?1 R9 f/ N3 R6 x7 [* D2 O try {6 W$ ^- j k5 K
modelActions.createActionTo$message
, M6 x. t- X6 d' W4 P6 Q9 k3 ?5 } (heat, new Selector (heat.getClass (), "stepRule", false));
5 I* ]8 S& N }% ?2 e2 Q3 u } catch (Exception e) {
' o1 A( q9 Q, K+ P7 ]6 W9 L System.err.println ("Exception stepRule: " + e.getMessage ());( t# G* y0 T1 p( |' Y% Z
}
# A) p: x+ b2 B" o, w4 X# N. b5 D! B
try {+ A# O7 N+ y3 ~/ g1 x- [, j- y
Heatbug proto = (Heatbug) heatbugList.get (0);
# d) d* q2 k9 j5 t Selector sel = % X- r! x# F' E7 [ G
new Selector (proto.getClass (), "heatbugStep", false);
( H, m1 H& g5 z! a% N" d* z3 t actionForEach =) U# j; s5 h/ j
modelActions.createFActionForEachHomogeneous$call! V) }* m8 D( y* y r# w# c: v
(heatbugList,5 v3 C$ a x5 d+ g
new FCallImpl (this, proto, sel,3 R+ `, L7 m, w% O0 v& @
new FArgumentsImpl (this, sel)));
) z- B @" k- [( W6 C. N } catch (Exception e) {4 C# S7 {+ R1 i& q
e.printStackTrace (System.err);
8 T, ?" w m) _" _/ g8 Q- C9 b8 W5 f }/ s' z q3 n; I
/ Q/ q. e9 k7 C* @: W4 \: A
syncUpdateOrder ();* @) { N/ w) }
8 Z9 Y2 H7 A, E# ]( d" G try {1 g* I7 H8 _* ?, Z3 k
modelActions.createActionTo$message
" T* f+ u6 L4 r! x; W (heat, new Selector (heat.getClass (), "updateLattice", false));
& e1 i' \: B( [. o$ @, B. i } catch (Exception e) {% C$ f6 a2 ?9 `: B+ U
System.err.println("Exception updateLattice: " + e.getMessage ());& ]: C! \( b/ |8 Z
} G6 B/ ~) B1 g2 `3 M
1 O0 X4 E2 x, {0 P9 D. [( Y
// Then we create a schedule that executes the
! B' K- N5 Y8 v# V& J0 K. ^+ {* M // modelActions. modelActions is an ActionGroup, by itself it3 R8 p8 x- V/ a8 Y/ K5 k
// has no notion of time. In order to have it executed in
* s; X4 [5 ]! g, A! B) B; a: U // time, we create a Schedule that says to use the; ]& B" v1 l9 s) M; `6 {
// modelActions ActionGroup at particular times. This
0 u# P7 \2 t4 u4 B' | // schedule has a repeat interval of 1, it will loop every+ `5 X2 C5 q. ?- N
// time step. The action is executed at time 0 relative to: W0 d( v9 ^& I+ \- Y; K# V
// the beginning of the loop. ^2 l0 T, C; G" a: [8 J' [
, ?7 Q+ }1 s3 {2 O* n7 k
// This is a simple schedule, with only one action that is- Y3 i5 j* K! y
// just repeated every time. See jmousetrap for more
/ @& s0 E/ P$ T0 ?/ L* e3 w // complicated schedules.. }3 J/ i, l$ A; _
' n, Y" W6 ^* f# z, q7 _5 s: t
modelSchedule = new ScheduleImpl (getZone (), 1);
; O6 N0 V0 I' O3 ~9 C modelSchedule.at$createAction (0, modelActions);
. W! c# R7 D; O7 S4 i ! B, C0 `0 i, H
return this;
/ N+ r. E0 @1 x* d* X } |