HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ f2 V( r0 Q6 E
" t; m! {, X9 d# Y8 F public Object buildActions () {9 f: C' N3 a" ]8 L d) ~
super.buildActions();
( }9 H2 X! f6 r5 g- j
* q* }" | V( r# P4 u // Create the list of simulation actions. We put these in
% H+ J5 @& a& s // an action group, because we want these actions to be
5 b- j5 w0 V8 d- s. H. H: p j( t( M // executed in a specific order, but these steps should9 o& _. ?5 H1 F
// take no (simulated) time. The M(foo) means "The message
6 \8 F ~$ a. b // called <foo>". You can send a message To a particular
. P- ~2 x5 q/ d, j# R9 I" f+ j, T8 h // object, or ForEach object in a collection.
1 m5 n0 Y1 E8 o6 \7 D# ~
2 U, I0 m! N0 V# |/ I // Note we update the heatspace in two phases: first run
! j/ f1 s8 K8 N# h6 Z5 d8 q // diffusion, then run "updateWorld" to actually enact the& n3 ?( p$ P% ~0 C/ U+ o7 l+ A9 L
// changes the heatbugs have made. The ordering here is3 m; z) s' [/ }. P
// significant!1 D# j# _. ^$ H! b
: n( J0 D" u7 n5 _
// Note also, that with the additional) z: R+ u- j# C# W+ s
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ K- [2 {) S2 e C6 L
// randomize the order in which the bugs actually run
% R! p# U& f% ?1 [: j // their step rule. This has the effect of removing any! D- Z2 H: |" M# \
// systematic bias in the iteration throught the heatbug" R( H1 H. M6 ~0 l& h
// list from timestep to timestep) W3 [, v- l' _0 `6 g8 u
2 P2 S2 L X) d7 w: }- |
// By default, all `createActionForEach' modelActions have
1 _! \6 v3 s' Y5 A. d1 @! G7 b // a default order of `Sequential', which means that the$ X- C, L4 k/ N: R) Q! Q% S. k
// order of iteration through the `heatbugList' will be
* i5 S5 ?9 o, B // identical (assuming the list order is not changed
8 x$ \3 {/ e( M& `$ H // indirectly by some other process).
3 b" S$ [5 c9 A" G n) e: N& ?* l- G: D
modelActions = new ActionGroupImpl (getZone ());
8 H! a' A( ~4 V( P. x0 A1 L6 J% y
try {
3 z, d+ j) h- [! u/ Q- E# A modelActions.createActionTo$message
; ?) c' {. P' n, O% N. ^ (heat, new Selector (heat.getClass (), "stepRule", false));* J' `) c/ o& b1 [$ X
} catch (Exception e) {
- Y6 f( p) c% e5 @7 D( v System.err.println ("Exception stepRule: " + e.getMessage ());( P. l+ {4 c1 x4 O
}
5 K# L7 k$ b) m1 ?. J3 W2 {1 H# B7 W3 c
try {9 p8 m4 X* q6 I9 ~4 v, D) M$ D. x
Heatbug proto = (Heatbug) heatbugList.get (0);$ R0 n/ K1 v: E P! X
Selector sel = ) z- A' d7 d( o- }0 A! F
new Selector (proto.getClass (), "heatbugStep", false);
2 q7 \! \! W. G actionForEach =7 |2 Y" t# l m3 k. Z* P
modelActions.createFActionForEachHomogeneous$call% y7 Z& [& Z8 Y" D% I
(heatbugList,' v* r( s( V8 o* X5 @
new FCallImpl (this, proto, sel,( D4 O$ y, f. ]' B- p8 L+ B, s
new FArgumentsImpl (this, sel)));$ U5 ^1 h5 \. g& q
} catch (Exception e) {
) ^( p- j2 A. e9 D5 r$ }* X e.printStackTrace (System.err);
9 m. w4 k/ u. O+ D6 B; Z, Y }0 |1 t) n1 ?4 f
, B1 s0 G% j, g! R! C! v) u" B/ ^. f syncUpdateOrder ();
0 z& S9 C8 E4 }8 I9 O4 X3 a' a( ^" J H- V5 E+ C
try {. z+ p# T8 `7 [) L
modelActions.createActionTo$message ; g- D, x a* q+ y5 i- h2 O$ f
(heat, new Selector (heat.getClass (), "updateLattice", false));
# {; [' x) C; H, F W6 ]5 |, [- b0 Q; T% B } catch (Exception e) {
6 t# \) ]& b9 |7 h" C; H System.err.println("Exception updateLattice: " + e.getMessage ());
& ~. i2 D# e, g7 o }) X2 V2 n& G3 Y9 A
5 d/ T5 ?' s1 j# r3 f: t8 j; O
// Then we create a schedule that executes the% G& A! V' R+ y- r( D' A, V. Z! C/ `
// modelActions. modelActions is an ActionGroup, by itself it
7 a- f9 a. D# [6 x. P1 ^! H // has no notion of time. In order to have it executed in) m* j* a$ s' I
// time, we create a Schedule that says to use the
3 g4 j( q2 b7 m2 @ // modelActions ActionGroup at particular times. This: s& r# d. b7 G! ^8 s
// schedule has a repeat interval of 1, it will loop every
/ k [; F, {- }" A4 g5 N& ]' P // time step. The action is executed at time 0 relative to
* A* B: _8 Q3 s/ m1 h0 \' ]8 T // the beginning of the loop.
, q3 ~ ]3 Q3 k6 y5 U6 ?' _( |2 N- b+ E9 c
// This is a simple schedule, with only one action that is
7 W) }6 [' _2 n // just repeated every time. See jmousetrap for more
5 H+ N0 y! p( E. N, O // complicated schedules.9 n( E4 l* v+ |- }1 W+ M" h! [
]3 {; {0 O1 A
modelSchedule = new ScheduleImpl (getZone (), 1);
, h4 N7 U/ ?2 j/ o1 s modelSchedule.at$createAction (0, modelActions);
9 M# [2 D# B& x ; |# O6 N# |6 O2 K9 D+ m+ k
return this;( h# _4 P) ]# m" v/ `
} |