HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 m& H! E9 M; |9 N0 y" L, O
e# x) Z1 Q/ o2 ` public Object buildActions () {6 ?3 i4 _; X6 B* e+ \1 k* T1 N
super.buildActions();, ^& ^7 L% H0 }: E7 [
* A" I7 e* ?$ o9 Y8 b8 H }+ ] // Create the list of simulation actions. We put these in
" f. s1 R4 W0 M // an action group, because we want these actions to be# ?& {9 D/ B$ x* d4 {+ }2 u0 k
// executed in a specific order, but these steps should5 @4 {( M/ n0 J' |/ w/ \! ^+ m
// take no (simulated) time. The M(foo) means "The message
* W- Q/ ]1 s- |% V // called <foo>". You can send a message To a particular/ V1 V8 b F7 H! O4 K4 t
// object, or ForEach object in a collection.
3 ]/ n: O# ]1 q
% v% k- g( i" N/ m // Note we update the heatspace in two phases: first run& }" E9 z$ |5 q, P! M \
// diffusion, then run "updateWorld" to actually enact the
T4 e r; Q, k) a // changes the heatbugs have made. The ordering here is
5 N4 A1 c# j- g) [% l // significant!# ~5 X3 J& R/ \/ e% |& S
3 ^+ a2 V* w/ p% W
// Note also, that with the additional% F8 |3 b' s1 l8 n
// `randomizeHeatbugUpdateOrder' Boolean flag we can4 U( ~& ]; H( C1 D
// randomize the order in which the bugs actually run
. h9 v& a( N. C) H // their step rule. This has the effect of removing any
P5 L) e6 h& T: z // systematic bias in the iteration throught the heatbug7 _4 r( d3 r' x' |6 p' }! I, v
// list from timestep to timestep
% e1 s- j* D# ]0 m2 ~, H$ P ; J7 e" i# \; y5 n+ j L+ q' T/ j
// By default, all `createActionForEach' modelActions have& k; S$ H# _, p ]
// a default order of `Sequential', which means that the
# M) c# {3 k% g4 m" A // order of iteration through the `heatbugList' will be
& z: H V: i# v* y( K1 i // identical (assuming the list order is not changed
& ?6 k( I( o G( r6 H* y" A // indirectly by some other process).
! E9 L0 D3 q. ?$ Q7 A- o0 T8 I , J( ~8 \$ q3 Q9 B$ ^, [2 z
modelActions = new ActionGroupImpl (getZone ());
6 f/ D: `7 v- L; k! e( o5 Q" {' R- O4 E8 F
try {
0 ^- Z0 {5 p0 g/ K modelActions.createActionTo$message9 ^8 N, }; C3 g& m8 M
(heat, new Selector (heat.getClass (), "stepRule", false));/ ? N/ E7 R' V$ p! @0 E- q
} catch (Exception e) {
/ S7 R# N, N5 ` M( p- N System.err.println ("Exception stepRule: " + e.getMessage ());% F8 B$ T- j0 b6 u: p2 o, _0 j! ]
}
& [6 C' |8 i: V4 t
0 _3 @6 o* J5 w! a/ J) z$ d try {5 I/ _3 {( ~, e
Heatbug proto = (Heatbug) heatbugList.get (0);
' {" j5 t+ t% _. g: q! [ Selector sel =
0 _% c( o) s) }9 A, {9 T$ X' F0 E+ \ new Selector (proto.getClass (), "heatbugStep", false);
7 v% v6 B4 M& G9 g( h+ h( s; u' r actionForEach =% p- T- I' m8 a! Y' O, F( O6 I
modelActions.createFActionForEachHomogeneous$call
7 U+ K/ m$ c- x' B( r' Q, H, Z4 B (heatbugList,: \ G7 V; }/ i5 r/ C8 F) _7 E
new FCallImpl (this, proto, sel,
5 H& {( B1 E+ J5 z0 } new FArgumentsImpl (this, sel)));
: V0 t& m* F, j: Y } catch (Exception e) {7 ?* K, {1 H7 V. f
e.printStackTrace (System.err);
' D( W: A; X9 Y1 y3 T& I }" y$ M0 J3 F% X$ b
; J$ [) q5 N- c" z$ N2 L7 M& T v syncUpdateOrder ();
8 F3 K+ ]- O6 O
- a+ W, M7 n* U4 s try {5 [' `# q# @6 J* Q+ g \
modelActions.createActionTo$message
4 I5 m! \$ a5 k! b. t0 y8 e (heat, new Selector (heat.getClass (), "updateLattice", false));
* ?- K6 J+ \5 g: [ } catch (Exception e) {
0 i" U& A+ v9 D' q1 i System.err.println("Exception updateLattice: " + e.getMessage ());
% {1 Y: C* E4 {7 K }( Q$ J- e) L, v% B- a W
& z8 x$ S: s' T; {1 j // Then we create a schedule that executes the
% F7 ]" @* Q- z; k6 J' G) [# b // modelActions. modelActions is an ActionGroup, by itself it k3 L$ Y/ t1 s6 M1 t
// has no notion of time. In order to have it executed in& E" |0 J- T* U7 F! W, i/ Y
// time, we create a Schedule that says to use the
: J( u7 h* B4 V2 w- v- p1 |0 ?( V3 Y // modelActions ActionGroup at particular times. This# f0 U2 E4 e) Q
// schedule has a repeat interval of 1, it will loop every v: e+ Y: {$ T
// time step. The action is executed at time 0 relative to
, B+ V/ j3 O2 F% c0 w! ` r/ r // the beginning of the loop.+ J! t* I) I' L- V. v+ [; c
& N! z9 U* j, P/ G& c // This is a simple schedule, with only one action that is& `& a2 @1 C3 Z% \
// just repeated every time. See jmousetrap for more; J3 c; {* S6 p
// complicated schedules.
; E3 h2 m, N) B' P- P5 s7 M : }5 m! X, [$ @' T* @' h: U! n
modelSchedule = new ScheduleImpl (getZone (), 1);
+ C1 _) l8 w% }) w- c j$ P+ f" U modelSchedule.at$createAction (0, modelActions);
$ d* u- {! j/ G1 h
c$ J( r8 A- }+ S5 K6 M2 s return this;
+ m3 p9 Z8 F2 H; J' @ H% \ } |