HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. j7 a; t+ F* E5 X" X5 [( b: V3 q, }+ n
public Object buildActions () {# j3 E+ F2 l5 d6 u/ ^7 h
super.buildActions();
! R, G+ N+ z/ T, b& a, X: m ' l7 {9 z1 F( ^6 w& \
// Create the list of simulation actions. We put these in% S, a9 n' m& W0 r
// an action group, because we want these actions to be- e, N3 }, {" F# e8 T$ A- x
// executed in a specific order, but these steps should
- K' l& |3 `& \1 O4 P/ O; J; | // take no (simulated) time. The M(foo) means "The message, h0 C1 S9 s/ Z3 W1 |
// called <foo>". You can send a message To a particular
- M9 }4 O7 t. k7 S$ G( ^0 M2 o8 s& u // object, or ForEach object in a collection./ c2 \& d- G8 {( W5 u* z! k) \
) B* x6 s& p0 V/ O. G% V. D. F% M5 g6 c // Note we update the heatspace in two phases: first run
' H2 D6 }, @0 K( M // diffusion, then run "updateWorld" to actually enact the
3 U, i3 E# F% C // changes the heatbugs have made. The ordering here is- b1 u$ c2 M- d! q6 R
// significant!
% V# M7 s# A4 A6 e) f 4 _( O4 n* U, J7 c4 q
// Note also, that with the additional
# k$ b; ?; q2 a1 _4 X8 j* B7 h5 P // `randomizeHeatbugUpdateOrder' Boolean flag we can
1 V+ c+ F% e. ]9 C" g1 v" H // randomize the order in which the bugs actually run, k- S. H. T+ E6 k0 B U
// their step rule. This has the effect of removing any
, ~5 N! P( O/ X // systematic bias in the iteration throught the heatbug# a2 d- ]& U j* |. \/ m3 {% G
// list from timestep to timestep
' _& T- S# O0 g4 g2 w 4 c: ^$ t2 Y# Y# @9 X
// By default, all `createActionForEach' modelActions have3 ?5 n* K/ n: v f. H. H
// a default order of `Sequential', which means that the
! c7 r. n& E- r" |: M2 M/ v" B( q // order of iteration through the `heatbugList' will be
5 @, E/ z) `: ]( a& y$ q3 p // identical (assuming the list order is not changed
6 U ?0 D, t5 F1 n4 X // indirectly by some other process).
' W% d- x' D7 ^3 C3 w5 g) D 0 J2 Y4 ]" \# N" f
modelActions = new ActionGroupImpl (getZone ());
# q! W0 S8 P: i* o8 m
! J+ o8 h. f5 `0 z6 R: g0 R try {
) a* z5 b8 @4 x! T modelActions.createActionTo$message* |1 Y3 n1 C' @. `0 p
(heat, new Selector (heat.getClass (), "stepRule", false));" m1 h# L7 ^' k& b' g3 E# T
} catch (Exception e) {2 a5 J' m+ W4 L
System.err.println ("Exception stepRule: " + e.getMessage ());3 Y+ A1 Q0 o) D# J
}
/ {' Z( S+ Z: K( T" v$ u9 t* i* ?: V) @) F
try {3 y: _' r* H, o& m$ Z8 N
Heatbug proto = (Heatbug) heatbugList.get (0);
; m4 K/ N+ c, v8 B% g4 T/ w Selector sel =
- q+ E$ a2 C5 S( e+ m4 m new Selector (proto.getClass (), "heatbugStep", false);
' `) }) T- m* T$ X actionForEach =$ m. i. p9 ]* K7 k
modelActions.createFActionForEachHomogeneous$call
- t* W8 i# w& f* Y (heatbugList,1 W& n; p- V" G
new FCallImpl (this, proto, sel,
* N6 o( M4 d* ]* Z% j& G* C* J new FArgumentsImpl (this, sel)));2 g9 G5 v* y' o* y8 { j& `
} catch (Exception e) {# Q) k! a. g+ a6 c! B
e.printStackTrace (System.err);) w' H$ N( B( ?9 E& f3 D
}' s9 M- V/ J/ T2 I+ Q
" R* g! w1 [1 z: Y, x
syncUpdateOrder (); Z$ m: [& X" \. w3 v2 f' `+ s
0 J" t5 y! u+ a( A* v4 x9 | try {; M1 r' L; B7 i+ _" W! \( }+ w5 V
modelActions.createActionTo$message ; O8 s% C9 p# M9 ?
(heat, new Selector (heat.getClass (), "updateLattice", false));
' Q9 g) `& X5 }9 _5 N* W } catch (Exception e) {8 n+ @+ J- } H( f+ U# L
System.err.println("Exception updateLattice: " + e.getMessage ());
) z+ {1 c! l8 L. q }4 `2 v. R9 J: |4 h5 k
, V! z2 S3 e- u& d6 R4 B
// Then we create a schedule that executes the
, \5 N% P1 d3 T // modelActions. modelActions is an ActionGroup, by itself it
4 d3 u6 C; F1 Y5 Q+ e# G: T // has no notion of time. In order to have it executed in/ r: b. o) J8 B9 m4 l8 P% f$ d
// time, we create a Schedule that says to use the
. C) ? r: T& S# m; H) o: L5 ~ // modelActions ActionGroup at particular times. This
. ^8 M; v1 j3 @# z. e2 z // schedule has a repeat interval of 1, it will loop every
5 L4 ]' U) \+ {/ P* i // time step. The action is executed at time 0 relative to* q) ~3 x) y+ i1 ~& A
// the beginning of the loop.
6 I4 ~0 L) F" x- S: Q
& n a9 t, d6 f // This is a simple schedule, with only one action that is
* r1 e+ g1 m Z% T `7 u // just repeated every time. See jmousetrap for more& t! J* G9 O7 s% Q' H
// complicated schedules.
7 U7 p2 ~- \( Y$ _/ x! H3 x. W , c& @: |: i! F. F4 z2 Y" i$ U( v& D
modelSchedule = new ScheduleImpl (getZone (), 1);3 }: M% S, |& z( R
modelSchedule.at$createAction (0, modelActions);
! {- i. Y1 I j, q% H9 j
1 H' N' O! z& X6 m! y- y$ n return this;
6 h/ Y/ ]3 `+ e G; T* n; g0 M } |