HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% Y% i% j: x$ n' p% u% z
/ |) L. q x* P$ W: l* P* M4 X public Object buildActions () {
) t T2 P3 h# _6 y: a: m0 K super.buildActions();& j; ?# P, b. j4 c
# ~ [3 H' M' E6 ]0 |
// Create the list of simulation actions. We put these in7 b; D) i) `9 V+ Z. |
// an action group, because we want these actions to be, d3 Z/ q |+ E
// executed in a specific order, but these steps should$ l/ J; @0 |. L/ z0 @2 y
// take no (simulated) time. The M(foo) means "The message
5 T' d) w& k6 c& q3 { // called <foo>". You can send a message To a particular) W: V7 x( c6 d
// object, or ForEach object in a collection.( a+ K# d1 ]! S. Y
5 s; ~7 P# ~& ]6 N. R/ D+ n // Note we update the heatspace in two phases: first run! L, |5 D, J& e! W, N% C) T1 g* w
// diffusion, then run "updateWorld" to actually enact the
6 q2 R7 B4 N& d' p2 { // changes the heatbugs have made. The ordering here is
, C3 H& q7 V, { Y/ d- ^; @ // significant!, k; b% K0 U# k
$ _2 Z) q' [3 F$ G3 ^
// Note also, that with the additional
u7 Z7 J& @* E& k! B // `randomizeHeatbugUpdateOrder' Boolean flag we can
, t# h; `" T4 v: C // randomize the order in which the bugs actually run$ J# j2 k: c9 ?0 j- z: u$ z
// their step rule. This has the effect of removing any
2 K" P1 u, P/ x2 E* Q5 R, y7 p3 t // systematic bias in the iteration throught the heatbug
$ ^8 G4 x! c; c! g7 ~ // list from timestep to timestep
+ i, E; }0 Y& s: k- p. K4 g, M " G4 l, }0 [% ~$ i' L& X3 _! ^
// By default, all `createActionForEach' modelActions have$ |% p, f4 X/ Y0 @8 G
// a default order of `Sequential', which means that the
: R; e! w% |- }4 R: V1 q4 G' k // order of iteration through the `heatbugList' will be
+ b$ e ]0 E P- J- u J E // identical (assuming the list order is not changed' F8 b/ N( ]. Y) Z$ l% Y
// indirectly by some other process).' p, \% D. [/ M4 S! f2 ?
) S8 s8 f* J9 j# F$ _ modelActions = new ActionGroupImpl (getZone ());
1 I2 w- N0 H+ c+ B( w& {, b( D8 X$ P( d+ `2 N% [3 @" N
try {0 y+ Y: S; E6 X( b
modelActions.createActionTo$message
p- q* y/ `0 `2 k, L7 {) h5 C (heat, new Selector (heat.getClass (), "stepRule", false));
1 R+ m" l5 V4 Q } catch (Exception e) {
8 d" W- w& C9 g, `/ j System.err.println ("Exception stepRule: " + e.getMessage ());5 H6 ?% k+ r7 I; m! b
}
1 B3 u, D# a; @: l+ d+ V, f" E
0 y0 |9 K1 @7 a! d/ j% ~6 o0 H. G try {
$ }$ v( V' S, E/ D$ y) X0 m Heatbug proto = (Heatbug) heatbugList.get (0);; u, h8 `( [' w& h. D' [1 T
Selector sel =
/ E1 a' M8 u2 S# U a$ g0 y$ j new Selector (proto.getClass (), "heatbugStep", false);
?$ K- Z) R$ D$ y8 C1 v3 O- t actionForEach =8 Q5 A! \# o. }# L
modelActions.createFActionForEachHomogeneous$call
# T8 i8 K7 v* f; J+ d (heatbugList,
/ y' R9 F( w- v3 n& R new FCallImpl (this, proto, sel,; G6 I' l4 e0 w/ g. ~' L# H
new FArgumentsImpl (this, sel)));# n: [) ]; P! y. \+ P+ v
} catch (Exception e) {6 ^4 W2 ?& X) j# M* a
e.printStackTrace (System.err);
& j: K; Z1 ~! c3 G! l2 e, U }, m% c$ @ [7 i9 b% W
7 _6 d+ m' K" b. Z$ `$ E& g
syncUpdateOrder ();
) S% T1 V3 H# {- U( ]
" }- i0 c, c& ? try {
, m, R) W5 R, V! z4 C! c7 x modelActions.createActionTo$message * a3 S. [$ k6 ^. f3 y
(heat, new Selector (heat.getClass (), "updateLattice", false));
6 _* j$ {/ ?' o! o7 c+ S } catch (Exception e) {
7 f. M- |3 b( Z& q7 E# W) O: ? System.err.println("Exception updateLattice: " + e.getMessage ());
# a1 w2 [8 A! V5 B) m }- [5 @5 q i& Z# j7 _2 ?% M
" P* X! c& W8 x) q, v4 S* ^4 H8 X
// Then we create a schedule that executes the
( F0 q6 r- j* e1 j // modelActions. modelActions is an ActionGroup, by itself it
2 ]4 u. v$ d3 O5 O3 a* u7 W, s. |$ i4 E // has no notion of time. In order to have it executed in
9 G3 B, Y7 K: F ]. H5 s // time, we create a Schedule that says to use the3 ^( s% B$ M E6 H% U
// modelActions ActionGroup at particular times. This/ D- r) V) W1 O! r8 R
// schedule has a repeat interval of 1, it will loop every
: I& Q1 d& q S% v // time step. The action is executed at time 0 relative to* z. ?% p& Q9 ~$ p. D t
// the beginning of the loop.2 Y: b/ ]6 P2 A) V3 u/ n
5 T& n* D9 e7 v" V! V
// This is a simple schedule, with only one action that is4 o- Z: S7 X* p$ q6 j5 c/ [
// just repeated every time. See jmousetrap for more
) |7 r% E3 ~3 z$ L" c" ~ // complicated schedules.; T: ?* y& `2 J6 ~( {
! L4 D) S5 i" p
modelSchedule = new ScheduleImpl (getZone (), 1);
! e- H i2 b9 d$ }; W# A3 B, N modelSchedule.at$createAction (0, modelActions);/ G) s, A3 Q# G) z
4 w% |# N0 ^. ?/ I+ Z& w X& j
return this;
5 M6 ~, ?2 {6 z% } } |