HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ V! d# ~3 \2 `4 x: j: k5 }5 R Z; N0 i6 f) ~
public Object buildActions () {
]# e2 b2 `$ A! E$ n: r super.buildActions();
# T' j+ ~" m" G) S1 C( @% M 6 I0 K9 s7 L1 Z; R: Y. |' @+ P) H
// Create the list of simulation actions. We put these in* J7 t. Z/ S+ ?" X" M9 B* Y/ L9 M
// an action group, because we want these actions to be
$ |" D6 _) V9 L7 K- l' X3 a( L6 a // executed in a specific order, but these steps should
$ u; W) h) `7 g8 o1 ] // take no (simulated) time. The M(foo) means "The message3 e4 ?* B6 Y& G; a/ P/ X9 j
// called <foo>". You can send a message To a particular
8 `3 ~0 c3 A8 h p I/ m5 V0 k5 t // object, or ForEach object in a collection.. J8 P5 }7 N+ `& I) T% g7 v2 |
. W- |# B- e, _; W1 G9 ~, T% L$ ~! P // Note we update the heatspace in two phases: first run
_2 @. G! b% `/ C // diffusion, then run "updateWorld" to actually enact the
& S7 v, t/ u" a4 r9 |/ [ J // changes the heatbugs have made. The ordering here is& Z& @8 ~6 w3 M
// significant!8 n# J; o) e( Z* x7 q
8 n* T+ g ?7 K5 h% ~: I' X( b" H
// Note also, that with the additional
6 B* ^) Y4 @' `6 Z // `randomizeHeatbugUpdateOrder' Boolean flag we can9 k/ w0 z( c; M: Q. S
// randomize the order in which the bugs actually run$ E% v7 B2 G+ v+ R2 D. Y
// their step rule. This has the effect of removing any$ V3 a# Y7 E' x0 v
// systematic bias in the iteration throught the heatbug' f% }+ h- j# l; o* h) _
// list from timestep to timestep' S- M' @8 x- `; B' {! l
~1 l% b2 R& y3 C" G5 \
// By default, all `createActionForEach' modelActions have
1 e) K/ i& j' q // a default order of `Sequential', which means that the0 ?* K7 P' j% v+ i! P8 S
// order of iteration through the `heatbugList' will be
J$ q; w- @$ x+ H) f" V // identical (assuming the list order is not changed
N3 a3 c8 f+ L8 l // indirectly by some other process).; I# @( g9 X. ^. i9 r$ K
' J, E( y. {7 f4 d) n modelActions = new ActionGroupImpl (getZone ());
/ u3 M8 l7 K# L
$ q* F9 D/ Y% E! T9 L! ~/ O# \ try {
( g }! L1 T, \: x( @) q3 i4 V% C5 p' n modelActions.createActionTo$message ?1 q, A I0 L3 L) R: s: Y- i
(heat, new Selector (heat.getClass (), "stepRule", false));: Z0 v/ P8 H& T4 h# F
} catch (Exception e) {4 k `$ ~6 n+ l& E- m0 k
System.err.println ("Exception stepRule: " + e.getMessage ());
1 Q$ n* N5 D# h3 v, @5 Y }
6 d( r7 U% I5 [. n" a' j
! w: A8 R" ^: V0 [* s' f try {
/ |( Q8 |6 N7 V0 c1 I Heatbug proto = (Heatbug) heatbugList.get (0);
' C1 L* X9 D% a6 M Selector sel = ' e m, b8 t" z9 H/ D, x
new Selector (proto.getClass (), "heatbugStep", false);
, h7 f* T- P3 r actionForEach =
; J2 u, G+ Q7 Z" v6 b/ ^ ~: K; T modelActions.createFActionForEachHomogeneous$call/ _2 [$ O' F4 e. r2 g# c
(heatbugList,
- M$ [6 ^# m5 s) R% ~) C; x; i new FCallImpl (this, proto, sel,
+ K* p) M& G% E2 M5 ] new FArgumentsImpl (this, sel)));
7 C4 n0 K# D" S- Q$ ?. V9 b } catch (Exception e) {) ^# ?# J& o+ d
e.printStackTrace (System.err);* @! z/ |; A6 }5 l0 Z& a2 D" q
}; M/ _0 G" Z6 N9 Q" t4 U
$ ^6 A6 j- V) p( o
syncUpdateOrder ();9 a$ N. Q# I y# Q7 l
9 O* j3 F# w: _5 g* R/ l7 T" q try {
. N: V% ~& W1 x5 h modelActions.createActionTo$message / h# V! m" @. y: {% b- H1 T
(heat, new Selector (heat.getClass (), "updateLattice", false));7 B/ q6 ^+ C/ p) F3 R
} catch (Exception e) {$ w+ `7 o1 m% `3 {. V# @
System.err.println("Exception updateLattice: " + e.getMessage ());
: D) M( s% m3 |2 J }5 \* y% q# l4 V% M' g0 \
( r# e _- |7 @ // Then we create a schedule that executes the
9 E. M& P6 e; ?7 |3 L. v // modelActions. modelActions is an ActionGroup, by itself it5 N2 o2 ~/ a: ?$ L! q( y9 L
// has no notion of time. In order to have it executed in8 g. z5 [# L! @; f) {; q- S
// time, we create a Schedule that says to use the
, v6 U# t4 Q- O8 J // modelActions ActionGroup at particular times. This$ C1 L; @0 x; |3 e+ C
// schedule has a repeat interval of 1, it will loop every
8 `" y' v6 ^3 e7 b- _' ~1 A // time step. The action is executed at time 0 relative to9 P0 W7 s8 h2 |$ k, x9 a: {
// the beginning of the loop.
* a1 T4 Z# w" ~$ i: d. K
8 r# N* u6 x* |+ q // This is a simple schedule, with only one action that is
4 f) @- V) M" m+ m+ e // just repeated every time. See jmousetrap for more
: L3 I; O3 s4 w* L: I! L // complicated schedules.
, r, u4 x* \! |5 v% ] ! {% m2 x0 K/ u. D
modelSchedule = new ScheduleImpl (getZone (), 1);) }7 C2 _' \& G7 P5 b/ Z) R
modelSchedule.at$createAction (0, modelActions);4 k0 M' b4 ~6 y9 V0 J
w4 g5 E6 X: n- C; k9 X$ ]8 l8 L
return this;( b3 k, h5 @# ]! M
} |