HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) e/ M: N1 Q0 \" H% }) U d, y
: R: B+ R T2 a( Y) \, u
public Object buildActions () {
5 L! H# v7 a4 D super.buildActions();% [& B' n% J! @3 {1 _% P' k
: _* c k6 f& m! d. Y2 ] C( J // Create the list of simulation actions. We put these in
9 w/ N4 _! E# w$ G // an action group, because we want these actions to be& ]2 ?5 i9 x0 h7 d: O- z
// executed in a specific order, but these steps should& B/ J, W4 [. y+ k6 U5 _7 B
// take no (simulated) time. The M(foo) means "The message
) j8 T% Y! {7 f0 Y5 b* ^/ q // called <foo>". You can send a message To a particular
' L2 ]8 f0 f8 U. d // object, or ForEach object in a collection.# u5 v P! y. T3 u8 P
4 }/ _. x% h2 ?% n S // Note we update the heatspace in two phases: first run9 ?4 H& ?' I( a" Q5 s3 k
// diffusion, then run "updateWorld" to actually enact the) ~. h, w2 j1 [) ?1 w# ^$ ?3 C6 v+ g: H
// changes the heatbugs have made. The ordering here is
, u p" L! P; G/ s1 { // significant!
; t7 g: Q" b. Y
/ Q8 U% e% \7 [) A% n9 x0 [ // Note also, that with the additional: Z) s# q( D+ {$ M
// `randomizeHeatbugUpdateOrder' Boolean flag we can
0 W& K6 W$ L5 r6 V // randomize the order in which the bugs actually run
$ x8 B5 y3 K$ w* ]& r6 S/ f) n // their step rule. This has the effect of removing any
[# {+ r/ L; l% O // systematic bias in the iteration throught the heatbug
1 r9 G S! _7 y, w$ f // list from timestep to timestep& o! j0 b" f% {1 B
! t0 m1 ~4 G' v
// By default, all `createActionForEach' modelActions have
/ C, c: `( m8 F // a default order of `Sequential', which means that the% F4 S2 A+ j; B7 a# {7 s
// order of iteration through the `heatbugList' will be3 ^+ Z3 d' s H* y8 I3 p
// identical (assuming the list order is not changed& \6 Q+ }" u* w6 p/ N3 O2 E% L
// indirectly by some other process).0 @2 J% O% O# r. ~' l# f) z0 ~
8 C" b N2 D! w2 U4 [
modelActions = new ActionGroupImpl (getZone ());
* i6 w, t0 o8 N0 A- m& Q! O1 c) ~! M
4 a7 [$ e" H5 }& A7 I% R try {2 D* Z: g* n; ^8 O
modelActions.createActionTo$message
2 r$ y0 S% X# E1 ]! A* S4 t; h- _+ e- } (heat, new Selector (heat.getClass (), "stepRule", false));, ^9 Y# m7 _0 w' j0 e4 L
} catch (Exception e) {
! W1 |- U+ \! j6 G1 W { System.err.println ("Exception stepRule: " + e.getMessage ());
; u) n4 \. g9 {5 E6 G( `) t }3 ?8 W D& {9 e M5 K H* h4 q5 @
" U4 ` p! v, @# E5 e# J try {9 P! @+ [; W5 u# H k$ c: Y
Heatbug proto = (Heatbug) heatbugList.get (0);! y% k2 Q3 ~" s
Selector sel =
/ }- g8 y' C3 i4 j new Selector (proto.getClass (), "heatbugStep", false);
+ \9 D: r* A P& M actionForEach =
9 e8 l' m f6 p, e2 p; d. g modelActions.createFActionForEachHomogeneous$call) h& H* N G0 e( R- }
(heatbugList,9 y0 ^. x3 l0 I9 }% f1 I
new FCallImpl (this, proto, sel,
8 q0 \$ [2 M. h% J! o5 ? new FArgumentsImpl (this, sel)));
/ [1 I2 E# j7 l } catch (Exception e) { ]5 M/ l6 R. e" o' X' K0 e
e.printStackTrace (System.err);% x6 [) m& y* [/ r1 ~5 o1 T# e
}
O+ ~$ {% d+ y- T" u & f* ?/ R: y, a z
syncUpdateOrder ();0 F. d4 e: x9 ~0 e. P
' C+ P' V/ a& n" ?9 l( G7 w6 \ try {
2 `9 \# z8 r4 t$ q modelActions.createActionTo$message & ^, `* N6 u3 K, d2 Z
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ P1 v+ v! r; t" }. m" v, Q } catch (Exception e) {, J" }! a- ]! A/ }9 D. F0 ]
System.err.println("Exception updateLattice: " + e.getMessage ());7 J2 @8 M/ M+ q4 N# f
}
7 \, g9 r* y- T- J+ u' D$ \' w
1 u, G r* Q1 t0 K: p) y // Then we create a schedule that executes the
9 F0 m, w# B. S% ^) r // modelActions. modelActions is an ActionGroup, by itself it
8 H. K; o* j$ O8 U5 x // has no notion of time. In order to have it executed in
! S0 P% O4 }2 I' f // time, we create a Schedule that says to use the) a5 K4 @- a0 F: M
// modelActions ActionGroup at particular times. This
2 r- s d; X5 ~: M1 y // schedule has a repeat interval of 1, it will loop every( n/ c4 f( C- m* w. I, ], A/ \/ p
// time step. The action is executed at time 0 relative to
; O1 b6 j' t0 W- V. m l // the beginning of the loop.9 N$ D4 U5 `/ r* V: l' g/ i
8 J0 i8 r) ?8 S9 B8 L: Q5 \! Z // This is a simple schedule, with only one action that is6 T/ n4 b0 t( ~) P' i
// just repeated every time. See jmousetrap for more/ C3 K- j1 b/ y: l( h, ]
// complicated schedules.6 U7 T4 _2 ~: V, W' L9 w
) G' V5 I: g' @- B
modelSchedule = new ScheduleImpl (getZone (), 1);7 h% O& m Z2 x6 m+ y" [1 Q0 o: M
modelSchedule.at$createAction (0, modelActions);
; L/ B" \4 L7 f- Z* F* G; Z % S& X% g) l3 |0 |
return this;4 M* [/ d5 w7 r
} |