HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 a3 g1 s( h6 e" a. m0 G& X" `% t$ O' C
public Object buildActions () {; F3 u2 q' {; C
super.buildActions();
6 B) y( D4 j; B+ \8 u" R & a+ F+ g5 B' x1 Y k' w" x" y$ @
// Create the list of simulation actions. We put these in* M' P+ j- |* N1 @! F6 g2 b5 T% |
// an action group, because we want these actions to be T( z" `" R. s8 ^3 q4 } f
// executed in a specific order, but these steps should' j# s5 c9 ^! j( }
// take no (simulated) time. The M(foo) means "The message
$ o6 ]; L! W( [( F7 @) E9 t // called <foo>". You can send a message To a particular1 D) b9 y; p& d) v) h
// object, or ForEach object in a collection.
/ e# p( G5 E, S# u$ b
5 o. U5 o( q' K // Note we update the heatspace in two phases: first run& a7 @& T. L0 t9 D- W
// diffusion, then run "updateWorld" to actually enact the, B- N/ \( y. `# P+ I
// changes the heatbugs have made. The ordering here is8 s- z7 s) D8 s7 ]/ _# C
// significant!5 p2 b0 z: a5 K( A; K9 e3 p
+ C' y( }" l s3 f% z( x" S7 @0 D$ _ // Note also, that with the additional
1 h9 {- G" j) F- I0 c. o" r // `randomizeHeatbugUpdateOrder' Boolean flag we can$ m- d, N. Y4 B2 o- U9 m
// randomize the order in which the bugs actually run
; e; ] B5 \5 e/ t0 k/ W* i! O, x1 Z% @ // their step rule. This has the effect of removing any
* ?; \4 z/ v7 c9 P' E% p2 B // systematic bias in the iteration throught the heatbug
' w7 B* c/ _ ]( [0 B // list from timestep to timestep
7 A8 E% R+ y; Z% t/ b p: s 3 X, |3 K: H; P5 ~# ?
// By default, all `createActionForEach' modelActions have
5 S/ q* Q8 e4 h9 Y // a default order of `Sequential', which means that the
w, T9 C( j5 u- x" Y // order of iteration through the `heatbugList' will be: @# x3 \5 [: B+ E% p1 _' E
// identical (assuming the list order is not changed' A* I: L u5 G: V8 r8 n
// indirectly by some other process).( r/ z0 o0 L/ h: F! P1 j
# f G2 p, C0 a. h1 y5 Y) w
modelActions = new ActionGroupImpl (getZone ());# }" s$ t! O, n( y& G, }2 v# L/ Z
; x& G6 `/ G3 ~ try {! H% o2 Q5 R/ @/ t' c, D
modelActions.createActionTo$message# m3 i3 r' H+ b) l8 W3 G
(heat, new Selector (heat.getClass (), "stepRule", false));
5 Y0 Y' r4 c8 [2 A/ w } catch (Exception e) {4 |; o* O# M0 t3 W
System.err.println ("Exception stepRule: " + e.getMessage ());7 J) ]: E% X+ k# i# B
}! b, I! j& a# L. f# m( _
" e+ ` H6 q! `0 y# C) A _ try {9 ~' w3 S. R/ |8 K$ ^* N
Heatbug proto = (Heatbug) heatbugList.get (0);/ i* N A+ l( B# j
Selector sel = % }; R: U+ D7 {' m' k
new Selector (proto.getClass (), "heatbugStep", false);% }- B5 I* c; u7 }; N& O
actionForEach =
E' Z; e; Y- v5 ^" x3 H! W modelActions.createFActionForEachHomogeneous$call
* @+ u/ F4 l0 @, a0 k+ v1 W (heatbugList,
3 W7 c! ?$ r& @- V) t new FCallImpl (this, proto, sel,/ q. H4 v8 H: Y5 {/ ^
new FArgumentsImpl (this, sel)));
$ e( z" a `2 H) \# a9 c } catch (Exception e) {+ ^7 i; {3 ]- t
e.printStackTrace (System.err);
s) z6 J- p9 I; I) B }) z1 m1 ~) j7 K; }' D
0 H( L7 T% P6 r3 y; p0 C+ m
syncUpdateOrder ();- e' i8 Y8 J6 m
1 [- ?* \2 [: `) k) @7 Y try {" m8 o) A, i* b5 i9 f* N
modelActions.createActionTo$message 6 a. X" ]) y; G- ^: m! {
(heat, new Selector (heat.getClass (), "updateLattice", false));% O8 T3 {) i+ r' |) ` \0 l
} catch (Exception e) {( w9 ]& z3 z! q K. |: z b
System.err.println("Exception updateLattice: " + e.getMessage ());
8 T3 Z" `; G. T }
' ]$ Q) j- U) b1 r1 ] @8 C- N ! w/ ]! T3 R6 Y# `
// Then we create a schedule that executes the c( S3 T+ x9 j' {9 P3 u
// modelActions. modelActions is an ActionGroup, by itself it
; v T3 T. j2 i // has no notion of time. In order to have it executed in
) m* n) x3 B' {2 T { // time, we create a Schedule that says to use the
1 _% F- @. ?& G- }7 ^, R // modelActions ActionGroup at particular times. This
# B, X$ k* D9 h, u8 Z* p+ y // schedule has a repeat interval of 1, it will loop every
- m0 @/ N3 h0 Y" v0 S8 m // time step. The action is executed at time 0 relative to, j; |$ `3 C. G* Q4 c7 _
// the beginning of the loop.
+ g* U4 a# N7 |' h
* B7 X* j/ h% {2 Z- } // This is a simple schedule, with only one action that is
R* y$ I+ u+ V. x* N u // just repeated every time. See jmousetrap for more
; S; X3 p3 R" M6 Z! e // complicated schedules.
n3 W# M4 g8 y( W1 ~% u( P: ~ 8 h, m. j4 T, H0 d; Z! T
modelSchedule = new ScheduleImpl (getZone (), 1);: A, u& X7 ? P
modelSchedule.at$createAction (0, modelActions);& w3 ?% B; ~) h- R
9 e2 f { h( i" |( j7 J7 g
return this;
3 T; j$ m: u% z% e5 z% U! P% L } |