HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ w! m3 x$ i( ] v& [
3 h5 Z0 Q6 {/ W* o0 S8 d6 d9 S X" Y
public Object buildActions () {. P4 t$ z' A8 q3 m# F) x
super.buildActions();( {$ \# a0 C/ e' s. {
$ X4 g5 z8 b. x. @/ [ // Create the list of simulation actions. We put these in
) g6 I& ]# w! ~. P0 b) O, p. W // an action group, because we want these actions to be
3 l5 g, ~; j/ H- C+ @) M // executed in a specific order, but these steps should
i8 {3 O9 F; W0 ^; M0 l // take no (simulated) time. The M(foo) means "The message
. A. b- {/ d6 O: }; y% d; B) f // called <foo>". You can send a message To a particular
" r7 ^6 V H$ M& k& ?6 M, U0 v // object, or ForEach object in a collection.
' _: C" ~/ X% A% Z2 q$ Y7 n
% D! D; T! S0 d. @1 V, C/ [ // Note we update the heatspace in two phases: first run
+ ?+ G/ y) U. Y* ^# ^# U // diffusion, then run "updateWorld" to actually enact the9 \8 q/ @3 E4 u/ [9 v# p
// changes the heatbugs have made. The ordering here is
: A% C+ [& L$ Z# A, P6 l% j // significant!. j0 t/ y4 T5 R: l( C
0 q }0 ^! N6 ?3 W$ Y // Note also, that with the additional
7 j2 b0 W- A, R% e& I; k3 D // `randomizeHeatbugUpdateOrder' Boolean flag we can
: ?& l) q ^, i P1 X // randomize the order in which the bugs actually run
1 N4 @8 |3 ]& g3 b // their step rule. This has the effect of removing any3 A& z2 m! ?* m! I
// systematic bias in the iteration throught the heatbug
( ?& F3 o) d# Q1 h6 T" I // list from timestep to timestep
. S6 O0 Y0 F* P- P3 ~
2 G3 e c9 W' N, i6 o // By default, all `createActionForEach' modelActions have
R: Z4 @" ^$ g: y4 m( q, q. a! J8 q // a default order of `Sequential', which means that the
7 A' @/ X0 m% M+ ^; I // order of iteration through the `heatbugList' will be
5 y% L$ N# y. e5 [$ h. l/ d5 [ // identical (assuming the list order is not changed- L; F7 }$ r0 n* i2 a1 H
// indirectly by some other process).
# o! b# J% c6 l) f0 q4 z . p6 N' M( w" A2 j
modelActions = new ActionGroupImpl (getZone ());! Y& ~. K# j! w5 o% a0 l2 E) T
0 T( ]& A5 Y5 \! v+ G, F* V
try {8 [& t) ^" T! s7 n, \6 n* E
modelActions.createActionTo$message+ U! z& z# X+ ?* T& ]) u
(heat, new Selector (heat.getClass (), "stepRule", false));" o1 y" y; y; }
} catch (Exception e) {
6 O0 z5 E7 S$ I* J! E1 S System.err.println ("Exception stepRule: " + e.getMessage ());
/ z) ~" A6 o o% y, |. W& W }8 x- _: i* B; Y( w& M, G
# B6 I" ^7 L- d try {/ i. r; z$ j! ?4 r
Heatbug proto = (Heatbug) heatbugList.get (0);2 P D1 Z& ?% |5 G- m& w
Selector sel =
0 _* E0 M: F; u2 @ new Selector (proto.getClass (), "heatbugStep", false);
' u4 ^% @: z& b% F actionForEach =
- v( _0 L6 P7 D$ h H5 E% i( J modelActions.createFActionForEachHomogeneous$call
9 S* f! s' F( B, o. ~1 R9 O (heatbugList,
' W3 {7 v- m9 t1 F# G new FCallImpl (this, proto, sel,, f9 d: Z+ o H. Y1 M# w
new FArgumentsImpl (this, sel)));: L# n; t2 @* I; L9 b/ B
} catch (Exception e) {
% D5 u: C: S0 Z7 A/ \6 B/ L e.printStackTrace (System.err);9 N- N: k+ g. o* D0 I
}) T: a( e+ Q. r. t/ B; c) U
1 k+ i( B0 g! p) V7 e- @* D4 N
syncUpdateOrder ();" B$ y3 v, U- Z4 P8 x
8 {; n% A! i) g, Y5 J try {
1 f* X; r5 d0 {# @ modelActions.createActionTo$message / W; s# v: Z1 ?1 n& O
(heat, new Selector (heat.getClass (), "updateLattice", false));3 \/ {, k& h8 E" d
} catch (Exception e) {
* J* ~1 E b& j5 [( M System.err.println("Exception updateLattice: " + e.getMessage ());. N) A. W, n/ e8 C0 W0 Z8 F
}
8 o8 ?3 s/ R, K" P ; ^3 v# U3 ?2 N8 c# J
// Then we create a schedule that executes the3 p4 U; j V& W7 Y
// modelActions. modelActions is an ActionGroup, by itself it9 W6 b6 p# [$ h1 ~: a2 o" n9 a. r
// has no notion of time. In order to have it executed in
2 Z% `2 H8 `* x2 ?) F // time, we create a Schedule that says to use the2 o% f* f0 O8 A2 ~. z Y( r* H1 f
// modelActions ActionGroup at particular times. This
! N7 g4 y7 D* B! r( I i // schedule has a repeat interval of 1, it will loop every; c) g* x" W4 A) M) j
// time step. The action is executed at time 0 relative to4 q! p5 y+ p" r) N* h: y
// the beginning of the loop., f! j+ L4 W4 d% o9 U
, Q* R# B1 K$ z // This is a simple schedule, with only one action that is2 K0 v9 p: u+ O2 S9 F
// just repeated every time. See jmousetrap for more3 h* O( ~$ d$ M' s/ O; B+ [& F
// complicated schedules.4 _0 T7 H: W! f2 y3 m* l: s
5 y8 e$ \$ f) A! H; V3 V$ B5 I
modelSchedule = new ScheduleImpl (getZone (), 1);4 A& D; J: {) f! b( {
modelSchedule.at$createAction (0, modelActions);
# V1 B. i4 c% B: {. W$ `9 w/ y 1 S* T3 B+ m6 E
return this;
$ s; t( O; X+ } } |