HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' }$ K0 P3 M+ [" @' f+ T
6 k4 [- H- F# i+ s public Object buildActions () {
7 J$ [5 F: u6 B4 F* n super.buildActions();, E/ y3 \+ @1 T3 e" o6 K
$ K! B( p. ~: C; \7 j% f
// Create the list of simulation actions. We put these in
" c# n5 n4 M# U% a9 o: V // an action group, because we want these actions to be
& _' B1 n8 F: j3 }4 ^0 e4 l) K // executed in a specific order, but these steps should, X/ J6 J: {% i4 D* r
// take no (simulated) time. The M(foo) means "The message; V w, A! ^9 m
// called <foo>". You can send a message To a particular
5 W* P$ r8 U1 K // object, or ForEach object in a collection.4 B; t6 H! X$ @+ s
# I4 ~% p+ j" Q: p* f9 i6 G0 ^ // Note we update the heatspace in two phases: first run
. y* j: `, C2 C/ o! I9 i" a+ ? // diffusion, then run "updateWorld" to actually enact the4 A# g: j* I0 @: L. e+ d R8 [
// changes the heatbugs have made. The ordering here is
3 U) I" y. Z/ n4 }8 Z& Y, p) L. I // significant!
0 u- Q. b6 ^ q+ N0 M r + W" L0 R9 p2 V1 x8 O% ~( W" ]9 G
// Note also, that with the additional
4 s3 n* M/ W6 j) m // `randomizeHeatbugUpdateOrder' Boolean flag we can
3 ~/ E% g. D/ ?. [) q: l // randomize the order in which the bugs actually run
6 y0 B( u. R3 t& p W' R+ `4 l // their step rule. This has the effect of removing any
3 ~( t+ V; w/ ~ // systematic bias in the iteration throught the heatbug
; E( Q$ u" S: r) r# N6 s // list from timestep to timestep
% R# k0 ]4 O9 ^1 j8 i8 G
: w/ ?6 n J( L/ s+ r // By default, all `createActionForEach' modelActions have
7 `3 M, _% Q# h j( v2 v // a default order of `Sequential', which means that the$ O! ~- i- G' {* W
// order of iteration through the `heatbugList' will be
( F8 ^& i& k) b; F5 ? // identical (assuming the list order is not changed( v; Y. a1 O* n7 J
// indirectly by some other process).
! b; z- ~: T/ K& e; S9 q h : Q/ o! H X; E, b0 C) Z
modelActions = new ActionGroupImpl (getZone ());) A& _* }5 W5 A$ n$ U
5 u; m7 Y; `+ m) h v3 {6 Q: N2 N try {
1 O7 v' I: e8 G0 z+ ?' `+ ~ modelActions.createActionTo$message
, y2 l$ L- p' T, C* d (heat, new Selector (heat.getClass (), "stepRule", false));/ D+ E) F& {" G, i7 G U& M( {
} catch (Exception e) {8 [( `# k' x% T% E
System.err.println ("Exception stepRule: " + e.getMessage ());% ?. g( I* E+ L; F+ ]
}
: U& S1 e' S$ q! X$ f# U
8 V! Y |) }, Z0 ] try {
5 V. \) G1 M3 {6 M- T( c Heatbug proto = (Heatbug) heatbugList.get (0);
$ J3 h, ]/ U7 } ]7 p% ` Selector sel = ; H0 D" Y1 H- p: M
new Selector (proto.getClass (), "heatbugStep", false);9 g0 _- I4 r) l/ t. `# t9 K6 V
actionForEach =
6 Z, G! N3 V$ [9 G( [ modelActions.createFActionForEachHomogeneous$call; y* n3 }. B4 Z$ S f* Z, p
(heatbugList,5 b- i! q/ O _' f+ `9 [9 S! [! P
new FCallImpl (this, proto, sel,
; Q k+ J& ^- W: D5 r6 \' l2 L new FArgumentsImpl (this, sel)));
7 B% L' J& }1 v/ j8 D8 k } catch (Exception e) {! \/ D. W7 [9 S5 N
e.printStackTrace (System.err);
$ w* o: a+ \& i4 h+ ~! l y; L }
6 a ^% f5 Y5 T
4 w9 G7 f4 N- G! G/ Z' \* ~ syncUpdateOrder ();/ T: [+ J, o) `( }$ e
! e! _. j$ Z1 `" P
try {
1 P7 j6 a$ {# g modelActions.createActionTo$message ) u5 Y9 N+ Y9 q5 W {
(heat, new Selector (heat.getClass (), "updateLattice", false));/ C& N8 V6 F8 r6 k+ Z
} catch (Exception e) {
! P+ l' o1 G# ~4 e% p% { System.err.println("Exception updateLattice: " + e.getMessage ());/ y5 k2 t9 }+ C* o: t7 d" I
}9 t) }! S/ E9 G% C' Y, n: O+ Z0 p6 \' Q
4 ]: H0 z! h1 I! h // Then we create a schedule that executes the1 q I4 W y$ o' k' l& b
// modelActions. modelActions is an ActionGroup, by itself it, B! l7 u! Q' C: C
// has no notion of time. In order to have it executed in
4 A* J5 n+ G% p# \4 i // time, we create a Schedule that says to use the* d& `) K( R% y
// modelActions ActionGroup at particular times. This% j2 L& s' e$ R8 a
// schedule has a repeat interval of 1, it will loop every
3 j0 s, G! q8 W. n. |. ]: r // time step. The action is executed at time 0 relative to
3 c% Y7 d( H. `0 C& x4 F9 r // the beginning of the loop.) V- M4 P# ~1 w4 {) E& x
. c7 c8 @9 B- n: z( L
// This is a simple schedule, with only one action that is0 A2 v# ?# `9 W) O* R$ I( ?1 ?$ }
// just repeated every time. See jmousetrap for more
: {- J7 n+ U- o* W% q // complicated schedules.# e7 \9 Z, E2 C1 }4 c% k2 c
4 s: ~" h- Q2 ?" r' P
modelSchedule = new ScheduleImpl (getZone (), 1);
& k4 |1 V( h' [3 V6 K9 t modelSchedule.at$createAction (0, modelActions);+ ^( B9 f1 ]2 x* ]0 @2 r" @
% V0 E ^( {3 d' ]: l1 X) B
return this;
( a8 ]& c' _2 Y } |