HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 S) Z+ J# r0 ]& @( B9 i! o0 e1 L. V# V: T6 e
public Object buildActions () {1 E3 I$ _9 W- w$ t( s% t5 ^
super.buildActions();# @# m' k" o7 g& [ L. n- m. ~
- r. ^) I6 M6 b- P. ^
// Create the list of simulation actions. We put these in( l: ^* d0 Y$ o3 U
// an action group, because we want these actions to be* t' N8 d7 c, p3 K7 T& [, Q' S) _
// executed in a specific order, but these steps should* j5 o4 k7 g' f0 I! \
// take no (simulated) time. The M(foo) means "The message L6 d: h( K/ D/ D3 P7 d& F
// called <foo>". You can send a message To a particular9 Q, s- I3 o/ v( x8 F1 T
// object, or ForEach object in a collection.
! j; l+ J% i+ {: L; v5 W 4 ^# V4 P7 D$ A9 u7 T
// Note we update the heatspace in two phases: first run
; @, I+ ^1 ?; i& t0 B1 c: b- ~/ x) e0 l // diffusion, then run "updateWorld" to actually enact the
: B2 l6 Y+ w# H% a3 R // changes the heatbugs have made. The ordering here is
0 a& e* N( Y' B5 p0 m2 n% O) U( n // significant!# P+ y' [+ n/ g/ t
0 g9 @1 M$ s9 t7 C9 ?/ C( ]- y // Note also, that with the additional
9 E+ [4 V2 @" d7 n2 S7 V9 | // `randomizeHeatbugUpdateOrder' Boolean flag we can
% ?1 x) Z' @, [1 b' M // randomize the order in which the bugs actually run( f4 B/ j/ ?4 l3 [' c: n
// their step rule. This has the effect of removing any& O3 t6 C9 m( T5 e* Y. \- U
// systematic bias in the iteration throught the heatbug1 u7 U* g4 l3 O. Y
// list from timestep to timestep
2 f1 f% b/ d' m9 _$ J ! T! l# _7 {8 M f
// By default, all `createActionForEach' modelActions have8 f% }0 k0 k7 g: ^6 w/ g/ |9 t1 N
// a default order of `Sequential', which means that the, T0 Q7 ]5 x; Z- J; k
// order of iteration through the `heatbugList' will be7 m: C9 g. c8 y2 H
// identical (assuming the list order is not changed4 `2 j9 p2 C9 A. D
// indirectly by some other process).
* M% K! f# t5 M) ]/ g/ e' s ! G ^: v7 e- L* A" B8 j- T2 Y
modelActions = new ActionGroupImpl (getZone ());. D' L) A) m& ~. j. ?% Y) X9 D
6 I5 \, h- T) s, u% I
try {
" @- R- H, V+ A8 S modelActions.createActionTo$message; B! f$ l) C* N$ Y
(heat, new Selector (heat.getClass (), "stepRule", false));
& x& d+ {: u" o; _( P, F3 x; s } catch (Exception e) {) ?2 y9 C& b! f M j
System.err.println ("Exception stepRule: " + e.getMessage ());' T5 x! J: z# O1 n
}
! ^ X3 A' k3 G) W& ^0 f5 S6 _$ ^0 `0 ~( x# j2 M" \
try {7 l( p+ y+ M/ i
Heatbug proto = (Heatbug) heatbugList.get (0);
5 _; A5 m- m8 J Selector sel =
5 D' q* p: ]: W" a new Selector (proto.getClass (), "heatbugStep", false);
2 s: a' I. g( _0 d& C actionForEach =
7 k& E, Z2 K" c# B, _) Y7 r! O modelActions.createFActionForEachHomogeneous$call: F. J. b# V1 J$ ~+ o
(heatbugList,, [* y/ U# }# w" C
new FCallImpl (this, proto, sel,
9 }0 j$ j7 T% M0 E) E) ~ new FArgumentsImpl (this, sel)));& W9 O7 x; _0 W) `9 \& t1 j7 N9 S
} catch (Exception e) {
7 T( ^; S% q5 d8 O ], O e.printStackTrace (System.err);! `; x# j7 p% _( `" q# B
}% f; a* o" p: a/ I. }
2 Q) w; n3 k: [' K: `
syncUpdateOrder ();1 k1 h& c2 D" D) j* d& Q
; n" @+ V" I4 v& d2 @6 e3 \ try {
! h2 h+ w0 T2 i( w% ^ modelActions.createActionTo$message ) V9 \# L! ^2 J0 M1 k0 M
(heat, new Selector (heat.getClass (), "updateLattice", false));* {! R- S% c; T6 P( i2 i1 Z& v! R
} catch (Exception e) {9 t8 z: A$ k# s: f9 [" ~5 P5 R
System.err.println("Exception updateLattice: " + e.getMessage ());
+ s$ s. H& u2 Z" M% }" Y7 `$ q9 E }# n g& ]% x4 l% P) Q- M
/ R5 n& R5 A1 Q3 r& l$ E L
// Then we create a schedule that executes the5 F5 a' \( i1 C
// modelActions. modelActions is an ActionGroup, by itself it. W! j8 a+ p2 y
// has no notion of time. In order to have it executed in
$ B: U6 H W/ j# U" F& L, \) c/ g0 _ // time, we create a Schedule that says to use the! }+ c! i5 r: o7 ]2 s
// modelActions ActionGroup at particular times. This
$ a5 [% e& L$ X! g! z# Q // schedule has a repeat interval of 1, it will loop every9 R j' R5 K9 @: w! T
// time step. The action is executed at time 0 relative to
# [5 Q5 X% M% q \9 A9 V7 R0 l3 L+ N // the beginning of the loop.
. s0 N7 d, g; P4 d' P, R, D( E$ Q9 ]' A! N/ M1 @: I
// This is a simple schedule, with only one action that is
2 o7 M, ? l1 Y // just repeated every time. See jmousetrap for more$ M4 Z) L5 X( n- o" ~
// complicated schedules.* B) s. b! K O
) X1 X. m) l! p3 ~& k) y modelSchedule = new ScheduleImpl (getZone (), 1);! y/ Y& ^5 @- \' m& @3 H" P
modelSchedule.at$createAction (0, modelActions);) w9 m+ D2 [5 P! H" R4 l- n
9 {. e, f# R1 t& N7 Y; t8 Q' Z/ a return this;4 ~# v) M9 h' I- ^; c$ r
} |