HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: N# j: c# ~6 G- V' D
4 P) F1 W( L9 N public Object buildActions () {1 R" `' c4 o9 ^# I
super.buildActions();
& f( D( L& s6 U0 A3 l% G. m
) L0 C7 ]( b; z5 e // Create the list of simulation actions. We put these in
7 X$ n' B* { e5 ]8 t9 X // an action group, because we want these actions to be2 M( H# t+ a& c: o4 Q
// executed in a specific order, but these steps should) P, a8 o& X$ n( P
// take no (simulated) time. The M(foo) means "The message
+ O, B# t% l- E# r // called <foo>". You can send a message To a particular; N6 M* q* ]' Z" o2 Z- E
// object, or ForEach object in a collection.$ H5 h5 {: }3 L8 H* R; h
' i9 U# y+ r% e2 z
// Note we update the heatspace in two phases: first run* b! b |, N% U# o' j" `: Z @2 U d
// diffusion, then run "updateWorld" to actually enact the
3 ]2 H( T3 Q1 _: F& T8 \6 Q2 A+ }; M // changes the heatbugs have made. The ordering here is1 s- |" u5 h' ~) a& l2 K. X* C
// significant!
2 Y) f- W) O) ?- e/ q4 O) E
; l: y, P/ e6 u7 ]; i // Note also, that with the additional
, E8 Q( q6 @0 S( d5 w8 r3 ]! j // `randomizeHeatbugUpdateOrder' Boolean flag we can7 M. O6 y9 T I: g
// randomize the order in which the bugs actually run
1 K& y9 j+ y4 \5 r // their step rule. This has the effect of removing any) Q) E7 f5 q: b
// systematic bias in the iteration throught the heatbug
. Y6 N( E; f" r$ o% I5 q // list from timestep to timestep
2 ~2 x& Y+ r. [1 M 6 w6 \; C* m2 o( Q x' z
// By default, all `createActionForEach' modelActions have g" Y, b. G6 m/ b- U
// a default order of `Sequential', which means that the
2 z8 r/ I, K1 p4 B) `7 N6 S // order of iteration through the `heatbugList' will be% Q, k7 }6 q. M/ x
// identical (assuming the list order is not changed* ~8 [# Y) \- D: J6 ~9 Z4 g: k
// indirectly by some other process).
8 R: d- ~) c: T, _, ~1 A) s! B
# q( b0 L6 r$ P* V modelActions = new ActionGroupImpl (getZone ()); T/ d8 M w$ f1 \$ L4 f
7 f7 |* L( x5 w2 f try {
3 \1 n4 K4 D% R3 b; f8 f modelActions.createActionTo$message
3 `( {) l( {, E/ H (heat, new Selector (heat.getClass (), "stepRule", false));, J9 G+ n% O. e$ X; G
} catch (Exception e) {
) I' X+ u, O7 m. D: A. e System.err.println ("Exception stepRule: " + e.getMessage ());( L, F" p1 F+ u% g# L% o
}
" a) z5 k3 u& v1 n$ M
& X9 ?. ~! O6 T try {0 n5 w0 o. y) l- V W7 e0 S( y
Heatbug proto = (Heatbug) heatbugList.get (0);# m; w: P4 d7 ]8 s/ i3 J) F
Selector sel =
3 a; E' Z. O$ _, _# a, [ new Selector (proto.getClass (), "heatbugStep", false);
- z/ J' {9 o9 @4 R* [# n! \: y actionForEach =
1 X1 B" |" J1 Z( R) t6 @ modelActions.createFActionForEachHomogeneous$call
$ o- i8 v+ A$ x9 E/ [ (heatbugList,: I5 y& Y& j0 n) N1 k7 H, f
new FCallImpl (this, proto, sel,/ N5 ~; i! c+ U* f% A- j) c% V" z" G
new FArgumentsImpl (this, sel)));
, M5 E% |8 L- D5 c } catch (Exception e) {# E: W8 S: s8 U1 G, y, }
e.printStackTrace (System.err);( |' y) ~0 N( T& p4 Y
}
3 W/ Z+ f5 O5 h1 B4 t& o! w: |% G j3 V' ` G' U! j* H
syncUpdateOrder ();! X; l( S& e5 R. v) R
+ Y& i& R8 U" [) I0 ]4 L try {* [- {/ T" y, i) V
modelActions.createActionTo$message
. u: z' ~0 _7 N5 X (heat, new Selector (heat.getClass (), "updateLattice", false));( t6 `9 G' q1 Z6 T
} catch (Exception e) {
( H& s4 t" M( { System.err.println("Exception updateLattice: " + e.getMessage ());8 ^8 e) @0 n) {/ ?& {$ B6 e9 ?
}
+ R! Z9 l/ \: [4 k ; T9 g- Y8 A! k5 H6 X( B- c
// Then we create a schedule that executes the8 ^+ x g$ h+ v$ \$ ~- E4 f$ O; B3 W
// modelActions. modelActions is an ActionGroup, by itself it+ u- B4 L8 J* z/ |4 A
// has no notion of time. In order to have it executed in
1 j3 r8 K, M/ ^# E; ^ // time, we create a Schedule that says to use the( |! L8 K+ U2 I/ u2 Q$ s
// modelActions ActionGroup at particular times. This
; H4 R. x( T4 ^ // schedule has a repeat interval of 1, it will loop every9 ]: C! q2 L; w7 ?1 H; Y# ]
// time step. The action is executed at time 0 relative to
! r5 H+ J, {+ E5 l4 n1 r! y' _9 S+ n // the beginning of the loop.
& Z2 A7 a* d$ F8 J9 Y. J$ o% R- o7 D
3 j$ q7 n5 \* N // This is a simple schedule, with only one action that is
1 T. @* v; }$ g" b4 N // just repeated every time. See jmousetrap for more% [) Y# g1 [) n8 q$ f
// complicated schedules.' ^8 g9 K) [. @
6 y/ m$ z t' {* ~' V7 D5 |& q7 S
modelSchedule = new ScheduleImpl (getZone (), 1);
1 Q: f; I% B9 P4 f1 a5 d' r modelSchedule.at$createAction (0, modelActions);3 G& a4 f9 c S
1 b4 S/ t$ |% x, E4 E return this;+ h# e9 J1 {" ^. v6 y4 Y
} |