HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 ^& G8 L, n. f8 ^7 N; v$ z
7 {0 X3 Q7 l' c1 a6 q/ m2 e4 D, T
public Object buildActions () {
' G% O2 }. Q v( K1 f super.buildActions();
# x) d$ _/ B/ C* Z
4 |. z! H/ R! p6 F0 z j // Create the list of simulation actions. We put these in
7 i7 A# ^0 z6 t4 H. G& s! W( @' B' F // an action group, because we want these actions to be
) j* {' H1 O: e/ r- r" u // executed in a specific order, but these steps should
6 b1 k" s7 g: N; e. b // take no (simulated) time. The M(foo) means "The message
6 m: o* F) k7 R$ o( N4 z // called <foo>". You can send a message To a particular
S+ Q: X* X1 @% d# ^+ y, z // object, or ForEach object in a collection.
/ ]8 E4 H' E0 J" D6 n5 ]
# Z* \5 D0 l! s# L- I$ C2 q9 C# e // Note we update the heatspace in two phases: first run n2 F6 V9 G7 l' A- _- m1 w! O
// diffusion, then run "updateWorld" to actually enact the- Y! r% z4 O A' ~1 z
// changes the heatbugs have made. The ordering here is
9 x! W2 W: [' D9 P. v4 i // significant!
" V9 u3 G5 a: r% N, K0 D , L# q& L9 f3 w
// Note also, that with the additional0 y- U5 l2 |2 G2 h3 [
// `randomizeHeatbugUpdateOrder' Boolean flag we can4 `) m: ?* Q8 r* L) n: x I
// randomize the order in which the bugs actually run7 O: \1 `# e4 v
// their step rule. This has the effect of removing any! k( d3 [1 c4 D; j# }, U
// systematic bias in the iteration throught the heatbug
; p. O5 y3 ?& H! B+ p // list from timestep to timestep, m% p: B- {+ u5 o, R
) q# w( _; W, P. t* y- d // By default, all `createActionForEach' modelActions have ~( e8 X2 u2 f1 f% K+ a) M# {
// a default order of `Sequential', which means that the
+ O) L/ R" ~, V; {' C; k- m: U // order of iteration through the `heatbugList' will be
5 n' {, @# m* [; h. Z" T // identical (assuming the list order is not changed3 T' R c# g- V/ s
// indirectly by some other process).
% ^$ s0 C$ e- |8 a ' s q6 f6 ^! D# Q
modelActions = new ActionGroupImpl (getZone ());0 x: \$ N. r) v3 ~- Z
8 V3 D: Z9 n! M: r try {5 |: y! s H0 h1 F8 B
modelActions.createActionTo$message
3 y" @5 Y& F" ~! D (heat, new Selector (heat.getClass (), "stepRule", false));
. w. I# {* d. L- k6 U: | } catch (Exception e) {: e3 m5 D$ ~. H t. w3 _
System.err.println ("Exception stepRule: " + e.getMessage ());5 e5 N( V" S- C f! S
}* @7 e$ F" Z7 o1 T. u, T7 \
- \: t3 n0 x7 I, S& Q
try {
i; w1 F% Z7 s7 N7 a Heatbug proto = (Heatbug) heatbugList.get (0);
9 K& p* U+ c; V1 { Selector sel = ) A& F& v# H& j/ y! R- \- z. t
new Selector (proto.getClass (), "heatbugStep", false);) Z2 a& F9 X; Z% g# Y3 M7 g! H
actionForEach =. j" K3 g8 O- [9 N+ R( v( E% f+ H
modelActions.createFActionForEachHomogeneous$call8 R, ^4 }% j) h5 x8 d/ z
(heatbugList,6 l+ w* I7 q% a1 Z
new FCallImpl (this, proto, sel,1 T0 r6 h! q2 s3 ]6 t" {
new FArgumentsImpl (this, sel)));
9 ?% r: {5 C+ v7 A+ b } catch (Exception e) {% b, |# M u6 X+ c: N4 o
e.printStackTrace (System.err);
0 _& ~& o0 F/ G ^( ^" g }) v* y; Q# H& n# |
1 I8 X+ i0 z! T# j4 n1 R syncUpdateOrder ();. u+ [& m) s# n
- u1 R; t+ f& n/ }
try {8 O& e0 V; J, u+ ]" C3 m
modelActions.createActionTo$message
' b' v7 c. \7 h+ E9 P* L (heat, new Selector (heat.getClass (), "updateLattice", false));
2 l. d( r) T$ k( ]# H0 R } catch (Exception e) {# H0 p/ }% ~( i4 Q
System.err.println("Exception updateLattice: " + e.getMessage ());0 e( x) x% Q7 P3 @. H
}
$ l1 K' i& s! @1 } 6 u5 F6 f' a" Z
// Then we create a schedule that executes the5 U# [& m" R' I- W
// modelActions. modelActions is an ActionGroup, by itself it
2 k6 ^& L- {3 p4 I# }1 B // has no notion of time. In order to have it executed in. e' c9 P) n0 p4 n7 b
// time, we create a Schedule that says to use the
4 i4 C! ]1 l" w9 M# R5 [8 w' n // modelActions ActionGroup at particular times. This
; W& z' e% t) m0 ]. N% u. e // schedule has a repeat interval of 1, it will loop every4 V/ I2 A6 w0 e1 W
// time step. The action is executed at time 0 relative to
" `; B: d- {, x; u8 a! ` // the beginning of the loop.( _& C8 t; f' L$ J0 H
( d" ^3 t/ i5 |% Y1 p" Q // This is a simple schedule, with only one action that is7 {: j a" X0 x6 I- a' s2 z
// just repeated every time. See jmousetrap for more& I% B3 ~! v! J0 _& |& |
// complicated schedules.
) I$ W+ G7 }1 s, f( h) J7 m3 X" [ 3 g8 L4 N; I* R! H4 G2 \
modelSchedule = new ScheduleImpl (getZone (), 1);
0 l! U( S/ s3 _. Z, [4 ~ modelSchedule.at$createAction (0, modelActions);
0 e' P/ G! `7 m+ C. Z- V7 Q* ?
% Q' z5 M! X2 _; X" n" ~+ p- ] return this;
1 b# N+ _8 {% ? @+ c. G7 k8 d3 V } |