HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& ]6 \8 a5 O- e5 X
1 r. i' ~! q' M3 x( p
public Object buildActions () {
( `, Z) V" k% Q super.buildActions(); P9 d+ @" t9 Q4 u8 [& a
7 ?# d3 ~0 @/ v$ n3 a // Create the list of simulation actions. We put these in1 c0 V6 r% K% o8 I( B
// an action group, because we want these actions to be
& J. d1 Y( u0 f, a. A6 v // executed in a specific order, but these steps should1 _6 q# z6 ?5 S- [
// take no (simulated) time. The M(foo) means "The message
1 S# Y4 S+ c% o- m' I4 B // called <foo>". You can send a message To a particular
. |2 e$ v3 ^' [6 `$ l9 e1 e0 y // object, or ForEach object in a collection.
, ?1 W5 C' l& N$ U8 S2 x* b
; f3 |: k( q; K. a( b! l // Note we update the heatspace in two phases: first run
+ k& z$ u- j7 a8 G // diffusion, then run "updateWorld" to actually enact the) I9 h# \2 M( b5 z7 u! E
// changes the heatbugs have made. The ordering here is
& Z' m9 J/ E @$ ?" Q6 v4 L* G" J // significant!7 y. K1 y3 L, e! n% T: Z# W
. e5 o3 I) \: ]5 R" M* \8 @5 A" X
// Note also, that with the additional
! s/ p! K/ E/ z2 u // `randomizeHeatbugUpdateOrder' Boolean flag we can
, `) V2 `: F+ { // randomize the order in which the bugs actually run
3 l3 I+ G- k2 p, p/ X // their step rule. This has the effect of removing any5 Q7 U+ y) ~! b4 `
// systematic bias in the iteration throught the heatbug9 Y$ S7 ~0 G* z. {, @
// list from timestep to timestep2 z7 r5 X: u; ]4 E9 {
3 U8 x; M4 H# P& A Q
// By default, all `createActionForEach' modelActions have* ?2 ~" p, o( L( o0 H* c
// a default order of `Sequential', which means that the1 i+ I2 ]: h. k
// order of iteration through the `heatbugList' will be
@6 ~7 x9 B: H+ s, r, y9 J0 G; N // identical (assuming the list order is not changed
2 j9 L" l# s% c! M3 L% R. @ // indirectly by some other process).- q8 g7 J1 v( h! X# F) d) ?6 S
8 g6 Q1 }" } v4 s. c Y5 k7 _& G modelActions = new ActionGroupImpl (getZone ());( ^. h( Z$ a! I- H
. M7 \& ]8 T1 C4 w) A
try { |9 S8 c9 R1 n2 i' R- m9 A* Y
modelActions.createActionTo$message
: a9 M+ q {8 X0 { (heat, new Selector (heat.getClass (), "stepRule", false));9 R, ?$ Y$ J/ R2 K# d; l
} catch (Exception e) {- f8 g& [! ?+ H( ?2 _1 q9 B8 _7 E! Z
System.err.println ("Exception stepRule: " + e.getMessage ());
. l6 n/ `5 j ~, z }% p% }; o' b; ]6 ^1 }: N. f
/ p4 Z' B- C U" i \: Q# d9 r# l( Z try {
& C, y. U! U, s9 g! Q Heatbug proto = (Heatbug) heatbugList.get (0);
$ C* P4 P8 E( D% ? Selector sel =
0 T9 ?) _: K) h& c( x8 L new Selector (proto.getClass (), "heatbugStep", false);
# Q' H! }+ e3 {" q" z) ~ actionForEach =
, Y9 U% K- G: K& P0 M r: ]4 d modelActions.createFActionForEachHomogeneous$call) D: e5 Z: i$ {3 x& |6 I
(heatbugList,
4 l% O/ w! S* s new FCallImpl (this, proto, sel,. U4 ~4 ~" Q7 S! j
new FArgumentsImpl (this, sel)));
9 z6 ]' F J- _% W } catch (Exception e) {0 y8 d" q- R1 Y# Z
e.printStackTrace (System.err);
, |6 d& x9 S( x9 h1 p }( |/ ]7 X: d3 @6 Q7 ?; S
) ~3 ^7 P% L4 I# [8 i
syncUpdateOrder ();
P6 l' w2 [! o. E9 k! _/ E$ u, |3 g4 I0 r. J6 Z0 _1 C8 A
try { z, J, d: b. w% u& g: B1 a! D+ q
modelActions.createActionTo$message ! n6 { W/ i5 {+ x
(heat, new Selector (heat.getClass (), "updateLattice", false));( l7 ? _3 v o& P7 |# m
} catch (Exception e) {- @7 o8 ?) k/ V
System.err.println("Exception updateLattice: " + e.getMessage ());- a2 `. X6 W7 K( ?
}
/ M7 {' @. j, `: S% @% X; y 5 y( l8 d7 q! i. n
// Then we create a schedule that executes the* W% z- N7 ~9 l' `4 l
// modelActions. modelActions is an ActionGroup, by itself it( L& U: ~- q$ f3 H
// has no notion of time. In order to have it executed in7 F' A/ c# X+ r& Y) t4 P& g
// time, we create a Schedule that says to use the
! |6 s/ Q: \: h. D // modelActions ActionGroup at particular times. This
2 v$ M, z( S% P2 l5 h" |3 q" h! u e( C // schedule has a repeat interval of 1, it will loop every, }$ N6 q7 y4 S
// time step. The action is executed at time 0 relative to. Q: a4 W, j! Y, u3 |/ m
// the beginning of the loop.
( V2 V) l( G9 B& y5 ~ j+ u d4 w; R( \! w
// This is a simple schedule, with only one action that is! ^7 o" Q; Q2 g; f& h8 }
// just repeated every time. See jmousetrap for more& A6 k3 h# y! c3 O
// complicated schedules.2 a, _* H( I: n1 s4 [
2 E& N$ `! H. D: a/ ?" z
modelSchedule = new ScheduleImpl (getZone (), 1);
) h, v- D% i d# a$ N modelSchedule.at$createAction (0, modelActions);
+ \: O, E' m0 a: {8 m6 \
8 |* k, v% J$ u+ l return this;5 G! a4 y+ \2 y$ v
} |