HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ x$ ~ B7 @5 W1 l( B. h# w/ X* z
/ x& I' W% ]+ j1 b% C% F8 k+ m5 ?% E
public Object buildActions () {
; I% |/ t) D) C6 h, | super.buildActions();# H- P' T- w; J7 W- i9 t
) A+ L9 q+ n. j7 `, G5 z
// Create the list of simulation actions. We put these in
- g) T& e& x- Q // an action group, because we want these actions to be
- A( ?+ \9 j" d, O/ K6 E1 h2 E // executed in a specific order, but these steps should
0 P, d: q% T; h) ? // take no (simulated) time. The M(foo) means "The message6 x i* r+ f- ^; g4 f. W
// called <foo>". You can send a message To a particular
0 d! C+ d4 E1 b* U4 t- }) s // object, or ForEach object in a collection.' @1 T3 ]" j9 O' I( S1 O ?
$ v/ @4 S# ?5 K- Z6 h
// Note we update the heatspace in two phases: first run
9 ~/ @! B8 J; _4 X" W // diffusion, then run "updateWorld" to actually enact the( y4 A) e& o: [* {4 ?, C
// changes the heatbugs have made. The ordering here is4 W5 C7 i: Q. t( j3 ]6 B
// significant!
! A. n7 ]) J8 p
2 N$ a7 d5 l# C. I& k0 u // Note also, that with the additional
! G2 e7 B& |0 q( } // `randomizeHeatbugUpdateOrder' Boolean flag we can! Q/ N* T& V+ y
// randomize the order in which the bugs actually run
1 A" n2 `( V* w% @3 M, r: d // their step rule. This has the effect of removing any
9 d! w5 z4 J( X8 S9 ^ // systematic bias in the iteration throught the heatbug
1 G! @2 h1 Y0 s2 o+ W: { // list from timestep to timestep
]% ]# v- ~6 Z3 N: k) F 4 L" [% x- I/ n3 Z( Z
// By default, all `createActionForEach' modelActions have) i& h' z% f; q
// a default order of `Sequential', which means that the
9 n' ]- K" u4 E$ A$ e1 ~; x // order of iteration through the `heatbugList' will be0 K( H; p' _) d! u! w
// identical (assuming the list order is not changed$ s4 l% R+ S4 b) r4 y
// indirectly by some other process).
. T' \3 _: L1 M. k8 u9 ~
# U0 z) `& }, t) a/ `0 `6 }$ f modelActions = new ActionGroupImpl (getZone ());
! W0 U/ o0 B1 D- S. G
* r+ [: I+ I# g/ d6 ^% r/ `" C try {
7 L u0 N" k7 Z% {( f( ~ modelActions.createActionTo$message1 y- I- K$ E$ I& {$ d
(heat, new Selector (heat.getClass (), "stepRule", false));
# F) h: H: ^: s1 r% [ } catch (Exception e) {* _# h# r F2 i+ A
System.err.println ("Exception stepRule: " + e.getMessage ());' B0 L) @7 B5 o* H: c) q7 ]: u! ]
}
, l4 K, U0 e/ ?* y z+ I# [0 S% z; e# F# b9 j" _
try {5 ~; e3 Y% y* T( b8 D% I& A: H
Heatbug proto = (Heatbug) heatbugList.get (0);
+ u$ I+ N8 k+ F0 Q Selector sel =
$ R1 ~) M, q9 e5 z, o) J+ Y) z new Selector (proto.getClass (), "heatbugStep", false);% f" z. ~4 ~/ [: @5 k
actionForEach =4 a7 P0 r& F3 F4 Y
modelActions.createFActionForEachHomogeneous$call
6 H0 A7 ?4 N- A' V (heatbugList,
' p2 R, [ Z( p new FCallImpl (this, proto, sel,# a; `/ I, R# ~' }+ }
new FArgumentsImpl (this, sel)));
3 e. ]2 C1 `2 u5 N, ?: M } catch (Exception e) {( r. L. J; Z& N; h9 Z4 g: L
e.printStackTrace (System.err);/ f) V1 B3 z; V1 o& n+ l/ B: r
}
* A0 m1 ?0 E0 J9 Q9 ]9 ~! g! g
$ Q5 f! f2 T) g5 j X* i syncUpdateOrder ();
6 U) M: i0 B. d+ U# a2 ^+ R0 y/ c' @& t G2 X7 {# X
try {& t u4 K% k! @0 e+ J1 a
modelActions.createActionTo$message
7 e/ z% Y% ~% Y4 @4 r/ n# k (heat, new Selector (heat.getClass (), "updateLattice", false));0 i* e, b( T7 j( z2 a
} catch (Exception e) {: y- G6 N c! t
System.err.println("Exception updateLattice: " + e.getMessage ());' O8 K) ` g3 C, h
} Q2 ~5 l6 _8 }
' `! @6 K' C4 @- y& B+ D7 ?* V
// Then we create a schedule that executes the
; m9 E: B3 l) z: H // modelActions. modelActions is an ActionGroup, by itself it: o; H6 W9 p7 r7 _1 Y6 E6 A" R
// has no notion of time. In order to have it executed in
" [% {; m, u; G" m; G) V // time, we create a Schedule that says to use the; r- {! t* w d) j. O$ y$ n( ]9 L
// modelActions ActionGroup at particular times. This
- t5 Y0 c- f: p( a( Y3 B. x // schedule has a repeat interval of 1, it will loop every
1 I) t' C7 [% \/ P // time step. The action is executed at time 0 relative to) v( Q, v0 U( ]! r1 Q; t, u+ x
// the beginning of the loop.2 d2 K x2 H0 ~9 P+ w. v0 ]
- Y5 R/ e( k1 d$ x4 u: m# ~ // This is a simple schedule, with only one action that is. \. o# w0 |- W5 `
// just repeated every time. See jmousetrap for more
" {! ^7 n4 r5 R6 \) j // complicated schedules./ t+ y- z) J& p( k- c& ?' S
2 g. j6 i- b9 F) H1 f2 [
modelSchedule = new ScheduleImpl (getZone (), 1);0 |' r) y- r+ n x; ]% j: J
modelSchedule.at$createAction (0, modelActions);* e: b2 l8 @9 X% O% e
0 d3 ~) `, @9 n" ?" T7 a# L5 { B return this;
( \1 W5 x3 ?0 A) K0 p } |