HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ _5 L. d; E) `; p3 `7 m8 j( i/ w( F1 m& v
public Object buildActions () {
4 q8 d, F! a6 n$ r9 l4 C- `2 ~ super.buildActions();
- s8 a. _1 f; n& }& @) Z8 o
4 }; w2 B" ] p' }9 h. l9 \0 r // Create the list of simulation actions. We put these in; F% ]5 E: A6 I3 S+ f$ j7 T
// an action group, because we want these actions to be
9 U. O2 G; P# R // executed in a specific order, but these steps should
! g2 C$ P& G, I) ~" a+ Z; t // take no (simulated) time. The M(foo) means "The message
' s- d+ L2 x6 N: g* k1 R" J3 v // called <foo>". You can send a message To a particular
# m8 M: K& ]( F // object, or ForEach object in a collection.
! {7 B9 F& @3 N4 T
3 W6 B& o1 a+ S# k8 N/ Z0 }6 j // Note we update the heatspace in two phases: first run+ |# q5 b4 l, H( X" a! E& w1 t
// diffusion, then run "updateWorld" to actually enact the
2 T8 F" a, g2 |* y // changes the heatbugs have made. The ordering here is& d: u) H% G) U4 Z# H$ T9 Y# Y& F+ b
// significant!, N% V; X( P2 i9 j
% l Y; g* e! ~" ?2 [4 Y // Note also, that with the additional# j: |! K$ t# M( O3 D$ \5 Q3 y
// `randomizeHeatbugUpdateOrder' Boolean flag we can# h8 e0 a6 `3 C9 |* t8 z( E: {. M
// randomize the order in which the bugs actually run
# A( F( M5 ]& T5 U& H- \ // their step rule. This has the effect of removing any
5 d" I4 B* D ~6 ~3 ]+ r // systematic bias in the iteration throught the heatbug
/ \' g6 p' i2 W) Y // list from timestep to timestep
# }$ m7 d, M9 m. g; k9 A
) b- k' d! b& n) d // By default, all `createActionForEach' modelActions have Z, S" v7 l& K9 b
// a default order of `Sequential', which means that the2 V1 R# J7 f6 c! K# }
// order of iteration through the `heatbugList' will be
* t# P* L$ T7 ^- j: O B' z' z. J // identical (assuming the list order is not changed
a: M' S7 ]: s+ H& K // indirectly by some other process).
5 d" r) D8 R( v* w, J - A0 D% P s! a& q! ?- _* e, [: P
modelActions = new ActionGroupImpl (getZone ());
! x- i* e- n% [) @
5 {( u |( F' ~$ }2 Q6 J8 ^/ L try {
# I. f& d A8 Y. A! g modelActions.createActionTo$message
4 y" O* Y+ e" Q$ N (heat, new Selector (heat.getClass (), "stepRule", false));9 c0 x1 f' N% i& {9 a. G! ^
} catch (Exception e) {
/ M8 w+ Z. |7 M1 ~4 ~7 B System.err.println ("Exception stepRule: " + e.getMessage ());
' q7 {% h) k* J6 \" M5 b, q }
+ @- `+ B( N8 ~6 n& N" T
" J4 e6 K+ e T0 R try {
" ~6 u0 h$ k/ K, e* ]1 K" F Heatbug proto = (Heatbug) heatbugList.get (0);; A( T5 q7 |( G! I
Selector sel = ( Z# Y( |' m$ \* [7 l
new Selector (proto.getClass (), "heatbugStep", false);) Z2 _* U5 ^+ O
actionForEach =
* X- O4 z2 O+ E N2 i( {0 s modelActions.createFActionForEachHomogeneous$call
2 r$ `) M9 L1 i' n0 H (heatbugList,) h) O# \% `( A c' M
new FCallImpl (this, proto, sel,
- V, c4 }) J0 h% T new FArgumentsImpl (this, sel)));
4 J- A5 F/ ]/ @2 r# T7 |* l& Q, y } catch (Exception e) {& U& Q3 |1 }. e# l- G
e.printStackTrace (System.err);
1 h5 ?1 l+ V3 Z# g; S5 ~# o }% J3 z! k; g, w/ u
" i9 U6 Z0 C7 O# `* b/ F& T; Q( }, f
syncUpdateOrder ();8 G' {% `2 P) R! {( \
, p, H+ o9 j, R. D+ F1 \ try {0 T' Q: N5 M/ x2 G: ~& O& u
modelActions.createActionTo$message
4 l0 L# h) P. [ e (heat, new Selector (heat.getClass (), "updateLattice", false));
$ F) s: b, v4 k, q% K" c } catch (Exception e) {- r) [* p- ~' i& w
System.err.println("Exception updateLattice: " + e.getMessage ());
; s3 f4 S q! x/ N# F9 H' Q }2 h6 l) g, J( h8 \# I% l; d
+ x" O: Z0 z5 L! ]2 Q // Then we create a schedule that executes the
& l1 T% I/ l& w // modelActions. modelActions is an ActionGroup, by itself it
. Y: {7 @, ~ G! o, I6 t7 \ // has no notion of time. In order to have it executed in
4 B3 j D8 M- ^% g$ k3 [2 a // time, we create a Schedule that says to use the" n' \5 O4 e5 e4 Q4 C
// modelActions ActionGroup at particular times. This4 A3 |) [7 |9 ?' P1 l0 _/ C
// schedule has a repeat interval of 1, it will loop every
$ p+ G3 a+ B8 I& o+ p // time step. The action is executed at time 0 relative to4 R& S+ G$ U( p- n
// the beginning of the loop.' C0 O7 p9 o( N. D
9 w; R* ~' {) {2 Z" Y% a: ?
// This is a simple schedule, with only one action that is6 Y3 b) \, u1 u. X7 G
// just repeated every time. See jmousetrap for more: S9 ^5 Y* D4 k$ S7 r
// complicated schedules.
9 ~% _1 Z2 ^9 f& w2 u( H
) T4 Y7 h# j! m$ b, [+ P2 r/ ~ modelSchedule = new ScheduleImpl (getZone (), 1); v. e& L. p% t+ A' L: V2 e
modelSchedule.at$createAction (0, modelActions);
! o ? U3 B; W
6 p8 w& \5 G1 V1 g return this;
/ N0 O+ C6 f, @8 u$ p. L( H, }6 ^ } |