HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% Z( C6 }. p& H3 x/ A
7 E* N( z7 W1 g0 t: b public Object buildActions () {7 G/ a; V8 h ?) L9 e: A! V6 K
super.buildActions();
6 Y' F7 N q! B. K8 n! v4 Y 8 {0 \" a7 S N/ F) G2 s
// Create the list of simulation actions. We put these in! b- y: I. ^! Q+ P T) I6 x: C. d
// an action group, because we want these actions to be- l3 j: T4 c2 G" A
// executed in a specific order, but these steps should S1 S4 l* K0 [# X! f
// take no (simulated) time. The M(foo) means "The message4 g* Z% i/ p( q* d
// called <foo>". You can send a message To a particular
1 w+ y0 S' P1 ^) T8 p // object, or ForEach object in a collection.& L* ]" ~! F; f5 k' T8 x7 ~0 N ^
6 y! u6 d; Q S% k( z+ l s // Note we update the heatspace in two phases: first run
: b# V: e4 ~8 n9 R* Y) P7 A5 K- x // diffusion, then run "updateWorld" to actually enact the
0 ]. w/ d5 I4 B z- d // changes the heatbugs have made. The ordering here is8 v/ |. W2 @/ \; c
// significant!5 u5 z/ [; I: U8 [
8 m/ K1 {. a! N+ b; a; L. v0 B9 E // Note also, that with the additional
( u. t7 d" E* a M5 A; {0 i9 i // `randomizeHeatbugUpdateOrder' Boolean flag we can
% o3 E" D" [. p0 X( F // randomize the order in which the bugs actually run& l1 h2 L9 o1 S0 s
// their step rule. This has the effect of removing any
) D) G: B* C3 c1 W // systematic bias in the iteration throught the heatbug
% t# d+ K+ q: C' c% j // list from timestep to timestep3 s$ {/ _1 g+ _/ ~8 T
8 }' N8 ?) `3 q4 O( z% w // By default, all `createActionForEach' modelActions have
( K" v/ m5 j# D0 U // a default order of `Sequential', which means that the
- R% `, `1 E$ z3 m2 X // order of iteration through the `heatbugList' will be g: w5 v6 V E
// identical (assuming the list order is not changed# ^1 |" a. @& Z" _- J! ~% n" m
// indirectly by some other process).
) J3 n+ A$ ~0 j( b) ^% l % Q# P3 Q1 C; t1 }& ^. G4 K
modelActions = new ActionGroupImpl (getZone ());; ?5 q* n3 r6 u7 M8 J" V& ?
4 [. u8 {! K% `) N3 d; O. E' G try {9 D4 R: @& I, g
modelActions.createActionTo$message
* O. q j; E" o" ? (heat, new Selector (heat.getClass (), "stepRule", false));1 t1 F" q. x* L4 q9 G& J: S1 r
} catch (Exception e) {
D1 Z% t& Z" H6 {% s2 A4 O+ q System.err.println ("Exception stepRule: " + e.getMessage ());1 `5 t/ E1 T2 l& X& m; L! @; P
}3 E: {9 Z8 ?/ W* e. _. V
5 T+ g( k9 [9 i, F2 [
try {
5 j4 ~& q$ i7 P) C h Heatbug proto = (Heatbug) heatbugList.get (0);& T' V7 |+ a( m/ J1 ~$ Y
Selector sel =
1 I# k* e$ T3 b9 T0 z( p/ T new Selector (proto.getClass (), "heatbugStep", false);: C+ q' q; W) W: b' y
actionForEach =: i7 B' l" m' ~" h$ _ s
modelActions.createFActionForEachHomogeneous$call. \: G) Q1 P6 h9 v8 Q
(heatbugList,* z/ o* V) j3 q$ j7 |" {7 o; F5 n
new FCallImpl (this, proto, sel,% o2 ]1 Z$ ?$ b5 B
new FArgumentsImpl (this, sel)));& ?( z( m9 J4 x: Y
} catch (Exception e) {5 |3 N5 w$ @3 D# g$ R2 y# c
e.printStackTrace (System.err);! ~' s* n" S& x1 y1 t# {
}
9 `1 x6 Q- M1 _8 X5 a& C3 D: h( C
3 e3 J4 E- Q* F6 ]) H syncUpdateOrder ();
; B7 ~* E8 A8 Q5 L" _2 [8 K
+ P& L3 H6 o* K0 E% a& h try {2 {# W- C; e" H! H- ~2 D* H" Z* s# n
modelActions.createActionTo$message
, d e/ ], _& e# R (heat, new Selector (heat.getClass (), "updateLattice", false));
3 X% ~! I( q" M2 ` } catch (Exception e) {3 C+ H6 H- h+ N# _& ~1 S1 M3 b& S
System.err.println("Exception updateLattice: " + e.getMessage ());
2 I& \! B R) p6 f/ T' q: H- Z1 b }% a# l7 Q* \0 Q
9 b0 h, _- J4 H* }9 d
// Then we create a schedule that executes the
! o; a9 q( Q2 b // modelActions. modelActions is an ActionGroup, by itself it% n0 f7 z j4 f, k" ]# ]
// has no notion of time. In order to have it executed in% M2 v: {4 C; M# ~+ h
// time, we create a Schedule that says to use the
* q, N; L" F" i, _( g; H: p // modelActions ActionGroup at particular times. This
- x9 K- X- {1 {7 }3 b // schedule has a repeat interval of 1, it will loop every
b& Y) X( r( t$ ]. R( n! M. a // time step. The action is executed at time 0 relative to
! n& P' L: H2 Q // the beginning of the loop.$ l- u4 o* ~/ x# L A* Q/ r
" h) C" W e& @& ~' \) v
// This is a simple schedule, with only one action that is
) K7 ^% b0 [3 g3 H // just repeated every time. See jmousetrap for more
) a9 t% C% t% y1 X$ [; ? // complicated schedules.0 o) h3 q' C7 ~( X& Z
# O) h5 S4 O3 {4 S4 r& x* B9 i: {
modelSchedule = new ScheduleImpl (getZone (), 1);! C5 T" b+ v$ S, j% S
modelSchedule.at$createAction (0, modelActions);
H. C2 P- T' H
0 }0 k# W: i S+ _ return this;
& P9 H- c- q0 W$ P8 P } |