HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ W$ X1 k5 z" h( g
7 ]+ l! `$ R& u& ~ |0 P& ]9 o$ f public Object buildActions () {& m' x' L: \7 w8 y* _" b1 S
super.buildActions();
& P/ g! \- c5 y+ ^; l; h ( [; l! e+ \: g
// Create the list of simulation actions. We put these in! [0 F- J3 ], J& V" B- S
// an action group, because we want these actions to be
+ t8 m% A' o- e3 q7 @ // executed in a specific order, but these steps should
" t' k" u& o7 p4 p // take no (simulated) time. The M(foo) means "The message
( `; W/ V( { Q // called <foo>". You can send a message To a particular w' h k0 R# J- S
// object, or ForEach object in a collection.
- b8 G7 c* e$ j- m: ?/ }$ t: B
. h0 |- R+ _/ G // Note we update the heatspace in two phases: first run* g9 r4 P5 @: l6 S8 n
// diffusion, then run "updateWorld" to actually enact the
3 Q) N7 G/ [8 R // changes the heatbugs have made. The ordering here is( x% q% q# n9 y
// significant!
/ c( S2 f* x b! n/ V8 Z+ u
7 B% i- i8 [! I // Note also, that with the additional
) v# ^" O Y' M! B8 s# Y // `randomizeHeatbugUpdateOrder' Boolean flag we can
; W8 k) P! u5 h // randomize the order in which the bugs actually run+ _2 o! p( ~, b' C
// their step rule. This has the effect of removing any
, U b7 C. ^7 I; n, l! E( f9 B // systematic bias in the iteration throught the heatbug) S9 ~( ~$ _. o0 t
// list from timestep to timestep5 A, U, _3 }, ?! n; ?0 {6 d- J9 h
* Q+ ~# }5 t8 \: \4 | // By default, all `createActionForEach' modelActions have
! k! P- v9 ~/ C- @+ n // a default order of `Sequential', which means that the ]( w4 n1 Y) K( {$ t
// order of iteration through the `heatbugList' will be
' R' N/ X7 B4 N( g. m* u0 g' g // identical (assuming the list order is not changed
v9 j* E$ T; c // indirectly by some other process).& _3 I5 U% f7 u- C% X
9 y' [. l) r8 q, y2 A
modelActions = new ActionGroupImpl (getZone ());
" e% M) i5 n9 d) P# z# c% ?! |# t9 v& _+ `6 ?( v. N
try {
5 R0 s6 V& R0 b modelActions.createActionTo$message6 y4 Q0 k( t9 E5 Z2 M, ^
(heat, new Selector (heat.getClass (), "stepRule", false));" a% q) N; k# F+ C
} catch (Exception e) {
6 @, S# N- v- t) R5 d4 x/ P System.err.println ("Exception stepRule: " + e.getMessage ());
& O8 _! w+ y* I' i/ x. a: n }
) t" e( T# ^ }" }/ j9 ?0 Z* C
- y, x& t) j; G" a& k; s5 Q+ B; H try {" L- n3 `( C+ `/ M8 U- i. z) r
Heatbug proto = (Heatbug) heatbugList.get (0);
2 x, ~1 M, ~% Q4 m$ m$ s Selector sel =
8 q1 h# `/ ]; f+ d7 @' j new Selector (proto.getClass (), "heatbugStep", false);
: [4 J+ b/ B1 a$ q# p# q! \ actionForEach =
! w$ L9 D7 \- o0 g4 B modelActions.createFActionForEachHomogeneous$call8 t2 V& W W% d! s, m S: ]+ g
(heatbugList,8 y; A" j2 k# v! v! x
new FCallImpl (this, proto, sel,* c+ J' I0 W3 ~- l! N2 A1 o1 e' Y
new FArgumentsImpl (this, sel)));6 x6 S. Q7 B+ l9 R: e$ N$ }0 D0 U
} catch (Exception e) {, I% q q4 L) `9 {* a
e.printStackTrace (System.err);
& A b3 q) r, p% _& L% X }
. Z' f) M( g O0 y8 i) Z
: i* a1 g* `0 P5 D" |- ?& y syncUpdateOrder ();0 t; c8 o8 }5 T6 U5 N" f. Q
; y$ \) p! d& p3 [ try {- [* Z E# g# J+ p7 n
modelActions.createActionTo$message
7 J9 _! E1 T% E# Z5 a (heat, new Selector (heat.getClass (), "updateLattice", false));" O; \# L' C( h4 T3 y
} catch (Exception e) {
4 l7 R' b8 |6 O" b& T# |" l System.err.println("Exception updateLattice: " + e.getMessage ());% b- i9 N: R' R9 `; A3 W
}7 Z3 I2 N( x7 u6 H
5 D0 ]/ ~1 d4 n5 o' f! n, C
// Then we create a schedule that executes the
4 u! P# ?7 v) Y3 p5 G! | // modelActions. modelActions is an ActionGroup, by itself it
: @% S2 i" n( h6 ], N // has no notion of time. In order to have it executed in9 A/ H+ ?* D4 m
// time, we create a Schedule that says to use the0 h( c0 a9 p$ {8 }2 Z8 b! D
// modelActions ActionGroup at particular times. This8 K9 ^8 J; W! y' z# t& w3 v3 i. P
// schedule has a repeat interval of 1, it will loop every- h7 K6 C$ k' D7 o$ B( S
// time step. The action is executed at time 0 relative to; d. y/ F1 y3 w- j2 I; q
// the beginning of the loop.7 t* a8 a3 n1 B2 u) @7 S/ I
" g5 Q+ D9 v8 L* j) v Y1 X/ d // This is a simple schedule, with only one action that is$ W7 t6 T" e5 g. |
// just repeated every time. See jmousetrap for more
6 }+ o9 Q' W% k7 b+ e' I+ X5 X // complicated schedules. [ d2 q# K- {) ]; [
) K7 B# t! X2 f2 v: c1 i
modelSchedule = new ScheduleImpl (getZone (), 1);
" M& c3 O4 v0 q+ ?5 m modelSchedule.at$createAction (0, modelActions);' X. N9 t( }. V
5 f ~. R) S$ q5 ~2 q& v return this;
2 `+ w `: l8 X/ T/ P! S } |