HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: w m2 V) O$ A; {- L) g; o8 x* a' Z' o5 B/ \7 F. q& J7 S
public Object buildActions () {
* W& c1 T+ y/ v) X super.buildActions();0 N. t7 z8 k, X/ A
, J( W. M. o$ h3 [9 q
// Create the list of simulation actions. We put these in
+ A2 f, Y, L. v$ V: I' ` // an action group, because we want these actions to be! }+ `' ~0 J: Q! q
// executed in a specific order, but these steps should
4 \' c4 ~4 p0 ^% p1 g // take no (simulated) time. The M(foo) means "The message+ S u+ }2 ?5 H3 n2 A
// called <foo>". You can send a message To a particular
3 A& }- {1 y: W% K // object, or ForEach object in a collection.( Z5 W- z+ s, L
( q7 a2 C! N3 D7 w2 Z
// Note we update the heatspace in two phases: first run, Y# Q" }3 y4 ]: P I, n% g
// diffusion, then run "updateWorld" to actually enact the
# a& s6 z% _0 u5 ?( W4 d // changes the heatbugs have made. The ordering here is! ]' c& D: M) x8 O4 a& @. d3 |2 k
// significant!5 @8 n5 j2 }- R W5 i; M( ~
+ v1 Y$ G& ~! h) G" _
// Note also, that with the additional: N. w; w& I% w% i/ T; ~! y1 Y
// `randomizeHeatbugUpdateOrder' Boolean flag we can
k$ k$ X6 Q2 \$ s* `, d // randomize the order in which the bugs actually run; M: d2 C2 b( `9 p. }3 X, `
// their step rule. This has the effect of removing any6 R' \8 F$ P9 b3 k: V- ^- e
// systematic bias in the iteration throught the heatbug8 K' O0 q) V; I+ \( `5 Z: J4 [
// list from timestep to timestep
9 N* e2 Q2 ], o1 n' Y3 _7 l
" I! r+ U" p9 F6 Z, f/ z // By default, all `createActionForEach' modelActions have
$ C4 Q: O4 Y8 k/ O |- m; [, ? // a default order of `Sequential', which means that the& ^/ y# C- Z$ ?- S9 Y+ m
// order of iteration through the `heatbugList' will be- \) q' g/ r( A+ M
// identical (assuming the list order is not changed
0 K8 b# x5 m: l3 q5 P // indirectly by some other process).
: m( [* [- O! q* \8 v& t. T + B8 M2 {7 {# K' g
modelActions = new ActionGroupImpl (getZone ());
4 Z b6 {; y$ h# m0 J; v& s; r! \! V
try {
- I( ~+ M: R# a/ k modelActions.createActionTo$message- h# Y: s. L+ b( d& i9 b
(heat, new Selector (heat.getClass (), "stepRule", false));- s8 z0 q3 C) @2 L$ G8 W; s1 g
} catch (Exception e) {
6 @! Q3 k0 M! m3 `2 w+ y System.err.println ("Exception stepRule: " + e.getMessage ());7 \% M/ f2 b) f' w
} W" l4 K/ j8 S( Z
6 A3 {4 h7 v8 I4 w8 V try {" K1 C& o7 H/ a- ]! h
Heatbug proto = (Heatbug) heatbugList.get (0);* t# I" b( l, ]- O' o8 B M' ?
Selector sel =
9 |( {4 [& P" @5 c. C2 b! A( S( ^ new Selector (proto.getClass (), "heatbugStep", false);
0 _( ]) l: J/ ^9 A* E. Z actionForEach =5 O. Q1 `3 f1 W+ p
modelActions.createFActionForEachHomogeneous$call
7 l+ M' O/ {: T5 e) J) q C (heatbugList,$ [5 l z; ~2 e/ ? R6 G4 o# R: h
new FCallImpl (this, proto, sel,
6 C5 b4 \& N0 @( j' b new FArgumentsImpl (this, sel)));" d; d2 G" K! K3 E8 O
} catch (Exception e) {
) |% j5 y& r( [ t+ K( ~1 G e.printStackTrace (System.err);
5 c1 f6 W) a+ h8 _$ q }
% b+ J9 p5 T e0 X: Y9 M
3 D* a" q, X6 l3 s6 ^" U" q9 H syncUpdateOrder ();
8 p( P" m: C) G, h' ?2 [
# U* k: v' K3 D- ~# d) l try {
0 B" i. ?% D. [! E+ u, Y4 P+ P modelActions.createActionTo$message
6 p! ~" @) w/ M8 Y$ T" H# E (heat, new Selector (heat.getClass (), "updateLattice", false));- @0 F( q0 x- M1 K6 k
} catch (Exception e) {
) {* d/ f! }0 {# _4 [3 | System.err.println("Exception updateLattice: " + e.getMessage ());
9 M; l% J) [& F/ h }5 G/ O) [1 r& i! F1 ]0 ?3 @
8 C& K0 f. }" T* w
// Then we create a schedule that executes the
" y, f8 s. T) ^9 h // modelActions. modelActions is an ActionGroup, by itself it
* b! [# d7 q: j' S9 p // has no notion of time. In order to have it executed in
& T- a$ M! _* _9 [9 }2 ~1 j- L! S // time, we create a Schedule that says to use the) j1 J& y9 p K
// modelActions ActionGroup at particular times. This
% p# ]- Y& M( N3 q; H // schedule has a repeat interval of 1, it will loop every, _6 Z4 K6 L2 x' `7 G
// time step. The action is executed at time 0 relative to9 t: i3 o& B7 Q O
// the beginning of the loop.
?. ]5 I# \2 O, }8 j8 A' Q3 E/ q( F! k) h; x0 g
// This is a simple schedule, with only one action that is# c: G4 E; d' \8 L
// just repeated every time. See jmousetrap for more) j' C9 P: H8 I) z
// complicated schedules., F, ]9 V4 E0 i/ j5 _
. ? S1 M9 E. \' j- _8 T# Y$ f7 i modelSchedule = new ScheduleImpl (getZone (), 1);
- O% c% x6 F% y' H- ^0 R modelSchedule.at$createAction (0, modelActions);
8 N. m3 ~7 d9 v( I' G2 X7 }6 l
* ^% G7 X9 J% T0 I9 I3 K return this;
' N; l. g! [4 a3 y/ Q9 o* O } |