HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& A7 M. k# w+ x; m
+ g6 n0 f/ N% A3 D& _2 B | public Object buildActions () {5 [- ]5 ~% @* l% l
super.buildActions();% |, N" Z# N. b8 a. N* I
" c" c+ i# U/ ?
// Create the list of simulation actions. We put these in
; W+ V/ N [8 w- U" f // an action group, because we want these actions to be
: S! n1 }3 j$ I/ y // executed in a specific order, but these steps should
! [8 M }' e: n( s // take no (simulated) time. The M(foo) means "The message
8 }$ q) N6 G- P# K+ n // called <foo>". You can send a message To a particular2 {# U; U1 l/ l; b8 n
// object, or ForEach object in a collection.
6 d, n8 o# k' e5 @& q: {
* U4 _7 r# p4 l // Note we update the heatspace in two phases: first run+ m% w7 ?. t9 ` u) J
// diffusion, then run "updateWorld" to actually enact the- F* j1 ^$ r/ k) B
// changes the heatbugs have made. The ordering here is
7 m' m; x% O3 H/ h* f' c& f9 t. F // significant!$ t4 a2 I4 P9 P5 r1 y$ Y2 ~
4 C8 P3 ?5 ^" p // Note also, that with the additional
) X! u1 d. l. f8 @2 u3 c# M // `randomizeHeatbugUpdateOrder' Boolean flag we can2 A' l' p8 S4 C. u( c& l
// randomize the order in which the bugs actually run6 a$ ^9 k# K5 i$ V
// their step rule. This has the effect of removing any+ \) j. g2 h+ _" M' r, e
// systematic bias in the iteration throught the heatbug
$ s0 ~" l0 D+ z) D: B& M, t) [ // list from timestep to timestep
5 T+ q3 v% h+ R/ {# i9 z: n) d
$ }: R; `8 T' d7 U // By default, all `createActionForEach' modelActions have
6 }' z8 z3 j7 f6 s% E0 O // a default order of `Sequential', which means that the# ]8 g! s/ ~5 W; d
// order of iteration through the `heatbugList' will be
7 g+ e6 Z4 i* A6 t \& H // identical (assuming the list order is not changed; x: F9 L0 Z% o9 `+ [$ f
// indirectly by some other process).7 C9 g; L( Y4 l2 h q+ j, U, M" B/ s
0 v) B4 ^2 h* K7 l& o) V' A! E modelActions = new ActionGroupImpl (getZone ());
& u; }& ^8 v7 `+ e3 Y2 i2 Z' H g' C0 _* k6 b! o0 ]3 r) h) s+ ?
try {
3 X* k/ T4 c+ J% g2 g5 U modelActions.createActionTo$message* ^( @$ t" {/ [& Z, D9 v* M$ ?- o
(heat, new Selector (heat.getClass (), "stepRule", false));% Q6 @1 ]8 T9 C2 S% v; k
} catch (Exception e) {
* ?6 @7 t7 S3 u# D) j System.err.println ("Exception stepRule: " + e.getMessage ());/ g5 n9 e9 T& B$ t5 D. x( B
}0 _0 m8 u% h q
$ s; m0 n+ h7 I( d& z3 T6 i
try {' ^, y! \- }: V0 _: h( h% ~+ t
Heatbug proto = (Heatbug) heatbugList.get (0);
2 s6 n, B9 x0 ]/ q7 _; s Selector sel = * j H$ ^5 q& O+ A+ T; ?4 P! `
new Selector (proto.getClass (), "heatbugStep", false);/ r& c7 ~" o- x @
actionForEach =* s) g! D) ^' k
modelActions.createFActionForEachHomogeneous$call2 g2 X$ z1 W6 A/ C
(heatbugList,+ X' z. l: S) R3 e3 y
new FCallImpl (this, proto, sel,# f9 ~7 [# U) V1 M0 K: y+ B
new FArgumentsImpl (this, sel)));
' W6 ?) |( E! H9 z" O g0 X: |- b } catch (Exception e) {/ t; j, s/ f, ^
e.printStackTrace (System.err);8 f6 H$ N; I# M' O( ~
}
6 t5 d% \! m: l. Z0 a% b2 ? 0 a* ]' _" A0 g
syncUpdateOrder ();( C2 }. g( s$ n7 Q
( ]$ q* O# }( J; d! x( i+ t
try {
0 j5 f7 @' i& J* y8 d* Z0 V1 _; G" T& K modelActions.createActionTo$message $ G: V5 h+ Y! O$ j3 u6 E& [
(heat, new Selector (heat.getClass (), "updateLattice", false));3 |, x( ?, @) g7 n0 O
} catch (Exception e) {* E0 U! ~8 H( P2 |+ u( t
System.err.println("Exception updateLattice: " + e.getMessage ());* b. u* f% e }) A& X
}$ s# v8 ]! C% d4 L
# q! Y% ?7 k: L // Then we create a schedule that executes the( O }7 Q# s0 c. U& J) `
// modelActions. modelActions is an ActionGroup, by itself it! a0 {1 h: i4 a" W: b' S
// has no notion of time. In order to have it executed in
8 k8 U+ K9 |" q# T* d& ` j+ ? // time, we create a Schedule that says to use the
9 G( l' w$ a+ g3 c! f3 c // modelActions ActionGroup at particular times. This
* L! H! H5 g# `9 T4 w0 ] // schedule has a repeat interval of 1, it will loop every4 n2 Y, u) Q$ a
// time step. The action is executed at time 0 relative to, i/ F/ L, H* J9 @
// the beginning of the loop.& l. L8 \) p, M: ^( H x# o a3 E
, M: P: k& r2 c1 G6 s
// This is a simple schedule, with only one action that is$ z k5 U5 e/ {; F6 y; D2 p
// just repeated every time. See jmousetrap for more. }1 l0 x. v/ \) ]; I3 i* h6 ?, u
// complicated schedules." Q. G0 f+ W" {$ S
& X* M# }/ r' J3 ?( t1 ?
modelSchedule = new ScheduleImpl (getZone (), 1);) {0 X& b! a" @7 b; ^9 @' ^6 {
modelSchedule.at$createAction (0, modelActions);% h* H2 ^) X" }3 ^1 l
* H) n, Y( G3 ]/ `4 I5 h7 {* n# y6 d
return this;
$ d9 {8 Y0 x$ i) {$ g } |