HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ \) u4 ^! q' {
$ Y0 P7 t+ E, M" O5 @; w) R' @
public Object buildActions () {
( J% k& {3 F+ u2 o! m/ c1 U. ` super.buildActions();! c0 m' j+ R1 R4 _- W
* x; ^* W# l" l0 J5 g
// Create the list of simulation actions. We put these in
* y7 W' M& V3 ~. Q- _ // an action group, because we want these actions to be
( T+ q3 N4 B! h0 Y1 J9 j* V // executed in a specific order, but these steps should9 |6 v% ` D/ e
// take no (simulated) time. The M(foo) means "The message% }) u0 a+ u+ S: _. B- B- g1 f
// called <foo>". You can send a message To a particular
( ^# ]& Z) z' v1 R: D2 W; Q // object, or ForEach object in a collection.9 O) U0 o6 s! g$ i$ X
% S4 C Z g9 \& O! q& w- C // Note we update the heatspace in two phases: first run% Y0 |; I+ L- `0 t% T
// diffusion, then run "updateWorld" to actually enact the [$ m3 w2 Q& q$ r2 t: w' Z
// changes the heatbugs have made. The ordering here is ?2 y5 \8 S) {
// significant!5 K& b6 n7 k' p8 J" h
" }7 a6 x4 V: h- F5 A; z7 C x" k // Note also, that with the additional
- w! i- H$ {/ t8 h: v$ _ // `randomizeHeatbugUpdateOrder' Boolean flag we can
0 F/ X- K" D2 Z% z/ z8 M5 ` // randomize the order in which the bugs actually run
/ [* P K! z2 L% @6 ]9 c+ g // their step rule. This has the effect of removing any" ~* H6 m% p6 G% X
// systematic bias in the iteration throught the heatbug
9 c5 N* I3 W7 U! X0 f // list from timestep to timestep3 d+ z4 h" W. ~# }
" @. @" v+ L" H3 h% D$ X
// By default, all `createActionForEach' modelActions have
2 z3 p) _% m5 d# S // a default order of `Sequential', which means that the
' }; w" k; ^) m! o1 B5 Y // order of iteration through the `heatbugList' will be
) r( T7 X6 m6 b$ L; @' U9 A // identical (assuming the list order is not changed
& o6 |* u! } y% E // indirectly by some other process).
: c, d- L0 c. H' M# N% z6 e5 b % r: {. v: r, y) M% v9 O
modelActions = new ActionGroupImpl (getZone ());
0 n2 c) Y' E4 _7 l7 O
( Y+ S- Z/ Y0 I2 j* c, D& R try {9 h1 C0 ^+ A/ }
modelActions.createActionTo$message
4 K8 U" G8 G8 v5 @3 K (heat, new Selector (heat.getClass (), "stepRule", false));5 [$ e, t4 [ g! Q
} catch (Exception e) {
6 {' O0 _6 {0 M& K System.err.println ("Exception stepRule: " + e.getMessage ());) U$ K4 w" d" z: Y) O
}
7 O0 {* C. B7 r% ]- ]# v6 T. g; g$ z% l; \. W3 f( @
try {9 q& d& X6 P# d+ l; R v0 I
Heatbug proto = (Heatbug) heatbugList.get (0);7 E3 O: |. t$ K. v* ^4 t
Selector sel =
{) R0 [' J# x" h) l new Selector (proto.getClass (), "heatbugStep", false);
7 U) z" N, y" B0 f+ R% j. ` actionForEach =& b) \% k5 q( `( P7 M
modelActions.createFActionForEachHomogeneous$call
: g1 ?- s7 [# V6 G4 G) G (heatbugList,* ?3 Z ^2 ~5 {& \4 s% U. c( q
new FCallImpl (this, proto, sel,' z8 X; S% Q6 P
new FArgumentsImpl (this, sel)));2 F( l- a% D- s, K
} catch (Exception e) {
) U) R1 I7 n6 J i9 J: X: `! U e.printStackTrace (System.err);/ i: k Q( z! w8 {
}
0 o9 g7 l- Y* a) u
, I" I: I( u- m; T1 j syncUpdateOrder ();
6 n& ]6 o8 @1 z0 S8 R! X! K" z, ?# B& q( T5 _
try {
& t2 _7 R& z. i) w8 I& J modelActions.createActionTo$message
$ _6 ]* q% E4 h. \* g (heat, new Selector (heat.getClass (), "updateLattice", false));
/ R' M- L3 A1 l& Y6 ~, y } catch (Exception e) {
2 l; _4 P/ u4 N8 \0 l. C! m System.err.println("Exception updateLattice: " + e.getMessage ());
9 W W, ~% O# {. E/ @ }' ]3 h. H: i. j1 {4 O7 Y) P
+ J i* X; M g: a* m; W
// Then we create a schedule that executes the
0 u9 W' g- I9 I1 p // modelActions. modelActions is an ActionGroup, by itself it1 Z5 Y& |0 G9 z+ o; _
// has no notion of time. In order to have it executed in; U. [2 P+ F. K8 G: W! d
// time, we create a Schedule that says to use the9 u: T6 j5 \- [* Z& f- o4 i
// modelActions ActionGroup at particular times. This$ |0 {3 r( O/ J. n
// schedule has a repeat interval of 1, it will loop every
0 i4 A/ s+ w4 B( Y1 ] // time step. The action is executed at time 0 relative to
, P0 j( P0 x5 L2 B! G( R( v // the beginning of the loop." |4 p, r. e; H
: b$ r& ^9 k7 P Q3 [2 e8 x // This is a simple schedule, with only one action that is0 @6 [& B+ [( [) w8 J5 D
// just repeated every time. See jmousetrap for more
& i1 v) a/ X' Y( Y2 N/ k // complicated schedules.) ~# I8 @9 t0 V9 e
5 Y2 k7 e! c U/ f3 J- ^ modelSchedule = new ScheduleImpl (getZone (), 1);
5 X6 V: u6 {: y7 O6 l& G; j modelSchedule.at$createAction (0, modelActions);
' }. } F3 t( J e6 h
1 b- p/ |/ _ N! [' U7 `6 N return this;
- Y8 _$ V$ i* Y6 J4 l6 `8 A( F3 t } |