HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# a; W& |! `9 q+ r5 B
% d2 j( C% o0 t/ R( {3 Q, i public Object buildActions () {
p n+ U) R6 L6 F super.buildActions();
' r& g. ?; G" [6 B( K7 L. g W6 }. q% A* A e* H4 p
// Create the list of simulation actions. We put these in
, c! h1 h# ]1 c' @/ G1 [" {8 W // an action group, because we want these actions to be
- l" @4 Q- y( P2 I; q0 a // executed in a specific order, but these steps should
% A2 n& R+ k% i // take no (simulated) time. The M(foo) means "The message0 S( I( |. q. W7 m$ B
// called <foo>". You can send a message To a particular
! z9 |$ w G% Q" ]( `, [: U! G9 q4 |/ x // object, or ForEach object in a collection.7 }- b: h6 q! z% v; `& N* L
" V4 `5 w( P6 @- a
// Note we update the heatspace in two phases: first run
; ]: z' e5 `1 y( z // diffusion, then run "updateWorld" to actually enact the* L/ ~, {& M1 a' p
// changes the heatbugs have made. The ordering here is1 A% h3 H* s3 O, `, z9 {! `$ ^
// significant!" w4 a, a' Q5 m8 L/ l B, i) C
' D3 k5 P$ O: Q& T3 |6 e
// Note also, that with the additional
1 f0 q0 U$ ^( Q; q% \" U // `randomizeHeatbugUpdateOrder' Boolean flag we can
& m( s, [) r# I: e l // randomize the order in which the bugs actually run
" Z- k$ W1 w2 e // their step rule. This has the effect of removing any1 s7 V3 p) U: l
// systematic bias in the iteration throught the heatbug
% I1 T2 P1 z, `9 t) }6 [ // list from timestep to timestep( u; T0 l6 O7 `, ^
4 S2 a' O( C9 p" O0 B5 N
// By default, all `createActionForEach' modelActions have, H8 {/ @' e/ s" X& }$ E
// a default order of `Sequential', which means that the% C, O% t, ?8 ~
// order of iteration through the `heatbugList' will be C+ c2 G$ @+ L) q+ Q
// identical (assuming the list order is not changed
6 C" r; W' Q- H# E' K$ c7 a // indirectly by some other process).
+ g! w5 ^. B1 B
3 P. B1 |9 `$ {0 |5 q modelActions = new ActionGroupImpl (getZone ());, B7 Q+ f$ {8 x
9 I9 j |) s9 _/ F; z
try {. N- \+ h: w8 m- ~- m3 [7 a4 R: K
modelActions.createActionTo$message6 i3 W2 C" G1 e9 f/ \, U, J# S- O
(heat, new Selector (heat.getClass (), "stepRule", false));
0 _ I5 A0 g& t0 z9 P8 B. Z1 q } catch (Exception e) {0 F* W' ^4 K4 V! R7 e; C
System.err.println ("Exception stepRule: " + e.getMessage ());
3 K {8 H9 n2 B0 o" I }! T: r1 a# @$ Y; ?* {
_0 \* H1 _0 k' u. L
try {3 Q" a% k( @: T( X' W% c6 C2 w/ ]
Heatbug proto = (Heatbug) heatbugList.get (0);
$ Q/ X8 F: j; @+ @+ N' H Selector sel = " f! x: d: n% e; m
new Selector (proto.getClass (), "heatbugStep", false);4 @2 h5 @3 g: r* }2 E/ E: o" G5 y3 m
actionForEach =; |% j# f5 S6 T7 ]
modelActions.createFActionForEachHomogeneous$call3 l0 R3 [4 \) l2 t0 X
(heatbugList,8 L; z+ J' d ^. F; D) f
new FCallImpl (this, proto, sel,
! |; Y! L0 w; [0 B7 F1 I% ^ new FArgumentsImpl (this, sel)));4 g1 h2 ]/ F/ W% b
} catch (Exception e) {
4 ?; Y! |8 [$ `: E e.printStackTrace (System.err);
* R( `* {7 }2 N' ]7 g, N. {' y3 M8 R }
' \* G/ e. M n _* ?- g) ^4 | 5 o- a4 n( ]9 w8 w/ k" U2 ?
syncUpdateOrder ();# j+ B' K7 `3 s1 ~ k0 J r
5 t1 P9 s; W' \; f& Y! \
try {' v+ l- C! H6 C1 Y6 b
modelActions.createActionTo$message / O# M" c; N( V9 u
(heat, new Selector (heat.getClass (), "updateLattice", false));
( I* i6 O8 J3 @3 ?! _8 e3 P: p } catch (Exception e) {
& R2 X! ~; w3 i- w" d( M System.err.println("Exception updateLattice: " + e.getMessage ());
4 I- B% e; B* X }: R' n) a: a5 l) Q3 i7 O) }9 T
" X* }3 A* I3 h7 C- |& }/ g // Then we create a schedule that executes the
, i3 V4 g" a% a0 R& ~8 J x, j& b; Z // modelActions. modelActions is an ActionGroup, by itself it( \9 a8 K3 n6 O! |
// has no notion of time. In order to have it executed in+ D& J5 |: Q% n1 ]
// time, we create a Schedule that says to use the
0 z+ g* B! T+ P3 ]$ W // modelActions ActionGroup at particular times. This3 W7 N$ c* a1 C5 K& x
// schedule has a repeat interval of 1, it will loop every5 ^, u# o$ O! o" H% s S$ ?% `8 e
// time step. The action is executed at time 0 relative to2 T) o0 u) \$ {) m
// the beginning of the loop.- h" ]" Q0 O9 h a7 X! `" m
& c& l" ? \. D+ i // This is a simple schedule, with only one action that is
2 E( d. c) X* U4 y) \* W; I // just repeated every time. See jmousetrap for more. I0 Y0 S: }/ h* _, G. A1 @
// complicated schedules.5 D8 b! \: b5 f& f
8 }) B. f. h: W4 H, } modelSchedule = new ScheduleImpl (getZone (), 1);6 z d, v3 V9 h! @
modelSchedule.at$createAction (0, modelActions);
* a9 L6 ~. U6 ?* F M& t
% ~ \$ Y% v5 o' o( x return this;
( _5 }# b' x6 z1 _9 w } |