HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 p* L9 g% I; e
' l6 @3 J3 n6 p# g/ }: h public Object buildActions () {1 x; M) u7 d2 C9 @4 a. s
super.buildActions();$ ?) N) S p0 T. t; I$ a( a! U" W& h
9 x- d8 I7 {) |; w! } // Create the list of simulation actions. We put these in
' Y& D# z3 O8 n5 A% n$ M // an action group, because we want these actions to be0 Q0 ^9 d) }" j3 E7 a: F' E* P% a
// executed in a specific order, but these steps should
- ]/ f- {9 M' z( G1 ^/ h, o) x% y // take no (simulated) time. The M(foo) means "The message2 u1 p& z- Y1 ~( p/ S
// called <foo>". You can send a message To a particular
- H& Q2 [0 I3 l // object, or ForEach object in a collection.
) J6 D% j: g$ E6 F0 @) _3 H
' W) l$ p) H) V! p: J/ @6 U // Note we update the heatspace in two phases: first run, ~$ ^1 I( S9 J' i% @( |: k
// diffusion, then run "updateWorld" to actually enact the' i. H; R( V+ q# _6 z
// changes the heatbugs have made. The ordering here is
7 @$ @/ K: ?0 S // significant!7 l7 K. I3 \0 A" G
( Q2 ]. s" U+ D+ U, o4 @
// Note also, that with the additional! b2 L' `; d6 A' c! i* E9 a
// `randomizeHeatbugUpdateOrder' Boolean flag we can6 {! N+ i) U( Q5 _2 b
// randomize the order in which the bugs actually run
( `% u2 H5 C) Y& p& a // their step rule. This has the effect of removing any3 f- J0 b1 u' [' s9 H- i5 m B8 M
// systematic bias in the iteration throught the heatbug, p0 ?5 X: I. h! W0 Z' y0 M) M
// list from timestep to timestep
) b- z6 o9 z3 X, ^* T5 s1 e
7 \9 k( p0 @7 y, w2 ^' | // By default, all `createActionForEach' modelActions have
& h+ a" d& a B5 {$ A3 v' I // a default order of `Sequential', which means that the
. T$ Q" U- R9 j // order of iteration through the `heatbugList' will be% F2 m& E7 |% e0 o; b7 d. `
// identical (assuming the list order is not changed. O% g$ s- p1 S) |
// indirectly by some other process)., i- E7 L* O8 a
/ C2 U' ^* q L3 D G. i1 Z
modelActions = new ActionGroupImpl (getZone ());+ R* s& A7 V" O) w: Y
- I" ?; o. k8 k5 \% c4 {/ ~8 M/ b
try {& c9 g7 H) l8 Z/ z$ O! C& Y
modelActions.createActionTo$message* c' h7 B) w2 o' J1 E- c" ?2 {* x
(heat, new Selector (heat.getClass (), "stepRule", false));3 [8 L' j! H: }
} catch (Exception e) {
" ~0 G! @8 G8 Z, c4 ]" o. t- M7 y System.err.println ("Exception stepRule: " + e.getMessage ());
0 Z; ^5 W) s4 X' ~ }
5 t" ^: k6 s1 N( |8 n ~
5 X3 C2 k0 e9 X& K, |" o try {
) C6 d3 s: ]: o" I Heatbug proto = (Heatbug) heatbugList.get (0);' w* V% Z* V- x0 [/ V
Selector sel = J/ Q$ q0 R o' @& m& y6 a
new Selector (proto.getClass (), "heatbugStep", false);- @" @' ^' p- F) _
actionForEach =
9 v! u, D! q* U/ S modelActions.createFActionForEachHomogeneous$call2 q$ j) i; g$ M- v; E$ b6 x7 B
(heatbugList,
7 \2 R# c& i% l0 S4 _( j new FCallImpl (this, proto, sel,
3 D# V5 F( h6 j2 ], _ new FArgumentsImpl (this, sel)));
4 s! R) h' V1 E5 a a3 _ } catch (Exception e) {
, b1 N& S! `* i2 I' L e.printStackTrace (System.err);: _; h) B$ l2 k2 Y& d* z; B/ [
}
' h( C4 `. r: y: Z! m' t ) Q9 n1 q! t) [
syncUpdateOrder ();$ a6 r. x8 w6 M
: T- U* Z, `) M8 N# [5 s; s try {8 m' N$ z, e3 h0 M! ^5 @
modelActions.createActionTo$message # h$ ^& t% j r$ t8 |6 X
(heat, new Selector (heat.getClass (), "updateLattice", false));2 X6 c Y6 M/ F4 G5 _6 ]" v
} catch (Exception e) {
: ], q/ F: q. x6 D- Y; h4 _% M System.err.println("Exception updateLattice: " + e.getMessage ());$ n$ v- s/ c7 s& U
}
8 S& e0 ^( Y/ {9 U5 _
9 v: [+ i+ A+ h! K% S // Then we create a schedule that executes the
' e6 c: m- P2 o4 _ // modelActions. modelActions is an ActionGroup, by itself it: n! g X3 i3 ]2 o) W {' R( O; `
// has no notion of time. In order to have it executed in
+ \; }/ g: E; y% O* K, g // time, we create a Schedule that says to use the# e# H* r8 u' U. K
// modelActions ActionGroup at particular times. This
8 j+ ^: m! `6 d" {9 I: U // schedule has a repeat interval of 1, it will loop every
, R: @( U/ U! ? j- l$ [" l, S- G2 X // time step. The action is executed at time 0 relative to
, _5 H8 W1 O) R- x" v$ q // the beginning of the loop.
/ f& Q6 l, M; {! H z5 i! T
1 {9 @- m5 E' ?. m3 Q // This is a simple schedule, with only one action that is7 c4 Z9 R2 E! h' Y, g
// just repeated every time. See jmousetrap for more6 f6 b |0 M, z& x8 N4 P
// complicated schedules.8 q0 ] f' y% q& D$ j- y
% B4 a, t: y* g4 i& c modelSchedule = new ScheduleImpl (getZone (), 1);
# |6 Z# h7 d1 x' w; V$ I modelSchedule.at$createAction (0, modelActions);9 l1 Q' J3 Q" R# `$ p: R& N
" \, f$ Q p2 i return this;6 G( N% N9 O+ [8 f7 B
} |