HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% u) _1 X) ~- \
' @+ ? b* U6 `2 c& q7 E* S9 k public Object buildActions () {& v8 f1 b6 |# l: h' s# V
super.buildActions();
* i# k2 b! q W' S# A) [ ) f" g2 F, M! F6 U5 v$ w8 u
// Create the list of simulation actions. We put these in
& ]+ N. q6 g4 h5 `; i // an action group, because we want these actions to be7 Z4 t8 t& c h- m& i. J" E
// executed in a specific order, but these steps should/ x. v' |0 S2 c6 ~/ Z
// take no (simulated) time. The M(foo) means "The message; M' }3 ?* T. ]) o# j9 D
// called <foo>". You can send a message To a particular
_. \# L" h( p9 F6 m // object, or ForEach object in a collection.0 y$ b" h; O U6 H" k
r$ q2 H$ p4 k# u( A // Note we update the heatspace in two phases: first run6 P, K$ H5 ]' C" ~ `
// diffusion, then run "updateWorld" to actually enact the; z: f: D- n% z( b0 m8 a
// changes the heatbugs have made. The ordering here is
/ d0 u: u, J/ N8 }- ?$ N+ \ // significant!1 t: ^, ^* W& b
- D/ I3 U/ A1 i2 p
// Note also, that with the additional, C5 B, y8 X3 ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can. r/ c; M- U0 u& X. b$ U7 `8 R2 \
// randomize the order in which the bugs actually run
* Z, Y/ a; _& ?$ A0 P1 z // their step rule. This has the effect of removing any
1 S+ ^* g) w- L3 c+ | // systematic bias in the iteration throught the heatbug
- K0 q" X% q; w/ z // list from timestep to timestep, j& x) k( b. ]
! w0 P. T3 W2 R2 ~
// By default, all `createActionForEach' modelActions have% J! w8 Z$ J, Q. d% E
// a default order of `Sequential', which means that the
5 D% }7 n( E6 v- U6 ` C // order of iteration through the `heatbugList' will be9 ~" Y& P) H: ~3 G2 z- k
// identical (assuming the list order is not changed
: O0 K# z; V3 _1 Z1 V // indirectly by some other process).
4 y' Z3 k4 Y6 Z0 W5 T & ?- H! {# O9 ]. {* y* Z/ [! i Z
modelActions = new ActionGroupImpl (getZone ());
* i2 j4 J9 B6 h/ R" y: L
+ t& s, \/ {: ^# S try {
9 {5 U8 T, f O# K+ l8 J6 A modelActions.createActionTo$message
0 {. M) Q5 a* ^/ s# Q0 c0 s (heat, new Selector (heat.getClass (), "stepRule", false));
4 G7 M: l+ V) q } catch (Exception e) {1 r- X5 }8 D+ Y9 i/ u" c
System.err.println ("Exception stepRule: " + e.getMessage ());% Y9 ?( T5 v. e- o
}
; L3 Z9 j3 {3 B0 P7 |. K) s% d& R
* _' ^8 z X% \( S0 L2 J try {
. h! o$ b' K' a2 J' f! h Heatbug proto = (Heatbug) heatbugList.get (0);! a+ z8 Z! k4 d- b2 p1 `
Selector sel =
+ H2 q$ a4 M: S1 {( V new Selector (proto.getClass (), "heatbugStep", false);; u& U- C5 X, s% \
actionForEach =% o* d0 B: B3 L; S0 V( E! G
modelActions.createFActionForEachHomogeneous$call0 L0 R) V* r1 S6 L
(heatbugList,5 q2 ?$ X$ V3 |; n$ w6 ^% ]: R
new FCallImpl (this, proto, sel,/ S3 j, h$ O" b3 H$ h2 s/ G$ p8 W8 C
new FArgumentsImpl (this, sel)));# k% V4 w* @3 z i1 c; E
} catch (Exception e) {+ W4 j C2 a2 L9 ]+ x3 y
e.printStackTrace (System.err);
4 x+ W4 g9 [' J7 M# v) c }
4 t6 d1 n" K y8 b! g0 D- G
! \! m. i- ^; n; Q/ u d+ s syncUpdateOrder ();
. }5 `/ |2 U: w' {$ F
9 { S& V9 W; V0 K% K. b try {
8 \) w, R+ w7 f/ x. [ modelActions.createActionTo$message
: `4 g9 i3 ^9 t$ _% m) y. @+ ? (heat, new Selector (heat.getClass (), "updateLattice", false));
& k8 w. ]$ ^# L: D, }, ?0 O } catch (Exception e) {
5 t A$ w7 E7 n. n+ w7 s System.err.println("Exception updateLattice: " + e.getMessage ());% d0 k6 U: r3 i! y9 t4 |
}
. f4 J. D/ W. S0 `8 u% _, K2 o
7 ?- U8 u# W9 W // Then we create a schedule that executes the: ]0 w& x7 H$ T7 P0 m6 T% c
// modelActions. modelActions is an ActionGroup, by itself it9 k: T0 Y0 F1 T/ }+ k1 {
// has no notion of time. In order to have it executed in
* O% c7 N+ N( ? // time, we create a Schedule that says to use the
$ ^: M# }8 x" p; D! j! |3 q // modelActions ActionGroup at particular times. This
3 i- U( j& ^. D // schedule has a repeat interval of 1, it will loop every; f& G G5 S1 j% S( R# Z d
// time step. The action is executed at time 0 relative to: k4 c3 r) v$ Q4 G2 U! E$ `" w
// the beginning of the loop.
: y6 e% i; ~& A% I8 {* U2 Q' k7 Z
: w( C- M; {/ C0 f4 } // This is a simple schedule, with only one action that is
0 i7 \5 U/ e* P. X: b( Z // just repeated every time. See jmousetrap for more
9 q" b- v P8 ?9 ` // complicated schedules.: x9 ]- u! X3 P: f% j- f
. B: D4 L: \ Z- @: ~ modelSchedule = new ScheduleImpl (getZone (), 1);/ {$ i; u$ v; H
modelSchedule.at$createAction (0, modelActions);8 g% O) A& Z; ]. q2 }; q! M' O
9 v |+ q/ b' S! ~0 `* U( ]) t
return this; ?9 d l r# i+ Y( T, d% G$ d
} |