HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- a4 r$ k. N- B6 R! p4 m* S
( i2 }3 _( I0 Z' L9 \7 _: O public Object buildActions () {2 B: Q0 I( ]8 B `( p4 }6 m
super.buildActions();! j6 Z X+ K l3 P
; S. i v ~* z. s: P2 @, e6 Y // Create the list of simulation actions. We put these in1 r- Q! V0 ^- F$ z. T* Q
// an action group, because we want these actions to be
; J, k* Z% d+ ~; L // executed in a specific order, but these steps should
( }5 H2 p5 } b- U* G, P# M) j- i" h, u // take no (simulated) time. The M(foo) means "The message' p" i# D( f7 r' R+ b+ i. u2 q1 p
// called <foo>". You can send a message To a particular
0 a( J2 |$ H" h% O8 n/ u; n( z // object, or ForEach object in a collection.
0 H+ l" v4 f) b+ S6 C - T A' b3 T/ L: z7 S7 ^
// Note we update the heatspace in two phases: first run/ m- i* o8 n- @) |/ Q8 `
// diffusion, then run "updateWorld" to actually enact the3 ^+ P1 {& n7 J# K% D# S
// changes the heatbugs have made. The ordering here is; a t4 j: L( n4 o0 O
// significant!
7 _" N. [/ X' X. Y+ p: E. x1 X7 i( C
+ j0 s0 F; |3 V: R& S& w0 W // Note also, that with the additional
" T) b. q3 \1 D0 I _' |) z // `randomizeHeatbugUpdateOrder' Boolean flag we can7 M. a1 y1 Y g5 \7 R" j8 u" {
// randomize the order in which the bugs actually run
% `* y4 n$ g/ y; t2 R7 ~ // their step rule. This has the effect of removing any3 r0 i8 \% m! L) W! ?7 t8 g5 P
// systematic bias in the iteration throught the heatbug1 y# ~1 e& f9 ^$ e
// list from timestep to timestep
* n4 B- |) Q& b5 P+ g' P
* Y3 u- N+ M$ V" U) h // By default, all `createActionForEach' modelActions have
, a# C7 J- n9 [3 q/ i0 `' W // a default order of `Sequential', which means that the
7 I( u5 l' | _( w // order of iteration through the `heatbugList' will be
. d/ V; g% L, z3 Z // identical (assuming the list order is not changed* G) x% i' d9 D1 N9 \" h: U
// indirectly by some other process).
! g8 e T G% X3 T; r * m) j+ K% h( f& G
modelActions = new ActionGroupImpl (getZone ());& U3 r$ w" i, f& }7 R
# A- I$ d) {* ]) j9 T9 L% k
try {
8 @- ^ O( n7 X" {4 l8 Z modelActions.createActionTo$message7 U- N) N0 a. f# n
(heat, new Selector (heat.getClass (), "stepRule", false));$ K3 j: M5 Z! L; v, J) P$ k. P
} catch (Exception e) {
% p2 I. Q0 k2 ] System.err.println ("Exception stepRule: " + e.getMessage ());
8 A; o+ B( w1 [' Z/ K" u6 D/ k9 l0 ] }$ @& ?4 S- B* C( v- c. S/ r
* @2 y& \. f6 O3 B \
try {# u1 \5 e0 o) Q: o, W, o$ D' a& A
Heatbug proto = (Heatbug) heatbugList.get (0);
" d# J l, v& Y8 A! S: R Selector sel = ' j" d9 q4 i$ |3 g N$ z- l
new Selector (proto.getClass (), "heatbugStep", false);) o. D O; K) t2 j- K- h
actionForEach = N: C8 n! U9 W
modelActions.createFActionForEachHomogeneous$call; O4 Z5 c6 e; t9 ^+ s
(heatbugList,( H3 B2 T# W% q
new FCallImpl (this, proto, sel,
7 W4 }9 w% c$ p" H: E# h new FArgumentsImpl (this, sel)));4 _! m. e- @8 ~. h6 b( B k* B9 C
} catch (Exception e) {. K# h. {% X s1 C* M y: i
e.printStackTrace (System.err);
+ g! Z. v! z8 S9 g9 \ }' d8 I4 `) c- a* h' R/ o4 x
3 R0 v& n3 i6 }0 j2 X syncUpdateOrder ();9 Q. O3 M' ^7 n# J
) Z5 D1 q n* t- Z8 \4 i- w5 v try {
( R7 n( v' R3 y+ x+ q( [ modelActions.createActionTo$message + q" d$ E4 O9 [6 S T) W5 g2 _& @
(heat, new Selector (heat.getClass (), "updateLattice", false));9 B( O$ ^# v' q* `. e/ L. V8 G
} catch (Exception e) {, ]2 {" c( F5 c0 c
System.err.println("Exception updateLattice: " + e.getMessage ());
1 w- `1 j: ~; | }
' {6 N, [* C. N8 I! O9 q4 u
* r: X4 _; P$ A& u: V/ B. w // Then we create a schedule that executes the! h4 \4 G* E" ?' t b1 t
// modelActions. modelActions is an ActionGroup, by itself it8 Y' p( ?/ H: R' ~, ^. p S
// has no notion of time. In order to have it executed in9 z/ g; T: L1 N$ E
// time, we create a Schedule that says to use the2 n+ y, b9 F/ s2 h) ]
// modelActions ActionGroup at particular times. This# A+ @6 ?, I6 o0 |2 g
// schedule has a repeat interval of 1, it will loop every9 K4 W! Q# q: Z3 H
// time step. The action is executed at time 0 relative to
0 a. a1 |& L2 @7 F: ` // the beginning of the loop.2 g0 Q6 I3 I0 Z/ c
' f1 \/ C, }& Q0 _1 ]! d/ f
// This is a simple schedule, with only one action that is) N6 k% {3 }9 _9 j+ i/ P* k K
// just repeated every time. See jmousetrap for more+ y% [# l( F2 K9 t
// complicated schedules.
" H6 \( h0 D. }4 `( ~9 K5 S9 [ ; C& g, a7 u9 H9 _8 h
modelSchedule = new ScheduleImpl (getZone (), 1);, W& n4 [/ e/ D: w7 w, y* q/ m
modelSchedule.at$createAction (0, modelActions);
0 e/ s7 ?, V( |( y& K0 V1 _. i5 X ; d8 J5 C! K: E5 `3 I
return this;
& a, J% ` Y' Y } |