HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
K- V+ y3 }( I7 p
( l, h- s7 L. d6 p0 t3 o' H( U' o public Object buildActions () {
& i2 H; l7 K3 R" K( j& p0 y& z super.buildActions();& E$ @; X% K3 f: J; \
8 ^6 Z P+ L6 b: A9 B/ q- d9 }
// Create the list of simulation actions. We put these in6 D1 m. w, a" s0 O- [* B% A
// an action group, because we want these actions to be
7 f0 T/ e) A- ^1 t+ b9 x: H0 o // executed in a specific order, but these steps should
* c4 w0 K, ?& y+ K0 Z // take no (simulated) time. The M(foo) means "The message
6 x( C' ]8 G% _8 Z' J4 i8 l8 s3 p // called <foo>". You can send a message To a particular7 H3 o7 b( M2 |' D
// object, or ForEach object in a collection.
% U3 U2 K U# _2 _& y" j, `
6 X& ?: U, V7 P7 Z) x // Note we update the heatspace in two phases: first run( _; S& i2 d+ l! A6 z/ N
// diffusion, then run "updateWorld" to actually enact the9 N0 M) C4 D. p8 S3 U' W
// changes the heatbugs have made. The ordering here is) J- m2 W8 J6 C) t, L
// significant!2 _. }2 H0 w; _8 f" A
2 \* X+ ?* v# \0 R N" ]6 I: E // Note also, that with the additional9 l' g! e; q; O1 `0 H
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 a8 f+ F3 G: P' y* r
// randomize the order in which the bugs actually run
% I4 u# F# y0 {$ i" }/ f/ \$ X // their step rule. This has the effect of removing any( ^- }" O( x. {) j: \! ~9 g
// systematic bias in the iteration throught the heatbug0 H* Z" J9 G" e8 w; \2 E
// list from timestep to timestep
' t6 |/ R* I7 f s% L+ v8 _! M. Q) n . `& y" Y# U ?
// By default, all `createActionForEach' modelActions have$ u8 X* l! @- k
// a default order of `Sequential', which means that the
; X p! ?* B' x! F; T6 X // order of iteration through the `heatbugList' will be
4 l% s- h- ^! X4 h, Y // identical (assuming the list order is not changed
6 v; [8 J. g: O8 B, R // indirectly by some other process).
2 K; C% ]) g3 A2 W6 ]& a j
) @' B; B9 x( H" a modelActions = new ActionGroupImpl (getZone ());
6 |& c" @! O) s- |+ `7 } o+ a0 e% G5 A/ L) u- }
try {* |; w l% e Z! u
modelActions.createActionTo$message
3 K" M: {; x* T" L. V. X/ [9 F( z (heat, new Selector (heat.getClass (), "stepRule", false));
$ f0 N+ i; d) C9 ? } catch (Exception e) {# Z" N! g; |+ M+ w! Q% K% v6 l
System.err.println ("Exception stepRule: " + e.getMessage ());4 q: P! q v" B* q) P, R
}
7 b! E3 f) E# h+ b3 X' U `& B: Z
. Z0 y/ n. [7 T. |2 b8 d try {
3 i3 [1 ]3 V: P7 R8 y# `5 [ Heatbug proto = (Heatbug) heatbugList.get (0); a0 h0 C: a' |
Selector sel = ( H' R4 Q3 x: g) ]% p0 A
new Selector (proto.getClass (), "heatbugStep", false);3 I0 z B" K! {# L- t9 I8 T
actionForEach =
: W, y) ~! w/ L; N3 F) k modelActions.createFActionForEachHomogeneous$call
* W) r1 J- P( k& W (heatbugList,
1 w; `( H2 y6 h# e, i new FCallImpl (this, proto, sel,% G9 h3 m4 d0 B
new FArgumentsImpl (this, sel)));) t# Z! [6 p1 a4 x( o& ^
} catch (Exception e) {
: h! ?/ J c6 \: Y! ~3 O1 q; { e.printStackTrace (System.err);. K( N) C* a8 R# e3 T
}
5 Y- R+ F0 A, d" H; `$ D. v! L0 ^) v 7 v6 ?3 v+ S% y# L
syncUpdateOrder ();& W6 x6 V2 g7 k, F g
/ ?. B! K3 ^/ z% H; Z' A
try {& g5 e; a" `$ _& Q) |5 G) R8 O
modelActions.createActionTo$message ' B5 m5 Y, |( D; J& o4 w; r9 S
(heat, new Selector (heat.getClass (), "updateLattice", false));
. O: j3 c; j" \8 Z7 W1 T } catch (Exception e) {
- ~4 m5 m2 r- {, _& i System.err.println("Exception updateLattice: " + e.getMessage ());
+ j0 U- w0 D8 i! k* U }6 A6 {- I/ N9 S. A$ |
4 K" ~6 z4 _2 |7 y8 _2 _# R$ i' E& Y // Then we create a schedule that executes the% {) w( w3 }* [* ^9 L
// modelActions. modelActions is an ActionGroup, by itself it; P, { w- L" \9 d. j. S
// has no notion of time. In order to have it executed in- _1 U- C3 s; E+ q" x! x' l
// time, we create a Schedule that says to use the
: I. H4 ?4 H$ F. M* \' P* J // modelActions ActionGroup at particular times. This
+ [: }6 t$ @3 ~ // schedule has a repeat interval of 1, it will loop every! v$ ^6 h" \4 m" L6 ^+ u
// time step. The action is executed at time 0 relative to: _, V% \" [9 z2 I6 }# S& i, o
// the beginning of the loop. c# M& Z$ A/ j7 z+ Y' H% k$ e: M
% a- v! a) C' @( J
// This is a simple schedule, with only one action that is
- r- Z$ l) c$ Q // just repeated every time. See jmousetrap for more) O K1 d. l5 Q. \1 x1 e
// complicated schedules.* z, g% U9 |$ C9 p
4 c0 S8 R. V& P. x$ z6 h* ^8 A modelSchedule = new ScheduleImpl (getZone (), 1);
3 w) Q5 F7 O: V7 B* y) L' z# l1 t modelSchedule.at$createAction (0, modelActions);
Y( ]( [$ h2 C* r
! Y4 I+ D5 f, Q4 B, K. ? return this;
) e+ L& }4 x/ I9 V } |