HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, {6 j* |" @/ {7 L1 y# x. E B$ _
8 d$ s4 ]7 h; { public Object buildActions () {
! i( m# O9 X) V super.buildActions();
/ [4 R8 N+ W8 z4 s- ], Y
7 b$ E4 E* J0 g' _! k H // Create the list of simulation actions. We put these in
. m& m( n2 Y5 _1 M // an action group, because we want these actions to be" h8 I- n5 A9 v
// executed in a specific order, but these steps should
! G/ n, j9 N0 T7 D // take no (simulated) time. The M(foo) means "The message$ ]! {+ q' C. `1 _8 x8 V8 f j
// called <foo>". You can send a message To a particular
% p @1 W2 F3 Q" H5 b // object, or ForEach object in a collection.
2 c) U! Z% T5 V" M4 `- E z3 m; q, F$ |* G0 q+ z; S
// Note we update the heatspace in two phases: first run- g+ g. T" W! p `3 d! V
// diffusion, then run "updateWorld" to actually enact the, T" r: x* s* a) M3 B
// changes the heatbugs have made. The ordering here is
& G4 a* G( @, z) M8 ?0 y // significant!2 j6 q+ L) i( m0 H Z* m. q
6 @* h' G4 K6 N# ? // Note also, that with the additional! `4 }. M/ x( l/ J
// `randomizeHeatbugUpdateOrder' Boolean flag we can8 ~& ^ M4 e6 J2 r7 Q
// randomize the order in which the bugs actually run% n. ]+ z' d4 Y
// their step rule. This has the effect of removing any2 n# v! p4 _9 f
// systematic bias in the iteration throught the heatbug
- Z, `7 \4 R" S' h9 L // list from timestep to timestep* p; _- [+ Q6 y$ A8 _. V3 _
3 n6 f, G/ e6 n) a( G: P* Z // By default, all `createActionForEach' modelActions have
% D8 n5 w+ r7 e0 ?1 u // a default order of `Sequential', which means that the
' `0 B5 G. P9 H' [0 } // order of iteration through the `heatbugList' will be7 t; k: a. v% X2 _ l# q3 ^
// identical (assuming the list order is not changed
7 Y& L$ `0 Y0 s# Q // indirectly by some other process).
$ B3 l5 b! A. K/ y3 N. q
) g* U8 B- B5 F: }1 N2 l modelActions = new ActionGroupImpl (getZone ());
" n& I4 N2 @2 Z o2 \, t! J9 \3 d. L4 g/ {4 R
try {
2 ?: [% o W4 a modelActions.createActionTo$message
: h& |$ `/ B& y" {+ j& _4 p (heat, new Selector (heat.getClass (), "stepRule", false));% L7 G( T/ o0 A. v1 c% F
} catch (Exception e) {
* j+ |! f5 i* Z System.err.println ("Exception stepRule: " + e.getMessage ());# \/ M3 D. Q2 p" O
}
2 y6 {1 B/ }( _/ T7 ~" v" P: y' P3 P/ }: i
try {' X Y7 y; Y q6 `. ^$ v |/ A' H
Heatbug proto = (Heatbug) heatbugList.get (0);
. @4 u }: @7 x) `& n Q A9 h Selector sel =
2 g* L# c2 l/ V+ A new Selector (proto.getClass (), "heatbugStep", false);
3 U% Y- ?) G( B6 q O actionForEach =) _' r$ C! u1 Q: G0 {1 H, m+ L
modelActions.createFActionForEachHomogeneous$call& [: W7 l" t" K4 u
(heatbugList,
( v+ `- U' x! V" N+ w4 i new FCallImpl (this, proto, sel,) d8 M7 O9 Z5 S# B, e. _0 G
new FArgumentsImpl (this, sel)));
5 J# h5 q9 K5 v/ e } catch (Exception e) {
v: z% O6 _8 V) Q, N. b e.printStackTrace (System.err);
1 q L) S2 \& x x" v/ h F O }
! M+ y' G/ _- A
% P. c2 H0 e, Z syncUpdateOrder ();6 N6 D0 s2 r- V" p( u+ X
+ Y: _; @$ H7 E y" p
try {
% l* {/ X: D6 a/ }6 y modelActions.createActionTo$message 6 K) Q4 I i8 m( u9 T
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ Q* j& e0 l) a) q' a* Z+ a } catch (Exception e) {* P) w& Q) y' @
System.err.println("Exception updateLattice: " + e.getMessage ());
. K A) ]( h% U+ u1 \ }
- a6 P# l; U2 k1 h8 u % v/ Y0 T o' }# R; N0 y. G
// Then we create a schedule that executes the
4 {# l6 W& ^) E8 T2 e // modelActions. modelActions is an ActionGroup, by itself it
% x U3 r/ [- J // has no notion of time. In order to have it executed in% h1 W L2 Y7 `
// time, we create a Schedule that says to use the& l/ s4 a/ _5 Q8 C S6 S* @
// modelActions ActionGroup at particular times. This% H- Q5 g! m3 \. f( E
// schedule has a repeat interval of 1, it will loop every. R8 V7 J; E+ t' z: K
// time step. The action is executed at time 0 relative to
- G( R% F: w* m+ N* n) a4 ] // the beginning of the loop.0 f9 a0 g) \' `! ~
A4 J7 t0 ~/ ~, y# ~7 ^3 q // This is a simple schedule, with only one action that is: T( O( J0 A: V0 M
// just repeated every time. See jmousetrap for more
. w$ m# ^; t6 E/ R' R ^ // complicated schedules.2 f2 a& h8 S5 X1 E- N9 |
, V0 u1 }- E* Z( R+ ]3 X x9 u# {
modelSchedule = new ScheduleImpl (getZone (), 1);' I2 r+ b+ W# c( z8 b4 `
modelSchedule.at$createAction (0, modelActions);. ^( g8 p6 Q7 z/ V. P$ T7 s4 o0 A
1 ]2 K/ X* K' x, L* u+ v6 N, p
return this;+ z" \3 d- J1 h# b. o$ r
} |