HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
V4 |% {1 l6 C7 R( ^. o0 h# q& r0 g* a4 C* F
public Object buildActions () {7 R( A& P" U5 o1 Q8 U
super.buildActions();& E3 P7 S8 O& [ U3 _
) [ [. ]/ V1 `+ Q7 K
// Create the list of simulation actions. We put these in
6 r7 W$ i# F" T+ m7 t( L // an action group, because we want these actions to be. W$ O ^7 |* f* f4 B1 u3 U
// executed in a specific order, but these steps should
3 [) T5 Q/ i: X) L( ~8 M" [ // take no (simulated) time. The M(foo) means "The message. o* U0 a6 s; _
// called <foo>". You can send a message To a particular
3 V' a* W$ ^" d3 r // object, or ForEach object in a collection.3 a( u# V+ |$ [' l
% o9 D& x4 T% m5 A
// Note we update the heatspace in two phases: first run
$ g u) ^ L5 q // diffusion, then run "updateWorld" to actually enact the
) b% o3 ~0 N# f8 l6 |) I- _ // changes the heatbugs have made. The ordering here is+ \2 g0 W' L+ G. T
// significant!
% Y* w+ F% n/ p3 d" p8 P # W& i! G `) `9 ~8 I
// Note also, that with the additional
, r# E' [0 u0 Q: l) z1 E) [; U: { // `randomizeHeatbugUpdateOrder' Boolean flag we can
8 l9 x7 L$ u; J! F3 ^7 K // randomize the order in which the bugs actually run
6 L! `+ w1 B4 s6 Z // their step rule. This has the effect of removing any
( |. d# v& M r% l3 C+ p2 J, {2 h // systematic bias in the iteration throught the heatbug
) u( F6 d! d- c% s$ y9 p4 ? // list from timestep to timestep
% V1 M& d0 H' ?3 r" n 3 i8 j: q: F/ w
// By default, all `createActionForEach' modelActions have
) V$ d. o$ f* Y' ?( W* p6 t7 C // a default order of `Sequential', which means that the
! r5 O2 U5 I+ @+ _9 ~$ D2 D // order of iteration through the `heatbugList' will be
( G: ]) E- M" U( _' J( E: t3 ? // identical (assuming the list order is not changed
1 h# }+ f H" n+ {; q# R# G // indirectly by some other process)./ ^7 l4 g0 U# K& R( V0 X7 x; \
& C4 O# w6 X3 v9 k* D& x
modelActions = new ActionGroupImpl (getZone ());# @9 w' N; w* b0 I, p$ s. h" B3 Z
) a, r$ Q- o& K/ O B w- M' U! Q
try {
@: n0 u: V' K' p. ^ modelActions.createActionTo$message
$ @4 y+ X+ c& H* n6 D (heat, new Selector (heat.getClass (), "stepRule", false));: c) A( L5 a( J
} catch (Exception e) {/ M4 w0 n% {. D d
System.err.println ("Exception stepRule: " + e.getMessage ());
9 Z5 P' u1 ~: ` q6 h }3 k2 H! S, s6 d' p E/ ?/ q
4 k/ F R. e7 y4 H try {
. {6 C% s* F# g T9 k, k6 R Heatbug proto = (Heatbug) heatbugList.get (0);" @9 U" m, |0 s
Selector sel =
. A( q: ]9 k6 ^$ k z0 [! c$ B new Selector (proto.getClass (), "heatbugStep", false);
" C. ~+ _6 s8 c+ A3 d& W actionForEach =
. {) }* H; H$ C0 k- r1 a) R modelActions.createFActionForEachHomogeneous$call
$ d6 g! q/ s$ p5 j( Z7 { (heatbugList," K& D9 o* S; ^) X$ b9 u4 r# e+ K
new FCallImpl (this, proto, sel,
3 f7 S6 d0 W5 Z: S new FArgumentsImpl (this, sel)));1 H( \0 Q9 R! [0 t; @) B4 x/ {
} catch (Exception e) {8 U2 A4 P/ R- i$ n& X# B
e.printStackTrace (System.err);! V6 j; R+ _3 Q! m3 ^* w8 H! g/ A
}
; D' m' U( E2 `
( z% E& Q& G& Q% V" B$ _9 U syncUpdateOrder ();$ f& r# B! x: U# k7 o1 |
# U4 N( `8 N! |' ~5 f
try {
8 i6 i% ]; b, v- \9 f% }& p( p modelActions.createActionTo$message
4 y: F/ b: B0 t: \& ~' ^ p, f: C (heat, new Selector (heat.getClass (), "updateLattice", false));/ [: I+ g8 ^" ~7 Q7 F8 ]
} catch (Exception e) {
* Z: E) A K2 r6 c0 G/ W: Q3 [7 Y* o System.err.println("Exception updateLattice: " + e.getMessage ());
3 u/ R0 y, h4 X1 A) Q4 A0 ^* F- E }3 g9 g0 E7 H+ t$ ]6 I% U
3 P) P4 G k& Z; y* t
// Then we create a schedule that executes the8 I; n) V6 b' f2 O
// modelActions. modelActions is an ActionGroup, by itself it
, G7 g2 z' b, h- E // has no notion of time. In order to have it executed in) m# ]0 x1 M9 O. R! t) H1 e
// time, we create a Schedule that says to use the+ ~4 v% @/ @ T1 ^: B
// modelActions ActionGroup at particular times. This
, h0 ]' q/ |" N% C+ v // schedule has a repeat interval of 1, it will loop every/ ^/ M; M% J1 b. t. w) J+ @& y
// time step. The action is executed at time 0 relative to# j6 x# D- e8 ~7 [8 P: I. Y& Y
// the beginning of the loop.
! }, X ?" Z' Z/ o2 O* L; e- v9 G r6 q( L
// This is a simple schedule, with only one action that is) Z4 X- ?( _) v0 b" _
// just repeated every time. See jmousetrap for more
. ^0 ?9 ~) }/ m // complicated schedules.
' K& q9 y; T+ @& D' c$ e
( `6 d1 p, p+ i3 C" T modelSchedule = new ScheduleImpl (getZone (), 1);
: h; u$ Q8 P' G# `( R3 r modelSchedule.at$createAction (0, modelActions);( Z, o& v" N, t& I
2 f4 U& M# Y* x/ y% M/ a0 m* { return this;
* D w2 I! V+ G } |