HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; v& [, H v/ {. z- i1 I
) A' d3 L$ Z3 f% I
public Object buildActions () {
0 L5 i' g7 a' c. j1 B super.buildActions();
# q8 f% y+ z6 y9 V: X( W, z# e
) B) d# } W( D) C // Create the list of simulation actions. We put these in
% e9 t; j, Y) Y6 V) f) d0 l // an action group, because we want these actions to be- ~7 y. K S) r
// executed in a specific order, but these steps should
' L* H0 P7 R% Y# f/ t# m) O; L // take no (simulated) time. The M(foo) means "The message
9 @ L' s2 q0 W p5 E // called <foo>". You can send a message To a particular9 z l: R/ d3 B% W3 D
// object, or ForEach object in a collection.9 w: C- K! I. u
5 x( v) M; _" @3 z: p# L
// Note we update the heatspace in two phases: first run4 t3 E2 {9 y3 e% i$ j
// diffusion, then run "updateWorld" to actually enact the
2 F3 t# c9 _: w // changes the heatbugs have made. The ordering here is' f8 V& O/ m$ W& T0 t* a! H& e
// significant!
% z3 n- W: ]$ x, z e' ^ 5 o8 s1 ]# V, i6 S# G5 ]& C
// Note also, that with the additional
! L1 B8 U+ F. N7 j" Y( s // `randomizeHeatbugUpdateOrder' Boolean flag we can5 ]; J$ Z% T1 @
// randomize the order in which the bugs actually run, G% U+ v. h& A% X9 q" a0 Q
// their step rule. This has the effect of removing any- X8 Y8 M8 { V6 ?. A
// systematic bias in the iteration throught the heatbug
' {# ]+ v! A. V- q9 Z: J0 _ // list from timestep to timestep
; o; }+ S( U2 w5 D $ o# N* J5 b5 b( O; ^1 Y
// By default, all `createActionForEach' modelActions have) t" J+ @4 ?! H) y4 M( u
// a default order of `Sequential', which means that the
" L) N5 ^( j8 t4 ~. {3 s // order of iteration through the `heatbugList' will be
" b$ N0 f' x& p: S# F0 P // identical (assuming the list order is not changed. m) C3 i( [( J( V
// indirectly by some other process).
8 j# u: K: G. a/ A T
| ^+ O- F1 \9 m- {* K modelActions = new ActionGroupImpl (getZone ());) R0 x; h- T7 A1 c0 l) o X! w
8 H' v g: o- [/ @ try {
- _# E; @+ V3 n# b, a modelActions.createActionTo$message
6 u0 @( N. n v+ L (heat, new Selector (heat.getClass (), "stepRule", false));1 P, \1 _7 `& {8 }
} catch (Exception e) {
2 ~( @6 ]2 f" }3 E& c' u* l System.err.println ("Exception stepRule: " + e.getMessage ());3 G4 X, l6 [1 u. j O! n, P
}
" I1 N" \4 k! `1 a
3 T! v! a- E$ P) c0 I6 Y; ^5 Z9 A try {- p; E' ~4 z7 c' G. A( U. U- C
Heatbug proto = (Heatbug) heatbugList.get (0);+ k- a+ H# N+ x! ~6 a3 j, ~& |
Selector sel =
9 u3 Z" z) X) W! w new Selector (proto.getClass (), "heatbugStep", false);1 R& K$ z, Y5 d) q* r/ e# E3 f
actionForEach =2 R2 H1 l7 C/ [1 h3 S
modelActions.createFActionForEachHomogeneous$call
8 _5 A7 q. n. z p6 ~1 @' B" k; { (heatbugList,& K5 z6 H0 c8 W8 U- q7 c
new FCallImpl (this, proto, sel,
0 t& B8 r" N1 a, }, F) z1 d# U new FArgumentsImpl (this, sel)));# I5 G _+ S* [' q& S
} catch (Exception e) {
; ?& p8 ?5 n V- ~1 w e.printStackTrace (System.err);
# \9 c$ a' y3 B0 T9 _- l& r) Z }$ Y- z# p* x& i4 `- f3 z9 n
" k- B4 w, k9 L syncUpdateOrder ();
0 @0 i. M1 p, V3 }9 i, p f6 C B) `+ j
try {+ ~( f$ \. ]1 ~- [5 N& P: Y
modelActions.createActionTo$message / ?- L& D$ Y8 \
(heat, new Selector (heat.getClass (), "updateLattice", false));$ |, x$ ~% J/ Z' n8 `, O/ P( [6 K
} catch (Exception e) {1 k4 m+ ~0 T3 B: E" _, d; O4 ^# j- E
System.err.println("Exception updateLattice: " + e.getMessage ());
H( B4 t( p3 W" Z5 a }4 O- a) W* H N" T8 F9 c) w, [3 [' W
# z& L( Q6 X9 d/ P% y( s
// Then we create a schedule that executes the
6 w6 E( K4 ^& @9 g' C- `- r, T, q // modelActions. modelActions is an ActionGroup, by itself it
, J- `5 ?0 b ~! M2 } // has no notion of time. In order to have it executed in! s- x! g' @8 H& k# \3 w7 D% l
// time, we create a Schedule that says to use the# Q, d" n/ B( ]) x7 U
// modelActions ActionGroup at particular times. This
: R: w J5 }, \2 D // schedule has a repeat interval of 1, it will loop every8 U9 j8 w* Z, d" O! K$ x$ K
// time step. The action is executed at time 0 relative to
* \8 R; \: P/ k // the beginning of the loop.- B) h9 t# ` I0 {* W
8 y$ f& T- X4 J0 Y1 ~) y( z: m
// This is a simple schedule, with only one action that is
+ e: x1 S: r6 @( \* D5 J% k& u1 e8 g6 K // just repeated every time. See jmousetrap for more& ]4 q: k B i. q
// complicated schedules.9 q2 m" M+ b4 {! f& C- z3 \% `8 D% O
7 M6 ~: [ x [# R& @' e- D) b modelSchedule = new ScheduleImpl (getZone (), 1);. u4 b( w2 p2 L# ?& y' e% I
modelSchedule.at$createAction (0, modelActions);' }5 i. g" j2 I- o- l& t+ k
$ e6 O$ k5 G% o$ ]8 I
return this;
7 j' G2 I9 l% `% q } |