HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 j1 G8 M) Y% Z2 a; N
?: b+ U) |& a2 [( X- u) Q
public Object buildActions () {
0 {/ y1 b' h1 [5 P, i4 e super.buildActions();* v3 Y* A! Q, C- X! m8 W! \$ ^2 e8 ^
) z4 T; F. D' P* m6 }7 D5 \
// Create the list of simulation actions. We put these in) G+ D% S {6 F0 I9 S
// an action group, because we want these actions to be3 Z" C% V$ U, x" [+ f
// executed in a specific order, but these steps should& D4 E* `1 Y* Q$ |5 `! F$ J) T
// take no (simulated) time. The M(foo) means "The message
, ]9 s4 Z- Y1 o) t* B: ~/ ^ // called <foo>". You can send a message To a particular
+ D2 a' \9 `" w' H& j5 d( J( ? // object, or ForEach object in a collection.
6 B% u' b: u3 v8 j- f & b8 _ F+ f5 `- d0 y( q
// Note we update the heatspace in two phases: first run/ e$ O$ v8 M2 q0 l. |- }
// diffusion, then run "updateWorld" to actually enact the
4 e5 F/ v2 S- D& d+ {' i // changes the heatbugs have made. The ordering here is ?/ |, n' _; u/ N& c, ?
// significant!% c: x1 T' t/ L
- e. a0 K2 a5 N( @7 [8 n // Note also, that with the additional6 }' u# j& w! p& q
// `randomizeHeatbugUpdateOrder' Boolean flag we can
5 q4 H- C: I9 x2 u7 }8 S // randomize the order in which the bugs actually run D2 q1 Q) C1 \: u
// their step rule. This has the effect of removing any2 G, L5 }; `; a4 ~ g a
// systematic bias in the iteration throught the heatbug
& c" D% L% T# D4 @$ t // list from timestep to timestep
/ K6 m* L- e5 P4 T4 L
4 {1 S! M( n, V // By default, all `createActionForEach' modelActions have
: K. R) z5 S4 \ // a default order of `Sequential', which means that the
3 i2 g j7 B. y9 l8 n" u& } // order of iteration through the `heatbugList' will be6 K B; p/ r; E, r0 d# B$ e! a
// identical (assuming the list order is not changed
( @+ j3 C& v0 N1 Y9 ?+ t ` // indirectly by some other process).% `4 H1 V O- x7 J7 i. Z; f) I
2 t0 ~, a$ N; `* L9 A s modelActions = new ActionGroupImpl (getZone ());
) e4 J7 R) s5 T. |# V" Y
) Z9 m k/ j' }7 T try {
5 y3 m3 j. B+ \ modelActions.createActionTo$message' T! z- y! W, a+ r3 j
(heat, new Selector (heat.getClass (), "stepRule", false));6 V6 L8 [5 V! `- T7 |, e
} catch (Exception e) {- \$ C+ I& ~- ?5 T
System.err.println ("Exception stepRule: " + e.getMessage ());
6 ]& K6 i: r8 n/ F/ ]' |8 Z }
) @4 B' |. b5 {0 X* J% o+ ?' \, p! q8 U" {1 l2 _( z
try {; s0 x) L, C0 K$ |2 g1 d1 u
Heatbug proto = (Heatbug) heatbugList.get (0);
' V9 k* ]+ h1 V1 s O Selector sel = ( M/ f. }( l+ e. c) q
new Selector (proto.getClass (), "heatbugStep", false);
3 n: y6 o( L0 A H) { actionForEach =& d2 P+ \/ |# x' T
modelActions.createFActionForEachHomogeneous$call7 k5 A" ~/ i; [/ z( d
(heatbugList,
+ @+ k z( R# n new FCallImpl (this, proto, sel,0 w1 x5 I/ F: d
new FArgumentsImpl (this, sel)));
2 T! v' E6 A) D1 e! ? } catch (Exception e) {
_! U- i8 X+ k1 O2 \0 g e.printStackTrace (System.err);
: o f; b$ ~, e1 E. _ }+ G' w* j5 M8 A: r/ [- M5 E8 I7 z/ y) k
% U3 |! v y3 }! z( {# {9 X! o
syncUpdateOrder ();4 \, I" Q) O$ A% r
* v, s5 _1 j! N& w
try {# y5 K" E& a! e/ d
modelActions.createActionTo$message
6 o5 N! T* C# X( k9 `; |6 }( s (heat, new Selector (heat.getClass (), "updateLattice", false));4 e% S4 e! u0 J ?& Q
} catch (Exception e) {1 o. O8 C+ D% t* h
System.err.println("Exception updateLattice: " + e.getMessage ());0 j# J. y$ f. X/ O; i4 V
}
( q: L/ v* \0 l0 W' A% s
, @. c. }0 a* {& H8 h' M // Then we create a schedule that executes the1 a1 s# J9 i0 u
// modelActions. modelActions is an ActionGroup, by itself it
: S/ F+ i5 b1 U: { // has no notion of time. In order to have it executed in0 q$ `9 x _" o& w' J, g4 _
// time, we create a Schedule that says to use the) c; w! ^' E& h1 |) Y3 s
// modelActions ActionGroup at particular times. This$ p* {% A; L( H! @0 O, f. X
// schedule has a repeat interval of 1, it will loop every
5 H. w5 C4 i) O6 ^' c7 V // time step. The action is executed at time 0 relative to7 S3 x! F8 \7 \, y* ]$ {# e5 u* Q' r
// the beginning of the loop.( ?/ v+ B- V7 K5 t$ l& U# P y, v
9 u2 o" k# X j5 P [* L& i1 b" R // This is a simple schedule, with only one action that is9 W9 ]) y& a( }* [' c c9 W" K( b
// just repeated every time. See jmousetrap for more9 z9 E. P# F& j( j
// complicated schedules.+ ~1 n. U' e [9 U5 M- B
- u! W' [. w2 ] modelSchedule = new ScheduleImpl (getZone (), 1);
6 z3 v7 w( M+ E" Y; C4 ^ modelSchedule.at$createAction (0, modelActions);
5 L3 I) g7 A4 [! Q* ]
+ W# t1 V5 x; e" A, } return this;/ b f, R9 g+ T
} |