HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ {( `1 j4 W$ t; A# b" E+ ^9 ?* Y# p- c
public Object buildActions () {& ]. L) E9 |; k3 v. M
super.buildActions();
' I* h# j+ m. v2 G 4 _* ]! O- @+ h9 r3 \* o
// Create the list of simulation actions. We put these in
/ d& }9 @) e4 D0 \ // an action group, because we want these actions to be3 K. s& ^# `- Q6 O# I9 K4 j9 ^
// executed in a specific order, but these steps should+ }. B7 q7 m% x/ t F
// take no (simulated) time. The M(foo) means "The message. {" ~" y; P1 L3 ]& R
// called <foo>". You can send a message To a particular
9 h# x3 ^! A9 f ~7 r5 m) @1 c" D! c // object, or ForEach object in a collection.* s$ @% B: q1 x W7 \" h5 N8 L
: y' @. x% F, U: R // Note we update the heatspace in two phases: first run
: ~9 t( q9 Q& F, ]3 G: m // diffusion, then run "updateWorld" to actually enact the% w# V7 ~' o/ J& X/ q
// changes the heatbugs have made. The ordering here is6 m0 d+ K/ R- r
// significant!3 k ^; w9 J2 y- V6 w( g
1 L+ H! B" Y; C // Note also, that with the additional3 g* ~. D( n. v I. q- I
// `randomizeHeatbugUpdateOrder' Boolean flag we can" W+ z) t4 p# B: ^& J; @4 |
// randomize the order in which the bugs actually run
5 `+ z0 S! L6 R0 f+ z // their step rule. This has the effect of removing any
0 c% G) a4 p0 g% }: V& r! M7 @ // systematic bias in the iteration throught the heatbug
C9 Q% x6 d! o4 W9 Y2 v' D% t // list from timestep to timestep
N, ~7 [7 Y* o/ M" c" r, k + r' L3 Z5 O% W7 c; E; A
// By default, all `createActionForEach' modelActions have
4 r& o' W$ b% ? // a default order of `Sequential', which means that the, X% ~0 m @ ?2 _
// order of iteration through the `heatbugList' will be# E$ s5 z; k" _5 C4 Q
// identical (assuming the list order is not changed! D# ^6 F k% z, Q8 {& a R
// indirectly by some other process)./ W; B( Z3 f2 Q5 ~( U' ~& R
7 A, ]5 ?9 X% M modelActions = new ActionGroupImpl (getZone ());4 |: V3 p7 E* g0 z) U7 V3 T; o
) }! }* G2 D, {" t" h9 o
try {" ~0 I# _$ @- h4 W. o+ D1 f7 q p* v9 a3 v
modelActions.createActionTo$message3 O q% ]. D4 i* j- X2 w! C7 D
(heat, new Selector (heat.getClass (), "stepRule", false));
% A; B% }' g0 I9 p) ^2 e } catch (Exception e) {
/ C0 }4 a; s' X8 r* R" c3 f System.err.println ("Exception stepRule: " + e.getMessage ());7 M9 @) K" p: e1 S$ f6 z5 E
}9 \' K( X4 k; F- `
4 g& [3 `3 {) Y5 H) |1 q
try { e6 n7 ]7 J- ~. m( i
Heatbug proto = (Heatbug) heatbugList.get (0);; U8 W2 |% M2 m- \: w
Selector sel = * t. A" M% `1 Y$ o4 D. j, M" M! _3 H1 W
new Selector (proto.getClass (), "heatbugStep", false);
+ i @. h+ }0 \& o- n actionForEach =
6 Y$ w& J/ S! q6 n0 K# j modelActions.createFActionForEachHomogeneous$call) E% n' G+ x+ c% I+ ^
(heatbugList,$ g$ {, P+ `0 M, w/ b* d
new FCallImpl (this, proto, sel,
8 ` n+ S/ J" C+ S new FArgumentsImpl (this, sel)));
9 O Q$ u+ |4 f8 u; @ } catch (Exception e) {0 r* l, x' I5 M7 B- U+ o
e.printStackTrace (System.err);
1 V0 n% \. g0 W- F. W# Z }+ `8 B) K) E" v& f
! R( r) C! X2 Z9 s+ N! S f6 U syncUpdateOrder ();- l& N' T, A3 D q8 r" k6 L# p
7 M4 S3 v2 b$ y" L1 U try {
$ C- f E" T4 U+ ~ modelActions.createActionTo$message 8 g/ p6 B' o; G5 F) v( V4 C
(heat, new Selector (heat.getClass (), "updateLattice", false));
$ N8 _' g. j q. Y } catch (Exception e) {
7 p* M# S/ H7 o3 z System.err.println("Exception updateLattice: " + e.getMessage ());
" N" g0 D: t' @9 s- N, U' a/ j3 x& S4 q }
) L" [2 U) d# l( T* Q 5 t. a8 K0 k% P# p) [
// Then we create a schedule that executes the! {5 y0 P) L: E0 C' D2 R% _
// modelActions. modelActions is an ActionGroup, by itself it* B( p' P% u0 ?1 o5 I; v9 b
// has no notion of time. In order to have it executed in. a5 m/ E( O) d* Q
// time, we create a Schedule that says to use the
* ^) f3 Z0 |, ]0 H! x7 S // modelActions ActionGroup at particular times. This
8 b' P5 l1 q! k4 A) Q& D! ?' |3 j // schedule has a repeat interval of 1, it will loop every
5 B. ^7 h& T% {/ | // time step. The action is executed at time 0 relative to
F% I4 G5 Z7 L+ u" W // the beginning of the loop.0 A& p- R' \# X
) O4 P0 g6 i$ C
// This is a simple schedule, with only one action that is
' q) t' ^; i! y: f1 y/ {( Q; Y // just repeated every time. See jmousetrap for more/ A! G4 p8 z% i8 [
// complicated schedules.
k. w3 S3 f! j9 c
8 G2 p# Y: ]$ P; T$ C1 ~ modelSchedule = new ScheduleImpl (getZone (), 1);
8 q& U7 k( v- `* g+ H6 O6 _ modelSchedule.at$createAction (0, modelActions);5 t C3 x, [6 y
1 |3 y6 i1 ?: i. w return this;
) [! t4 ~8 X9 D% E% a7 o } |