HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# H- j" V, A, `: q7 u
% U! X5 C; e4 k. A9 J public Object buildActions () {
% }( U2 l S* L- N* h super.buildActions();
J) n$ p! j$ ]8 T1 a . I9 i1 ~0 A# \7 i
// Create the list of simulation actions. We put these in
1 R' g8 B: ~; w( [ D+ H // an action group, because we want these actions to be% H" y9 I$ @+ D! @. k5 z4 j+ G
// executed in a specific order, but these steps should
" e0 \6 Z, H( s // take no (simulated) time. The M(foo) means "The message% U S$ h8 W* Q+ f( O$ F" v- v
// called <foo>". You can send a message To a particular! t2 T# \: C% G0 M& z. O' L
// object, or ForEach object in a collection.
/ {0 w9 \* l: E
3 M/ D" M* ^9 Y4 O1 f5 O% w // Note we update the heatspace in two phases: first run
# ~$ i4 S% G/ y8 Y // diffusion, then run "updateWorld" to actually enact the
- s5 x' U. K5 i: G) t- C" T // changes the heatbugs have made. The ordering here is
" V v0 \# X; T" V# h! O2 | // significant!% b6 g3 ? Q! \6 T& t, I2 G
$ X6 h4 I" R# y" r6 ]/ p& h
// Note also, that with the additional2 L6 Y! @4 \9 L" U1 ^+ l. h# i
// `randomizeHeatbugUpdateOrder' Boolean flag we can
% n \3 o# y+ N9 c+ Y6 t: H. y' P: w // randomize the order in which the bugs actually run- I* L4 | b9 [8 E$ C
// their step rule. This has the effect of removing any
0 m! A/ V! v' S: B# ?0 A // systematic bias in the iteration throught the heatbug a/ c9 G; }( p! a* A$ a5 |- K6 ^
// list from timestep to timestep
4 z( k$ B; ^ ]6 g; V
# m! c: `6 {: d2 Y: R4 E // By default, all `createActionForEach' modelActions have1 R: y2 c/ V. @1 R6 {* o0 ~' I
// a default order of `Sequential', which means that the( B |& u( }! P& y! N
// order of iteration through the `heatbugList' will be
4 a, s3 _# {" \ L // identical (assuming the list order is not changed& \! ^9 x: P! z& M$ J4 ]% a
// indirectly by some other process).6 j' s. t" T6 y
, b( v9 }: n) o$ w1 w' A1 k: \
modelActions = new ActionGroupImpl (getZone ());' h p% {/ B9 q+ c, Y7 J$ C- T
- u, c1 E U' l5 A, m) `/ p try {& X$ d' n* D8 o% M1 r) P
modelActions.createActionTo$message
2 Q7 X/ r9 t/ M4 W# b (heat, new Selector (heat.getClass (), "stepRule", false));) _- J8 U) w+ \# Z* `
} catch (Exception e) {9 ~. n8 u. r* i q. `
System.err.println ("Exception stepRule: " + e.getMessage ());6 n; V7 ^8 u" b/ M9 E
}
$ f9 B$ O( l2 ~+ }8 Y& B" }2 y
, l& J0 M3 ^+ T. Y9 t. i$ G# n try {
5 ^8 {8 _" u- A; Q5 v Heatbug proto = (Heatbug) heatbugList.get (0);
% l" O# a7 g* \' I3 a# k$ C+ V3 e2 m Selector sel = ! f" P8 A0 e; h, T. P5 l# U9 o1 S
new Selector (proto.getClass (), "heatbugStep", false);
. C( L L) I- i7 p! f actionForEach =2 A9 R% G- E! g
modelActions.createFActionForEachHomogeneous$call' c9 c7 E/ K( H4 ^% s
(heatbugList,
4 b( G& ~% c+ i$ K% S* Z new FCallImpl (this, proto, sel,$ E! a8 d( w( R( {' j& C9 N/ G! P
new FArgumentsImpl (this, sel)));2 U+ x% S. G- i
} catch (Exception e) {6 a( e+ l, _8 u9 R6 f! s3 j
e.printStackTrace (System.err);
+ A7 N! ?# ~* E }0 x8 u3 z+ w; u6 O
" l. K. P7 g3 O7 A syncUpdateOrder ();3 s5 E3 S$ v1 F m5 E r& o
* p( B" m6 d. v5 L' K" T
try {* S, p2 J0 U9 f& ~
modelActions.createActionTo$message - ]1 O8 [( G& ~0 p; k% B+ Q
(heat, new Selector (heat.getClass (), "updateLattice", false));
# ~( V" o3 {6 m" D- a } catch (Exception e) {
) H {- H2 \2 u) Q System.err.println("Exception updateLattice: " + e.getMessage ());+ e3 {4 ]. W& X7 K1 a: x0 N
}
# r s/ Y2 @9 |3 t. f* G8 l
9 X7 X4 i8 b8 Z1 d8 U // Then we create a schedule that executes the
# y7 W. X1 f7 m // modelActions. modelActions is an ActionGroup, by itself it
9 y& O; K; i) q( C" J // has no notion of time. In order to have it executed in
1 ~; y3 K9 G) k5 p- Z/ f. c // time, we create a Schedule that says to use the
9 f! u! H& l: k/ F2 _3 z // modelActions ActionGroup at particular times. This
! {, {, L( Q) [& {* P* m // schedule has a repeat interval of 1, it will loop every
' y( J6 K* ]' m // time step. The action is executed at time 0 relative to7 _$ |* X: v- d- H% y
// the beginning of the loop.
7 H. z1 k7 g- f$ h, i7 G& u6 x7 ~- c+ B7 E5 z
// This is a simple schedule, with only one action that is" n1 Z* |0 J) y# Y) c
// just repeated every time. See jmousetrap for more
: W$ d: m: V+ Z9 @2 v // complicated schedules.3 b& L( L9 d; v4 z( p& a
t$ X6 o+ N' n5 D$ r* j1 K
modelSchedule = new ScheduleImpl (getZone (), 1);
" G& R' L( K/ ]; F/ s* k3 s9 L! g modelSchedule.at$createAction (0, modelActions);1 |' h |5 J2 J3 }
3 ]5 n' Z+ {6 A return this; L' w7 Q. Z# a$ X8 L8 U9 X" e
} |