HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; q4 S; x+ J6 M* x4 o" K- [/ w, {
; L% c4 P' Q: P, j& N) b: V public Object buildActions () {
. S8 W9 u! p6 W super.buildActions();- _8 S6 O) Z" ]4 x4 n
T7 G4 c* f4 Q& J3 U2 o. T- ^7 i // Create the list of simulation actions. We put these in
6 Y5 ?( `4 y6 K$ P, @3 u // an action group, because we want these actions to be# v& D, ?& R4 ^
// executed in a specific order, but these steps should" O {/ V4 `& }2 X7 p
// take no (simulated) time. The M(foo) means "The message W$ L) L; a# r2 L3 _5 G$ U
// called <foo>". You can send a message To a particular
7 J' M9 I6 Z$ s) Y8 I // object, or ForEach object in a collection.
2 M7 r8 s: C! s " p. `9 D. ^! x+ { @ n' K# D
// Note we update the heatspace in two phases: first run5 C4 E6 |2 ~/ }6 W: Z. p
// diffusion, then run "updateWorld" to actually enact the
+ }) o& Z0 P' ^ // changes the heatbugs have made. The ordering here is% p" T9 c! y' d2 u6 ?+ L$ P
// significant!
6 ~ t+ U9 j7 U0 @
3 K6 a9 A7 o$ q6 B0 R // Note also, that with the additional
' P/ M$ a. T$ \1 x9 W# V // `randomizeHeatbugUpdateOrder' Boolean flag we can# r$ g3 z5 e" X) |
// randomize the order in which the bugs actually run0 A. _0 d2 I6 I. X7 [
// their step rule. This has the effect of removing any) `, c! I7 y! T, ]+ j
// systematic bias in the iteration throught the heatbug; T1 Y% f/ f( ?& }# I E
// list from timestep to timestep! V/ M0 Y; S9 E" J% f: D+ x
4 S: L0 _4 O! P) @" y // By default, all `createActionForEach' modelActions have
: L) Z9 D& Q( Z+ h0 E" q$ X: |5 P% x // a default order of `Sequential', which means that the* |2 L, \5 q: N+ Y+ q
// order of iteration through the `heatbugList' will be8 B/ ~! A5 d+ c3 p8 g
// identical (assuming the list order is not changed
6 M& j' \: i& h) n, ]% I0 D' I6 @ // indirectly by some other process).
6 e9 T/ G9 @7 ]0 C+ D& H# M
% b1 y2 g+ g- m1 V& f modelActions = new ActionGroupImpl (getZone ()); ?. L3 l4 l `
# C7 \" j' ~) o4 H
try {
( X& t4 T6 o& \/ ~- Z# q6 q modelActions.createActionTo$message$ G G& W* f. I! w- a/ Y+ O
(heat, new Selector (heat.getClass (), "stepRule", false));$ X, z7 D# Y0 j
} catch (Exception e) {
0 I* [- d' V7 l% { System.err.println ("Exception stepRule: " + e.getMessage ());' z& x# m4 |1 @2 ^& P3 @
}% m- E" o8 ^5 e6 @1 X$ U4 J
" `( O8 e" X% y" c& e! Z4 P try {
# w+ d/ g& z- s) f1 s Heatbug proto = (Heatbug) heatbugList.get (0);: T! p' i( |6 r$ ~% b, \8 F- g
Selector sel =
7 u- A5 X3 p! N! B1 r: p- U, U& r new Selector (proto.getClass (), "heatbugStep", false);
# @# m5 |2 {$ S actionForEach =
" T. Y' m6 r& K( e% z" u modelActions.createFActionForEachHomogeneous$call
1 m! E$ `) f+ M+ D" J e; F (heatbugList,
, K8 ?2 j1 ?& a7 ^ new FCallImpl (this, proto, sel,
c5 F$ j; j, A# j3 H$ T new FArgumentsImpl (this, sel)));* @7 Y1 p" L! e$ k, D* W$ @
} catch (Exception e) {. I1 L* g0 R4 ^7 `: G
e.printStackTrace (System.err);
5 F# @/ N1 k# V: h }6 I7 s* t3 h4 c' B$ v% y
# J& a% D; a) J. I syncUpdateOrder ();0 l9 t* E/ _; T4 R4 H, c
' e7 _& h/ b2 }5 u& P- ^( Z try {' f7 `6 T+ J5 T
modelActions.createActionTo$message
0 ]" C$ g$ u; i2 \1 O (heat, new Selector (heat.getClass (), "updateLattice", false));: |+ @! f- e0 Z# K
} catch (Exception e) {. n r9 a. {0 C; l
System.err.println("Exception updateLattice: " + e.getMessage ());
' E5 s0 v2 k: Y: \! r }9 Y* L7 E; h* U% ?( u5 Z/ B. K1 i
/ n" H- k- C! h2 G; v
// Then we create a schedule that executes the9 [5 M! b# [" H+ J
// modelActions. modelActions is an ActionGroup, by itself it6 P; T( P1 T6 K- B) d0 r2 H* C
// has no notion of time. In order to have it executed in
; [' G' q1 S, K! m2 K; @ // time, we create a Schedule that says to use the# P( ~$ I- J D. a. M0 z! r
// modelActions ActionGroup at particular times. This* V* B( z! m5 h: @& U
// schedule has a repeat interval of 1, it will loop every+ ^' c$ b0 }& {! n' C3 L! Q
// time step. The action is executed at time 0 relative to
9 c& d" b7 X& Z5 h' d // the beginning of the loop.& K( }! n/ e8 T. q+ H4 V
1 j9 z8 H2 N! [9 Y" x% |
// This is a simple schedule, with only one action that is W8 g. |( e/ n! R" o! n
// just repeated every time. See jmousetrap for more: O* G( f; f0 E- y* y2 I9 Q k
// complicated schedules.1 n0 i0 B& j! k& v# ~1 j
+ b, K% |. C* R9 G
modelSchedule = new ScheduleImpl (getZone (), 1);
8 s$ ^5 C }: x0 N modelSchedule.at$createAction (0, modelActions);, u% H- u6 m& E" ~0 I4 C, a
H v8 {; x. ^ return this;
& J' }1 B6 Y. h } |