HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; X u$ b- e7 s* F
; X: k2 M2 [: u3 Z) b9 f( p" n public Object buildActions () {9 _8 c4 T2 O( m. [9 [2 I0 f
super.buildActions();
% I4 B- O$ V( O9 }" } 0 G& {8 K6 w. ~: C
// Create the list of simulation actions. We put these in9 Y, D0 K& m3 c$ e8 I D; I
// an action group, because we want these actions to be
5 z9 W. Y1 Z+ a+ h( g+ R9 k // executed in a specific order, but these steps should
3 _0 O7 y4 x2 O3 }# W( { l // take no (simulated) time. The M(foo) means "The message! e/ O, q/ a) P$ z( z! F( c- O/ @
// called <foo>". You can send a message To a particular
+ b. O7 h& m& O6 _0 ] // object, or ForEach object in a collection.
6 e: _( O, l5 o8 o& c
j( T, r- N, Q // Note we update the heatspace in two phases: first run+ o; s6 u1 ]0 u& S# v9 k, y
// diffusion, then run "updateWorld" to actually enact the
' W* t/ R+ H+ x1 A3 ~& v* \ // changes the heatbugs have made. The ordering here is c8 T: g; Q: ] R) ]3 H
// significant!
5 O o" u9 i! L
$ \3 C' X! D: t9 K& Q // Note also, that with the additional
9 x2 g5 L+ t8 g1 N( b0 U3 [ R // `randomizeHeatbugUpdateOrder' Boolean flag we can( H: M- s1 Q) M P5 e' q
// randomize the order in which the bugs actually run8 H0 Y( O1 c' L" L5 q0 ]4 r; M
// their step rule. This has the effect of removing any4 {* k0 S9 s4 H& E4 `" E6 Y
// systematic bias in the iteration throught the heatbug
" b/ `/ B+ S$ x4 m5 f: d& T // list from timestep to timestep
& H d5 R# Z [0 x, T0 c) ?
9 y& R6 P) i* S* G7 ~ // By default, all `createActionForEach' modelActions have
& d- u1 _# ?0 t$ e // a default order of `Sequential', which means that the% j6 A7 z# \( u- ]( s+ U1 n
// order of iteration through the `heatbugList' will be: X1 j; K8 {- R: J- `% _& f
// identical (assuming the list order is not changed
' m5 }* z$ r. S" o; r // indirectly by some other process).
7 e. A8 _ {7 i: G q0 _% A
/ h( J6 r1 M* O; J& |( r modelActions = new ActionGroupImpl (getZone ());
# l# _8 A* w- s; t- m8 y
- `' j$ P3 Z% x Y0 N0 |# Y try {
G9 x, W7 z1 {' l4 s% \3 ` modelActions.createActionTo$message5 X: u: o% I3 v) F" A% P: U
(heat, new Selector (heat.getClass (), "stepRule", false));# G K( T: J' ~3 V" u
} catch (Exception e) {
* l7 s- q9 g, ~0 a G& B System.err.println ("Exception stepRule: " + e.getMessage ());- Q8 Q* N# V6 Q' O" _$ u1 \
}
; Q3 U6 ? l1 f$ c. k1 R, u1 Q2 {; }6 N3 n k% T1 ?
try {
7 j- V: q* U2 r: z Heatbug proto = (Heatbug) heatbugList.get (0);+ b+ X- K; N# `5 L& n$ _; f0 r
Selector sel =
! U7 C X6 M8 m V& D# h new Selector (proto.getClass (), "heatbugStep", false);4 m3 N. D, f* L) [+ A8 h4 e, L
actionForEach =+ v0 s8 e6 S4 H$ Z! ]1 \2 y' a* G
modelActions.createFActionForEachHomogeneous$call' ]0 r* y: ]3 o
(heatbugList,7 X0 W; q3 u& B. |: w3 c1 ^
new FCallImpl (this, proto, sel,
. s4 x6 n1 K3 x7 Z8 W0 ?% i- b" Y new FArgumentsImpl (this, sel)));8 y8 z5 \! f" n) E
} catch (Exception e) {
+ M ~$ Z5 s. i, H9 H& r* Q, w; w e.printStackTrace (System.err);6 I* b2 G7 W/ t- g) w* z0 x
}
) T; \6 u+ {' S" I3 [ , O7 _; E+ u; ~) @3 N; j* b: N! T
syncUpdateOrder ();
2 J7 a: k9 w! N4 Y0 H9 C8 v6 l" H8 v9 l0 }* T T
try {9 u" i% t6 N+ b
modelActions.createActionTo$message - s' m+ V) r- N
(heat, new Selector (heat.getClass (), "updateLattice", false));
; t) A5 J% }# ~! p! s } catch (Exception e) {, u J+ A/ Y+ q2 k' j$ _9 n, q
System.err.println("Exception updateLattice: " + e.getMessage ());- ]$ q2 u* ?. X- c1 Z7 F* a) K
}
# G- }# c, L9 e' H% m
w/ I/ H8 z) l- r( {. k5 ?8 ?# f // Then we create a schedule that executes the6 c: F! g# l2 F- G. R
// modelActions. modelActions is an ActionGroup, by itself it9 H w9 w2 J- G/ W& a' Z3 q
// has no notion of time. In order to have it executed in. z1 Z( ? A" O
// time, we create a Schedule that says to use the7 h) A& K& [1 [, A" i0 ]6 j
// modelActions ActionGroup at particular times. This
1 ] t7 g; I) L // schedule has a repeat interval of 1, it will loop every/ q+ e; R# y! g/ Z
// time step. The action is executed at time 0 relative to
) q9 F# f# @) z+ W+ H // the beginning of the loop.
5 o0 |* _/ S, b% ]7 v8 e i# I7 V) f% d, C ^# T Y- X) D
// This is a simple schedule, with only one action that is
; |! c- a; V0 L! M3 U // just repeated every time. See jmousetrap for more
/ E: X1 h" b6 t* o6 k // complicated schedules.* S+ s* l5 u% [5 b
# x# z5 |/ U: Z2 F! W
modelSchedule = new ScheduleImpl (getZone (), 1);
# p# c+ w Y8 S$ I modelSchedule.at$createAction (0, modelActions);
+ F$ u1 }* j3 ?. |0 g" e; s2 B0 j
4 P( u! X7 r6 s" m9 U return this;/ K5 R: l' K# J/ G% N/ Z
} |