HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: g. O. V" {; ]8 ], G0 E! }6 T$ V( m6 t% T4 E9 }
public Object buildActions () {$ D+ D3 F) F# g% N# ]$ n" U+ a
super.buildActions();! j3 M" F" s3 n d
7 L+ N' s) W. |- J$ j! g
// Create the list of simulation actions. We put these in- p/ i E& }6 l. x% Q
// an action group, because we want these actions to be. R4 P. Y& Q$ k1 Q8 r
// executed in a specific order, but these steps should2 i) _5 Q, l0 p- L4 q' @. A4 [
// take no (simulated) time. The M(foo) means "The message6 }# r: |! D* S# G, j
// called <foo>". You can send a message To a particular
P( [' K. K/ H3 R& ?7 ^ // object, or ForEach object in a collection.
# r: U# r4 U0 t' I; ~$ D8 v
6 f j: `- N9 u // Note we update the heatspace in two phases: first run; ^; q$ h. V7 ^3 Q) _1 B5 m. x* P
// diffusion, then run "updateWorld" to actually enact the
. d# ]% A& _$ | // changes the heatbugs have made. The ordering here is
- j) O& \! Q, \/ p; @ // significant!
6 z8 i0 s! Z2 h. p& B+ T# {/ q7 }; V- H
1 k8 Q6 f. c& Z/ X // Note also, that with the additional* V% U; o; s; e/ G: @4 m7 q
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ w1 z* w( [. s9 Y
// randomize the order in which the bugs actually run6 T- K1 `* `4 O7 H& V* F/ `3 v' F
// their step rule. This has the effect of removing any- U) r$ I$ b1 v( B h+ i
// systematic bias in the iteration throught the heatbug
7 n: \+ {9 j* ?' T // list from timestep to timestep
; L6 Y3 y: a$ l
$ n# P/ S8 F5 G1 O& H // By default, all `createActionForEach' modelActions have, f: V7 z1 e8 F p8 C3 b, g
// a default order of `Sequential', which means that the
/ D! H4 Y# H+ q) S) n! _ // order of iteration through the `heatbugList' will be
$ O+ r7 V. ^/ M // identical (assuming the list order is not changed$ D N/ v) |; E) \6 t" t9 G
// indirectly by some other process).5 S0 ^8 [# I: V6 ^: ~, b
, R- ^7 r; t# W, M, @* I
modelActions = new ActionGroupImpl (getZone ());3 y; W2 n% ]: x4 t4 l4 j- |, ?0 ], F
5 ?! g8 K& B9 S8 m s6 V5 n$ O" f
try {
1 }% K, V. i. O( ?: m0 p7 R modelActions.createActionTo$message
4 i/ Q9 I c; ~9 `1 ~) G5 F) h (heat, new Selector (heat.getClass (), "stepRule", false));; r' o0 s4 Q! j5 ?0 e
} catch (Exception e) {
3 W0 V1 n; w8 a* @ System.err.println ("Exception stepRule: " + e.getMessage ());
* ]7 m) j0 Q/ g" _: H/ C }
- A4 H4 h ]) D- ~! I( Q
1 M8 Y" n# Y$ m! @* k. i try {0 h' y3 R- W& F9 `
Heatbug proto = (Heatbug) heatbugList.get (0);
6 d( y" I8 r6 d! L Selector sel =
' a+ Q! P1 r% N new Selector (proto.getClass (), "heatbugStep", false);
. R+ E/ g. d& q actionForEach =0 U# H+ {# h6 Z$ _; _3 K$ s
modelActions.createFActionForEachHomogeneous$call0 U) d% [8 u) l: M5 U6 V% Q( D7 ^
(heatbugList,$ O0 V6 _9 O# K
new FCallImpl (this, proto, sel,8 T$ Q+ r: R/ H- I" L
new FArgumentsImpl (this, sel)));; V; e' r1 D" e6 v
} catch (Exception e) {: f4 \6 N( S* B/ Z1 b
e.printStackTrace (System.err);4 a. K' n) M k% T, Y* T2 ^
}
4 `6 C- k K) I8 u1 F$ m* k; k
1 d7 O# G' c p! W; D5 _! } syncUpdateOrder ();
$ T# {' ]* `+ Y
7 F& X6 \( B7 Y try {
$ K" m/ O/ h! e2 y% T" q modelActions.createActionTo$message
: z+ e6 i* g/ N; c. E4 j6 P8 }& b8 a (heat, new Selector (heat.getClass (), "updateLattice", false));
1 _# Z( f( i" n# ?* F' Z+ o% J5 D0 T! v } catch (Exception e) {
) m4 z3 r: g3 E System.err.println("Exception updateLattice: " + e.getMessage ());) H% R# `' q/ H8 O' V
}! K) G4 Y5 u" e# ]7 c! N
7 c! n% m5 a7 ~- N
// Then we create a schedule that executes the
" a+ Z5 N- z& i( E* `$ E/ X5 U // modelActions. modelActions is an ActionGroup, by itself it+ D# ~2 p+ o( c, t" d+ H
// has no notion of time. In order to have it executed in: t/ M+ V: S) L/ K1 y
// time, we create a Schedule that says to use the
x3 B8 F6 Z" I- W // modelActions ActionGroup at particular times. This3 ^# l8 Z: A- Z6 I5 _+ J/ l% }
// schedule has a repeat interval of 1, it will loop every
% U+ X+ e6 p3 e5 l // time step. The action is executed at time 0 relative to
1 c' d8 y! H% k+ [: f1 k // the beginning of the loop.
?+ P% g- ^. |0 A
5 l+ O7 k- A: g2 ~% A% {% x, K1 r // This is a simple schedule, with only one action that is+ U& F2 h) `' S3 Q' Z' v9 h3 m5 D
// just repeated every time. See jmousetrap for more
2 p4 Z; o$ M. k2 J# v% q // complicated schedules.$ w& y3 _9 f2 b
& W4 s) J2 w m5 ^: ?' G* I( e
modelSchedule = new ScheduleImpl (getZone (), 1);
" H. \# G+ [, Y( ]' V modelSchedule.at$createAction (0, modelActions);
5 E: ?* c; L. v3 V8 ~ ( ?7 n, o6 ?" J' C9 A( ]
return this;
% L% G% y: h1 f: ^ } |