HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, r5 B3 H& \5 v* v( d. W. D$ Y, k4 u+ m" ~5 [
public Object buildActions () {4 z% C; |/ `/ _9 U1 G
super.buildActions();. \# b" Q4 x3 ~2 C3 q
/ n; x% _: N0 j3 p, s K
// Create the list of simulation actions. We put these in7 L- r/ B# X/ j
// an action group, because we want these actions to be
6 \" [+ l( \% E$ E+ u z // executed in a specific order, but these steps should0 H, q/ m3 j6 ~' P' V
// take no (simulated) time. The M(foo) means "The message. T; Q7 k' Q: v9 b
// called <foo>". You can send a message To a particular6 h. O) j7 h3 X* g3 o
// object, or ForEach object in a collection.) g, p0 v) C8 u/ d% u- ?- W
: J) m. t4 c" d/ M! U7 G // Note we update the heatspace in two phases: first run
9 m( J& U3 m, O6 ~! T // diffusion, then run "updateWorld" to actually enact the
( g* V( ?. ^3 E3 a2 K- q% f // changes the heatbugs have made. The ordering here is& b0 U" P5 H6 s; `; Z0 d
// significant!8 F' J2 g9 {2 k5 E" ]" s: f% X
3 b. j! m9 x+ T/ A2 r) h; M7 d
// Note also, that with the additional
: O7 C2 ?- M7 j7 M* m // `randomizeHeatbugUpdateOrder' Boolean flag we can7 W2 l/ E. G: |' h/ P/ K2 o/ t6 s
// randomize the order in which the bugs actually run+ l) Z5 X0 ~* h% D
// their step rule. This has the effect of removing any
% y; c7 X* Q8 B9 e- E // systematic bias in the iteration throught the heatbug! l) x1 J! E. a4 l7 h( I
// list from timestep to timestep5 H: H3 T6 F1 c
4 ^# R# m8 v# P& `7 u( ` // By default, all `createActionForEach' modelActions have# q) U7 ~" O5 j
// a default order of `Sequential', which means that the1 O7 O1 f' f5 \1 p5 t6 ]- Y
// order of iteration through the `heatbugList' will be5 x) w5 X J$ G* w
// identical (assuming the list order is not changed: ^# H9 Q5 u. X' }: b! ^
// indirectly by some other process).
% e3 p% D6 q S4 X; ~9 m9 l
1 E% D) v/ O. o0 I% Z e2 a9 u modelActions = new ActionGroupImpl (getZone ());" `6 p, Q/ N& d5 ~1 l- S. O# q. q. j
, ]" x+ P b" f: a7 k- D
try {
' b4 E8 z% i+ k } modelActions.createActionTo$message/ r% J+ Y" t! O' A/ U; ^
(heat, new Selector (heat.getClass (), "stepRule", false));
4 ^- Z# w1 p7 O8 c } catch (Exception e) {! A9 N- ^' w8 A3 q$ ~ I! j
System.err.println ("Exception stepRule: " + e.getMessage ());
) D4 k# T6 Q5 z8 p }) h) w! { \/ t" K0 T; ^2 S
, d. |3 U M. c' W3 y
try {
5 n. ]& T% h. ?' m" A6 ^$ p Heatbug proto = (Heatbug) heatbugList.get (0);) A" ?# ^ o/ v# Q( h8 y
Selector sel =
, z3 F% q/ |: X% l) ]& c3 o3 X new Selector (proto.getClass (), "heatbugStep", false);
6 C# |( N, m0 ~$ ~. T- w actionForEach =
* f! ?$ a% {$ C2 F9 T0 F modelActions.createFActionForEachHomogeneous$call8 w$ e5 v- N' a
(heatbugList,
" Q& g2 F# O; C q. f$ D new FCallImpl (this, proto, sel,! a) o( }% a1 e2 n4 g
new FArgumentsImpl (this, sel)));
: x- ?% t. X7 s } catch (Exception e) {+ r+ D& J6 u$ z8 R `9 D7 \
e.printStackTrace (System.err);
: \5 C N. S0 k' u8 @: K7 r0 ` } T5 j- C! \) V
4 z( U9 J& |- R9 q
syncUpdateOrder ();
1 p0 V* K, m/ l3 \. C( n8 c. c; b' P# \1 S& y6 ^2 P9 @
try {
" E" W3 Y9 N7 q: m5 C- }3 Y9 ` modelActions.createActionTo$message
" P1 i5 _& G! T6 O% \# d (heat, new Selector (heat.getClass (), "updateLattice", false));
% v* v6 z" {, Y1 p4 V. ?6 W6 u( X/ v } catch (Exception e) {/ f. L! X4 H" H. S
System.err.println("Exception updateLattice: " + e.getMessage ());- H( E; A1 j6 Z" G* ^
}* e9 Y" _ V! ?: v
2 c# v" Z" X4 C- v! m# ?
// Then we create a schedule that executes the8 s, ?8 [4 w" D. N& C, `
// modelActions. modelActions is an ActionGroup, by itself it
' A5 n& G: i3 w) f // has no notion of time. In order to have it executed in* P" n9 |( D1 {9 w# Y2 M* Z X
// time, we create a Schedule that says to use the
" ?' B6 ?: w. U# p% N // modelActions ActionGroup at particular times. This
5 u' _2 k0 y3 ~4 u // schedule has a repeat interval of 1, it will loop every
4 }( `; I& F* R) Z/ x // time step. The action is executed at time 0 relative to
+ {1 B/ ]3 t6 ~$ g // the beginning of the loop.
J' w( y, C& L9 g6 t5 Y7 j5 _ w9 ?9 }
// This is a simple schedule, with only one action that is2 u2 i. `( w& H& |, c1 p
// just repeated every time. See jmousetrap for more' h" Y; ^# `3 Z# g' l
// complicated schedules.
* b- ` P9 a- b" ]( n , L; ^/ U- l, A- z! ^5 \
modelSchedule = new ScheduleImpl (getZone (), 1);
8 S) h8 J) C; O2 n; V X" z" f! B# \ modelSchedule.at$createAction (0, modelActions);8 E3 O' |7 Q" V# a# c: G
7 X$ |* I! X( T) K: L return this;
( ^: f/ L! u6 @/ T } |