HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- C. q/ ]' C+ f8 E, b/ n* a7 t0 B1 @6 S _
public Object buildActions () {6 g+ D* f8 t- x3 {& ?8 L
super.buildActions();
% A9 B$ e( ~- |4 t# t& A W! `/ e2 n7 n% L8 b$ y
// Create the list of simulation actions. We put these in9 p8 i" x$ n0 k- {# q/ l0 r; u
// an action group, because we want these actions to be1 A: m Q& c0 W6 y: B
// executed in a specific order, but these steps should- r, f1 s! Y7 [. I' D
// take no (simulated) time. The M(foo) means "The message
' t v* `& U( l6 c$ O; k+ i // called <foo>". You can send a message To a particular
! e9 h5 {0 @6 K& u; O // object, or ForEach object in a collection.; T2 ?- ^0 j y' Q* X) u5 z
. D/ F0 p, m$ E5 ? // Note we update the heatspace in two phases: first run
% {" n) |' _5 {" x: a- J H // diffusion, then run "updateWorld" to actually enact the& ?5 j$ E1 s& D% b: \. R
// changes the heatbugs have made. The ordering here is
4 d7 c/ _! D* w" Y // significant!7 l/ c1 q) l( {
2 t) x; H* y, X: T1 j8 I6 [6 `
// Note also, that with the additional3 @# ?* M. L0 i2 e
// `randomizeHeatbugUpdateOrder' Boolean flag we can- A9 i0 j1 a5 h I% [) @
// randomize the order in which the bugs actually run
2 Y ~7 `% \# }" K // their step rule. This has the effect of removing any' b2 B$ Q3 c6 l. E9 J" {
// systematic bias in the iteration throught the heatbug
) @$ h, |" @) G7 i6 L3 n9 Q6 [ // list from timestep to timestep" U; l _( f( d: J
; l/ f3 x) J! n$ {0 F
// By default, all `createActionForEach' modelActions have) k" J" f \: ]) H
// a default order of `Sequential', which means that the0 u2 ~& h, D7 t5 S
// order of iteration through the `heatbugList' will be
8 N+ {1 r) a. U9 P' N! r! Q3 J // identical (assuming the list order is not changed0 t4 _. P$ N# S, p
// indirectly by some other process).
; T" s8 Z5 ?; D* C6 x8 a# t
# O* U3 f, X( H. ?: W modelActions = new ActionGroupImpl (getZone ());
" z! a. _6 `+ z3 j/ R/ c* p" D1 S; o
try {# h v, F4 D7 _0 Q: J8 v( Z
modelActions.createActionTo$message
: H1 B" [+ K$ Z1 ]. b) y% r (heat, new Selector (heat.getClass (), "stepRule", false));
' m \, X& y$ ~. r8 \* j0 a } catch (Exception e) {% C- c2 R! }2 P& K- h! D
System.err.println ("Exception stepRule: " + e.getMessage ());0 p+ b6 F( i6 c3 ?+ C* u6 c2 X1 y/ r
}& S) u- t) Z2 m* @
. Q3 J+ J" x3 D5 s, v
try {; f; {) C' i! Z1 [( i
Heatbug proto = (Heatbug) heatbugList.get (0);
6 V( c6 Z4 l @+ g( i Selector sel =
% b/ P$ T- e( a: v% o9 d2 x0 P; n1 W new Selector (proto.getClass (), "heatbugStep", false);
9 Y# i r% a+ | A5 n actionForEach =
: k1 {' g, n- ?) y* V* A modelActions.createFActionForEachHomogeneous$call% {+ z# l9 W8 R4 ^) i
(heatbugList,
" N0 I1 Y& s* G/ B J0 ?8 e4 j new FCallImpl (this, proto, sel,
9 g& c8 B. g: e( m0 l3 |, R new FArgumentsImpl (this, sel)));
3 f" \* r1 z: D/ Z9 K1 k } catch (Exception e) {
^! c; s0 s( ]0 e D# e& X, N, ~0 X- h1 k e.printStackTrace (System.err);, x, ]- t3 D" M0 f3 @
}& H. b& R8 e- E: c. y/ [
+ i/ C# M; L+ z$ u
syncUpdateOrder ();, _" j" E- {3 [3 B9 t9 ]
- x* E3 ?, P' p1 l try {
) ^4 c2 Y$ P8 ^ modelActions.createActionTo$message , d; d/ l( }! j- i0 R6 I% b! k
(heat, new Selector (heat.getClass (), "updateLattice", false));$ y3 c6 m! w0 d) i( N: e* w! U* P
} catch (Exception e) {# v' ~+ b. L1 a$ n% W
System.err.println("Exception updateLattice: " + e.getMessage ());
! j( K" A4 g% P/ P }
$ _" S1 q/ I3 J8 i & q* P+ W/ U4 H9 E6 l" M
// Then we create a schedule that executes the
- f4 I$ C& ?$ O. i3 m // modelActions. modelActions is an ActionGroup, by itself it
; y% V; u1 w2 \1 f f: Z4 y( F // has no notion of time. In order to have it executed in" U( r X; \! p. h% U3 a% A
// time, we create a Schedule that says to use the
! _. m; ~& E! v // modelActions ActionGroup at particular times. This
' U |8 Q. A3 \ n! N5 u // schedule has a repeat interval of 1, it will loop every
W: ?! j, u" n# o$ @/ D // time step. The action is executed at time 0 relative to6 c! a1 d% z: }- F( z' u
// the beginning of the loop.5 S9 v4 _7 k! C9 K* u4 |1 Q
& p1 J' K- B& l% y7 \6 t8 m2 s0 g
// This is a simple schedule, with only one action that is
) M+ J+ u4 m! q) E+ I& j! a2 z // just repeated every time. See jmousetrap for more
. C% k/ P/ M; X( I // complicated schedules.
o' W$ X# D) ^& u4 v. C) R; u
9 f6 [+ j/ Z2 e9 v! T; k" t+ O modelSchedule = new ScheduleImpl (getZone (), 1);
1 U5 d7 |8 f, ?6 w* M/ g modelSchedule.at$createAction (0, modelActions);; k& k% _, F; F- n
/ H: G- {3 K+ g+ j return this; X& G f8 ]6 |
} |