HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& o# p6 ^! |: c
+ p5 B4 ]5 L0 }0 c Y( G( D public Object buildActions () {
" J6 U6 D2 |# c( P1 l8 K super.buildActions();
3 y7 ?/ E H+ ? ?
8 h/ X+ {: W; S' k* m // Create the list of simulation actions. We put these in% Y0 C6 j- T I7 i( W
// an action group, because we want these actions to be" Z( u& j& j& {) N3 a$ w0 f
// executed in a specific order, but these steps should
; ?7 S" ^6 F: K. E3 Z // take no (simulated) time. The M(foo) means "The message1 c2 I+ A: r! k7 h( ?- `0 I
// called <foo>". You can send a message To a particular
; N8 m! y [! l+ ]7 V // object, or ForEach object in a collection." M# T5 ]4 o+ r" ^) G1 K
7 I, E! ~2 _/ |1 B // Note we update the heatspace in two phases: first run
. j' x! g# D0 L3 X" t5 N // diffusion, then run "updateWorld" to actually enact the
& S6 r5 E. e6 q8 A // changes the heatbugs have made. The ordering here is
, H! ^% M& h+ f( n" \/ |6 w2 A // significant!
- r! k7 Z9 `' F+ [ 4 ^! W E+ p* k# D9 f
// Note also, that with the additional: j* Q" g' G$ O O( O: i
// `randomizeHeatbugUpdateOrder' Boolean flag we can
+ u1 r, w8 V. b // randomize the order in which the bugs actually run5 {' R- Q9 u* t7 X( i/ M
// their step rule. This has the effect of removing any/ v; ?" T! a+ t# e2 y
// systematic bias in the iteration throught the heatbug2 ]: z( L; O0 h- f! x" j% }
// list from timestep to timestep
+ s8 F4 G) Y8 {. _; d5 w: c : q3 D, M; B1 h/ `. @
// By default, all `createActionForEach' modelActions have! V- n. I7 Q* v9 n! k/ R
// a default order of `Sequential', which means that the5 `8 X; v2 o: n5 l
// order of iteration through the `heatbugList' will be. P) X7 t5 ?3 K2 w
// identical (assuming the list order is not changed
8 H1 q' S* A: P // indirectly by some other process).
; }2 d% ?. D5 j8 R 9 C1 [! \. ~! S3 B8 G; A
modelActions = new ActionGroupImpl (getZone ());
3 ^% R( h) j0 \+ x. c5 M* h
2 I: N+ s# h3 W/ C try {3 V$ ^: ]2 Z( h: E- B
modelActions.createActionTo$message; Z7 y% _1 l) b; R
(heat, new Selector (heat.getClass (), "stepRule", false));0 w6 B, ^* F! k& H9 z3 I9 v8 ?
} catch (Exception e) {' {& N8 O# p6 [: g3 y
System.err.println ("Exception stepRule: " + e.getMessage ());$ b+ f2 W8 l/ s/ K' R4 r
}$ ?/ }" ~. ^# S/ I: f, a& n" q c" ]
; B! e3 ^& }4 b6 D
try {
2 b7 E6 z; X, L) O3 c% `& o1 P Heatbug proto = (Heatbug) heatbugList.get (0);5 D) l) l8 T0 {, `
Selector sel =
- F7 w! w; z$ T1 O$ L' m# m0 f new Selector (proto.getClass (), "heatbugStep", false);9 B# S3 V) S @. O; h+ a
actionForEach = ?0 h' d, v# o: G: Y5 J7 V
modelActions.createFActionForEachHomogeneous$call8 ]$ h% J6 H, S" S t
(heatbugList,
- M- K0 M4 Q* S& e0 h4 O* ~" i new FCallImpl (this, proto, sel,: t! k" Y7 N3 K. V- z* N* f
new FArgumentsImpl (this, sel)));
" ]) z$ i ^5 L } catch (Exception e) {
% r J! O- K' s7 w+ r8 X e.printStackTrace (System.err);
7 [0 d5 r9 s& t" Z/ n5 D }
9 v2 x. a0 t! l8 D
5 `$ \: n' Z1 H4 A. G( N syncUpdateOrder ();+ }& A4 M! g( l; ~# M: s
- q( L& K+ c8 _# Z9 N try {
1 e. }# j, w( z8 y. P9 q modelActions.createActionTo$message
" v5 s# M# N; G (heat, new Selector (heat.getClass (), "updateLattice", false));2 H* H6 O- K% K5 t4 i. D4 y
} catch (Exception e) {
. a+ j5 A+ ?: {- \8 f$ R System.err.println("Exception updateLattice: " + e.getMessage ());
4 G) N0 W" M2 Z# h1 @ }
S8 J# n U- k5 F! l! p
& J2 D- V* t) G7 m // Then we create a schedule that executes the, ]& F; ~ B0 J) I, T: [
// modelActions. modelActions is an ActionGroup, by itself it
7 ]+ k" [3 J# T5 @ // has no notion of time. In order to have it executed in" I8 ]0 P! Z" l- f- C. G
// time, we create a Schedule that says to use the) G' [5 J$ }2 T2 ?# t! _
// modelActions ActionGroup at particular times. This
* W# a% s2 E- O7 s- w( y // schedule has a repeat interval of 1, it will loop every
% \- ]7 |9 L+ F8 ^% j; N+ [% M! E2 | // time step. The action is executed at time 0 relative to
. }3 f1 Z f% |6 @, T( ~ // the beginning of the loop.
' U- a. p5 W# K5 y* p6 s
4 } P8 ], o, K2 |2 s // This is a simple schedule, with only one action that is
0 S; o1 @& M1 {2 O/ ^ // just repeated every time. See jmousetrap for more3 c0 I5 o+ k6 b u/ d
// complicated schedules.
" e. c, s* G, g8 Y8 `% Y
: w3 H: C! v9 u6 y2 p modelSchedule = new ScheduleImpl (getZone (), 1);
9 h+ _. D* q, O! p0 s5 y/ T modelSchedule.at$createAction (0, modelActions);
2 ]2 g1 m- Y6 z' J! R
7 W4 g+ z& o2 ?( H2 q* C$ e/ q return this;/ P/ G* \' E. u# P
} |