HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 j6 y( Z# \4 z" |
/ R6 M( G* L/ b# q- l9 E public Object buildActions () {
) ^* E! W5 A! F super.buildActions();/ E9 C. o* f; ~) @- b& |4 V
4 B1 M8 i! r; Q% Y& R+ D& ?* _, i; g // Create the list of simulation actions. We put these in: t1 v S( }' g$ f
// an action group, because we want these actions to be
, s1 t# ]8 u# n% j // executed in a specific order, but these steps should
- h3 ~/ l! ]8 d& y; q // take no (simulated) time. The M(foo) means "The message9 @. y2 [; L* @9 r( d
// called <foo>". You can send a message To a particular
+ U' o7 m( Q5 }/ I7 I" O // object, or ForEach object in a collection.
7 T9 p3 N. B* f& {( i) j; c 5 h; M- b9 S# L
// Note we update the heatspace in two phases: first run/ o" B: ]0 g* @' G
// diffusion, then run "updateWorld" to actually enact the
1 g! ^3 c8 u0 D9 Y5 [( b% p6 F; k: h // changes the heatbugs have made. The ordering here is
# M) x6 ]3 @: X) h3 Y7 [' m // significant!
& A$ G, h( t* `+ B/ ^' m ) K9 I" n0 L2 z" K9 C) X
// Note also, that with the additional
R" p7 R5 O) T. d) d- x& Y4 h // `randomizeHeatbugUpdateOrder' Boolean flag we can6 f( |: g9 L$ e4 B
// randomize the order in which the bugs actually run
: q% x4 Z, T+ e( C+ d5 F3 R // their step rule. This has the effect of removing any
: x8 J2 y* f! f# k- @2 u/ s // systematic bias in the iteration throught the heatbug
& u+ E, n7 Y0 M G0 Y& z // list from timestep to timestep
% m0 r% o, l: _! k) G
$ T/ O% |$ N+ b8 b5 e% [/ q // By default, all `createActionForEach' modelActions have$ s, ?7 e- K" _6 v9 J3 h/ n
// a default order of `Sequential', which means that the
6 a* m7 K$ n) h8 d6 d R // order of iteration through the `heatbugList' will be
5 t1 Z) R! s% t // identical (assuming the list order is not changed+ ^% W }9 I3 I7 ]2 Y" C {
// indirectly by some other process).
4 ?- U1 w' v& Y* E8 `! Y$ Z : ^$ A" }8 G# F. z; I
modelActions = new ActionGroupImpl (getZone ());
8 q3 u6 a( p4 ^1 |& }' ~) f3 b0 D5 G2 U3 Q p7 D: S
try {
$ q+ [( m1 f- }! e/ p modelActions.createActionTo$message5 _+ A9 d! b8 p6 f
(heat, new Selector (heat.getClass (), "stepRule", false));, ^# m* O1 S& w
} catch (Exception e) {4 o4 u7 X& z7 i( H) ^% x+ h, l
System.err.println ("Exception stepRule: " + e.getMessage ());
# |" H9 }, ~7 m, M }' ~$ X! H5 q% E, F6 E/ `
+ E6 T, k+ w, |1 |# s( [. A
try {
+ N% N" f; H( c* b+ x* r Heatbug proto = (Heatbug) heatbugList.get (0);- }! x2 Z) U7 X: B2 y
Selector sel = 9 x/ c5 Q" b! ]) M
new Selector (proto.getClass (), "heatbugStep", false);
3 ^1 p( B4 e2 S/ s( d actionForEach =
, I9 o7 K0 A" K& t' ]/ i modelActions.createFActionForEachHomogeneous$call
# a6 i- W8 `0 _ q3 P, p8 w (heatbugList,1 e5 s2 R( T% l) `
new FCallImpl (this, proto, sel,
; k/ `7 f0 G' m3 I! }; P. ] new FArgumentsImpl (this, sel)));
& D7 o0 `) y- T1 b6 a0 A/ K* a' L } catch (Exception e) {
) T; E- L* b9 G e.printStackTrace (System.err);8 k8 q! l: M, v( e
}9 ?) v2 [( d6 Z! e$ e6 V
* F/ D, c4 J' L1 n syncUpdateOrder ();
& ~/ C* l$ Y- A" S; n% l) K5 d& m4 l' p) F$ x6 T
try {8 W8 I* k4 w0 Y1 ~, b
modelActions.createActionTo$message
& ]2 H' H; I7 i1 Z# F5 k (heat, new Selector (heat.getClass (), "updateLattice", false));
+ f+ r* U0 A; E- Z; O4 X } catch (Exception e) {, \% M+ M1 K7 X" J, u2 {
System.err.println("Exception updateLattice: " + e.getMessage ());
. C, C6 j, z# M0 a( ~% B0 \ }
3 G$ `; p8 I3 b2 r& {8 L1 o% n( j 5 e" R$ a) \9 Y8 T7 a6 e4 L
// Then we create a schedule that executes the) V2 L$ s9 y" a- D, g# t3 r2 M" h
// modelActions. modelActions is an ActionGroup, by itself it
/ a3 a1 W4 N: A) g2 x // has no notion of time. In order to have it executed in
0 g4 F2 S4 t( V5 {& W1 s* G0 g // time, we create a Schedule that says to use the
( Y; Z$ z8 Z, s2 d4 s9 g // modelActions ActionGroup at particular times. This& ?1 F3 q" }& d( f/ P4 h5 w6 E" Z
// schedule has a repeat interval of 1, it will loop every @4 Y2 n8 B6 G. ]9 w9 D
// time step. The action is executed at time 0 relative to* v% L, Z$ e/ D2 k' I, h$ N
// the beginning of the loop.
' l: ?7 h% n. U, s* B% d
; N/ r- X" J! _8 l0 f6 h z // This is a simple schedule, with only one action that is0 J/ b ?; S; m2 H+ U; ?/ {
// just repeated every time. See jmousetrap for more
; V4 t' M' H h5 u' e0 U V8 o // complicated schedules.
% z* e8 L! M- v! Q& y
7 k3 Z1 z) {, D8 H1 u modelSchedule = new ScheduleImpl (getZone (), 1);
5 [+ y# N- k, g" e modelSchedule.at$createAction (0, modelActions);
3 o V; G4 ?, { a& b2 ^; q 8 ` B$ R( F1 w% W
return this;
: p+ E+ |2 U( @% V) y1 Q( d3 X! i } |