HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. ~$ `9 G5 `% a, [ c
3 h1 x5 D1 b$ Z) M8 w public Object buildActions () {- \+ \) Y5 c2 m5 w: W
super.buildActions();
' `, x+ y/ Q: E9 |; K6 M& [, ~; K( X ! ?1 i5 t# I1 y! ^, t% G( C
// Create the list of simulation actions. We put these in* Y! m9 e* z/ ~8 u: s& L. G; W
// an action group, because we want these actions to be
* \ j* k' d8 P C/ s // executed in a specific order, but these steps should! q7 F5 `: p- M, m" [
// take no (simulated) time. The M(foo) means "The message
3 {6 r6 X) f6 m; z // called <foo>". You can send a message To a particular
8 S6 z5 E6 y% `9 y" a& y // object, or ForEach object in a collection.
+ O, P4 ?1 X/ A0 S' U
' }: K; _2 b$ c2 j& T2 L // Note we update the heatspace in two phases: first run7 K' B4 Z! ?' B- i# S' x
// diffusion, then run "updateWorld" to actually enact the7 n" x6 j% J: _
// changes the heatbugs have made. The ordering here is
( i _/ W7 ?5 L4 s6 |* X# p0 {) c // significant!
2 F3 l9 {* _" c' @7 q 2 z. R5 x% E6 d' |& h" D* @
// Note also, that with the additional& G/ p, s: L3 W) r( i" Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
: d/ b' p, I/ {$ [ // randomize the order in which the bugs actually run
% E) c9 Q" C1 |( \8 ` // their step rule. This has the effect of removing any
! W; E4 q8 e1 Y' o& j // systematic bias in the iteration throught the heatbug
$ S" N3 Q. ?+ V // list from timestep to timestep% l" A& F& X5 \$ F i A# M9 T% B
1 p4 ?7 v1 x1 b' P
// By default, all `createActionForEach' modelActions have
: F: T/ a2 z: Y$ L7 s3 M // a default order of `Sequential', which means that the; C, u5 S* R1 s$ m h$ q
// order of iteration through the `heatbugList' will be
6 g0 e7 T A8 X' S& [ // identical (assuming the list order is not changed
1 [6 x3 i0 g& H0 K; J, B // indirectly by some other process).
n& f9 X4 e( c
. u- `7 L a0 A j modelActions = new ActionGroupImpl (getZone ());8 f5 z @' ]- ~
3 D# _; z, d" u5 Z try {
; U$ R2 X1 e6 S" \2 X2 P. J) J6 ]3 I modelActions.createActionTo$message
8 {+ Y9 j. b+ y5 x" _7 i& F& u# z8 b5 o4 u (heat, new Selector (heat.getClass (), "stepRule", false));
, Y3 X5 i' B. w3 W" D M } catch (Exception e) {" `0 }6 G. }5 M
System.err.println ("Exception stepRule: " + e.getMessage ());% b7 z' T4 x% F3 F6 n! |. A! ]
}7 \0 Z1 O/ ?% H1 j7 F6 Q
- o; t$ X' Z! m8 z2 N& d7 @ try {* V6 G! |1 V& M/ Z! T
Heatbug proto = (Heatbug) heatbugList.get (0);
9 @. j; X7 w2 e1 w Selector sel = 0 s6 Z( n/ D) c' }2 x
new Selector (proto.getClass (), "heatbugStep", false);/ `2 F* e% f# R* `( K
actionForEach =" M o$ r3 f# t* ^- @
modelActions.createFActionForEachHomogeneous$call
* n3 _6 j6 U, s5 i8 P' u (heatbugList,
: f) ^* O$ J& I new FCallImpl (this, proto, sel,, U: s! }3 D+ P, N# p6 _9 w
new FArgumentsImpl (this, sel)));
: X; j+ j) N( O6 { } catch (Exception e) {
# o( @% y3 a" K% o5 B2 q) v e.printStackTrace (System.err);; l" v" j" T+ T; e7 i- x$ [, {- `% {
}- f$ S$ W: v% E! W4 W
) @+ j5 _9 A3 C7 l: [ syncUpdateOrder ();
( w+ P7 s1 C- ]& L$ A; T( T/ P+ \" Y# h3 Z3 X% ]
try {
' s8 q4 `7 @4 P$ x- Z# B2 u4 I modelActions.createActionTo$message ( U: E! ^: Y) S7 w: x, L8 L) O- X
(heat, new Selector (heat.getClass (), "updateLattice", false));8 w) t: _9 S: K# Y) T6 d3 o0 V
} catch (Exception e) {' V; Z$ U9 ^* \& z T6 ~
System.err.println("Exception updateLattice: " + e.getMessage ());
& u4 x7 B" T. A }
- ?: q! o5 A! P7 ?# r/ X) n) v' i 3 Q& E9 G, ?; ]+ A/ ?5 H7 s3 w
// Then we create a schedule that executes the, X |5 a. I. V7 l1 z8 b6 c
// modelActions. modelActions is an ActionGroup, by itself it
$ c1 `0 B8 k& N // has no notion of time. In order to have it executed in
: c4 m- d5 u6 q" F* { // time, we create a Schedule that says to use the6 o1 S% j/ W) [/ w
// modelActions ActionGroup at particular times. This7 k6 t! Q5 @% x3 n6 f
// schedule has a repeat interval of 1, it will loop every
3 n0 T# C9 P! T- Z1 u8 X F // time step. The action is executed at time 0 relative to
* W8 h) B9 P- t8 \7 D; y1 r: p // the beginning of the loop.
! |* g$ o% ^& P0 \3 i, M( G& @) `( p; l* ]' i
// This is a simple schedule, with only one action that is
* @0 i0 W: K) u- h // just repeated every time. See jmousetrap for more
, {% {' n( \. b6 E$ p // complicated schedules.
" g, X% ]3 h: q
. j/ O$ g8 _ l7 a5 O$ }" k$ _ modelSchedule = new ScheduleImpl (getZone (), 1);
K9 n' y4 E. Q6 b6 y; m7 W modelSchedule.at$createAction (0, modelActions);
. L5 s/ z& f0 n+ \4 h: c + \# |+ b1 v1 \: g5 m ?
return this;) t$ ^' e# |" {; _) p0 t+ o
} |