HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 g8 N) {) g: D% z6 ^( D
8 p8 _6 e* ^ N! w( }; c1 l, U public Object buildActions () {
5 ]0 ^; ? o) |3 j super.buildActions();. d6 t) \ p L5 h5 a
1 a% }1 k' |9 g! }' f* ^ // Create the list of simulation actions. We put these in% a" M S2 B+ F% Z0 b
// an action group, because we want these actions to be" L0 h5 F8 _; v, K
// executed in a specific order, but these steps should* z# s1 @- i7 y2 B9 B. {8 G% b' z' i
// take no (simulated) time. The M(foo) means "The message4 i$ G7 o; v. n0 a9 y% Y2 e
// called <foo>". You can send a message To a particular/ H2 v d% D0 M6 T: Z8 H
// object, or ForEach object in a collection.3 Z) B6 g" `3 T# W8 a& P
4 U5 ^+ C; n; I$ Y' J // Note we update the heatspace in two phases: first run
, v" Y4 y( Z5 i& M // diffusion, then run "updateWorld" to actually enact the$ I. _: [% _' @# B/ U
// changes the heatbugs have made. The ordering here is
$ b; f! o2 I/ l. M& a% X // significant!
7 X6 E' d: K5 r6 j$ r* D, b- ~$ Y5 N , Q# z8 @5 D3 B) f
// Note also, that with the additional3 d* W. a) W {2 D+ C& H' w# Q( {% S
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 K; D9 q: m3 U1 F& O4 r' a. T
// randomize the order in which the bugs actually run
# Z" H8 D: T1 R9 ?- h* ^" `6 a // their step rule. This has the effect of removing any
# r. ?5 }' }- d2 r! S // systematic bias in the iteration throught the heatbug
( L: f$ g8 d% O9 X // list from timestep to timestep
/ D6 T0 n: O4 Z 3 B$ {; n6 c; g! p
// By default, all `createActionForEach' modelActions have
! G! I* Z M4 S2 a @ // a default order of `Sequential', which means that the1 L0 h6 V' Z( P2 y# @
// order of iteration through the `heatbugList' will be
' n( r9 q6 Q5 A( P; T7 _ // identical (assuming the list order is not changed
6 Q, j( X2 I# V // indirectly by some other process).
1 B0 ]4 r$ @! d7 H) D& H) d 7 [$ a; k/ z2 [5 a4 U5 d$ s/ S
modelActions = new ActionGroupImpl (getZone ());
$ [6 j P! q2 z8 \8 J' l7 \1 L) g' i/ t" E
try {) Y* @1 W0 v4 W5 c9 r: h$ D
modelActions.createActionTo$message
9 F6 u( ]9 \8 H) X7 W (heat, new Selector (heat.getClass (), "stepRule", false));/ a% v; q, p' d% ?
} catch (Exception e) {
9 |' K: n/ s2 E" j, c- w6 _ System.err.println ("Exception stepRule: " + e.getMessage ());
* n) }# k p! i8 x0 n }) v9 ]- y, k% T' A, g' f
7 q3 F( Q) C# e try {
+ H" l) ?; H2 |: L6 Q8 \% O Heatbug proto = (Heatbug) heatbugList.get (0);
) w! u0 _! q% y+ q2 e: v3 ?$ v Selector sel =
& P3 i& i; m! m' v+ b8 ? new Selector (proto.getClass (), "heatbugStep", false);: t8 I5 x% @& D5 i9 Q1 N
actionForEach =
% t2 Q+ e: f5 L1 q! A( o0 a modelActions.createFActionForEachHomogeneous$call4 g9 K% K$ D) n8 D- T5 U, w
(heatbugList,7 P6 g, ]) B/ R, I/ S: y& e, J
new FCallImpl (this, proto, sel,+ I p" ?% Y q L% C
new FArgumentsImpl (this, sel)));
3 ^- X% J. i3 K, e2 w+ m. u1 m } catch (Exception e) {; P" U) E. [. s/ L- Q
e.printStackTrace (System.err);
; t- V8 N8 O1 L8 L) Y8 m+ C1 k7 r }3 \: \# `; R1 k1 C2 E
! \7 h, Z; B* x0 \6 C) n& a
syncUpdateOrder ();# n- m' k( @1 ^0 B& D
7 {9 q& _1 s7 i/ H5 n try {; j$ G8 D% N6 o: {) B/ G# f
modelActions.createActionTo$message
4 D; H0 W" h H5 A0 U# V1 g: M (heat, new Selector (heat.getClass (), "updateLattice", false));
) P1 ~. M a4 g } catch (Exception e) {+ y% p$ Y0 z7 a- m
System.err.println("Exception updateLattice: " + e.getMessage ());/ l. C% _, \5 p4 v* I3 \
}
/ w- h, Z+ G0 f2 H4 g& z2 ]7 W
1 _3 x: F+ \, R9 H4 B( P& M // Then we create a schedule that executes the
9 `. u4 L& N; V$ ~( O // modelActions. modelActions is an ActionGroup, by itself it5 e" ^$ h: b# {" L
// has no notion of time. In order to have it executed in
) L2 i! l0 b; c4 @% _$ {! u; @ // time, we create a Schedule that says to use the# z2 {/ g u V/ [6 p' ?) ?
// modelActions ActionGroup at particular times. This
' P' @, r! C: a8 @0 j // schedule has a repeat interval of 1, it will loop every
/ Q s3 ~% T- Y: j- ]) L% c* j5 ~! U // time step. The action is executed at time 0 relative to
& ^8 |3 H! @3 k4 \/ J S // the beginning of the loop.
. L) E( O0 Y5 x& ]- L0 e1 l) c0 x0 S2 Q7 A i8 M8 k: M/ w1 `3 H
// This is a simple schedule, with only one action that is
5 w/ m! E6 v: B1 i+ C( [/ N // just repeated every time. See jmousetrap for more/ t$ d$ w6 F' M8 o+ I7 w
// complicated schedules.
: C. K. P$ o9 G* u8 W ; p% ^( c( N u- U% E: B
modelSchedule = new ScheduleImpl (getZone (), 1);, r v4 s* t5 `$ w
modelSchedule.at$createAction (0, modelActions);
/ }$ X% Q; |) S6 u8 P6 m
. w. a9 _9 t) K6 l: X: U0 ] return this;" B9 I; n% `7 b- A3 V4 l W! r
} |