HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- p* {. U; ?. h3 y
5 j+ p6 k7 @8 ^4 _$ W/ A
public Object buildActions () {. B1 W) |* b6 F
super.buildActions();, {. g# ~ Y% |7 B
1 y, ?9 c) O* I+ ]" n/ r
// Create the list of simulation actions. We put these in; q, h1 \0 d$ X
// an action group, because we want these actions to be# [( c" `; H2 o6 C- r1 I/ v+ ]& {
// executed in a specific order, but these steps should
; ~6 l3 Q6 P) M% P5 |5 V // take no (simulated) time. The M(foo) means "The message
) Z# q1 S& J! x$ ? // called <foo>". You can send a message To a particular
1 M5 _) ~9 U. t* r9 y // object, or ForEach object in a collection.
. q; h$ J, v) [; B1 _. J - X4 t+ k- b' ^" W
// Note we update the heatspace in two phases: first run
, {4 Y) m+ Q1 c: c // diffusion, then run "updateWorld" to actually enact the) [/ a9 [$ h9 C! I- S
// changes the heatbugs have made. The ordering here is6 F0 R; c6 w* ^+ ~( C, o$ } s+ k
// significant!
5 p. H) M/ ~' C 8 b2 n8 C5 ^% E' R) X+ R
// Note also, that with the additional
& @3 W0 G% y" E3 C // `randomizeHeatbugUpdateOrder' Boolean flag we can' O1 V+ f6 `8 h' F$ U) Y F& ^
// randomize the order in which the bugs actually run
* b9 Q( y- J( D: W" e, u" f // their step rule. This has the effect of removing any
6 Q7 f) k( C2 R" C7 M // systematic bias in the iteration throught the heatbug
8 [! A" X: K( I# U! O! t5 k0 V // list from timestep to timestep# I/ ]: i+ Q5 b1 z- K3 ^" }9 W
2 F& V' T, ^. @" s
// By default, all `createActionForEach' modelActions have
+ E( \+ H: H( Q, a5 ?3 N% ~ // a default order of `Sequential', which means that the) E; a. a2 V0 O( \5 S0 ~, D7 t
// order of iteration through the `heatbugList' will be
8 y- L \1 R2 q) V* C // identical (assuming the list order is not changed
1 ~ `/ z6 ?. z* R7 i( C- A // indirectly by some other process).
+ @5 J# y7 d9 [" r$ O3 w0 b+ \ ( H3 \* ?- C3 w# N- j
modelActions = new ActionGroupImpl (getZone ());
: K n( b/ W: w8 T
: D% a, t0 u, e( ^ try {
* V G5 D) |% j4 U0 V. [ modelActions.createActionTo$message
0 C4 \5 t$ p1 q1 z, Z; h (heat, new Selector (heat.getClass (), "stepRule", false));
8 r; X, d& p. Z1 p } catch (Exception e) {3 w# T+ o: d L/ t
System.err.println ("Exception stepRule: " + e.getMessage ());
9 w# z! j/ }0 x+ U& u/ l }
3 K+ ~$ L* C" a* R$ S) u4 j7 {7 ~" e4 f
try {
" v8 z/ k0 M6 y Heatbug proto = (Heatbug) heatbugList.get (0);
0 p" I) B9 a/ u/ }5 b Selector sel =
. j) W6 W, k: k new Selector (proto.getClass (), "heatbugStep", false);" g4 S b" U" D) n9 i1 ]' ]
actionForEach = }$ Y2 r) g ^# B, U" U8 {
modelActions.createFActionForEachHomogeneous$call ~) p3 N U6 m. |: c, C
(heatbugList,. E4 h3 e2 m8 \6 _7 d
new FCallImpl (this, proto, sel,
) N) J, }& u. m, p( H new FArgumentsImpl (this, sel)));
% |8 Z2 @. V+ _5 A' \ } catch (Exception e) {: c3 R9 W5 d; q1 ?& ]* m" ~
e.printStackTrace (System.err);8 Q3 O2 k0 o5 d1 G' X4 L- C
}
" e+ I E; k: i5 [6 F
/ V3 ~" a" A4 K) _ syncUpdateOrder ();
- I2 N- H! [) k* j1 G5 Z) P
8 w6 _% L1 o8 `$ k4 Y try {
; @ ]4 G# i k& ~ modelActions.createActionTo$message
2 _0 J& m2 N% n+ Y (heat, new Selector (heat.getClass (), "updateLattice", false));+ I& Y6 ^* S2 v% h: ]2 c4 X/ F5 R
} catch (Exception e) {: n8 k6 t) A8 K X* ~( T
System.err.println("Exception updateLattice: " + e.getMessage ());7 T% ?$ G% O4 ^
}$ j# y" d$ @: i' O, Z/ u5 T
. [; H4 B& h( p; `& @7 ^) l // Then we create a schedule that executes the
R4 m( z1 V* x g2 \ v" T1 c( \8 g // modelActions. modelActions is an ActionGroup, by itself it6 g* g4 R5 x# Y4 G
// has no notion of time. In order to have it executed in/ r# @& G' a% o$ p+ m
// time, we create a Schedule that says to use the
7 }* c' K5 {2 J9 q$ Y/ g( _ // modelActions ActionGroup at particular times. This
$ a* ^4 H+ G; I* i) Z: J$ X( u // schedule has a repeat interval of 1, it will loop every
2 D+ b9 Q# `1 W5 c3 s8 S // time step. The action is executed at time 0 relative to
, R' C1 X4 c e: y // the beginning of the loop.: Y# U" T) ?4 j: q. M
, Q8 X+ z+ ], P9 @: w' P6 h r // This is a simple schedule, with only one action that is9 X$ n: u, E+ d0 i1 ?0 U
// just repeated every time. See jmousetrap for more
N6 r. ]. e, w // complicated schedules./ N( [% h, Z+ q# [1 s* x0 E
4 k% y8 u2 s2 X# r3 W; C' n
modelSchedule = new ScheduleImpl (getZone (), 1);
: { z& A, n) R, y+ ^3 }0 o modelSchedule.at$createAction (0, modelActions);: y/ e& i8 {) G3 P( K" _; D4 h
1 ]7 {! r6 F+ E5 ^7 s/ S- ~! Y. b return this;
; d& W& D4 [! {7 C$ [" z% G } |