HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) I9 A1 u) w, A. [% w+ W( F6 }. T
) R( @; j1 D- {2 |( V R public Object buildActions () {
L. R" C- |1 y7 L) O9 ~ super.buildActions();7 D2 N" E& }5 y3 w
5 {# n0 T6 Z! d // Create the list of simulation actions. We put these in
" j" y6 v# Z, I9 x: \* h, W& n! ~ // an action group, because we want these actions to be: i# ~ f- d/ ?
// executed in a specific order, but these steps should
' ]6 S9 f* I4 Q2 B // take no (simulated) time. The M(foo) means "The message
# X" v* k6 t4 g5 E4 q4 r // called <foo>". You can send a message To a particular
) s F) U" P4 f! B% p! N' i // object, or ForEach object in a collection.
; d l( }9 i/ y1 m0 M! e1 l
3 L1 `9 I( P% a" v // Note we update the heatspace in two phases: first run( e# A" [- M. }. E' _3 A
// diffusion, then run "updateWorld" to actually enact the
1 F) W1 Y2 L% l1 _! K // changes the heatbugs have made. The ordering here is
! k- t6 v( d" m" E2 m // significant!5 I# p) v; w8 L3 w
+ ^; Y ^- f8 L w4 }. z
// Note also, that with the additional
# F& O/ Y% e9 m& I" o- k% e // `randomizeHeatbugUpdateOrder' Boolean flag we can1 L& k. a. A+ s+ ?, b
// randomize the order in which the bugs actually run' |4 X% H6 Y, l' z; [& r
// their step rule. This has the effect of removing any V! S1 D0 }" o! u6 d5 e h" [# t% T
// systematic bias in the iteration throught the heatbug) x. x/ n6 H! c( U0 m
// list from timestep to timestep5 a3 X0 p; f. F' X$ z9 ~
: a+ F0 P, {: p% H. O
// By default, all `createActionForEach' modelActions have
/ D: ?" y* d5 x3 l4 S* x# A // a default order of `Sequential', which means that the
4 F7 c5 y8 S5 w* r: a // order of iteration through the `heatbugList' will be0 U* ~; L0 d0 }& a+ C2 h6 Z! S
// identical (assuming the list order is not changed
8 v% Z6 _" V b // indirectly by some other process).
v% ?) P* U" L& y% @2 {7 C2 J 6 v& _8 r3 T; f% J9 g
modelActions = new ActionGroupImpl (getZone ());
4 k6 [, W1 k: s% J7 R- _; N' k$ [9 d/ o: P! h9 p2 O. X u+ P
try { s! t X5 r y' ?1 Q
modelActions.createActionTo$message2 H% j" i" E7 D& ? Z( c6 G
(heat, new Selector (heat.getClass (), "stepRule", false));
! u" _2 A" C {2 P } catch (Exception e) {9 K$ r" v/ w, y6 N C
System.err.println ("Exception stepRule: " + e.getMessage ());
9 a+ ~4 x0 O7 K6 ]/ N) E* | }# Z# y7 p E8 U! A; K
4 M7 ~$ j. |( e% I
try {
8 e- ]" { M d6 S0 u5 U* q( C1 W Heatbug proto = (Heatbug) heatbugList.get (0);* J) q' ?* Y4 m; c
Selector sel =
0 n0 N4 P8 P9 Y: d- L$ `% J new Selector (proto.getClass (), "heatbugStep", false);
4 x) |5 e& m; ]) @ actionForEach =
$ o* u7 W) n# m modelActions.createFActionForEachHomogeneous$call
`4 R; j" o _1 T% X (heatbugList,
/ C' U ?( \( q3 h7 ~0 ] new FCallImpl (this, proto, sel,
# ~$ r6 u2 w; M8 j new FArgumentsImpl (this, sel)));% N! y6 D: y% l) \4 m; q$ q
} catch (Exception e) {, U' s: n4 J/ P& c" J
e.printStackTrace (System.err);* E' X% S M: P1 o* U" o
}
C2 l0 O* F8 w$ ?; }' H
0 S: K+ F9 d' R1 U( ~ syncUpdateOrder ();+ F- Y5 p0 `" j! ~
( G% R. o8 p& c1 ~: `4 \. v+ { try {
% n7 Y/ O3 ~. g0 \ modelActions.createActionTo$message
# w& a$ [* o8 _" C. r (heat, new Selector (heat.getClass (), "updateLattice", false));
4 W& ^' m6 p% O U' @- o& L0 V6 S4 E4 H } catch (Exception e) {
( F% f8 D) C" j5 E# N System.err.println("Exception updateLattice: " + e.getMessage ());
' S" V+ w2 m; @2 h5 u }; Z5 k; |5 F, V' A$ L2 k
0 a0 a# V+ I0 E: B* v; I! y. U6 [ // Then we create a schedule that executes the4 |& o! d6 D4 [$ n; `: ]9 u
// modelActions. modelActions is an ActionGroup, by itself it
/ f: b& x4 W; |* t- ^! E // has no notion of time. In order to have it executed in
$ l5 m) s& Z) F0 v // time, we create a Schedule that says to use the1 t: e# y# w" J+ k) U( V
// modelActions ActionGroup at particular times. This
{: s0 v7 _1 S8 g' |& ~ // schedule has a repeat interval of 1, it will loop every! Y8 y# p# e4 X7 s9 Q5 i- p
// time step. The action is executed at time 0 relative to7 K( v2 h7 S2 R- G: o" f( s8 A
// the beginning of the loop.
) Y3 v$ R( m6 P5 L' E' `4 `- d' U+ j; L+ d. M9 }3 v/ C
// This is a simple schedule, with only one action that is2 m \& w3 W4 N
// just repeated every time. See jmousetrap for more6 z& N4 f" @4 m; t- [6 W9 }1 L
// complicated schedules.' ~ S* F; ~% R5 f$ m- M$ R
* W. b" t! \, I modelSchedule = new ScheduleImpl (getZone (), 1);) W! x; X8 ]+ h
modelSchedule.at$createAction (0, modelActions);+ S1 s D3 _9 p% T$ O2 S+ p4 m/ N
/ a& m- K! ~5 z0 w! y! @ d
return this;4 ^8 X3 w3 b8 {) Y* z+ I( }9 {' f
} |