HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! O' _$ q& Q# ^
# A( H6 c5 f0 e' N" B/ e public Object buildActions () {
$ b! x P2 P& W, P Q+ C A super.buildActions(); C7 @% e9 q- w2 h7 `7 V! K3 T
$ R& e0 z, N9 [! U. K _* P
// Create the list of simulation actions. We put these in
; u9 P" Q; W0 l0 l- X/ C6 V // an action group, because we want these actions to be
9 w P$ }' r% Y+ [- h, q3 X, J' \5 A // executed in a specific order, but these steps should' M; |% p4 C; R, Y. l9 l& n$ f
// take no (simulated) time. The M(foo) means "The message4 ~) C7 x- S0 F
// called <foo>". You can send a message To a particular
: k7 N) D8 a& Y; O) ` // object, or ForEach object in a collection.
' Z2 a0 ]7 I5 a( a1 p
2 p" H& q9 t' i9 q W // Note we update the heatspace in two phases: first run3 b; G3 Z, s9 |
// diffusion, then run "updateWorld" to actually enact the
9 w* ]* }6 s+ S // changes the heatbugs have made. The ordering here is
9 N; F5 F. m( m // significant!
4 J' x; [% [2 x * \8 S. _; p0 ?! E7 v9 }( v# Y. ~
// Note also, that with the additional
5 M' q; e" P9 k- D( y5 O1 v7 e // `randomizeHeatbugUpdateOrder' Boolean flag we can
$ O* a! ~4 O+ L7 f- L+ [, e' D // randomize the order in which the bugs actually run
, E/ ?" U* n& D% p // their step rule. This has the effect of removing any
4 {( S8 U8 L, D; R' M2 |. { // systematic bias in the iteration throught the heatbug
- u. q. E' b7 [6 G // list from timestep to timestep
3 r3 y* X& M7 `4 R ' I3 A7 U3 z4 Y' E7 @' n
// By default, all `createActionForEach' modelActions have' ^: @4 z* b$ _# k7 H/ e5 h2 W
// a default order of `Sequential', which means that the
3 Y$ x7 H1 { K // order of iteration through the `heatbugList' will be
3 R8 o" }% D5 X$ V$ M // identical (assuming the list order is not changed
- t9 E3 S' l6 |, t // indirectly by some other process).
( N @9 ] C2 L0 B; r' s& V
" ~( v+ f7 S' ]; ] modelActions = new ActionGroupImpl (getZone ());
8 \" Y8 Q) {& f) I0 `$ Z
4 ^' `, v0 T' \ try {
K0 }2 J- e+ m( H Z modelActions.createActionTo$message$ z( ?! V1 H! a4 j. J! E
(heat, new Selector (heat.getClass (), "stepRule", false));; A. I0 g* M6 N+ d+ N
} catch (Exception e) {/ z8 G+ p- J3 N+ {) D
System.err.println ("Exception stepRule: " + e.getMessage ());
5 J- O8 K d {& G5 g4 m) o. Y }, g5 _2 {! V! H J9 k9 Z
2 L& M. A0 |( v+ y7 ] try {$ z/ T; u, e1 t! s) b. c9 o4 T
Heatbug proto = (Heatbug) heatbugList.get (0);
; } G4 {% t& _# Y Selector sel =
% g' l1 s Q! C new Selector (proto.getClass (), "heatbugStep", false);
* V$ I0 B. K# D% ^, P! f5 o actionForEach =
2 F2 F# Y; j8 ]$ W modelActions.createFActionForEachHomogeneous$call, h6 r$ R D( n
(heatbugList,
9 f b$ V. f1 A/ R. }- l- p8 B new FCallImpl (this, proto, sel,
* f% J9 Z1 u3 r) ^+ o new FArgumentsImpl (this, sel)));
+ [; E$ V$ Z. R } catch (Exception e) {# q+ {" _$ q [" K8 R. Y9 `5 `$ F, F
e.printStackTrace (System.err);) V6 D& z+ B# h# B
}
7 c& A* ]( ^3 U r! h8 C/ j' e
syncUpdateOrder ();
# M' E, s, K2 D2 b( E R0 I( }/ Q8 o0 L2 m- j7 w4 O
try {
. d+ {! ^$ j) w' p: j modelActions.createActionTo$message 4 ?& Z, S2 e% S) g0 r9 j
(heat, new Selector (heat.getClass (), "updateLattice", false));
9 v/ H' {$ f$ [2 J \/ Y& c } catch (Exception e) {5 A9 H, o" I! V0 @
System.err.println("Exception updateLattice: " + e.getMessage ());8 G8 Q0 @3 {8 ]
}
/ H4 t# B. p4 i* M. ~, O2 l 3 k% j3 j9 `( l, f9 {; c
// Then we create a schedule that executes the% f- f& b+ |0 c1 V' w
// modelActions. modelActions is an ActionGroup, by itself it8 K4 t& A3 ~# O# A) r( g( d
// has no notion of time. In order to have it executed in
5 P5 d2 @! \: [- X2 l // time, we create a Schedule that says to use the" T) c: F, T* s' m; K
// modelActions ActionGroup at particular times. This. [/ }, C& I- k
// schedule has a repeat interval of 1, it will loop every
$ l- @: V: D+ Z0 M; O8 \% d0 f // time step. The action is executed at time 0 relative to9 S( P8 V1 h$ T; W1 W
// the beginning of the loop.
3 G! ]+ A' f/ s9 B$ o" Y# c9 s$ O1 k2 W/ ]" f) `
// This is a simple schedule, with only one action that is
$ c. R! ~( x M3 W // just repeated every time. See jmousetrap for more/ ]0 x% b; Q+ k1 P
// complicated schedules.: f, z% C# V/ M# ~, G) k
4 m- f! g' R' m* M. E4 i
modelSchedule = new ScheduleImpl (getZone (), 1);5 H6 j# @9 U! T* _+ d. }! i/ T
modelSchedule.at$createAction (0, modelActions);. w" h8 c" m1 U9 i! p" ]
( _$ c! U3 @! w8 l* ?4 d& o, x return this;* T ~ x s3 k& _1 H4 p7 f/ n" |
} |