HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 G+ r6 R0 H* ]+ |; Z, ^2 S8 F3 J4 y( j
public Object buildActions () {
: h3 K) k3 a3 V4 } super.buildActions();
, r) R- V& P; Y6 G
3 O7 h9 v* N$ Y' R7 j$ L5 b // Create the list of simulation actions. We put these in. Y. W# f6 c4 J5 _2 u
// an action group, because we want these actions to be
- k7 n4 ~( C& |7 q6 v1 L' M // executed in a specific order, but these steps should% \: x) }1 t A, R# r8 z: B
// take no (simulated) time. The M(foo) means "The message
3 G2 Q4 L* e1 O // called <foo>". You can send a message To a particular
1 k; x/ r7 n3 [3 i/ ^ // object, or ForEach object in a collection.$ a y; K& ^/ v g7 D' I
+ E# m+ k9 Y1 t6 p. v // Note we update the heatspace in two phases: first run, g; e- N7 s: N
// diffusion, then run "updateWorld" to actually enact the
' r: D# m' n1 X2 q. f // changes the heatbugs have made. The ordering here is
- A& K8 |! U5 Y) d" v% o // significant!
/ m! F8 _3 C) A: A
+ O- K, B8 E$ ~9 o% u // Note also, that with the additional7 c8 o: e6 s- }
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. R9 _$ n9 y+ n4 s, N6 T // randomize the order in which the bugs actually run- a, C# e6 k) N# L: S
// their step rule. This has the effect of removing any
0 L5 z7 D6 p* y3 ~) |. P // systematic bias in the iteration throught the heatbug
7 J! A+ [7 T9 a' }0 i5 j7 q // list from timestep to timestep
0 a* }1 v7 P8 ^- l1 e1 H$ k " v& n! ?9 u, o7 d( K @
// By default, all `createActionForEach' modelActions have
( W l) J/ w4 l/ B- W- z4 | // a default order of `Sequential', which means that the
3 L7 c1 x o. x0 `$ s0 ~/ w // order of iteration through the `heatbugList' will be& A' Y7 D; m/ m9 w
// identical (assuming the list order is not changed
2 v/ c" V( d" T1 x5 x* Z // indirectly by some other process).& d" C$ v) ~" x: W) r( ^) v9 x/ u
* V, p5 ~2 w* ~9 X5 @ modelActions = new ActionGroupImpl (getZone ());$ t' {' t; ?2 H" v+ G- K
3 e6 c' H; f: p9 g! x4 d0 f: s r1 W try {
" f6 k! I: q" {1 G! d" u: [ modelActions.createActionTo$message
7 l& T" m/ j" \1 w3 [9 F- D (heat, new Selector (heat.getClass (), "stepRule", false));
3 {, F( H, g( e+ z2 K } catch (Exception e) {, S* v. v7 w0 `$ ^
System.err.println ("Exception stepRule: " + e.getMessage ());: \; S1 H2 ^; l0 a9 x& v
}
9 V; X6 x6 ]; E1 ~5 ?8 m4 B
0 j' v7 x# G0 }, F try {1 k8 ], D( i$ T4 y1 r' n0 _& ~+ S
Heatbug proto = (Heatbug) heatbugList.get (0);
) g9 v/ Q- w, i: J% x7 A Selector sel = : g3 t1 s3 W% k
new Selector (proto.getClass (), "heatbugStep", false);
' ~$ h' v% ]# t) j* b actionForEach =
$ E. a1 v* o8 o+ a, K" V. b modelActions.createFActionForEachHomogeneous$call6 t% f F/ D' m1 z
(heatbugList,0 P8 ~* K' S3 z! l: N) ^) f
new FCallImpl (this, proto, sel,
- A/ X9 s; i7 O$ n. ?9 C new FArgumentsImpl (this, sel)));8 Y K" ?% V3 ?
} catch (Exception e) {7 b# B, p+ `5 L q% a# j
e.printStackTrace (System.err);
% a- g4 _+ f# P( K" P, J p }* j( ]/ q$ w* s( o( R+ t' P
/ i: ~0 v4 t) b2 D% \% d
syncUpdateOrder ();
( Y# c% d* Z6 u& g
- m' B' n. M4 d try {
) I6 v- x7 Q$ l; J, ` ]9 _ ]+ c modelActions.createActionTo$message
: K7 d; M: _! d# ~( U: O5 s (heat, new Selector (heat.getClass (), "updateLattice", false));
& e- H A/ U Y5 p } catch (Exception e) {
: x4 b- p+ _* U System.err.println("Exception updateLattice: " + e.getMessage ());
, w6 {# g: {5 z0 C. [" l }& |- i+ V1 ]+ d! X
% r- u6 y/ P$ n5 _
// Then we create a schedule that executes the
0 D8 I" t; d' _7 m s2 ^ // modelActions. modelActions is an ActionGroup, by itself it# K, d& e; y9 I* O" Q! p
// has no notion of time. In order to have it executed in& R! m- b0 `8 A6 i+ O
// time, we create a Schedule that says to use the
+ C0 i& H& q3 w1 j3 L9 H5 z4 A( z // modelActions ActionGroup at particular times. This# k- U8 ~/ g# q c; V# r A2 J
// schedule has a repeat interval of 1, it will loop every
$ \/ p/ {3 b1 Z$ W/ ^" D# `& q // time step. The action is executed at time 0 relative to
. t8 Y) [1 s# c // the beginning of the loop.
3 V6 G. K) t0 w ^8 i" Q8 N! _& Z) G
// This is a simple schedule, with only one action that is
3 G# P0 [# N' T4 Y+ G, N3 B- P) T // just repeated every time. See jmousetrap for more8 I' k: D& x& T' U5 {
// complicated schedules.
$ T! x& D" R! J/ \0 l4 d
; K6 E7 d5 ]2 d# z9 a modelSchedule = new ScheduleImpl (getZone (), 1);5 R y4 l, p U/ ]0 {) ?
modelSchedule.at$createAction (0, modelActions);
0 i+ q% P1 S; k& ^! D4 v8 W ( ^( G& W7 y& g" s
return this;0 d) h; \1 A/ f0 }) Z
} |