HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 v9 H3 d5 J O4 \
. M* k) s' g0 b1 V2 O7 }7 ` public Object buildActions () {+ h1 ` t, F" }/ U, H- o
super.buildActions();
, ] S, E- R" o$ J 0 |; X% A5 m1 r% D1 p% e8 T
// Create the list of simulation actions. We put these in
% p1 ]6 m, I7 O4 g; w // an action group, because we want these actions to be
5 L' A) j& f, \3 p- m/ X5 v // executed in a specific order, but these steps should
% Q0 x- |) ?- |# { // take no (simulated) time. The M(foo) means "The message3 l0 e9 r1 i/ c! d
// called <foo>". You can send a message To a particular
% u# x& D8 [9 K, \; } // object, or ForEach object in a collection.
8 E: d; J3 s& U% j
, e3 ^ {8 P* P/ O // Note we update the heatspace in two phases: first run. E1 y3 ?6 R- Z
// diffusion, then run "updateWorld" to actually enact the
+ A6 r7 g. E o6 z // changes the heatbugs have made. The ordering here is* k# W8 s3 u9 x5 V) K4 q
// significant!
+ q1 _# U4 Z8 @" w: f1 C6 C ' g4 W/ s! M9 b
// Note also, that with the additional$ d: n3 D. U H
// `randomizeHeatbugUpdateOrder' Boolean flag we can* q I4 ~4 }. B2 s' E" R8 o( D
// randomize the order in which the bugs actually run
+ V. o `' I* z H" ~ // their step rule. This has the effect of removing any# O3 K7 r d) J0 p# x% G7 _
// systematic bias in the iteration throught the heatbug" _7 Z1 \3 \) B; m. {( w
// list from timestep to timestep1 A, g% ^3 o* M7 S, }: x- d
. k y+ u' ?( s, s& b
// By default, all `createActionForEach' modelActions have
0 p! e/ ?* @/ K // a default order of `Sequential', which means that the+ t6 I* v; C$ x
// order of iteration through the `heatbugList' will be w. {% L$ p5 S e. n1 N
// identical (assuming the list order is not changed
- V. o' u" Y% h. W% Q+ q* l( c# H0 _5 y // indirectly by some other process).
0 S, @" t Z) c% {7 b. T9 | ( L- l) V, ^$ T
modelActions = new ActionGroupImpl (getZone ());
' u) T0 a! z. i/ O/ H* c, z0 y
' ]2 }! S/ r4 @, i1 j7 b, w# F try {
& F$ ~0 f, v: h modelActions.createActionTo$message- I: K' J9 R& S" D8 Z+ T+ t
(heat, new Selector (heat.getClass (), "stepRule", false));. ?; E- A6 c1 V4 C4 D0 z
} catch (Exception e) {9 K3 a* j+ Y$ ~" k
System.err.println ("Exception stepRule: " + e.getMessage ());' ^1 ]1 r: h7 o9 o6 {
}5 |: {3 e. z8 T8 k
1 m4 r' u- O g% r
try {8 p5 F$ M; D1 \2 ^
Heatbug proto = (Heatbug) heatbugList.get (0);
' r2 d7 v$ D" p Selector sel =
* D% _7 h; k0 O& @2 D: n/ V% D new Selector (proto.getClass (), "heatbugStep", false);# I) \" ~# @/ ]5 k( A. g' o
actionForEach =
! }% Q7 u$ u( ~# {' r& B( I3 o! q modelActions.createFActionForEachHomogeneous$call
3 V3 I/ v( ?8 J: p0 ^: h (heatbugList,* {4 r1 M5 w; V* r: E3 x
new FCallImpl (this, proto, sel,) h: o3 _% m5 M4 p
new FArgumentsImpl (this, sel)));( y' N& j1 n: J( d3 | m
} catch (Exception e) {( m7 r9 a) M9 ^8 @
e.printStackTrace (System.err);
V6 t ~0 a" x( V$ } }
. Z; w# b# k/ p, p' Y( G - \$ ?/ m' ?% K# l |- [! q
syncUpdateOrder ();
; z: M1 ~! @4 N6 i V7 J4 B) E& z+ T* \! G6 z
try {
2 n+ e& [" n' G5 j. p$ J modelActions.createActionTo$message
$ F; L d8 ?3 }6 b% T9 v) o3 v (heat, new Selector (heat.getClass (), "updateLattice", false));
$ X& w3 \: G# S } catch (Exception e) {
) Z: u+ s V. E8 v System.err.println("Exception updateLattice: " + e.getMessage ());
+ j- m* _8 d8 p" Q- P) O3 A }0 w" f& S2 G! H
E @( H( O4 j9 D
// Then we create a schedule that executes the( ^8 L {" ?6 p
// modelActions. modelActions is an ActionGroup, by itself it
, I- P# z6 t, { // has no notion of time. In order to have it executed in6 j, h- K1 U4 f; A
// time, we create a Schedule that says to use the
9 \4 a( X S6 l // modelActions ActionGroup at particular times. This
0 o9 j) S; \ G# z* Q# _- Q // schedule has a repeat interval of 1, it will loop every" m1 Q' }! p$ j: f7 O
// time step. The action is executed at time 0 relative to
+ T/ S3 u# u' q3 F% Q1 _! Q( c4 Z // the beginning of the loop.2 a/ M) {1 s9 B* F( D1 ?
6 E2 w) A- a) x
// This is a simple schedule, with only one action that is9 n! I) @: E. n1 W9 H3 j& X: M. A0 ?
// just repeated every time. See jmousetrap for more: l% {4 K1 x* E
// complicated schedules.
3 U# j5 k* e3 l% q8 S/ d9 Y & l; V1 @' L5 V& [
modelSchedule = new ScheduleImpl (getZone (), 1);
! R- Y6 a1 D0 d modelSchedule.at$createAction (0, modelActions);! k$ j& c/ k5 y
8 o$ C; H$ |3 W7 t9 S$ n return this;- A4 L4 e4 n7 `- j+ |
} |