HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* f [- D8 _, |5 M' [( z! q: {4 K
: t1 M& h6 e8 I4 {! m public Object buildActions () {
& w/ W' [/ f e, M1 y super.buildActions();
" v: [' i+ U9 P3 H( C# U ! ^6 Q- d% x. O. n/ @0 a- m8 c/ u
// Create the list of simulation actions. We put these in
) t+ n5 M/ h7 O- u, b: n: Q7 m! a // an action group, because we want these actions to be
' x; E3 O/ j" S+ p9 F- @ // executed in a specific order, but these steps should' Z0 R+ ]& a" A' V8 V
// take no (simulated) time. The M(foo) means "The message0 l. a6 N0 z% E* u
// called <foo>". You can send a message To a particular- `3 m: ^* k7 ?: V" E& ~2 R
// object, or ForEach object in a collection.
/ j; {/ ]6 f) P0 l3 h1 ^/ Z2 X+ q 1 O/ V* K' Q' z
// Note we update the heatspace in two phases: first run! \6 j3 N0 n3 A- j6 Z7 g" h
// diffusion, then run "updateWorld" to actually enact the7 {0 Z) J8 d" R& i) o
// changes the heatbugs have made. The ordering here is) }9 s1 Z5 H# K& L4 K& o, g- E
// significant!
' i7 B. X4 i* k- c. F& X$ w
6 a* X4 S) H. q0 X9 n% I- F // Note also, that with the additional; N4 ^' R) a$ k' i$ f5 s
// `randomizeHeatbugUpdateOrder' Boolean flag we can& G. V# y6 {* C4 `6 O9 A4 T
// randomize the order in which the bugs actually run
2 I/ [4 X0 D- L1 G // their step rule. This has the effect of removing any: L' @! g2 n7 i6 ?
// systematic bias in the iteration throught the heatbug, X7 M9 g; W0 h/ S A% b2 B6 J7 @
// list from timestep to timestep; X0 I4 l" A" M
& h% D9 }0 }4 g
// By default, all `createActionForEach' modelActions have
. H- q* T- c9 \# L. `9 w) e- S // a default order of `Sequential', which means that the
1 i: c2 [* _- x8 @ // order of iteration through the `heatbugList' will be# Q. P; a! b1 s; f
// identical (assuming the list order is not changed
( N0 C0 h7 D- A2 {' T // indirectly by some other process).( v; |& |' d6 R: C1 o( i, T F: g; w
% ^) h1 i9 b, {3 O* \2 o% O, U9 Z modelActions = new ActionGroupImpl (getZone ());* l' E% v7 x# W" k. f) Z
4 c3 c! b# |9 S
try {" Y- |6 ~! M4 ?' W' \% l2 x
modelActions.createActionTo$message
: n+ m7 {- q3 u (heat, new Selector (heat.getClass (), "stepRule", false));1 O" W# G# U7 y4 @0 o1 A- x g9 Z( G
} catch (Exception e) {2 Q4 |' {& W# t2 _
System.err.println ("Exception stepRule: " + e.getMessage ());
1 c3 _/ \2 d1 c" x8 I# Q4 X+ A }' F3 W+ V# a, s6 K" i, M
4 ?, _; o8 b' h try {% k. }5 @# j3 u4 }/ m ]
Heatbug proto = (Heatbug) heatbugList.get (0);( }& E. ^7 e! z
Selector sel = . a- ?0 A: b( _
new Selector (proto.getClass (), "heatbugStep", false);- U, k0 y( K5 L+ u; `
actionForEach =7 t" Z$ |& j' W2 I2 G/ A- [
modelActions.createFActionForEachHomogeneous$call
, J, D& V( x% }2 X# q (heatbugList,; L; C! f5 k! Q2 ^# ]9 `% N6 R
new FCallImpl (this, proto, sel,
1 j+ k% O0 U4 T) l& } new FArgumentsImpl (this, sel)));
9 R& i! t( ^* ^, [ b } catch (Exception e) {- n2 s" T( m* R$ n$ [) \1 L7 f
e.printStackTrace (System.err);
) E4 ]( ?2 T$ Z; _ }$ Q: h# {3 @% e9 ^, a
& r' l& {0 B! a6 C% l" M
syncUpdateOrder ();- v0 D$ _6 _, r- f$ I; x
h1 A/ ~, W# S+ B try {
& Y( q8 }6 L( m" t' h o modelActions.createActionTo$message
A1 L! v, _; x7 Y/ @. W8 G; W (heat, new Selector (heat.getClass (), "updateLattice", false));
+ {# ^* c E" e/ X$ F$ H } catch (Exception e) {
) m `3 }" B$ U" S+ N/ X% y6 [ System.err.println("Exception updateLattice: " + e.getMessage ());
6 g# j" J( R- e8 I! J: ` }, H0 D7 s: i9 _9 s/ p
3 O$ i4 W+ ?; G7 T // Then we create a schedule that executes the
- `9 Q& K7 H" h# F# I // modelActions. modelActions is an ActionGroup, by itself it
" P; [$ i0 B, t) s // has no notion of time. In order to have it executed in4 H3 j/ ^% ]$ m7 ]
// time, we create a Schedule that says to use the
+ c$ T7 D1 q7 G8 r // modelActions ActionGroup at particular times. This
! ?2 Q3 Z' h/ V: o) C) T // schedule has a repeat interval of 1, it will loop every
1 z: q* T3 a3 y // time step. The action is executed at time 0 relative to
- C6 o) n3 T h0 P // the beginning of the loop.
# A* G( H; P7 f" h! w8 S* z; P) P8 y& l; f- x/ ~, a3 ~
// This is a simple schedule, with only one action that is8 b& D8 B5 P* |( i6 A- Q5 w7 Q
// just repeated every time. See jmousetrap for more8 o: e' c4 w$ p& Q+ L
// complicated schedules.
1 c0 ]9 x; i7 @& J" ^
8 ^: q: U2 W) I3 e! i$ ]+ z, h modelSchedule = new ScheduleImpl (getZone (), 1);
2 _0 R4 k8 a! @- E' u+ m modelSchedule.at$createAction (0, modelActions);
: o! c4 |7 E+ f+ k& Z8 y
. X; w. X8 i7 S' L' M& a return this;6 U$ v% m; w( |
} |