HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 T1 j6 w8 Y+ Y* V6 }: I1 Z' r' ~
public Object buildActions () {
9 Z# b! h& _! I$ d) }) z4 | super.buildActions();
3 a& v6 G. j8 R; d% ^ 7 {# n! X* a0 d7 c2 c) Y8 D4 t
// Create the list of simulation actions. We put these in% C2 `7 [& o9 n8 h
// an action group, because we want these actions to be/ V) Z9 _2 G' f- |7 E1 I+ R/ d: Q
// executed in a specific order, but these steps should
2 ]9 Z0 h. I( ]' \, | // take no (simulated) time. The M(foo) means "The message& Q ~7 X; ?) Z3 v3 o) S# \
// called <foo>". You can send a message To a particular
' W% Y/ b. c" \3 m; F // object, or ForEach object in a collection.5 v! H* t" M" Y ?+ f
9 J7 P) r( u5 z! A // Note we update the heatspace in two phases: first run
3 l) K) J# @/ E) ~+ s' e. { // diffusion, then run "updateWorld" to actually enact the; a& \, J: U; ~, S' \
// changes the heatbugs have made. The ordering here is/ c U9 Q" R6 C& ~. g% c
// significant!
2 y# E7 K4 B- l+ _# g
' ?" X, p. `. W5 ~ // Note also, that with the additional" v+ K! ~" h6 y9 Y- i/ C
// `randomizeHeatbugUpdateOrder' Boolean flag we can
5 o2 U3 N* F" t/ S; d$ ~1 ]& e // randomize the order in which the bugs actually run& k7 Y0 p2 S! L3 u6 \
// their step rule. This has the effect of removing any
5 p& B) ~# X0 [9 ]0 i // systematic bias in the iteration throught the heatbug
. g, A- b; b! N1 [* r3 k5 R; R // list from timestep to timestep9 G) j5 L" g$ m0 @
. _" X5 v9 K! }0 n7 E3 ?
// By default, all `createActionForEach' modelActions have
' s9 H- M& f. h9 ?) M Q" s1 ? // a default order of `Sequential', which means that the
5 y# }9 V& A7 ~% A. S // order of iteration through the `heatbugList' will be( k1 M5 k- o! t( A
// identical (assuming the list order is not changed
5 }& c, h& g9 l' E, u9 S* M // indirectly by some other process).% [: r+ Y4 m$ ]) v, Z$ l6 G% Y! M
: M% I, `7 Q4 ^* T5 p8 n9 x5 Q modelActions = new ActionGroupImpl (getZone ());
$ L3 n, I! s$ k- R) H0 D
) ?: Z W6 U) U' [* K try {: K/ l( @/ ], V; D7 }' p+ T
modelActions.createActionTo$message1 x; F' X+ g$ Q" b' ?
(heat, new Selector (heat.getClass (), "stepRule", false));0 F2 V, y0 x! |) a1 I8 d
} catch (Exception e) {
, s* a" u5 Z9 \ System.err.println ("Exception stepRule: " + e.getMessage ());9 ?" ^; D @: o& U1 J
}+ g) O0 d- m9 _/ i7 R
" a% I+ a9 Q7 v- F
try {6 S6 N' \6 I! R) r2 k! m
Heatbug proto = (Heatbug) heatbugList.get (0);
, t( U* d" D9 O, F Selector sel =
! ]% k8 c- `, t' C w3 t new Selector (proto.getClass (), "heatbugStep", false);4 p1 |' F# l2 z; f9 t- }4 i
actionForEach =6 H7 P4 [) X! l* u, v r% V
modelActions.createFActionForEachHomogeneous$call
3 d9 j) [; A: L% c4 l6 |* h1 U (heatbugList,
) t6 D* y! W" K3 C$ f2 `3 Z new FCallImpl (this, proto, sel,
/ w3 p$ e+ J* U. L$ { new FArgumentsImpl (this, sel)));& r4 l! o% b; Z+ g0 E0 r4 F, p7 v
} catch (Exception e) {. p- Z0 f) _) [: F! O6 X
e.printStackTrace (System.err);, x# P$ |- n' U/ ]
}, m8 X+ c& K; _ R" m* Z
+ b/ o$ m) c: W+ m/ @$ O
syncUpdateOrder ();9 q0 l4 [ H- d+ d) i# S& G$ q
" x: ^$ ^ U! w8 i+ b( E, }8 `
try {" o) B7 V) X! W9 K H2 U
modelActions.createActionTo$message
2 b3 [0 d: |3 o) w/ D+ p m; a A o (heat, new Selector (heat.getClass (), "updateLattice", false)); U8 f/ f# \* I7 D
} catch (Exception e) {
6 k* }9 t: ]# `" w" d: ? System.err.println("Exception updateLattice: " + e.getMessage ());+ ]* l/ q8 L0 b* c' W# ^
}
6 k2 J; Y, }2 }* g) }4 [9 i$ \* [ ! G- J2 z+ }; d
// Then we create a schedule that executes the4 g& s: @2 A4 Z% c: S8 Z B, ]
// modelActions. modelActions is an ActionGroup, by itself it
' ]: W# X( G2 }1 J. X // has no notion of time. In order to have it executed in3 T8 r9 J% r9 s% g% s" v9 t0 t! d
// time, we create a Schedule that says to use the, I/ i* x; E: |* @( O
// modelActions ActionGroup at particular times. This
/ s! |; `% C. \" s // schedule has a repeat interval of 1, it will loop every
. B7 Q" S: m: e% S6 J; R: r // time step. The action is executed at time 0 relative to/ B. N# s! G: N: r' | @
// the beginning of the loop.
7 F5 B+ `' ? t9 T
( Y' C1 n4 l) K& z0 x6 } // This is a simple schedule, with only one action that is( ^5 T& Z, D6 j2 t1 d/ T, i
// just repeated every time. See jmousetrap for more
) [' T6 n7 q. Q( L! h. p% o // complicated schedules.
8 J0 ]3 t( u7 ^/ W: u* P: B
+ d' y9 G2 t8 V* u" g, [ modelSchedule = new ScheduleImpl (getZone (), 1);3 d6 J) G2 {( L0 @$ j7 M2 c
modelSchedule.at$createAction (0, modelActions);4 b/ C1 V- |$ U h6 H
0 T, y- G; @. t G7 Z" F& U; S return this;/ ]: H! B9 r. ^* i: Y- n
} |