HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 _2 G2 ^4 h( G: P' h% a
+ P' a2 [1 _. s! X2 V public Object buildActions () {* j# m* I" }+ _; @) T
super.buildActions();1 o' y1 |( X( y0 k5 r
h1 j1 q* ^/ W5 n2 A
// Create the list of simulation actions. We put these in
9 h3 c- W3 ^( [4 I2 E // an action group, because we want these actions to be
* |5 Y! l) I6 `8 T" u // executed in a specific order, but these steps should
8 K/ y3 A0 L3 }# T // take no (simulated) time. The M(foo) means "The message
6 U$ w( ~# A/ D; R // called <foo>". You can send a message To a particular
! U" f2 F6 S/ R // object, or ForEach object in a collection.! x6 v. H& j; V! h0 x
8 P, v4 X$ u3 O# t; d2 L* {# ? // Note we update the heatspace in two phases: first run2 p) c% V$ z6 `# `1 L& B
// diffusion, then run "updateWorld" to actually enact the
1 r; b L; t/ i: I9 r g // changes the heatbugs have made. The ordering here is
7 u% ^3 h+ ^1 D6 E( o$ a/ R // significant!
* k, J V7 V5 T$ [
* W" ?& e0 y8 f E // Note also, that with the additional
, }8 @3 t/ w. v% W // `randomizeHeatbugUpdateOrder' Boolean flag we can$ T- ?# p; e: J
// randomize the order in which the bugs actually run1 c9 ?' k7 R1 g" }8 R
// their step rule. This has the effect of removing any# b: F8 z' c8 u
// systematic bias in the iteration throught the heatbug
! X) I& |8 Y @) V, y // list from timestep to timestep& k/ h( A. [2 T8 t1 s
# \ S+ O1 f% H% \
// By default, all `createActionForEach' modelActions have
7 H8 w/ D7 v. |$ ]" R \) V9 u2 \ // a default order of `Sequential', which means that the9 c: ~, W/ y/ g' C* N4 J
// order of iteration through the `heatbugList' will be
; R/ n" I5 M+ b* H& C! U& P/ ? // identical (assuming the list order is not changed
' P$ w: x0 r* X // indirectly by some other process).
% h# D" d f$ @, p+ i
7 ^8 o' ^8 M( A+ g5 P modelActions = new ActionGroupImpl (getZone ());
0 Y& U U) [! P7 ?# r! e* S* ~5 r6 C% V4 ^7 a' q! Y% U! F6 R5 D
try {
: `; P6 `+ {5 L# o8 M modelActions.createActionTo$message
. `' ~3 C1 c# j (heat, new Selector (heat.getClass (), "stepRule", false)); c: K; ^( Z. l) S) i
} catch (Exception e) {& e8 t7 m$ p0 w$ ]2 q: v$ v( F
System.err.println ("Exception stepRule: " + e.getMessage ());* {) Z- S. U/ p- m2 O6 A
}3 a |8 @7 W/ S+ o5 z
N( U% [9 c4 s1 q
try {* A0 a- M. k; x E. t1 A# f
Heatbug proto = (Heatbug) heatbugList.get (0); Z& a$ C* ]; ^! J( c
Selector sel =
2 Y, f3 G" o, m new Selector (proto.getClass (), "heatbugStep", false);
# X/ o+ U4 z3 x3 v2 |: P" L& |1 v actionForEach =/ r! g0 ?2 U3 P" a
modelActions.createFActionForEachHomogeneous$call; y' y& Y7 n) X2 r+ I7 l6 {
(heatbugList,2 P j7 i# N* i: P7 X5 z9 E; \" j
new FCallImpl (this, proto, sel,# | @( ~) S* @) `
new FArgumentsImpl (this, sel)));
q# t% K% J( f; i* u8 R } catch (Exception e) {& }3 R- y8 D. y2 u9 M) w, i- b
e.printStackTrace (System.err);
3 {7 i4 L6 t7 X9 A4 z5 U# m* z }4 k- D- k3 A2 e% s* p
& R) J! @2 Q* K) Q( G+ D syncUpdateOrder ();
) U9 Z: u2 k; P z9 Q1 I5 W* l+ Z! K! a
try {
2 h4 J2 V0 O+ A. U modelActions.createActionTo$message
Y7 S5 i7 |) J$ h- ]7 } (heat, new Selector (heat.getClass (), "updateLattice", false));
% z# V$ t( Z0 w3 E* g) y6 U, W } catch (Exception e) {
, j6 ]6 [: _! L1 D/ G System.err.println("Exception updateLattice: " + e.getMessage ());' r9 x1 \" h5 n0 K5 e; x% d
}2 g- M5 g4 e/ l. a: n5 e2 y# Y
2 I5 i+ T% h4 k' X" G8 z // Then we create a schedule that executes the
6 Z6 y/ f! K# y6 [7 I* u& n // modelActions. modelActions is an ActionGroup, by itself it$ w/ [3 \6 u( s
// has no notion of time. In order to have it executed in& D# Z( M4 t8 @
// time, we create a Schedule that says to use the
9 H$ |$ `# o& j; j1 f: X x0 i // modelActions ActionGroup at particular times. This8 ]6 E- d: `7 K( z
// schedule has a repeat interval of 1, it will loop every/ t6 r$ M1 R5 F1 `2 J# J
// time step. The action is executed at time 0 relative to+ b- G Z* d5 e( O% M4 x
// the beginning of the loop.; M1 T- n& Z1 o" |1 p8 F% |0 b9 V5 o' \
2 n7 T h$ v0 C/ ]1 n( j) }. ~+ H // This is a simple schedule, with only one action that is
$ w$ g( K6 q6 `; h7 ~ M; q8 |7 `. O // just repeated every time. See jmousetrap for more
9 [7 N! `, r* F' G& l0 ~. d // complicated schedules.7 B( W: v! c; \9 @1 L- k- _
# v/ T: A! z! Y$ L9 E modelSchedule = new ScheduleImpl (getZone (), 1);$ v0 s5 c7 o" n
modelSchedule.at$createAction (0, modelActions);
! m5 S: R* c" {9 k3 _0 K2 H6 [: r $ X+ P2 E$ Q& T6 u4 y
return this;/ y6 `9 a! S; j! l7 ^3 w
} |