HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 |: W1 j9 b9 L6 H X
6 m8 f/ ^; S. o; U3 u public Object buildActions () {
( B e( J- `& t+ G2 D( a$ t. U! t super.buildActions();
) l, [: c- Z: Y* x
) g! B) p# H# n1 I // Create the list of simulation actions. We put these in
0 k9 l' m( z8 @/ e% | // an action group, because we want these actions to be
# Q6 f# l* X6 r9 o. ^ H6 E // executed in a specific order, but these steps should$ l& y9 W& X4 _7 o
// take no (simulated) time. The M(foo) means "The message" S- T* q* p- H2 j3 b
// called <foo>". You can send a message To a particular
|4 B+ U' K7 h3 \( e5 E4 Q( z% y // object, or ForEach object in a collection./ \9 R' b: w* V- _. Y* L& D
& `' X. k9 Q5 n: w2 ^/ ~
// Note we update the heatspace in two phases: first run& N4 {/ S7 q4 R. G4 k& N
// diffusion, then run "updateWorld" to actually enact the; ]* [5 Q' i( e2 r
// changes the heatbugs have made. The ordering here is7 C: P- {/ f( [# b" }; A Q
// significant!! X8 z D: |5 r: M9 \) Q6 r
0 t6 z0 J9 W7 @3 u. F // Note also, that with the additional, d( c0 _% e% R, D
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& G& B/ P! B- ^6 Y* F // randomize the order in which the bugs actually run
' ], }8 ^2 C6 ~3 Q2 H5 Y+ _" Y! q6 c // their step rule. This has the effect of removing any1 ]0 G2 d& s0 I+ t5 s0 z: q
// systematic bias in the iteration throught the heatbug
7 H% @, `0 E, \4 N# `6 q // list from timestep to timestep
; Q2 A) \ F! Y+ e7 a
5 p4 c9 s; v" X+ l+ M // By default, all `createActionForEach' modelActions have
. _5 _# l, O- Y' i! Q$ Y // a default order of `Sequential', which means that the
+ J7 B; ^) J7 X7 C // order of iteration through the `heatbugList' will be' p, X% P; @6 \/ w9 j
// identical (assuming the list order is not changed
; q; r1 l4 }0 P4 w( u // indirectly by some other process).9 o% c! F; ]. c+ G4 c2 C
: M& k& X% T& i3 T5 w, a/ z" J
modelActions = new ActionGroupImpl (getZone ());
2 N1 h+ d5 }$ N' I; y3 u) ^* ]2 s$ |. \. _ k2 R$ L
try {& h7 \" u1 T @! G, c
modelActions.createActionTo$message
0 H& A- S+ H: t$ c5 V (heat, new Selector (heat.getClass (), "stepRule", false));
2 I3 _ E% {: d R0 c% z* l } catch (Exception e) {
/ j6 U( V7 _/ |- f" e8 d7 K System.err.println ("Exception stepRule: " + e.getMessage ());) K- I# g8 R3 ~/ |$ y% a4 i
}
2 p2 T6 Z4 L$ b9 I9 `; D
! t; T* M ~8 J% m try {1 a) e" k/ ?. U7 e
Heatbug proto = (Heatbug) heatbugList.get (0);
3 ^2 U5 ]: i' w Selector sel = " c I0 D# Z" j! Y7 |, h/ A& H$ [
new Selector (proto.getClass (), "heatbugStep", false);% v/ M0 D" J* A8 I$ b- ?
actionForEach =, W; f! q/ t a; m
modelActions.createFActionForEachHomogeneous$call
! W) q9 |' Z/ b (heatbugList,& m* R1 B# R( J
new FCallImpl (this, proto, sel,
5 U: i* }$ |# D new FArgumentsImpl (this, sel)));9 K7 _- G# S& N) E( c
} catch (Exception e) {8 l, f/ j8 g( q
e.printStackTrace (System.err);. V* t9 g9 d% _/ |
}
0 F* a) ^5 G7 K/ B' W8 o, v
5 v; s; r7 N8 m3 l% C syncUpdateOrder ();
0 b/ A/ X) e8 d. X& n
: C) x9 y+ z% r1 y1 y3 R try {
[. ]6 |) q7 ?+ w1 T: A modelActions.createActionTo$message
; |% D2 Z+ A5 W! x# D0 u (heat, new Selector (heat.getClass (), "updateLattice", false));% Z- a3 |+ Y2 i/ _/ r
} catch (Exception e) {
$ z; j3 u0 I! I) n/ l System.err.println("Exception updateLattice: " + e.getMessage ());
5 ?( s7 ^5 w- L1 c }4 l* I C$ H* o6 x& E% c
, `1 s, H" |0 F* R M
// Then we create a schedule that executes the$ g! B* p# k+ A& _
// modelActions. modelActions is an ActionGroup, by itself it. k6 p" S! o7 o& r* B
// has no notion of time. In order to have it executed in
4 U3 w! }9 p& R! n" E // time, we create a Schedule that says to use the8 h, e j' ]- R/ }
// modelActions ActionGroup at particular times. This: b9 U' \$ w3 j* Q( P
// schedule has a repeat interval of 1, it will loop every
6 i! Z p, c+ t' N. w // time step. The action is executed at time 0 relative to
( C1 f1 c" Q5 R" ]8 _ Z // the beginning of the loop.& ^, {6 i4 Q4 l
# J+ @9 ~6 w ]. N6 g8 J3 x
// This is a simple schedule, with only one action that is
$ Q3 G1 w1 L8 o% t, G // just repeated every time. See jmousetrap for more
0 j5 ^, R; ~6 o5 c$ t // complicated schedules.
$ q' x6 T5 D3 Y
: J/ h" }" o) ~% e0 | modelSchedule = new ScheduleImpl (getZone (), 1);
1 ?, ?' S1 f" r k4 q modelSchedule.at$createAction (0, modelActions);3 K3 C. }1 c* H1 p
3 X: D7 N, r$ ?9 u/ f0 b4 J return this;
& h3 O4 y- f+ D }6 ?( B } |