HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* ]# k( w& x+ t2 G
; z: o# J! s) A5 [
public Object buildActions () {
+ r4 r1 F& a/ `3 Y8 p super.buildActions();
f0 ~& G+ z0 Y+ v* w5 U; b
/ u$ w* K, P* K/ S; W1 p // Create the list of simulation actions. We put these in% |* n5 Z8 C9 b& J4 y
// an action group, because we want these actions to be
$ R1 y8 n9 X2 E( O# r G, Y // executed in a specific order, but these steps should8 u9 O$ ^/ ?4 }' ]& x
// take no (simulated) time. The M(foo) means "The message; t6 B7 z! p9 N
// called <foo>". You can send a message To a particular
1 j2 {5 ?6 |% T2 J4 t // object, or ForEach object in a collection.
5 K: p+ Q0 _3 f! G- x 4 ?4 Z4 _8 a% ?( B. D% P& P1 E
// Note we update the heatspace in two phases: first run
" j' H$ h1 _, e5 a0 g* ]0 p // diffusion, then run "updateWorld" to actually enact the
( n: k1 c0 ]3 I6 O // changes the heatbugs have made. The ordering here is* A9 Z9 j- c0 m: r* ]; q& m
// significant!% D+ B: a% B% _, T' h
0 E- l8 V: l! v* K' s) L8 |! I // Note also, that with the additional
" ~8 t a) j W" a( u" z( ^0 N" T // `randomizeHeatbugUpdateOrder' Boolean flag we can
3 Q/ L7 P5 x- C {7 D0 P& f9 n$ o // randomize the order in which the bugs actually run4 ?, [9 q; V) Z) ^6 P" W, ]8 d
// their step rule. This has the effect of removing any9 ~7 O2 c0 @* w8 V" m% T: o% I
// systematic bias in the iteration throught the heatbug
5 \: Z- ?& S; `; \1 @' y( C // list from timestep to timestep* I' L k0 T' s) | y
8 I) ~* h7 m! ~! L; Y // By default, all `createActionForEach' modelActions have
! [4 M$ a) X) a5 U1 m // a default order of `Sequential', which means that the
9 b/ W8 |" ]3 v- G! F: D9 | // order of iteration through the `heatbugList' will be
2 G' Q; j& i; w& a3 F // identical (assuming the list order is not changed
* ? D9 Z5 R O0 G. k% X // indirectly by some other process).
) G" G6 _% a) W: j* Q
8 Z/ \) p; _& y2 R modelActions = new ActionGroupImpl (getZone ());4 ^ m2 z+ e% R
0 v8 }8 y% Z! |7 u' d4 i
try {$ p9 z5 M' N# |! T, g$ B- V
modelActions.createActionTo$message- W! b) o9 \+ a0 K" @, l
(heat, new Selector (heat.getClass (), "stepRule", false));
5 U- k7 H5 b% y. s } catch (Exception e) {
/ X0 i9 b- a* q7 }# g3 H7 _ System.err.println ("Exception stepRule: " + e.getMessage ());
4 A7 m) }5 I- J5 g- `' G1 s( n }
/ w) Z. V& M# E/ }; @7 i% ]0 x1 s2 l) O5 G% @. t* D% c
try {
8 t% S1 w& L* d2 U7 x' E Heatbug proto = (Heatbug) heatbugList.get (0);
& l4 D6 Y$ r: S- K Selector sel = ' S1 m* O0 ~# J- D. `: Y/ _- B0 U
new Selector (proto.getClass (), "heatbugStep", false);9 B+ ?: ~& r( b, h+ m* q5 V) K, @. C
actionForEach =
6 {4 p# Y# \9 d2 X ` modelActions.createFActionForEachHomogeneous$call4 Y' B: w9 d" t& Y# s' m5 x
(heatbugList,
/ z5 K( W4 G) y2 J new FCallImpl (this, proto, sel,
6 p' [! V2 Z- h4 ~4 v* W' l- ^, A new FArgumentsImpl (this, sel)));
- d+ w8 q' ^0 d; ^" ]+ ^ } catch (Exception e) {
3 ~. Y; p; V: j2 J1 v# a+ ` e.printStackTrace (System.err);) V( O* v5 a; c4 q# M N
}
5 Y* R0 B' _' K5 J( J( W
& C5 i8 a) S" }) w* E% s' U5 \: h syncUpdateOrder ();
$ Z: F) C3 a) ?/ j) K/ O
, q3 t1 K" o: x) N7 a: J try {- b: k5 P: C) b+ `9 F
modelActions.createActionTo$message
8 t6 \ q5 ~, {& S# Y (heat, new Selector (heat.getClass (), "updateLattice", false));
, a6 B- q0 v! e' x$ ^% E2 I } catch (Exception e) {
, R5 {2 G0 [! Y! g7 L System.err.println("Exception updateLattice: " + e.getMessage ());& ~1 |$ g7 D3 S
}' r. E. X: X( u0 P8 l3 s; F( j/ h
/ Q0 T# ] Y, Q/ g+ ^ // Then we create a schedule that executes the) c5 p3 s9 D" z0 y/ `
// modelActions. modelActions is an ActionGroup, by itself it0 a% v7 }7 ?" h0 I8 p/ {
// has no notion of time. In order to have it executed in0 i# Y- W* l' D
// time, we create a Schedule that says to use the7 \6 ]( G# w! S$ s3 H* l# w& u; Q
// modelActions ActionGroup at particular times. This
: p9 N" @) N4 E8 x4 V // schedule has a repeat interval of 1, it will loop every" v2 h5 }- D3 t1 l6 p& l
// time step. The action is executed at time 0 relative to1 A9 ?& ^. J2 j. p
// the beginning of the loop.# ~& ^3 y f9 A A
4 Q( v8 ?; T/ K4 v
// This is a simple schedule, with only one action that is
1 ~# l3 }0 x; Y+ N" k& |: N8 D; ` // just repeated every time. See jmousetrap for more$ }! F( e. t( V8 m7 Z
// complicated schedules.; a9 c6 Q1 L5 d" m$ W& b% p1 w# [" h
' h( h" }( C( v- e( p
modelSchedule = new ScheduleImpl (getZone (), 1);, P5 ] }2 D( w! G1 l: ]
modelSchedule.at$createAction (0, modelActions);6 [# h- s% D- K. Y+ V0 D0 j
* z5 ~ T4 ?' ?% y- {- D0 l
return this;' e, D, s+ |: \" F1 e
} |