HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 H& e# }: `5 o/ U0 `: R
4 ?/ B) X0 @! [3 w public Object buildActions () {
0 \' ]1 x* c" d1 r0 f$ t0 O6 B super.buildActions();3 B5 c. A) G' `
% S8 ?# H0 o* ?# `# E% ]$ r) }
// Create the list of simulation actions. We put these in9 a1 u9 A, \# i* L# _( d" b* C7 J
// an action group, because we want these actions to be! z& e [' g5 L e6 U: c2 U3 R
// executed in a specific order, but these steps should" I8 \8 t2 @! H- s, I+ p
// take no (simulated) time. The M(foo) means "The message
' Q3 q" K" k: j: n4 _4 D. v. G // called <foo>". You can send a message To a particular" H* f8 Q6 Q: l' T
// object, or ForEach object in a collection.
. |( |3 C1 b- T$ @
t g0 ~4 k6 @) b5 h+ Y/ d1 V. ? // Note we update the heatspace in two phases: first run
$ v, k% `& w# i // diffusion, then run "updateWorld" to actually enact the
6 u4 p) A0 n& F* t2 M // changes the heatbugs have made. The ordering here is
, Q& |' i& X8 Z1 U // significant!. e# ^/ F0 t. a' ]. U2 e% Q
4 @1 ?* j- B" q6 z, Z7 w
// Note also, that with the additional3 S% F6 A" _+ q8 U5 a; _
// `randomizeHeatbugUpdateOrder' Boolean flag we can# N# T& R- O3 F$ g2 }9 K
// randomize the order in which the bugs actually run
4 ^- t8 g' N" X5 |$ S% l // their step rule. This has the effect of removing any
. L- `: S# c o: I // systematic bias in the iteration throught the heatbug
& a5 h4 s" |3 _: ?- |& s // list from timestep to timestep: `+ }2 I* [8 }6 x$ a6 T$ v1 t
) w R \( O6 ~2 n( }
// By default, all `createActionForEach' modelActions have
& `2 D6 T0 n: O // a default order of `Sequential', which means that the( o+ b" Y& r' ]# i
// order of iteration through the `heatbugList' will be6 M: Z8 Y& r) k
// identical (assuming the list order is not changed/ k- T% P/ }$ o# D3 h
// indirectly by some other process).
o' P7 s: ]5 G: G
8 T- Y; ^9 ~, m& S' ^( w( l modelActions = new ActionGroupImpl (getZone ());
/ g' ?& D! B" t& @& K3 s1 L" x
/ e) u* Y N7 j4 p; P try {4 k1 l! s- F4 J+ [% j3 B5 F4 |
modelActions.createActionTo$message
7 a. u2 D# ^5 a& Q, E/ A; Y e( j2 @ (heat, new Selector (heat.getClass (), "stepRule", false));
6 t2 Q, j/ H% |) I0 B } catch (Exception e) {
- G. {) h* X# F8 ] @$ D System.err.println ("Exception stepRule: " + e.getMessage ());
& s; R4 Y: v( z) P5 }2 U1 [ }
% r, c$ |3 j( d0 K L Y! q( J# p8 o. M% M* _
try {0 J% e: O% K) u
Heatbug proto = (Heatbug) heatbugList.get (0);4 c, e3 X" |( {1 Y
Selector sel = 1 y F: L" A8 L0 G9 `- R
new Selector (proto.getClass (), "heatbugStep", false);
1 O9 V4 ^& M" [; o actionForEach =0 H, R/ M0 Z* h+ B
modelActions.createFActionForEachHomogeneous$call
6 k* ~* R7 C( W% n0 A (heatbugList,
( v) I: _3 r! p new FCallImpl (this, proto, sel,9 z) q$ x! m% X4 @1 q ~
new FArgumentsImpl (this, sel)));8 [; [4 ^- w9 {7 }: g( @
} catch (Exception e) {5 J e8 V( M4 h$ q) E
e.printStackTrace (System.err);# n$ O' q; y) u
}
. k$ i/ S% D7 S! F- p f ( q+ C2 N( B3 V. [8 U/ a8 p0 ?
syncUpdateOrder ();
8 b3 Q* t* ?/ D5 A! n2 N) j1 Q8 @2 [! j% c, h. p4 M" L5 O% l
try {
" i" ^5 d# {5 C: M modelActions.createActionTo$message
1 _; j& r' M" e; E' D7 | (heat, new Selector (heat.getClass (), "updateLattice", false));' q% t/ ~1 g$ B
} catch (Exception e) {
9 V8 z) o- W- f# h" A6 k$ L* Z# D System.err.println("Exception updateLattice: " + e.getMessage ()); V# `9 i& Y5 c' H5 q- b& l
}
0 O' n* H9 Q- {4 ?$ {
6 G0 D" \6 [8 F; ~+ h // Then we create a schedule that executes the: ~, i" O! m# t$ W1 i8 C; B
// modelActions. modelActions is an ActionGroup, by itself it7 ]4 E' c2 f% z! |
// has no notion of time. In order to have it executed in' _7 T3 @/ B2 N ^1 [4 A- r5 S c
// time, we create a Schedule that says to use the$ S% a( @6 v2 f4 v. }
// modelActions ActionGroup at particular times. This
" p$ w+ l$ `$ t5 V/ ^. ~ // schedule has a repeat interval of 1, it will loop every
0 i! S2 S B% w( L, _ // time step. The action is executed at time 0 relative to
6 X& a, {; t6 F( M2 A // the beginning of the loop.
# s' W! @/ J! C1 U" J; S
6 ?5 Q; o3 U* f: K# v \( G // This is a simple schedule, with only one action that is
4 C0 o7 _6 b% C. l8 [ // just repeated every time. See jmousetrap for more
j+ t7 R; J" A5 J' R/ B) T // complicated schedules.
" U* J7 j5 D- W' C
, a( A$ y0 ?1 w6 j2 D5 | @& R3 W modelSchedule = new ScheduleImpl (getZone (), 1);
0 c" u1 {2 ]$ j6 w modelSchedule.at$createAction (0, modelActions);, \) Z) Q8 z9 v& e' P: Q1 p* M1 H8 |
# T& z: g/ R5 @' e2 X% c return this;2 H, j/ Z7 Q! F- _( P
} |