HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 A# z4 C2 S/ r A u. x% U y
* }2 O" y4 v+ I
public Object buildActions () {
$ y# I) F1 b: a6 R# n super.buildActions();7 O$ B) R# f. y: }1 x* g& i
& a' z. F, l+ F$ K% r0 U // Create the list of simulation actions. We put these in$ C5 n C- F3 _9 E, P. \' r$ a. j
// an action group, because we want these actions to be
5 ]3 r$ o+ o1 m+ B1 _ // executed in a specific order, but these steps should
* g9 n$ p4 W& b2 ^! @ // take no (simulated) time. The M(foo) means "The message
) o0 s; p9 \7 S3 \ // called <foo>". You can send a message To a particular
8 x* ~$ P" t9 _7 U% K // object, or ForEach object in a collection.
1 m4 U! X. }9 v; L$ D % Q) P- R& S( J1 F
// Note we update the heatspace in two phases: first run ?: Q) X0 o; t6 u
// diffusion, then run "updateWorld" to actually enact the$ P, m: H+ F- h- e4 e# F6 `
// changes the heatbugs have made. The ordering here is" d& B! A |& t/ j% O3 f3 T$ ]
// significant!
5 a1 q% v7 u6 P' E2 Y- {9 W! @
, F* I( q" C8 L: _6 a2 K // Note also, that with the additional
& g6 e! }4 J; @4 w6 I% f$ o // `randomizeHeatbugUpdateOrder' Boolean flag we can5 d$ O2 q6 M% k5 N1 N
// randomize the order in which the bugs actually run: {* `0 _( S6 S/ g4 {1 [8 Z+ P! c
// their step rule. This has the effect of removing any
6 _8 M. p& A$ Z1 u# | // systematic bias in the iteration throught the heatbug$ o+ _+ z& n. S4 D3 D/ {7 j( w
// list from timestep to timestep P0 v! E0 C; ]
, v i7 l' a2 A: x* B // By default, all `createActionForEach' modelActions have" U4 Z5 R8 K% K& B& J1 h& T
// a default order of `Sequential', which means that the) Q4 S, f% W/ U; w) w
// order of iteration through the `heatbugList' will be0 ^# v/ S; W" q4 _* W
// identical (assuming the list order is not changed
( J$ p+ X. U& ]2 b. N& k2 ~ // indirectly by some other process).
$ x# d X2 q: E - R# C) _: L1 u9 b N/ [
modelActions = new ActionGroupImpl (getZone ());
1 F! r4 `5 X1 y6 I. E7 D
' N7 H2 j. |1 U* o8 Z9 ? try {+ b% t) d0 x7 D1 }# v2 E
modelActions.createActionTo$message
( Z3 y* j' N! h! l) Q2 `4 A (heat, new Selector (heat.getClass (), "stepRule", false));+ f' e2 w$ s4 g& }+ [
} catch (Exception e) {8 k( ?$ L5 G7 D( `5 p) o
System.err.println ("Exception stepRule: " + e.getMessage ());$ Y: W7 i9 }: b7 L) Y% X; g
}
3 |+ b( I+ r7 p* U, C# ?4 Y! o; n j- }" e% O
try {
8 k8 l# b2 c) m Heatbug proto = (Heatbug) heatbugList.get (0);
7 S! Z" e; i o% { Selector sel =
% X+ J" V8 ]" n new Selector (proto.getClass (), "heatbugStep", false);
# F' K0 @ r1 X* M1 l! L6 h8 r" X actionForEach =
0 S+ j# \8 o. _4 R4 W) _5 ?4 A) q0 p modelActions.createFActionForEachHomogeneous$call
6 Z% t! ~1 j0 L (heatbugList," m* k" P9 X3 {4 A7 h# O: X$ S
new FCallImpl (this, proto, sel,2 d2 k( o, h7 B, {+ w2 y
new FArgumentsImpl (this, sel)));
) `; D8 S% j1 w* u. I5 E5 L } catch (Exception e) {" E; W3 b/ ]1 s) x B; ]2 v, H6 {) u
e.printStackTrace (System.err);$ e2 t% M0 C, V: E7 Q
}
# i6 o$ K" ~' v0 x2 r 6 a0 E7 s, D7 K0 W0 Q
syncUpdateOrder ();3 H( Z$ }% ~4 a: c( ]
+ d% o1 g; V: O
try {
6 ]2 M* k( s) y: e* N$ q/ ^ modelActions.createActionTo$message , a' w6 z: A& M
(heat, new Selector (heat.getClass (), "updateLattice", false));
! m$ H/ n6 `0 c( h: K7 ] } catch (Exception e) {+ L$ l" D- u# C) A/ b- N
System.err.println("Exception updateLattice: " + e.getMessage ());; A+ G9 ?! H2 P$ p- _
}
" o- V' H8 C% o- t5 z
+ A2 _+ M; j# l2 }5 I: W // Then we create a schedule that executes the P# @) Y( M [
// modelActions. modelActions is an ActionGroup, by itself it' C U- |9 q9 L" [" d% y; J
// has no notion of time. In order to have it executed in
, L. R/ I" a; L P; R" r( O" B // time, we create a Schedule that says to use the6 K! d- `: w& @ n2 G
// modelActions ActionGroup at particular times. This0 q9 t' K8 C! v+ [, X2 P" G' f' U+ H
// schedule has a repeat interval of 1, it will loop every9 ^. Y' o! Z j+ h' T) j
// time step. The action is executed at time 0 relative to
. x9 f6 s- j6 d) }0 H8 Q // the beginning of the loop./ e2 h: X. ~# |
2 v0 l( W+ q/ r" N // This is a simple schedule, with only one action that is$ Z" F9 c2 @4 h I
// just repeated every time. See jmousetrap for more8 Y0 N, e& b; P) S) _# ~
// complicated schedules.
; ?$ Q6 I# O, e \8 T( y+ y* t& i
d0 X- L- U! r2 e6 H2 ] modelSchedule = new ScheduleImpl (getZone (), 1);3 q8 y' o" d& s3 Y# p c
modelSchedule.at$createAction (0, modelActions);1 S# h; r9 G: Q8 D
1 a2 S v" r( |$ H7 L return this;5 |. i# G% ^9 ?6 g3 n, o
} |