HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 ?3 `& e, b: D9 W+ e
& T2 l1 A" t- D, t1 R public Object buildActions () {' @ [6 j, f0 }+ S/ L6 N& l# e
super.buildActions();) \& Z. n( {0 T
3 L6 v$ X6 q2 g- c
// Create the list of simulation actions. We put these in* e" R7 }0 n+ m0 F& Y3 r& w
// an action group, because we want these actions to be, n q& Q, S3 W
// executed in a specific order, but these steps should
' @; U' w1 L0 g2 ^. D // take no (simulated) time. The M(foo) means "The message. a* a7 S. I( w* _' d, e: s
// called <foo>". You can send a message To a particular
% m- ~, {- }7 a7 l2 G" R // object, or ForEach object in a collection.4 d7 W, S, W/ d) h. ]
1 A/ t- [! e, v, `
// Note we update the heatspace in two phases: first run
4 A( @# U/ r4 `3 H1 R: p // diffusion, then run "updateWorld" to actually enact the) u" `7 y8 c: Q8 p" @
// changes the heatbugs have made. The ordering here is
0 ]4 x" Q4 E4 d1 J, P( Q* B // significant!" K0 I5 F, V' `9 Q
) Y; b& t( N0 g4 y" s r \7 O- e0 v // Note also, that with the additional
; U8 @) i$ `! ~9 I" b* m5 s // `randomizeHeatbugUpdateOrder' Boolean flag we can/ D( f) P6 U M- z6 o7 z
// randomize the order in which the bugs actually run
5 H# B" S+ p+ V6 J; d5 } // their step rule. This has the effect of removing any- ]) J% y8 g2 \
// systematic bias in the iteration throught the heatbug; ^+ A) S9 ?8 f1 W0 B5 {3 L. i3 J
// list from timestep to timestep2 o3 W; L; F1 F" b& w8 k f$ T
' a9 @. k4 @6 r2 v% X // By default, all `createActionForEach' modelActions have0 \2 h; Z$ q7 P, Y7 V# U2 c
// a default order of `Sequential', which means that the8 i6 X! D# ?- z! B
// order of iteration through the `heatbugList' will be) j; k; ~9 t( ?) b
// identical (assuming the list order is not changed) }% W- d7 \9 r$ o w- k
// indirectly by some other process).+ H. V" I. W K8 u
% }& r* a2 Y' D' P) w3 v- P modelActions = new ActionGroupImpl (getZone ());
1 I& A, e$ u! E! D6 ?" i' w) F/ y) f
try {; @) d* M8 X) n
modelActions.createActionTo$message
2 ], g# Z+ W, v( O5 F (heat, new Selector (heat.getClass (), "stepRule", false));9 C' j* Q% D* J d8 X7 Q8 K# t/ f
} catch (Exception e) {+ @+ q5 x, b) ^& t: P% w2 H* A3 O
System.err.println ("Exception stepRule: " + e.getMessage ());
: k0 i+ x U+ ]( r* P; w9 Z* ] }9 ?6 ?2 \/ V6 N' P$ v( \2 e0 B- L
0 \9 `( o4 {5 U: W try {4 O- h5 u6 e$ Q/ p6 h6 p
Heatbug proto = (Heatbug) heatbugList.get (0);
. T2 C! X" ]3 X/ F7 N+ B t3 d Selector sel = 5 x% j# _7 S" h9 V6 I g5 e, z
new Selector (proto.getClass (), "heatbugStep", false);
7 R. G# M) e% N, K' L; O. G5 _* p actionForEach =
. x. n! G# [7 x6 q* [5 w/ X modelActions.createFActionForEachHomogeneous$call, {4 e6 E$ [; }) Z, U1 r
(heatbugList,) v% {( c$ f; c# Y: \ J
new FCallImpl (this, proto, sel,
, Z+ T8 ]1 S7 Q+ H/ y, X1 g( m) l new FArgumentsImpl (this, sel)));
6 A' X$ f" d$ t6 f9 j1 g! Q$ c* ~" F- H } catch (Exception e) {3 S; l2 r v4 r9 d
e.printStackTrace (System.err);
- N) \3 f" w. d! J; F) p }
) d' S' `+ m5 a; C" p! `! b 1 e# r/ ^ i! W5 T& x ~
syncUpdateOrder ();
, k4 E1 a. {6 u1 j3 h6 T* Y/ @6 N' N" X3 _: n
try {
! c3 y- w8 ~% \ modelActions.createActionTo$message + |3 s6 y/ w4 t
(heat, new Selector (heat.getClass (), "updateLattice", false));/ C. i/ F0 o+ P" e
} catch (Exception e) {
: y' b& S$ Q6 X2 |. H0 s- w System.err.println("Exception updateLattice: " + e.getMessage ());
, T; e& V) o' F6 ^% j( W }
" K7 \/ j! c6 X
# E7 x; N8 A1 g+ V // Then we create a schedule that executes the
! w# p3 \5 ?6 v& J; f$ g // modelActions. modelActions is an ActionGroup, by itself it
; l. Q/ G/ v7 |* D- y( \; e. e // has no notion of time. In order to have it executed in
; V7 \1 H1 w! S5 o. o# Q // time, we create a Schedule that says to use the
5 M$ q, w7 m3 A9 F$ K // modelActions ActionGroup at particular times. This
9 b& o+ n4 c: h( I) {# I- W: K) e // schedule has a repeat interval of 1, it will loop every
) M2 k, w7 f3 G: X // time step. The action is executed at time 0 relative to
( }2 U. r3 _- i* \, m) W+ f# [ // the beginning of the loop.3 @5 \/ l) @) p# Y8 s, v
8 I8 S& W& [, d0 h4 F
// This is a simple schedule, with only one action that is# V3 `8 _3 N' O* s! e/ o* S; o( M
// just repeated every time. See jmousetrap for more
' X6 O4 I4 |! `+ U3 G3 E // complicated schedules.
, C+ K5 b3 f& B: ? : \6 a( d0 h5 x9 I
modelSchedule = new ScheduleImpl (getZone (), 1);
2 a* t, w C" \. x modelSchedule.at$createAction (0, modelActions);
- Q& d" o2 ?) O- \* m9 w+ i" _
2 N: w& a% z+ F. I+ k6 f1 f$ ]0 a return this;9 Q0 a4 T3 d1 `" W5 l% O
} |