HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" H7 M( p! w) c! F! k; a: S8 \- {& r
7 |+ z/ d- H0 h public Object buildActions () {" V. p* j& c& _# D6 Q
super.buildActions();
6 i, |2 @; x6 u% V+ \ ! }; J. \ I$ N3 t/ j* a
// Create the list of simulation actions. We put these in
6 Y3 f: P" C" `* `0 k // an action group, because we want these actions to be2 I! K! A, g w2 d k9 [
// executed in a specific order, but these steps should
9 l L: r4 g) q, [! u // take no (simulated) time. The M(foo) means "The message1 D( \" U/ [3 G0 }0 q U f
// called <foo>". You can send a message To a particular4 ~- v; X5 |" B$ h3 K' @
// object, or ForEach object in a collection." g6 E# T" [6 V/ y
/ I h$ N. B2 H! \0 Z; B8 H' t
// Note we update the heatspace in two phases: first run
1 ]& m7 P7 M% E. K( V // diffusion, then run "updateWorld" to actually enact the- R& [1 N0 O+ {3 r* Y
// changes the heatbugs have made. The ordering here is4 T! _" A+ V1 [8 f5 w. }
// significant!6 h4 U) H& c& m6 k k; f+ T
- L# o" X7 V. b5 n
// Note also, that with the additional6 z7 y( \; |" e9 Q+ H: u: {
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& R& D1 d4 M/ H" @5 r // randomize the order in which the bugs actually run' M3 [% T; r9 `5 S4 J p# M2 r
// their step rule. This has the effect of removing any
: V' \2 K3 i/ X5 }1 w+ x | // systematic bias in the iteration throught the heatbug
Q0 i' _% I7 X6 e O // list from timestep to timestep
0 I8 v4 r4 G3 ?1 S) j 3 } c" ?, A9 ^
// By default, all `createActionForEach' modelActions have$ ^" w% @! C) U% W% ~% v
// a default order of `Sequential', which means that the5 Y( v: j! W3 J9 I( B$ i
// order of iteration through the `heatbugList' will be
: R% L8 S4 o* w // identical (assuming the list order is not changed
0 g/ a2 r, a6 p4 S+ {5 B: X // indirectly by some other process).
2 G1 f2 x) V" _ 3 a- A8 a j5 O- R9 @
modelActions = new ActionGroupImpl (getZone ());
' h) t& R) I( x- o; i4 g0 ?+ p( R& G% s2 ` f( C' W1 V
try {( E" I# O( }5 v w3 ]. \0 C2 C( Q
modelActions.createActionTo$message
: ~' |; U9 F. A (heat, new Selector (heat.getClass (), "stepRule", false));6 u1 E# z# n" k# |
} catch (Exception e) {
+ q6 p. H: _( Q1 d9 ^ System.err.println ("Exception stepRule: " + e.getMessage ());4 k$ R6 b4 Z# E2 _" z7 ^1 T6 M
}
5 N' \9 h n7 o- }) x) L
D$ U3 S1 S, q8 {: v try {9 W2 o: ^4 R# g7 V
Heatbug proto = (Heatbug) heatbugList.get (0);
7 d6 o' B9 `% {* X2 X: Q Selector sel = ; e$ b8 e9 B" W4 S* Y
new Selector (proto.getClass (), "heatbugStep", false);
! I( B( c [- d) Q2 T8 R; h actionForEach =( y* t8 c, Y: s$ j: n M
modelActions.createFActionForEachHomogeneous$call
* I2 H Q- P' F (heatbugList,7 h8 g; Y7 y9 v) ^: t
new FCallImpl (this, proto, sel,
7 {: E9 h0 ~0 R+ X9 F new FArgumentsImpl (this, sel)));
2 P2 d, t$ P9 U3 i: U } catch (Exception e) {0 W7 m Y+ l5 \! I) p" a/ u5 m, }& h
e.printStackTrace (System.err);
2 V6 \2 h6 Q7 j3 m! h' `( y' |5 U }
& U/ h) Z$ k* l; I6 ]+ a/ I 1 ^6 y& t* `% M2 {$ ^9 B
syncUpdateOrder ();' s6 k8 w: @: W% v2 }
( p/ r' Y7 r8 `* \- T: a+ E
try {+ `1 _& }/ ~1 V, o
modelActions.createActionTo$message 1 r. e9 V/ f8 W
(heat, new Selector (heat.getClass (), "updateLattice", false));
, @* z$ Q- U! f' [; o } catch (Exception e) {
: a; h$ m( O7 Z3 Q: n, R9 l* i; w System.err.println("Exception updateLattice: " + e.getMessage ());" k' Q4 ]2 p4 b/ ^( [6 t
}
0 ^1 r! o8 ^* j l# K* C9 f: s 3 X+ ~; e& _- p# L/ M# J" a
// Then we create a schedule that executes the
# f1 ?2 r! i! | // modelActions. modelActions is an ActionGroup, by itself it
; x% @* y9 B1 v7 v3 c( |; l // has no notion of time. In order to have it executed in% J4 j+ ~ k- L$ ?: c) S
// time, we create a Schedule that says to use the( Q5 e, J$ R5 X
// modelActions ActionGroup at particular times. This. }3 @: d- g( Y7 F
// schedule has a repeat interval of 1, it will loop every
2 z; O$ X, J1 j/ b% C/ ^% e // time step. The action is executed at time 0 relative to: [( @6 O& A. G- f
// the beginning of the loop.
s( H4 j: W, H& ^3 j/ r/ U# @
* `/ L% @ l( K/ l& |1 Y // This is a simple schedule, with only one action that is
: q* @- h$ k& k; F9 L" r // just repeated every time. See jmousetrap for more
) p5 n# @- ]7 [+ Q" _0 O2 y // complicated schedules.
+ ]$ r6 Y4 P% M3 d0 v2 ^! C 0 L$ I; E$ j" {7 m4 L, D+ a) ~
modelSchedule = new ScheduleImpl (getZone (), 1);
) R& T# s7 H4 o: x! H modelSchedule.at$createAction (0, modelActions);
/ n" h. h* e* f" N- l, {
" j" s b3 t, E7 y0 m4 A return this;9 ?# D1 ^! a' w
} |