HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: }7 a" l1 i$ f# @: K6 O- J5 ^, t' n; @ u) J) I
public Object buildActions () {
$ b3 H' X# ?2 D0 R; B) t/ R* V super.buildActions();
+ ` Y6 I4 Q! f' q! \; o
, J q% l5 s R+ O' b \ // Create the list of simulation actions. We put these in$ N" j7 c v/ e: w! r; S/ ?
// an action group, because we want these actions to be; c0 g$ X5 h1 c# l3 F* ~, j
// executed in a specific order, but these steps should( `3 A* v4 e9 @! A
// take no (simulated) time. The M(foo) means "The message. L5 K- m1 Q+ U5 S
// called <foo>". You can send a message To a particular, d, s8 F* W" ^5 O2 v; ?
// object, or ForEach object in a collection.
& c9 z8 u& @3 x5 C- T ! b# d0 E5 A* q; D! J
// Note we update the heatspace in two phases: first run
, y/ V3 G# z8 \5 Q- l! m' ^$ E // diffusion, then run "updateWorld" to actually enact the
" ?3 m5 F- F; o3 x, ]) G // changes the heatbugs have made. The ordering here is
6 Q& o7 d. n) O* {! d+ Q- R. ? // significant!
" |7 O' c# b5 n a# O" e* S! N u6 ?8 P9 i
// Note also, that with the additional
* ]9 R) y2 A. I- T7 R% ]% C // `randomizeHeatbugUpdateOrder' Boolean flag we can
+ @$ I. [3 r$ v5 w8 Q" T // randomize the order in which the bugs actually run
8 X& g0 z0 a, r8 A+ M0 F( [ // their step rule. This has the effect of removing any& H$ { Q }0 Y; x
// systematic bias in the iteration throught the heatbug- {/ E- D( V1 i
// list from timestep to timestep r* u: Q7 N& ]; l8 e
/ [6 v: D% @8 W5 K
// By default, all `createActionForEach' modelActions have% b3 W0 t. ~! V2 ]/ @
// a default order of `Sequential', which means that the' }- d" m1 _, s& E+ p
// order of iteration through the `heatbugList' will be1 g5 \4 Q' V5 e% p0 q2 d% h$ ~
// identical (assuming the list order is not changed/ I% o- F0 O, x7 {" ?- t4 M0 R
// indirectly by some other process).
?) A$ E6 d2 L/ s* W7 Y
, b7 @/ G2 L; a e C modelActions = new ActionGroupImpl (getZone ());
$ R% D- }& g0 C% r/ {
+ @. Q9 r8 R$ e$ S3 Q try {, z4 Q% F4 L; _, g3 q
modelActions.createActionTo$message
% h( a0 `1 y" O0 o (heat, new Selector (heat.getClass (), "stepRule", false));
: {7 G z4 {: S' x) ]2 M; P# c } catch (Exception e) {
5 H% G* M1 K8 Y: |0 r& q System.err.println ("Exception stepRule: " + e.getMessage ());/ U, h" T4 D) t3 p
}
( d7 r0 S9 \# x; K
: n" P8 M7 E8 O( H* Q& k* L try {
?6 I. B: u; E: d: W) E Heatbug proto = (Heatbug) heatbugList.get (0);7 C" M/ \2 O! h
Selector sel =
* S: J! V7 b4 B new Selector (proto.getClass (), "heatbugStep", false); v! O8 k9 Q0 L' Y: ~9 ]
actionForEach =: K; |7 V3 B. S0 C+ y* Y, |
modelActions.createFActionForEachHomogeneous$call
1 c& y6 x G+ l; j( y; k ^9 ] (heatbugList,' w5 g* }% s% l8 D, a7 t, U
new FCallImpl (this, proto, sel,
1 b/ J4 m, p+ D0 r new FArgumentsImpl (this, sel)));$ k" H# o5 y* c v% w2 H: W
} catch (Exception e) {$ |, T$ m4 z2 G* }9 H( [0 D5 e7 k
e.printStackTrace (System.err);
% ^# ?8 |9 G2 [1 K! X2 r }# R! }6 m: d% l ]; q
6 e% ]4 ]5 Y) _2 R- p
syncUpdateOrder ();! R! H8 O4 i1 ~& ^
9 _- ?( @3 d' g) X( N try {
- B; o9 C% b# F& K8 G( G9 E+ d B7 t3 E modelActions.createActionTo$message * o7 H+ s! ^' h) G% ~# q/ \
(heat, new Selector (heat.getClass (), "updateLattice", false));. _7 a8 N& A/ y1 b2 x7 m* l- N
} catch (Exception e) {3 I$ ^3 u5 a. x# H# B
System.err.println("Exception updateLattice: " + e.getMessage ());
4 M" s' S- J, c# b' T" u' P' t }
6 F# z8 X! F8 A5 L3 o- q ) V0 p& f0 p5 L# @* V/ w1 e
// Then we create a schedule that executes the
% Y' e- k4 {! V7 d/ [& s9 m _ // modelActions. modelActions is an ActionGroup, by itself it2 c. T; w( ^& h& e7 T4 C
// has no notion of time. In order to have it executed in
+ }& E; `/ R- P: C // time, we create a Schedule that says to use the2 r, L% k& J" M. F6 m% g u
// modelActions ActionGroup at particular times. This6 j' Y2 Z, g6 }2 G( l+ x5 N
// schedule has a repeat interval of 1, it will loop every
& P1 p0 V Q: E$ u3 C. i // time step. The action is executed at time 0 relative to
% w: i! y7 D+ n, e0 N // the beginning of the loop./ V0 Y; N5 p3 [/ d" P( z% \
3 ~0 u M2 v; ^5 n( [% E4 g6 r // This is a simple schedule, with only one action that is% ` Y- e* T- |: l6 a3 Y
// just repeated every time. See jmousetrap for more H0 x$ j% r" ?' C' l; I. r$ ^6 F
// complicated schedules.
" l: r& S2 n4 l9 G+ i3 B5 w& M / S6 N: O! H$ V& A/ ^7 M7 I; _
modelSchedule = new ScheduleImpl (getZone (), 1);
& A. B5 k n E" E0 f- ?! Z modelSchedule.at$createAction (0, modelActions);, G: r! F ]: r0 U2 O
1 |4 w; @/ o, d J. n7 ~2 W return this;5 X, ?' q1 ]. i, f
} |