HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' `- V' Y1 Y- {
* P0 |/ J$ ?$ b% v; v: G
public Object buildActions () {
, A2 I, X, h0 _) \8 Y super.buildActions();0 x, e9 n" `# j
! B. G; T$ a0 O T // Create the list of simulation actions. We put these in
- l% S) F7 [" E; F: X // an action group, because we want these actions to be0 ]1 \( R" z' y
// executed in a specific order, but these steps should# M5 [8 q' l2 p0 v1 Q' A. `+ p% Q# O$ |
// take no (simulated) time. The M(foo) means "The message }3 V9 |3 R+ Y& |3 O B9 N
// called <foo>". You can send a message To a particular
( n _) E! I% J // object, or ForEach object in a collection.# d; j, ~' Y5 g9 s, J9 M8 \8 ^$ S
" x ?7 b- {4 j4 g7 G // Note we update the heatspace in two phases: first run8 [2 y3 O' g4 i. }: T
// diffusion, then run "updateWorld" to actually enact the% b" |2 x+ z! k/ z
// changes the heatbugs have made. The ordering here is/ d( h. D0 s- j
// significant!
# M6 [8 ~/ K! R& n0 c4 O+ C
6 r4 s+ {7 T8 W: A // Note also, that with the additional) A4 @3 S7 i- ]7 r+ _- U: _* I5 d4 a
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ m" U8 p! ?3 C& \ // randomize the order in which the bugs actually run6 _* W3 `8 @2 `4 ?) N4 s
// their step rule. This has the effect of removing any7 g" ?& t5 B+ |, ?
// systematic bias in the iteration throught the heatbug1 t8 c* F' e0 l1 M' d* b2 k* i. O# K
// list from timestep to timestep [2 L D& a& T7 z1 F" w8 Z% b; I
4 t- }+ U9 g; W1 R+ J // By default, all `createActionForEach' modelActions have
- L7 o# }8 |4 v // a default order of `Sequential', which means that the+ [. X! L; J: Z4 X
// order of iteration through the `heatbugList' will be
& [+ a+ F( W+ `; I! p1 v; N // identical (assuming the list order is not changed0 k* y# g- ^. C
// indirectly by some other process).
( d& S8 w+ u) t( | ( Y. U. s, ?* [; O
modelActions = new ActionGroupImpl (getZone ());
l7 Z0 u- R# V; L, g
@. H `: j6 w! D1 M+ s o1 k try {6 G. C4 ]$ ?9 H: Y9 _) j! X" d0 l! {
modelActions.createActionTo$message4 t! C: H/ e0 s1 K
(heat, new Selector (heat.getClass (), "stepRule", false));' w; w+ v2 m2 L3 ]1 s
} catch (Exception e) {4 j, g. U8 [- f9 D1 a
System.err.println ("Exception stepRule: " + e.getMessage ());$ h" T E8 {( G( z
}
/ H5 S( h) \$ ?( o7 B+ ~; y+ ?( O/ z! K
try {
4 e/ i, D! j& s) K Heatbug proto = (Heatbug) heatbugList.get (0);" L5 u1 K+ w' L/ t7 G; g0 m" }
Selector sel = 9 m. o" P- Q' z: [7 k
new Selector (proto.getClass (), "heatbugStep", false);9 E q$ U- i. K: m3 {* {" |1 E" r
actionForEach =
$ F' Q R |% d$ w! |- s$ s* I modelActions.createFActionForEachHomogeneous$call
8 z) v9 ]! C+ k4 R (heatbugList,, z( l' g2 c' o% R4 H, G5 b0 i
new FCallImpl (this, proto, sel,( R7 j Y! p8 e( k" @
new FArgumentsImpl (this, sel)));: S2 v+ k6 U4 i5 Q: X+ A" g6 r
} catch (Exception e) {
6 a1 C j v2 o: ?0 _5 j e.printStackTrace (System.err);/ t7 K, _& l: l ]/ o+ j
}
: H9 J: v: m' d& h& t/ b1 N: q ; V. J/ P; O# h
syncUpdateOrder ();/ v9 i" D% D* r: D
2 \6 {$ X5 {- P% T2 d' Y
try {$ G( N3 U/ u# E: Q" q6 v
modelActions.createActionTo$message
7 X" d- s: P, E, z) d2 E (heat, new Selector (heat.getClass (), "updateLattice", false));
7 m- \6 o% z- C# u) H } catch (Exception e) {
. e) o% g7 m/ d: {6 J4 B( f3 Q System.err.println("Exception updateLattice: " + e.getMessage ());8 b5 a$ Y9 H3 m2 [3 L' @( w
}
( Q9 N8 L! J; ?0 ?. o( V ' {0 Q. I( a8 r
// Then we create a schedule that executes the( |" O& v! D/ S# s0 Z
// modelActions. modelActions is an ActionGroup, by itself it4 S- ?6 S; I+ y% e; w
// has no notion of time. In order to have it executed in
" B0 U. e- T* Q c2 _ C6 x // time, we create a Schedule that says to use the
) z$ v& j8 T& B0 _* F // modelActions ActionGroup at particular times. This
. u, Q+ [* w" P // schedule has a repeat interval of 1, it will loop every8 E+ o" ?: ]0 _/ i6 Q, N6 D+ T, |
// time step. The action is executed at time 0 relative to
) s$ E+ g) D( j; O8 G // the beginning of the loop., ~( [3 W4 O% D
9 O# {. I5 n5 v7 y% u
// This is a simple schedule, with only one action that is
2 y7 T0 Q! X: `# E- L. Q4 } // just repeated every time. See jmousetrap for more
8 V t8 q! Y4 { // complicated schedules.5 C1 a- S* w' u: r
; i* ]0 P( J" h5 [ modelSchedule = new ScheduleImpl (getZone (), 1);! H b, L! d4 y; ]
modelSchedule.at$createAction (0, modelActions);. i: c( C I" z) x- R
8 u' Q" @0 D8 |+ f return this;
# T% H1 X2 |6 Y8 O7 O6 L4 z5 V) ^5 ] } |