HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 ~* E+ p9 F6 u' r/ [* K
) y5 s2 M; M6 N ` public Object buildActions () {
9 ]4 g" Z" ^9 Y* A( d) p: S super.buildActions();5 i% B& Z: C8 O9 i# V) ]
# c: H7 Y1 k G( F // Create the list of simulation actions. We put these in
2 f' Q$ }' r% s // an action group, because we want these actions to be# n. D$ f) n9 b+ b* K d3 W
// executed in a specific order, but these steps should
7 C# k/ W5 p( @ // take no (simulated) time. The M(foo) means "The message
" A7 v: J, q, G* w; \2 L6 @ // called <foo>". You can send a message To a particular0 ^) }' Z, E/ b; M
// object, or ForEach object in a collection., _& W0 M4 F8 R. M: L, r6 t
- H$ \% [+ E6 R, k v2 Y: O // Note we update the heatspace in two phases: first run
: K# d- P% D$ [+ ~% p // diffusion, then run "updateWorld" to actually enact the
" W5 [" I/ |# \; T" V // changes the heatbugs have made. The ordering here is1 C- ~ I3 h& ?% u6 a; p ^8 a
// significant!# o$ k8 E1 K1 E
. c/ C" W+ D- O1 G- U2 J( A; {, Q // Note also, that with the additional
) q) c/ ~3 E5 R- A$ ?& m // `randomizeHeatbugUpdateOrder' Boolean flag we can
. d n' }- o1 c) u } // randomize the order in which the bugs actually run, u j' j. O1 h4 _
// their step rule. This has the effect of removing any2 {: f& x% Y( s+ I
// systematic bias in the iteration throught the heatbug
X: U. d! w& R3 T // list from timestep to timestep, F! L, p6 W# M$ c! X9 K
# C% Y+ e4 U. A1 j6 w. y3 c# ~ // By default, all `createActionForEach' modelActions have
) q2 G# K: i& C% W // a default order of `Sequential', which means that the+ Y% h J i/ D+ d% u6 `) d! F
// order of iteration through the `heatbugList' will be
, @ x2 f; a. x1 c% h# j // identical (assuming the list order is not changed. B$ L ]* q* I5 K, ~5 L% E$ ~. B5 V
// indirectly by some other process).
4 H! l8 o& `; K% M C$ o
& Q, r: Y; ^, l# m8 l) ]4 Q& M modelActions = new ActionGroupImpl (getZone ());5 l% i3 [) H { L h
! _0 z: }0 m. D5 e: [
try {6 J: z- P3 X' F2 `( c
modelActions.createActionTo$message+ s1 c. Q# N/ U8 A- |4 Y' Z
(heat, new Selector (heat.getClass (), "stepRule", false));
b* J+ R$ M9 d5 x& @ } catch (Exception e) {" X$ O# m3 [6 n( W) O! H l, @
System.err.println ("Exception stepRule: " + e.getMessage ());
\: j* o6 s& }% [ }
% ?" \; P1 P, R1 D0 D9 K( T2 z; n' [. H- ?# w' N$ j* L1 M+ N' A* L. o6 Z
try {+ o+ z( ?" S3 @. |7 D
Heatbug proto = (Heatbug) heatbugList.get (0);
/ Y+ k+ J* s8 v# [2 k8 U Selector sel = 0 s0 g2 [6 `; ]7 C2 f
new Selector (proto.getClass (), "heatbugStep", false); `1 C; L3 Q7 O3 L# I4 ^+ \" o
actionForEach =" P* ~8 i. ?- f# E) ^' ~
modelActions.createFActionForEachHomogeneous$call
1 ~! H t A8 u4 O/ C (heatbugList,
# J0 M |8 M8 @ new FCallImpl (this, proto, sel,; V; A2 {) v8 W! d0 S
new FArgumentsImpl (this, sel)));! e1 H5 @. f: d }+ B! {8 k
} catch (Exception e) {0 R4 ]# X) `3 B8 q; P1 ~
e.printStackTrace (System.err);# y5 c; i" J% z2 `) ?
}9 _$ q; X* C& V) p
! m, ~7 S+ N) X
syncUpdateOrder ();
' S5 E# D) A+ X6 {% E) p1 p9 `; y% D. v" P
try {8 \% A7 M2 v# m# K" y" {& J
modelActions.createActionTo$message
( n6 E: l" E% n7 M4 r6 X. x! M (heat, new Selector (heat.getClass (), "updateLattice", false));! A% }( d8 d0 [- G* k
} catch (Exception e) {
. V+ M$ M' A. V% Q System.err.println("Exception updateLattice: " + e.getMessage ());
8 \& H& R+ L1 L2 W- I/ ^ }2 D5 B8 z/ _) ?0 ]( M
# O I8 n+ L7 z) J; X2 ?# o- f" I
// Then we create a schedule that executes the7 n! j; D8 [$ K [) ?
// modelActions. modelActions is an ActionGroup, by itself it7 W- H+ {5 n# B8 l
// has no notion of time. In order to have it executed in
# p! V4 b6 v2 U7 Z& s8 C6 v) x1 i // time, we create a Schedule that says to use the
3 m0 U2 i1 q& W // modelActions ActionGroup at particular times. This/ k& H9 W9 h" I
// schedule has a repeat interval of 1, it will loop every6 I! w3 O8 B: N, c; M
// time step. The action is executed at time 0 relative to
% B( Q! t" w P( G- l/ m9 X // the beginning of the loop.
n+ `6 o6 `( `. H6 v1 C8 l8 Y+ ^: X: g
// This is a simple schedule, with only one action that is3 |. Q* P, B `: D6 ]9 [( s
// just repeated every time. See jmousetrap for more0 V1 K/ h% j" Z; T1 ~" [
// complicated schedules.
+ H* k: V+ M$ d
* |, D" | Q" S( F' B; x modelSchedule = new ScheduleImpl (getZone (), 1);
6 ?+ B9 d2 @5 ]. N' S) y) b modelSchedule.at$createAction (0, modelActions);* D. N8 F: i$ p9 H; N
1 b9 j7 J% J- ~: o return this;4 P; E( ~* k' A8 {( |# r
} |