HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) d0 t7 S% m7 L$ {2 |$ O
& M" N! Z6 P Q: x j* E
public Object buildActions () {
- U T) J# u1 G, G k super.buildActions();0 k" _& [2 V; ^, s
) j0 L- j5 N4 |- x
// Create the list of simulation actions. We put these in
! I, g" X% E! T& }2 b" Z3 g! { // an action group, because we want these actions to be) b+ E: K; |! Z6 P/ R/ J9 O" x
// executed in a specific order, but these steps should
1 M! z @+ P: k, T- Y9 r1 l // take no (simulated) time. The M(foo) means "The message. G1 y4 \! ]# y; Y
// called <foo>". You can send a message To a particular. N3 U# U& v& ?* {5 u% }! H
// object, or ForEach object in a collection.. ~6 w, A5 l. `: i, z! t! F M
( Y8 A4 X* }: Z8 T* Z: |! O' Z9 V
// Note we update the heatspace in two phases: first run6 t* }/ n6 D! A9 a: M. Q4 y, a. V
// diffusion, then run "updateWorld" to actually enact the. w8 b/ a4 q% k2 p; }4 t2 W( S) o0 F
// changes the heatbugs have made. The ordering here is3 N: t4 |8 R! e' M$ A" k2 b
// significant!
) P& W, A* G( ?
6 }8 t' T, Y, n" g9 B8 T // Note also, that with the additional
* K$ } V/ D# L- O) L // `randomizeHeatbugUpdateOrder' Boolean flag we can! _$ G8 J7 x. `4 s: [! N
// randomize the order in which the bugs actually run% F2 P( l! N: k
// their step rule. This has the effect of removing any
9 x4 {& p: U. U/ a2 E/ G // systematic bias in the iteration throught the heatbug
& W! \2 Y1 h; D // list from timestep to timestep( s# d/ G9 ?# Y* G4 w! G
& p* f F* x0 X" g. _- M // By default, all `createActionForEach' modelActions have& k. L- Z2 f& r5 D1 L" a2 Z
// a default order of `Sequential', which means that the" P/ W' t+ Z/ B
// order of iteration through the `heatbugList' will be6 V% n0 D/ f- j" v4 E
// identical (assuming the list order is not changed
. {9 W- U& r) X2 D g0 e // indirectly by some other process).% E3 E5 T: e" A8 Z2 l$ J
3 A& O0 B& i, R2 i/ p+ G
modelActions = new ActionGroupImpl (getZone ());2 P3 n9 v! @$ ^( T
9 L! ?5 {' \ a try {
- _2 J. c8 w1 r modelActions.createActionTo$message; T4 A: \/ {1 }1 s6 ^3 G, F
(heat, new Selector (heat.getClass (), "stepRule", false));6 K9 Y2 T* D& p- t
} catch (Exception e) {
, a. i! c& ?: t; m; T! E' G* o" d System.err.println ("Exception stepRule: " + e.getMessage ());
, J& c8 i, d: \2 W' @1 u- O. Z. S5 T }
% k- x( y5 a8 B! I7 ~# [2 Y: R7 N! J. O. [8 T% U& H1 P
try {) ^! d8 R. ~6 i. B8 l# z+ P6 D
Heatbug proto = (Heatbug) heatbugList.get (0);
) b. Q. g+ J: E7 H+ {. _& S Selector sel = % G& G; a4 N2 q- x" W
new Selector (proto.getClass (), "heatbugStep", false);
0 a: G# l( v) W9 Z' o9 m1 ? actionForEach =
- m4 e% q* r D5 B modelActions.createFActionForEachHomogeneous$call
$ p% }2 B& W n( r( j (heatbugList,
" R) F2 F1 A1 l# Z. G new FCallImpl (this, proto, sel,% r/ v! Y' w5 n6 x% b
new FArgumentsImpl (this, sel)));
# R9 K! H9 X/ k9 v1 ?, e: f7 ` } catch (Exception e) {! M" \7 Q' I% A
e.printStackTrace (System.err);( w& R; p, h7 b7 B9 G% V1 K: e
}( {/ x. P( p9 b: w0 t) V6 {
0 p. o: y* [( F2 P; T: P/ m: B syncUpdateOrder ();
4 I; ^$ b2 M% O/ y0 x: a: L# y2 j2 `( X- u9 w2 x
try {7 U8 H6 p4 J/ o/ s+ x5 t, ^7 G
modelActions.createActionTo$message
9 @; J5 g( c& |9 p: J (heat, new Selector (heat.getClass (), "updateLattice", false));" G, I7 ` D ]! k2 K
} catch (Exception e) {
; ~$ o' B1 Y9 z3 V. ^! r8 v" p System.err.println("Exception updateLattice: " + e.getMessage ());3 x/ K9 j# V6 j3 L8 |1 h/ d
}
5 O' V8 X- l5 v( {. N1 e7 ?
' z3 o# m8 H1 N+ r // Then we create a schedule that executes the
) o4 `6 P: `- C- A // modelActions. modelActions is an ActionGroup, by itself it, b% X& |; f+ i8 u2 v
// has no notion of time. In order to have it executed in2 B0 Q4 S6 d: @8 H4 j; x+ |
// time, we create a Schedule that says to use the
5 V7 _+ d1 ^; p" k // modelActions ActionGroup at particular times. This
: } r% _" N% T) M. h // schedule has a repeat interval of 1, it will loop every) _& L+ b; _$ ?
// time step. The action is executed at time 0 relative to/ n& T7 J* j: A$ L
// the beginning of the loop.
7 v/ |+ n4 }- S9 X
, b8 Y. ], q, K& L) k // This is a simple schedule, with only one action that is
- l& D$ v) x5 a5 a // just repeated every time. See jmousetrap for more
( [' E" w, u6 d, b! K // complicated schedules.
" G; h3 B' R' D1 b3 J4 } " x/ m# u* G: v3 `( j" \
modelSchedule = new ScheduleImpl (getZone (), 1);
P# H% T. K8 t6 ?& e- ^/ ~ modelSchedule.at$createAction (0, modelActions);
( x" q/ a1 @" }( r
4 o; x( P4 e: r( J return this;5 a% x$ _" f6 S$ M1 S* D
} |