HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) t; K' `; g! h: x% v+ ^- h! j. ^8 p
! @8 w% M# `- y' m+ q7 O
public Object buildActions () {
6 J3 @* l2 g, g7 E) ~* |" t& l super.buildActions();
9 l( \. F! [' ]+ r& c
. `1 t" ` S- v% d/ p // Create the list of simulation actions. We put these in7 t# @4 m! L6 D
// an action group, because we want these actions to be0 y; h5 H" v9 U2 x
// executed in a specific order, but these steps should/ P0 E" w2 Q L& ^, m6 R" [6 }
// take no (simulated) time. The M(foo) means "The message
. @; R% X3 a- |: ]2 v$ e! E$ ] // called <foo>". You can send a message To a particular1 K4 d0 l& a* {- Q: K& V
// object, or ForEach object in a collection.
4 C" l1 L4 f B2 o) Q z7 {6 R8 }) S: M. A" d2 U
// Note we update the heatspace in two phases: first run3 F/ ~9 c6 O5 z8 E4 h3 @
// diffusion, then run "updateWorld" to actually enact the- d3 [! s, V& b6 [4 G
// changes the heatbugs have made. The ordering here is$ X# ^2 N8 ?. L! S
// significant!+ n' l; h! I0 G
( e4 }. ~& X2 i4 I# X. h& W // Note also, that with the additional. K7 m8 c9 b! ?1 U7 H. `5 ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; `3 |2 R( k- j6 m: s9 q // randomize the order in which the bugs actually run
6 X! h: T( ^* x n1 H9 y' U // their step rule. This has the effect of removing any
! M6 w* L* y8 u/ [( U. O // systematic bias in the iteration throught the heatbug5 o6 [4 h6 D: `8 L
// list from timestep to timestep. J+ Y9 ?/ B4 S+ A z
: O' K7 P* H2 G2 h5 Q' V- B // By default, all `createActionForEach' modelActions have
3 `7 X" [3 y/ I8 ^ // a default order of `Sequential', which means that the
8 R: p8 U9 t# [: o1 H# E7 n% X // order of iteration through the `heatbugList' will be0 D$ ]# Z( |$ A' q5 _ Q
// identical (assuming the list order is not changed
8 B4 ?1 V! O1 j: F' X( { // indirectly by some other process).# _2 y1 j i4 @' J, f0 [0 U
* H# }: M$ P ? modelActions = new ActionGroupImpl (getZone ());
! H6 u! R. P) L- w4 S! [9 O, o/ X
try {
6 x' i* Q Q- e! Z0 o$ b$ u T modelActions.createActionTo$message$ M: ?) ]8 F1 u
(heat, new Selector (heat.getClass (), "stepRule", false));3 T& y1 ~! @# r: @- n
} catch (Exception e) {
/ s. A0 x* X$ m3 }) I4 q System.err.println ("Exception stepRule: " + e.getMessage ());
3 e' x9 l1 g1 L1 v }2 F( x2 T. N1 F: l8 A- r
8 b2 ?1 Q7 Z1 X3 s8 B) x+ P* ]# T try {" Y J9 s& o# Q3 _% \0 g
Heatbug proto = (Heatbug) heatbugList.get (0);
) |. p! P4 L, U& T, {& T8 }% w Selector sel = 5 M! z: D& T! |1 w; Q+ Y$ a* ~2 L2 J
new Selector (proto.getClass (), "heatbugStep", false);. W' b5 v i% u
actionForEach =" h4 w! ^( p# v/ s# ?9 I
modelActions.createFActionForEachHomogeneous$call- @1 b( k( A/ V& O3 r
(heatbugList,
/ a8 c4 ?* N& i! m* }, j1 u new FCallImpl (this, proto, sel,
# {5 r. N5 m+ F0 O) q1 ~ new FArgumentsImpl (this, sel)));' {. R+ s6 J4 X6 \# B9 Q
} catch (Exception e) {* X: c( P, J, n- E% `
e.printStackTrace (System.err);
5 p4 X4 j) _% e6 O+ K }
5 q. W; F, L# B4 ^& A 5 l1 g0 T8 I5 W9 I1 \. F
syncUpdateOrder ();1 n4 x; @: y+ Z3 L0 c3 J
/ j3 [: \. l( i6 o/ f [ try {
1 O4 q( `; b- O: Y* B a modelActions.createActionTo$message . Y& C* P8 r9 _: p& u r7 Q
(heat, new Selector (heat.getClass (), "updateLattice", false));; P1 G0 ?" O5 z; L* `+ N
} catch (Exception e) {3 i4 ^) X$ I8 A( q9 Y6 F: S
System.err.println("Exception updateLattice: " + e.getMessage ());
9 B, Y3 `2 {3 i6 f1 Y6 L! N' }" y }; b" q# h, L' n+ D
+ K; }. j+ @0 L" O2 Q" N. x
// Then we create a schedule that executes the) Q' O p' Z" ?9 `8 s# e$ L* a
// modelActions. modelActions is an ActionGroup, by itself it
# y3 F- }, q/ P' s, Q& H, m // has no notion of time. In order to have it executed in9 F( a+ g! D7 p5 v! A- I
// time, we create a Schedule that says to use the
. ?/ }' P( X" r6 F! w; q // modelActions ActionGroup at particular times. This
5 O3 ~/ ^4 P$ l, l6 r8 B! E // schedule has a repeat interval of 1, it will loop every/ S3 W; I U8 Q. r
// time step. The action is executed at time 0 relative to6 l I4 I( c% x X; {/ Y
// the beginning of the loop.2 c" b4 n2 {5 i4 h
* g" [3 X% Q) a! V // This is a simple schedule, with only one action that is* i6 w( L& i+ G7 r0 Q: ?
// just repeated every time. See jmousetrap for more
7 k: f" I8 v& i4 L1 x3 Y6 k // complicated schedules.1 t1 H) M+ m% L5 S0 F7 f
5 a( x; P4 P; g modelSchedule = new ScheduleImpl (getZone (), 1);
7 ?; W7 ?( Q+ x modelSchedule.at$createAction (0, modelActions);& R3 l0 T8 r$ P0 }
3 h5 i' `) r; P. b% \+ B1 l
return this;% g+ v9 a! u8 }" K/ ]: \
} |