HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) n& A d8 d$ O1 ?0 P( y+ w/ ?
+ _# k0 @; P$ g, n; @+ W2 F
public Object buildActions () {
2 u$ X% Y8 F1 D! R. D super.buildActions();8 u! f' V a+ _7 I
7 m3 J% v+ \( U$ u( I3 G
// Create the list of simulation actions. We put these in
2 S! R* Z4 a$ F: V3 l: L // an action group, because we want these actions to be
) V* C# j6 A7 e9 O5 N // executed in a specific order, but these steps should
+ l) @3 z" r' a' @. P+ y // take no (simulated) time. The M(foo) means "The message
# a: S M8 J. `/ S // called <foo>". You can send a message To a particular: B" K# b: [9 h- q' K! A1 }
// object, or ForEach object in a collection.; K1 H; p5 S: T$ |* L& b" }
) v9 F1 \/ F9 r- k. [
// Note we update the heatspace in two phases: first run
# p9 D9 q: ^5 e1 U( P // diffusion, then run "updateWorld" to actually enact the/ ^9 |! a6 B. n4 y
// changes the heatbugs have made. The ordering here is
% ~" m7 R1 a$ v' Q // significant!
" Y! j2 A/ c; i! ~4 m# [7 l5 \
& {9 X8 M; P9 l" k4 ^ // Note also, that with the additional
- L! f9 J, G7 A- k6 ? // `randomizeHeatbugUpdateOrder' Boolean flag we can3 N6 c4 V8 Q1 W
// randomize the order in which the bugs actually run
/ V$ j! O) ]: C3 }; G' S // their step rule. This has the effect of removing any. }. x: C& N4 d0 _# U W$ s# C% g
// systematic bias in the iteration throught the heatbug
: R) e1 Y, g6 k3 `% Y // list from timestep to timestep: |8 H; A1 q3 N8 C
( t0 O4 e# f' v* f // By default, all `createActionForEach' modelActions have/ a2 u6 z$ f8 J: a
// a default order of `Sequential', which means that the
' d- ]6 H% s7 O$ U. v, |) ]( X) m6 g e // order of iteration through the `heatbugList' will be
# b" l& {. M+ l // identical (assuming the list order is not changed
$ u& z1 O* n4 |9 k$ T // indirectly by some other process)., }6 d! W$ O# R/ _8 M$ i
$ R1 \8 l8 R2 t
modelActions = new ActionGroupImpl (getZone ());
' ?/ t7 n; b# U: M
) E) o' M7 Q: \6 |# c try {
$ ~+ T$ h4 D. |+ t( h) D/ V7 r modelActions.createActionTo$message* a3 _2 M# p1 \: y2 _
(heat, new Selector (heat.getClass (), "stepRule", false));
% \* O7 j% u6 K! I6 W } catch (Exception e) {
2 V+ ^9 I) w# f' n7 c" \ System.err.println ("Exception stepRule: " + e.getMessage ());' b1 |( `' g& |( x% Z$ h
}
# K* |& K4 i) k9 ?: F
" @# d( X* V. ~- Z7 [1 } s try {$ ]! \( c$ q4 C8 S$ M) x9 C
Heatbug proto = (Heatbug) heatbugList.get (0);
% A h: t; W( C9 W1 A Selector sel = 8 z2 c5 h9 Z! p4 \) i9 Z" f% j
new Selector (proto.getClass (), "heatbugStep", false);4 F- A3 R; ]3 G: j. u' @
actionForEach =# W) Y0 d8 W* u9 r7 ~
modelActions.createFActionForEachHomogeneous$call8 ?( O5 |, Q" e1 T
(heatbugList,
/ k/ X) Q0 B# [ new FCallImpl (this, proto, sel,
3 i2 m: d: A/ @: X$ m0 K new FArgumentsImpl (this, sel)));
0 V2 {/ C' n# t2 j } catch (Exception e) {
/ V7 m5 I; a- _# D# Y6 i4 e7 y7 h% s, X e.printStackTrace (System.err);
s9 J: U, Y2 A( ?" u; V }# r) v5 Q7 ^$ s4 b. H
; ^7 S% I* o# F% ?9 P syncUpdateOrder ();
: S5 L5 Z; d; C$ r' }3 x" s
! U( {5 j* l7 l" E& _ try {
/ S1 B; ?2 ~9 Z3 B1 ]) P modelActions.createActionTo$message
' U% X) E& B9 y) H& ] (heat, new Selector (heat.getClass (), "updateLattice", false));
0 I! v; n( g/ }3 Z4 y } catch (Exception e) {% F' b* @9 S% Y# v
System.err.println("Exception updateLattice: " + e.getMessage ());' J4 M+ E5 J# g
}
: w' @& d8 w5 t
8 k" V( z7 c o // Then we create a schedule that executes the
+ i4 w. m8 `7 c" H( D) t // modelActions. modelActions is an ActionGroup, by itself it
- M8 u" r' D. ~ // has no notion of time. In order to have it executed in
3 _# h- Y/ t0 R, I p% }! g // time, we create a Schedule that says to use the1 r) J( {8 B9 @9 k8 T7 a6 c
// modelActions ActionGroup at particular times. This
9 e$ J! n& d0 ` // schedule has a repeat interval of 1, it will loop every+ C9 }% K p6 T, y
// time step. The action is executed at time 0 relative to0 l0 W8 |' E! i% B
// the beginning of the loop.
+ P5 [# J3 a6 _- c
' l3 c: G2 J; O$ X1 S1 m // This is a simple schedule, with only one action that is
9 J1 `6 d& _* m' _0 C7 ^ w // just repeated every time. See jmousetrap for more% f9 e1 e$ p: I1 o$ f3 w4 o
// complicated schedules.( p/ E2 B6 O8 x) i4 |" t6 h; E
- W; r3 B# z) U- |' {2 ^9 ^) @
modelSchedule = new ScheduleImpl (getZone (), 1);
4 `" ~% k) K4 o7 {1 e8 H modelSchedule.at$createAction (0, modelActions);
) i" `0 I! b+ n4 z' I1 s- w/ s 1 O' R6 c$ A l# F
return this;
% ?, Q7 a) Z- g4 ^- L( N } |