HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ w8 r4 \" j3 q4 O4 Y& B: [: j! v
5 `* A: w4 t6 ?1 N public Object buildActions () {
, n7 U; K% K6 { super.buildActions();8 ?3 [( _* O7 s0 y% s
C7 h5 ^( i" s. x6 \ // Create the list of simulation actions. We put these in! `7 t& W/ y4 b
// an action group, because we want these actions to be8 [, |+ d- V4 _8 I) R! `
// executed in a specific order, but these steps should
3 U' z' J! @1 c4 @) X // take no (simulated) time. The M(foo) means "The message
) y& D" y* G8 N' O! ?3 p: X // called <foo>". You can send a message To a particular
/ X5 O. P2 \8 o7 \! R% i5 ^7 ?$ ^0 Q/ X // object, or ForEach object in a collection.
, n3 P8 s' j1 ? 8 S/ q) f7 x; y
// Note we update the heatspace in two phases: first run s* J* @: y. o% m5 C |, b8 u2 T
// diffusion, then run "updateWorld" to actually enact the9 j4 C% \1 p+ J0 ]+ E0 F! ]( {* H
// changes the heatbugs have made. The ordering here is
$ X2 }* _; @1 {* q. ?, J. h // significant!) U5 \; M& I8 H! H, }" }! c. X" E
& {. q" \! }2 x1 i: D1 W
// Note also, that with the additional# U5 ]0 K1 n& O7 k! _
// `randomizeHeatbugUpdateOrder' Boolean flag we can, Z, ]" K/ X! M& m' Y
// randomize the order in which the bugs actually run
4 I' b0 y! i+ L2 [% b; j // their step rule. This has the effect of removing any2 |% d s' T$ u7 {
// systematic bias in the iteration throught the heatbug$ i3 _4 y* L/ E- j& h7 `
// list from timestep to timestep
0 Y s: A( y% K 5 q+ k& k* ?0 u b
// By default, all `createActionForEach' modelActions have
# `+ I" W) }" R# P% u ~ // a default order of `Sequential', which means that the
7 F% f* {$ D$ i" h1 t3 x/ W // order of iteration through the `heatbugList' will be
/ z- N/ m' e; u& t* m5 @ // identical (assuming the list order is not changed
- I7 q; U- o* }0 p: H; H/ G& Z // indirectly by some other process).; l& B- \4 z3 _" u
0 M, r- S% _2 n: t
modelActions = new ActionGroupImpl (getZone ());
2 X5 _$ C$ j: R/ y1 O% N2 O: t( K/ d4 H+ q/ }3 T
try {8 X6 K& {; {6 v" D, q7 s% X8 S& D
modelActions.createActionTo$message8 {6 @4 m; y( d8 L2 b. }
(heat, new Selector (heat.getClass (), "stepRule", false));
6 Q4 \( s: f/ W3 q; r' t& g } catch (Exception e) {' m1 N( v* y/ [3 A# |
System.err.println ("Exception stepRule: " + e.getMessage ());
: {! f9 N3 K, \' q- |5 K }: _ L0 p- v0 `( r* Q
, e2 |- H# S# W) o/ p$ l( N$ ^* |. q
try {
- j% w9 q) V9 T8 @; s9 _ Heatbug proto = (Heatbug) heatbugList.get (0);
7 N8 ?1 K* T5 |' t( D Selector sel = 8 E! B: t9 J# u2 A
new Selector (proto.getClass (), "heatbugStep", false);
+ G9 G9 w% y$ t) _; \ actionForEach =- o) U4 O: V/ s- h$ ~9 o: W
modelActions.createFActionForEachHomogeneous$call
0 i+ Y) j/ l7 E) m, \9 @! ^+ W (heatbugList,
$ Q* _$ P0 B0 m' M3 H, M" F new FCallImpl (this, proto, sel,
! N5 w: ~! {4 f9 n new FArgumentsImpl (this, sel)));
! J7 P" K, u1 U& G$ ^7 g* N* v } catch (Exception e) {
& E: |9 @) M) d e.printStackTrace (System.err);" _/ k- W2 X( O8 Q$ G
}; s1 z/ Q! {/ O7 |7 P. T
; U7 x' P. o8 B5 A
syncUpdateOrder ();: G: i) R4 Y0 w0 [. P9 b0 [: F
& j/ S6 L z) Z& j) j
try {* h7 Y* } P: K2 p
modelActions.createActionTo$message $ o4 U: [# L9 ^# ^
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 x3 R6 h" p# t+ R4 Y6 V& G } catch (Exception e) {
3 C" Q6 o4 h j7 ^3 [5 ]6 u System.err.println("Exception updateLattice: " + e.getMessage ());, p" t- k0 }" [2 t: b+ Y' g# z
}
. X8 | E8 `! }4 x9 p0 l
5 p7 q4 Q; K6 _. c- u // Then we create a schedule that executes the
( b& G/ \3 q' n5 R; y7 A // modelActions. modelActions is an ActionGroup, by itself it
6 p0 b' {( P6 I- o9 z // has no notion of time. In order to have it executed in2 _' D# T7 X5 O6 ~
// time, we create a Schedule that says to use the
0 z8 L- d( X% }0 U* X9 i7 S // modelActions ActionGroup at particular times. This
, v6 Z2 \. p9 @: `8 Q3 B // schedule has a repeat interval of 1, it will loop every
' k+ J. l6 {4 R# S6 X // time step. The action is executed at time 0 relative to4 Z, l1 T9 S- e
// the beginning of the loop.
+ I. y! \% {9 f% T1 @+ H5 s2 A
2 f9 N5 _- P. J! U7 h; u3 q# ` // This is a simple schedule, with only one action that is, H2 O" F- e) |4 k+ F+ Z! f
// just repeated every time. See jmousetrap for more
6 \" E: l; b' v' A4 D // complicated schedules.
" D- k2 n; f& j* F) c" m6 v . M+ \- L0 [4 M+ @2 a6 _
modelSchedule = new ScheduleImpl (getZone (), 1);
6 K q' Z6 b# Q: { modelSchedule.at$createAction (0, modelActions);
, x* }4 {1 V1 e3 q# B: x8 Q# k
5 M; B$ ?; l, U( ~: ?8 t return this;
- a% c% |' z* v4 k) G& E } |