HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) r, _+ C2 C/ B' k5 x& d
5 b s% z6 q. d3 h' {# ? public Object buildActions () {$ d* w6 S) T) e" U: S
super.buildActions();
7 S4 F: s1 e. k! G, ~
. ]$ j4 z) i. L6 [4 W+ s+ ?2 q // Create the list of simulation actions. We put these in
9 g9 T2 B2 t8 n" h. w8 R6 `5 j) a // an action group, because we want these actions to be& i7 q) V2 s7 s( `3 q i: a
// executed in a specific order, but these steps should
( I) A/ }" w- B // take no (simulated) time. The M(foo) means "The message D5 W4 {3 B2 }% ?) a8 `
// called <foo>". You can send a message To a particular' o2 n9 O) A) C. V" D9 j k. ]4 P' Q
// object, or ForEach object in a collection.
: Q; Q5 ? r2 w. @
! i/ `& V! {3 A8 @& h+ b7 H* g* W) X // Note we update the heatspace in two phases: first run
# K/ u# u+ [% u // diffusion, then run "updateWorld" to actually enact the( l% `- Y& e# G# h: y. g3 f7 Z
// changes the heatbugs have made. The ordering here is
3 ~8 w v/ h4 ^4 C4 L. ]1 B // significant!- F1 x' i G0 ~& C) K
1 F4 ]( }0 l# X U7 d8 `" s2 M // Note also, that with the additional" c: O' H$ s; Z# B4 r8 V O/ p6 t
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 X& ?" J% B& p7 i) e
// randomize the order in which the bugs actually run" g* r& i& x( Y& g: n! H# B; a' j: T
// their step rule. This has the effect of removing any
: F5 U) E" [0 l // systematic bias in the iteration throught the heatbug
( h& I$ W8 t/ d+ {% A1 K7 s; m // list from timestep to timestep
& Q! {, G) A; I; _$ w + j* J4 V- c$ ^0 V6 @! K* ]
// By default, all `createActionForEach' modelActions have! n/ w f# g( y; f# B4 \+ Y- X
// a default order of `Sequential', which means that the
# B8 G3 u# D, {/ c5 [+ r) P // order of iteration through the `heatbugList' will be
$ A% Q8 U0 p$ l P4 p4 E% U // identical (assuming the list order is not changed3 D ]0 `& s6 x* t. @2 _
// indirectly by some other process).
1 M% d1 R5 D& [5 J) o% P 5 ~8 y' }* D/ O. P
modelActions = new ActionGroupImpl (getZone ());/ Z, ?3 I% G( r% \( I
% f. m: h3 |3 M5 O
try {
) q9 ^5 k2 y: f1 ~ modelActions.createActionTo$message" c& J+ t" O& W1 e9 c
(heat, new Selector (heat.getClass (), "stepRule", false));
8 ~, U8 F9 p3 }5 v$ S2 r% n4 \ } catch (Exception e) {
' b! {- q; a" D2 E9 T System.err.println ("Exception stepRule: " + e.getMessage ());' `7 `: o" s+ Q* d8 x
}) o: K% H* J* U( u) y- u0 A5 i
) ?; K- k [+ h; j9 g7 |; w try {
8 M. J3 J; c0 K O Heatbug proto = (Heatbug) heatbugList.get (0);
6 W' G; I% z! j) M* I Selector sel =
& A' G. Z x! @( H0 o new Selector (proto.getClass (), "heatbugStep", false);* @+ H& \9 T! S2 x6 t8 h1 F
actionForEach =
* X$ w0 F5 F6 h8 `; s modelActions.createFActionForEachHomogeneous$call
0 {# S/ B3 j# j q! ? (heatbugList,& T8 r" y0 {1 W$ j/ N. v& p
new FCallImpl (this, proto, sel,/ g& o* r M/ v) d8 ^! f
new FArgumentsImpl (this, sel)));4 a/ N$ }1 ~; Q. o( I# L
} catch (Exception e) {
! k. \, x5 e( R8 \, q4 Y' l e.printStackTrace (System.err);
4 X' T$ w2 Q0 R7 E! Q) z }
! m# G3 H0 x: h0 |7 l6 L. b/ N
( \; v1 O' _ K syncUpdateOrder ();7 N3 B" \2 f) T U
" Q" T# ~4 m/ V% q2 c" [ try {0 _! N5 e( w& r
modelActions.createActionTo$message % x5 |6 ], R8 M4 G% {
(heat, new Selector (heat.getClass (), "updateLattice", false));7 P* G* M; |/ o3 U/ p
} catch (Exception e) {
( h% c1 M, M5 @: F System.err.println("Exception updateLattice: " + e.getMessage ());" F2 l$ `, c, [1 B1 {
} f# ]" [& M$ h0 i. L5 V3 d
: Z( f% u5 u" u2 u7 x0 H
// Then we create a schedule that executes the, C6 B0 K x, q( ~4 k/ s
// modelActions. modelActions is an ActionGroup, by itself it
8 {- L$ n0 M) j8 C* j // has no notion of time. In order to have it executed in
' J a3 |) ~0 ?( Y' J7 H // time, we create a Schedule that says to use the3 h. K) I( a# O- r3 b2 g
// modelActions ActionGroup at particular times. This% T# N) t/ @! o* v; n7 ~
// schedule has a repeat interval of 1, it will loop every
2 g3 _% B8 ~+ e4 c+ w9 K3 t // time step. The action is executed at time 0 relative to1 Q( R! K* p8 u
// the beginning of the loop.
3 a M1 Q3 p, I: W$ V$ d3 v% F2 E+ \) V& D$ }" i/ v: a9 K1 t
// This is a simple schedule, with only one action that is
, D$ `, @: t) J // just repeated every time. See jmousetrap for more
7 y+ F& x3 r2 M$ x // complicated schedules.* |/ Y% c! ^( Z& ^6 c4 J5 M
! a( ~$ h- |% u9 N. T" ~! c
modelSchedule = new ScheduleImpl (getZone (), 1);# V5 H5 d- L, @
modelSchedule.at$createAction (0, modelActions);4 X3 B' n; ~$ r$ o
8 c1 u6 [/ _6 h$ y* Z( j' y1 b return this;
& n$ \0 B7 B0 Z1 D0 J* | } |