HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 y f* o1 n& @7 Z; f M3 v' r9 ]! H
+ x; g/ D, B2 E$ J
public Object buildActions () {
- ?" w- b. X5 M+ @) {6 c super.buildActions();
. g8 m( t4 V1 O% U) Z a/ A+ R# s0 W ^$ A1 e2 Q8 c
// Create the list of simulation actions. We put these in2 U* v* ^& C- A+ C' J3 q- f. }
// an action group, because we want these actions to be" E& U9 l1 `7 U3 R5 z: F( }
// executed in a specific order, but these steps should+ ~0 ?& m3 w- W: l/ c2 M; z
// take no (simulated) time. The M(foo) means "The message
8 W* Q3 }6 x+ H6 x7 s // called <foo>". You can send a message To a particular$ X! P9 ]+ S& S; [' W: j6 U
// object, or ForEach object in a collection.
3 e( u& k' z+ X5 B2 { " [" q0 A% _4 ~! A- w
// Note we update the heatspace in two phases: first run
4 n# L( m$ Y$ q1 y4 e // diffusion, then run "updateWorld" to actually enact the4 d4 \) b) O4 ~; y6 \0 J* [
// changes the heatbugs have made. The ordering here is
; R+ [. j) C; B0 I. p- g! n( \4 K // significant!
" x1 @' E9 n( A- s
# N& f3 N3 b( r" @# D" H/ S // Note also, that with the additional2 T) y) G8 @. M4 m2 v, A
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ n- D) d; x( I
// randomize the order in which the bugs actually run, H: ]. F( D% h0 F1 N5 T1 Y- n" j
// their step rule. This has the effect of removing any
5 _2 a/ q4 B- D$ r7 Q7 Q // systematic bias in the iteration throught the heatbug% e4 k7 _6 h$ j& ^' C' i8 W [7 `
// list from timestep to timestep
3 z5 V' ^3 v1 m$ u1 ` b $ g1 K3 L/ A; y. ?5 h: F2 L
// By default, all `createActionForEach' modelActions have
4 g% L) O* w( T# ]5 l // a default order of `Sequential', which means that the" w/ z+ z( E4 a! ^ g ]- T
// order of iteration through the `heatbugList' will be0 u4 a9 ?& ?2 i$ v6 L0 m# }$ G* A
// identical (assuming the list order is not changed' F$ T: U K0 [& G# `
// indirectly by some other process).( w D3 z. I3 Y$ {6 q' x% g k, L
( n8 n/ Q1 H% x) Z) g; j
modelActions = new ActionGroupImpl (getZone ());
; O u8 B+ S2 ], W+ i9 s9 y9 }6 G( ]& u9 v# o5 A
try {
/ c) E" B6 C& s' ]* Y4 `6 ^) V8 ]1 C modelActions.createActionTo$message
1 b6 o/ z; Z. @; }2 u1 D1 ] (heat, new Selector (heat.getClass (), "stepRule", false));/ }. G, ^0 [5 @4 i
} catch (Exception e) {! G3 j1 R8 G( e/ C
System.err.println ("Exception stepRule: " + e.getMessage ());
* G1 X3 B7 e$ s- D' p }( t i! |2 ^1 g& o/ c
1 {7 B" w1 I8 m" \; E try {" q6 ] J! X+ h' ~6 X0 g& p f! X
Heatbug proto = (Heatbug) heatbugList.get (0);" P `# r' |8 a6 s
Selector sel = 5 D/ M1 {" h$ v$ x% j9 s: L8 u, K
new Selector (proto.getClass (), "heatbugStep", false);
& x A' s% M: Z8 Z% ` actionForEach =- ?, M9 Y6 |9 D8 s+ {
modelActions.createFActionForEachHomogeneous$call: R) r& h: b5 \0 y# ?
(heatbugList,
' A: p v- q, v- T, \8 V$ T new FCallImpl (this, proto, sel, X0 n# ^; Q2 E
new FArgumentsImpl (this, sel)));' k; w- b/ n. @. O7 i; b
} catch (Exception e) {
) G* b6 X |- u1 W e.printStackTrace (System.err);
9 G: d) X8 H) ^; c! G }
5 v# G: e0 u3 }2 @. g
% Q: M8 T6 p% h/ c: t syncUpdateOrder ();
) [4 Y; L# | u W* p1 E# N
6 s# a4 K9 v) \" C try {
6 x! f( z/ ~* \+ k modelActions.createActionTo$message 9 g) W) i9 `3 P* r, A
(heat, new Selector (heat.getClass (), "updateLattice", false));
; E% k) G* O$ C) Q* e$ [ } catch (Exception e) { O' V8 \3 o) Z
System.err.println("Exception updateLattice: " + e.getMessage ());; Q/ @( r) z8 a8 k; S- g) A
}5 t$ L- y5 u T3 _4 o; W
% x# K" u S) r R/ W
// Then we create a schedule that executes the8 G) N: F3 S) ~# y/ @0 l5 S$ g6 x
// modelActions. modelActions is an ActionGroup, by itself it
$ D X% |( d+ J" O0 D. d/ @ // has no notion of time. In order to have it executed in2 d6 o5 u1 n3 U$ H( r
// time, we create a Schedule that says to use the. H$ o2 [! D8 v( E A/ l# d- q2 `
// modelActions ActionGroup at particular times. This4 D9 R' z5 u3 j: A. B* q$ f
// schedule has a repeat interval of 1, it will loop every
; R( L6 ]$ c0 K. s- T+ w. \$ \6 S // time step. The action is executed at time 0 relative to8 J* H0 ~, }$ W/ m- N5 J
// the beginning of the loop.2 q# x# j+ D7 t" c& T) z
* o8 ^7 t* \7 i% ~ // This is a simple schedule, with only one action that is5 U- O( j2 w1 h0 c+ A+ i
// just repeated every time. See jmousetrap for more
0 W- |8 U# i( ] // complicated schedules.# _& }6 y/ ?9 [3 P' l4 ^8 c
4 s# r" n3 c4 B. T8 g9 f' h
modelSchedule = new ScheduleImpl (getZone (), 1);! ~. n3 \$ t" [' K
modelSchedule.at$createAction (0, modelActions);0 {( l; ^; L6 j
2 x+ ]: {7 X; B4 t( c return this;' K# u5 ?. {# ?( K9 Y) D$ R
} |