HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* }9 r, d" v6 g: k1 O& I9 W
+ p9 X) J; p; R* G2 C5 T public Object buildActions () {' |0 r2 O* }5 Y9 q7 [+ O1 W: v0 Q
super.buildActions();% v5 i' T* Y: L* s# U4 F9 d7 j( g
0 a5 P% g5 N3 b4 f, [$ [5 }8 Y
// Create the list of simulation actions. We put these in% d2 D: t% I0 ^, [! h& I/ J
// an action group, because we want these actions to be" T. y& a0 q7 U. D: \# g4 o; i
// executed in a specific order, but these steps should
2 x$ |2 {/ o/ m' p$ g // take no (simulated) time. The M(foo) means "The message
: W$ I( u) ]# F% h! Z7 A- P // called <foo>". You can send a message To a particular. J' C: p" l: f) N3 ]$ o6 E
// object, or ForEach object in a collection.# M* s3 x2 ]$ L$ B. m- @) b- u: R
, X, Z9 ], m/ W9 t, U
// Note we update the heatspace in two phases: first run+ ]! |7 P9 R T) J
// diffusion, then run "updateWorld" to actually enact the4 Q, x$ G) {, `# K
// changes the heatbugs have made. The ordering here is& o3 S5 ^7 }! Z
// significant!# {5 i. F; x$ x
( @6 I4 A; ^' z. H% ?: V
// Note also, that with the additional% x: ?+ P: f9 o* O9 B! y3 M7 h
// `randomizeHeatbugUpdateOrder' Boolean flag we can' y: v: h3 g% E8 W+ K, z6 p+ N
// randomize the order in which the bugs actually run
& J( P2 v9 U2 O' T) }; I' U // their step rule. This has the effect of removing any5 I+ X v# G s7 {
// systematic bias in the iteration throught the heatbug; i" p+ z+ f- O$ I% @
// list from timestep to timestep3 _8 b; N8 V6 C7 Z' @
4 p$ G, v+ l9 d$ O$ p3 L // By default, all `createActionForEach' modelActions have0 r6 N2 _3 u$ R* @% k
// a default order of `Sequential', which means that the
1 Z& S: B0 w) C // order of iteration through the `heatbugList' will be
7 b8 P+ o/ D3 S) Q2 R // identical (assuming the list order is not changed0 b' T; ~; }9 f: m% `& h
// indirectly by some other process).
$ @: @; s: w+ G6 N
- J/ ]3 B+ o! K: ^ y& T+ ?. } modelActions = new ActionGroupImpl (getZone ());
# n$ g+ P) n* o7 O W2 V! O! e* q6 s7 F o: ?$ u
try {
( S. J. c# ^" ~' G modelActions.createActionTo$message! z) a0 Z1 N! L; _- k# P/ u7 n
(heat, new Selector (heat.getClass (), "stepRule", false));4 ?7 k4 i2 G* H2 M, e' v, ^
} catch (Exception e) {
( n4 g8 y' x+ u0 \" J1 _7 ?/ w System.err.println ("Exception stepRule: " + e.getMessage ());
3 ^/ Z1 K% A1 F8 T v }
8 S7 e* a) | w/ W
5 h2 U6 w) n! V/ D( `$ N3 ~ try {
* C6 }; n4 P% m4 e5 ]' E. F Heatbug proto = (Heatbug) heatbugList.get (0);
" x" ?! l! M# `/ p; ~. p7 j Selector sel =
) u2 a* ?5 a x/ S1 H new Selector (proto.getClass (), "heatbugStep", false);
: z' f5 ~$ [: }: P/ a+ r. X/ E actionForEach =
$ G9 B4 E) ~: H9 u modelActions.createFActionForEachHomogeneous$call, }% J" q. [% q/ D# v
(heatbugList,+ p) z5 W% W* \( b: s; M, _
new FCallImpl (this, proto, sel,
- u- Y) ?2 m0 [8 o4 R new FArgumentsImpl (this, sel)));
, \/ M! i8 A$ `/ Z2 C1 L } catch (Exception e) {9 t& K0 ^; G! S# B0 k) @
e.printStackTrace (System.err);4 p8 I& t$ i$ e: l( y! s; d
}! [; V1 j! y1 a
) E/ [' O' Z4 k
syncUpdateOrder ();7 i& g: X4 I9 c5 M' V
) y8 D! ^; [4 p5 H: P; ` try {0 i8 N* b1 H6 N- n
modelActions.createActionTo$message
* n7 P2 `! E) a: H8 c (heat, new Selector (heat.getClass (), "updateLattice", false));
( N. G; F3 B2 ~2 I3 P } catch (Exception e) {
6 n4 r" |0 l& L: U. d! J- L. c System.err.println("Exception updateLattice: " + e.getMessage ());+ J- ^) y# [+ v7 H2 B+ g) O/ o. ]
}
; C9 I& |* G }7 S) }( [ , y1 x, q; g: b% P; S
// Then we create a schedule that executes the
: m# }2 ~8 y9 j7 q8 I" ]: n' y // modelActions. modelActions is an ActionGroup, by itself it
/ E" K0 {" u T8 L# u3 X9 p" ~ // has no notion of time. In order to have it executed in! |/ I1 Y5 h. S! @+ l7 {4 [
// time, we create a Schedule that says to use the
7 I# H; M% [% c* e1 |! d& H // modelActions ActionGroup at particular times. This. J: Q1 C2 Y9 v \2 J
// schedule has a repeat interval of 1, it will loop every
9 S" n" Q. s2 d3 e // time step. The action is executed at time 0 relative to
7 N, J8 a3 k# I // the beginning of the loop.
0 Y% H! i, ]. L: d2 @- ]! |8 d* d9 I, y/ {7 J4 `+ }0 T
// This is a simple schedule, with only one action that is$ D( n6 X! @/ e; [/ H* j4 _1 o: B
// just repeated every time. See jmousetrap for more
( P7 v2 M7 r' ` // complicated schedules., M: z7 T' O, k7 v/ ?" u
0 P7 ~& h4 b# T* u% p: d modelSchedule = new ScheduleImpl (getZone (), 1);
, f$ q, J! y4 l modelSchedule.at$createAction (0, modelActions);$ w- {9 a: c" X1 r3 M2 j
% _$ U# H/ ]% R7 N; b1 p return this;
9 ?4 m' S p6 h; b; S1 S5 N } |