HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( Z8 ^5 M A2 l# t/ U
( [0 Y6 a6 B, w+ o public Object buildActions () {
2 v8 u5 F6 C* J5 o( q/ E super.buildActions();/ J! t5 ?' b+ x. `5 [
0 G% \: G/ ]" e+ F // Create the list of simulation actions. We put these in
7 Z$ w! e4 z1 t // an action group, because we want these actions to be+ w0 K7 W2 O6 ], b, _( O+ D8 ?: @
// executed in a specific order, but these steps should
9 v! D* G0 f! O& U6 @ @ // take no (simulated) time. The M(foo) means "The message9 ?: A8 ?* l; e5 ]3 k
// called <foo>". You can send a message To a particular
0 @# N, i$ v2 f# u // object, or ForEach object in a collection.
; W1 c1 _ x! a% [5 f/ Q
$ ]+ X& \. u: e" k // Note we update the heatspace in two phases: first run
1 Y- h {# D/ h // diffusion, then run "updateWorld" to actually enact the! ~, } C! T6 I3 R# w
// changes the heatbugs have made. The ordering here is
5 H5 i! B0 _" c0 _ // significant!
+ B+ s0 U1 R7 R) |6 P' @3 `, \
7 |4 V8 I8 u% p! q: p8 M; w8 t) y // Note also, that with the additional0 c: q0 D% ~5 J% E) L4 |
// `randomizeHeatbugUpdateOrder' Boolean flag we can) r+ d9 F- z/ e7 t- H; m' ]/ C+ X& O
// randomize the order in which the bugs actually run. ?7 A6 X4 ?$ s; q
// their step rule. This has the effect of removing any
6 a/ @2 t/ P) B( }5 Q% Y! t // systematic bias in the iteration throught the heatbug
5 V. Z' S! [. n. J2 s3 O* z // list from timestep to timestep; ]2 ]& L6 H" X) u p. I' h! @- `
- V _# p- ^; a7 l [3 S // By default, all `createActionForEach' modelActions have- y9 D) Z" }- z8 I
// a default order of `Sequential', which means that the
O' o$ R" J8 n2 h, d$ w; c8 Y // order of iteration through the `heatbugList' will be9 a3 n6 a6 }% @" }' |* }
// identical (assuming the list order is not changed
2 j* U3 h4 ?; b' [ C // indirectly by some other process).- q4 R/ y, B4 R9 u
+ F* F3 U% K* x& ~9 ]4 w9 u; z. z modelActions = new ActionGroupImpl (getZone ());; f$ v. k7 |+ a3 M/ }
, G7 S# F# A9 |# A' U' k5 f7 v
try {& I+ A& N0 i- V4 t! M% Y
modelActions.createActionTo$message. L T y; _$ S4 {
(heat, new Selector (heat.getClass (), "stepRule", false));
- ~0 W( Y$ G" {" q) h& q! u } catch (Exception e) {
3 c8 f/ x% t% W) k) s9 z( F System.err.println ("Exception stepRule: " + e.getMessage ());
/ S* u6 @! l7 r: y" ?. S }
B, y! t# ?" x+ _! G0 E! b* n' Z, ] C4 U! n- U! A. n
try {8 [$ P0 S' z& I0 E: g
Heatbug proto = (Heatbug) heatbugList.get (0);
$ g2 j9 Z0 p& o3 U( i Selector sel =
- N* i2 X3 d+ F$ y new Selector (proto.getClass (), "heatbugStep", false);: B5 n! ^! q* r b% U
actionForEach =
7 [3 @+ }$ Q. k% O$ s# b+ Y' H& F modelActions.createFActionForEachHomogeneous$call S" b7 ^1 e# a& o( \
(heatbugList, v! \) Z3 a5 a# A
new FCallImpl (this, proto, sel,; W6 E8 U+ V/ i) l& u0 K
new FArgumentsImpl (this, sel)));& l! c$ x$ r7 C
} catch (Exception e) {
2 D6 x/ d) \4 R; Z0 s$ q e.printStackTrace (System.err);
7 W7 `& b- r" F" |# ~ }6 c$ e$ k- R9 d& l- p/ k
' M" G7 E: i9 ~: I
syncUpdateOrder ();, P7 s' a4 N2 l$ [0 t6 `
/ C- g. k! c z( F! p. B
try {
' K. A" q! }" W' D* F modelActions.createActionTo$message # v! M) U, \& Y8 \% k' [5 q
(heat, new Selector (heat.getClass (), "updateLattice", false));# _2 S1 N: z _7 S) v: A1 s
} catch (Exception e) {& W1 L; S6 K9 [
System.err.println("Exception updateLattice: " + e.getMessage ());0 B( Z0 |/ R M& ?$ \
}
6 R, H6 }- C& C6 Z+ ^0 i6 G: m) `; p3 O 1 e# T0 j' I5 V, o1 T, T: A
// Then we create a schedule that executes the7 w; ]; j1 B( j0 J @
// modelActions. modelActions is an ActionGroup, by itself it" W, E: K+ a+ o: i! f
// has no notion of time. In order to have it executed in
5 D, Q& s8 P$ V$ R" {- c O // time, we create a Schedule that says to use the
( v, h# R% U+ \; `( B // modelActions ActionGroup at particular times. This: r* ]+ e$ X' N* N x) y
// schedule has a repeat interval of 1, it will loop every: w0 Q* t. v. ]$ o: Q3 z5 K
// time step. The action is executed at time 0 relative to6 p- }+ n9 A; N; m7 v% E3 a
// the beginning of the loop.
& d& e8 n; J) y1 F3 u4 \( R% a
& R1 Z& l4 Q9 ~: e. C: K // This is a simple schedule, with only one action that is) n. V0 N7 b: L& D) Y8 t0 J' r
// just repeated every time. See jmousetrap for more/ V% U3 E2 ?% H5 r
// complicated schedules.0 m) T' A; L2 ]) K- p" Q
5 T3 K( y4 t% `- c7 J
modelSchedule = new ScheduleImpl (getZone (), 1);
6 c& `) m: g" E1 Y modelSchedule.at$createAction (0, modelActions);
4 i, L3 |# _" f8 K1 [ , ^4 d, {2 P$ U
return this;
6 H" v% f5 F) o% R, a2 O8 E } |