HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( d+ J4 G4 a# s: s7 H0 M; W% M1 q _9 o
public Object buildActions () {
$ `* ]+ p( J. a super.buildActions();
6 `, Y& F5 `. ~$ _# a& T' V
, c8 z1 m5 F' U+ M2 a1 f // Create the list of simulation actions. We put these in
' p/ M/ W" D, w6 \+ C- a // an action group, because we want these actions to be9 J/ b9 W1 B9 ~8 U
// executed in a specific order, but these steps should- O6 t- O& B' C( m7 Z' w7 [
// take no (simulated) time. The M(foo) means "The message7 C8 L+ ^/ p+ u
// called <foo>". You can send a message To a particular% |2 ^& M1 `5 D% l8 k( n
// object, or ForEach object in a collection.
9 T& c, U! `# E$ W U+ n: L1 { 4 n: [4 u" F; i$ i A
// Note we update the heatspace in two phases: first run
9 Q5 p5 { r2 l8 a# ]6 ]* W1 D9 ~, [ // diffusion, then run "updateWorld" to actually enact the
7 ?$ o2 S7 M; m- \2 F8 P( W // changes the heatbugs have made. The ordering here is0 Y- R' t8 ]0 h3 r3 j# ]1 s
// significant!( a W1 @" a; J" R" Y$ k7 m
3 q1 }, I2 F6 f# _% i0 W6 S( S4 X, }% t
// Note also, that with the additional
7 l+ @: H6 X6 q& k% x // `randomizeHeatbugUpdateOrder' Boolean flag we can
% r" l) s& P- }7 y k // randomize the order in which the bugs actually run
' L9 _& a9 o+ _) |1 N* e: [; Y // their step rule. This has the effect of removing any
1 b# F" K: }9 \/ w // systematic bias in the iteration throught the heatbug
( e9 M' v6 Z& j! J6 f8 j: b* R9 V // list from timestep to timestep3 J+ j& v. x2 u! }: L+ J
- q8 h% }' u) h" K9 W, d, M // By default, all `createActionForEach' modelActions have
8 @" C0 ?. I9 h; h // a default order of `Sequential', which means that the
1 a4 f- h8 |4 I2 n$ V9 j; x // order of iteration through the `heatbugList' will be
1 e5 r4 g/ M3 U& G8 x5 Y3 O. z // identical (assuming the list order is not changed& v$ W% w: W4 Y5 B
// indirectly by some other process).
3 q1 K% Z. K* x8 r$ R: h
: W, u- K# n1 b modelActions = new ActionGroupImpl (getZone ());
6 P$ V2 @0 i. ^1 R" L6 X
( [. R: O$ L9 z3 C$ U( u% h1 S! E try {4 B j, A5 _4 r) ^- I
modelActions.createActionTo$message
+ K. z& j- I) }) j& l. e (heat, new Selector (heat.getClass (), "stepRule", false));. x* u) l" w7 R3 z" L
} catch (Exception e) {
% a" R+ j( v0 @- D8 {; b' g$ N System.err.println ("Exception stepRule: " + e.getMessage ());
0 u2 h- T8 G7 i3 t; d }
6 a% T5 l' O! N1 g5 [
. b" e' A* ]3 X& L try { d; T. R1 u. r J6 y9 l
Heatbug proto = (Heatbug) heatbugList.get (0);- M. T/ a2 p3 @4 `. C# D; o
Selector sel =
/ Y+ G1 K$ V; X/ b. l% V, p$ ]/ E) E new Selector (proto.getClass (), "heatbugStep", false);7 k' K; g& ? P& F+ E8 S: j
actionForEach =
& {1 }- b1 u$ h+ t modelActions.createFActionForEachHomogeneous$call, i, W" ~. Y2 [8 X7 ^5 h; ?
(heatbugList,8 ?7 X, m" v: C2 o
new FCallImpl (this, proto, sel,
, S, S' q& C& ?+ I, C7 @ new FArgumentsImpl (this, sel)));/ E9 c* G) }- A
} catch (Exception e) {: [3 I* U+ w) X c' E/ N0 M
e.printStackTrace (System.err);
2 C! ?) t- O6 _) E( [3 J; v9 h: D; I }
) R& [' L' ~# V2 y / z+ S$ T' ]. \0 c3 d- }
syncUpdateOrder ();2 _5 p x; u8 ^+ Z
, I2 m8 L; K1 i0 \9 d4 W1 g" t try {
! f5 \* G& U ^; k. J( c/ N- H modelActions.createActionTo$message
8 s! t" J# i; F* c (heat, new Selector (heat.getClass (), "updateLattice", false));
7 V4 q7 x, l# J4 {6 n } catch (Exception e) {
. J" f8 ]/ v, N' M0 w( a. C, Z System.err.println("Exception updateLattice: " + e.getMessage ());% [( z- U% p5 m3 i! [
}
4 @. ]1 ~; u1 N/ e& q " `! Y' g, r R8 N! e3 V
// Then we create a schedule that executes the
' F& ?6 K$ }. J6 x7 A, _ // modelActions. modelActions is an ActionGroup, by itself it
3 E1 U! T2 u- p& s! Z/ F ^$ C // has no notion of time. In order to have it executed in$ Y' l0 @. h6 n
// time, we create a Schedule that says to use the
. [4 l! U% K" a/ g2 ?8 y // modelActions ActionGroup at particular times. This
& k7 S% y2 Z6 s // schedule has a repeat interval of 1, it will loop every
, y7 X6 A3 k; W // time step. The action is executed at time 0 relative to
( Z0 r$ z) R* Q" b! b6 T# S // the beginning of the loop.' q. A, k- n8 f6 Y- B; o
. ?" N/ G" l. O. W( w A // This is a simple schedule, with only one action that is
, A3 ~+ t, U2 F2 U* C9 b8 Z // just repeated every time. See jmousetrap for more/ b6 ~$ |; N+ ?6 {! Z
// complicated schedules.- c+ |. ~+ g |- L; @" c4 _4 C. [$ R
" a. |) P6 T" X& G0 i! a; X& V
modelSchedule = new ScheduleImpl (getZone (), 1);
4 g B5 ~. L" |' E8 F+ t modelSchedule.at$createAction (0, modelActions);% ?" H7 j% S, T3 R8 s
( L, W" F' R) o4 f return this;) p! D. O% z4 j
} |