HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* e: {5 U8 x. h) K1 C7 L: H
3 }. A/ [$ s& g0 \- w
public Object buildActions () {, K9 m { p: d! C! T; o
super.buildActions();4 Q* M" ?% ?# g0 G
7 [% B9 u0 w# N/ q+ c
// Create the list of simulation actions. We put these in( Y% W g7 u. l2 \& o8 Y& E
// an action group, because we want these actions to be1 d/ Z& m3 O5 \
// executed in a specific order, but these steps should
4 z# y1 I' x h4 P0 w // take no (simulated) time. The M(foo) means "The message' @- B4 Q. W* ]% d& N1 I
// called <foo>". You can send a message To a particular1 L4 r( H0 Z0 q) k3 X: Z, Q* y7 B
// object, or ForEach object in a collection.+ R% `1 n0 c3 k# l7 Q7 ?
( S |+ e2 Q+ s- H7 e) T // Note we update the heatspace in two phases: first run; w: V5 F9 o/ ` k+ S) _9 M) E7 \
// diffusion, then run "updateWorld" to actually enact the. V/ K' ]. [1 ^8 S" C
// changes the heatbugs have made. The ordering here is
; h+ l4 j. ~# @. p$ J& \6 a // significant!: l* b$ Y% G* V( k5 _* ~% b
2 Y: f. o- t( y: @6 M9 N
// Note also, that with the additional/ {; c% k% s9 ~( x' V+ q3 q
// `randomizeHeatbugUpdateOrder' Boolean flag we can) D+ P8 K/ L) w/ _2 q* l
// randomize the order in which the bugs actually run
) t: ^* e2 c) s8 W0 A1 ?8 v // their step rule. This has the effect of removing any5 Y3 k, u0 z+ Q, A% B
// systematic bias in the iteration throught the heatbug
0 ^1 B7 l8 l4 w( ~4 b% D // list from timestep to timestep' q: n) k* r" S8 o9 W
) `: E" ?% m1 y // By default, all `createActionForEach' modelActions have
# V) m. [3 t7 `3 C* R- m // a default order of `Sequential', which means that the
7 f- a, f" p; }* m# S) T4 g2 _ // order of iteration through the `heatbugList' will be$ L; X( h7 X- L
// identical (assuming the list order is not changed
. t3 G+ V5 u5 `9 m2 x. V2 }' z6 _ // indirectly by some other process).
3 k4 D$ T8 G( e- i: ~/ e
, D# V' S7 ]2 \$ H# q8 s- n modelActions = new ActionGroupImpl (getZone ());9 n( y- [) y+ u9 r, D* \9 j0 @
( m' S* x0 M& R# @. J6 {
try {
, f7 h! {0 ~0 ] M1 R% u( X0 d6 Y5 V modelActions.createActionTo$message8 b+ K' Z3 ]- N6 u
(heat, new Selector (heat.getClass (), "stepRule", false));+ w( i4 h d, u
} catch (Exception e) {
; J3 W& m' M, {6 ^6 j w System.err.println ("Exception stepRule: " + e.getMessage ());
5 w2 Y& o9 c- p" i }
# w1 Z J1 ~2 P! J. [- X1 L
% @+ J* a$ i6 [& f; a+ Q% m try {
" q9 k! q6 c! a4 e( o Heatbug proto = (Heatbug) heatbugList.get (0);. A1 Y( e9 |3 p* t
Selector sel =
4 a. P7 s* z) A& e. t/ W new Selector (proto.getClass (), "heatbugStep", false);+ E z8 A& P" a9 G' i7 |3 J
actionForEach =
% E) X4 `2 j! i modelActions.createFActionForEachHomogeneous$call! k: h8 F) h9 c+ u) Z
(heatbugList,
9 }$ m6 s1 k2 L3 {) U new FCallImpl (this, proto, sel,: b0 y# e1 F& A, C- {& [9 B
new FArgumentsImpl (this, sel)));
/ k7 s( p; k; u. A O/ y" @ } catch (Exception e) {) W) I9 @& b& [( g
e.printStackTrace (System.err);7 s* z. P6 f, [7 R1 M6 h0 N* m% c6 J
}
, t3 z: ]# |& f7 J! k# P 9 o& C% ^/ k: Z' i& q. S7 h0 L- \
syncUpdateOrder ();
# T2 d' v1 r$ C- Y7 }1 b$ |8 p" q: O- J7 a8 J3 [$ K' p; A
try {
. o* B8 n0 D5 a) z R* R modelActions.createActionTo$message * ~7 m! F3 Q' w o0 |; {/ n
(heat, new Selector (heat.getClass (), "updateLattice", false));) I- C9 ^4 q& n9 c3 m, u f5 M1 d) |
} catch (Exception e) {; X0 @4 ^/ _0 p
System.err.println("Exception updateLattice: " + e.getMessage ());
A% |! S0 m8 U M8 T$ W }+ [. B4 O6 v$ ^7 U
1 p8 W; h3 j* b m* ^
// Then we create a schedule that executes the
* C9 L' @( Y8 O4 D6 [6 R6 Y1 J3 O // modelActions. modelActions is an ActionGroup, by itself it0 x: m. N# R* q
// has no notion of time. In order to have it executed in
% L F1 a+ R9 Q; d; T: v // time, we create a Schedule that says to use the
N; H- ] }1 C6 @ // modelActions ActionGroup at particular times. This
5 ]( H6 s |+ }3 c // schedule has a repeat interval of 1, it will loop every: a; ?. [0 J% N! e4 S: `! a
// time step. The action is executed at time 0 relative to# C. E3 n% Q; _7 R1 g: v4 p T" R6 D
// the beginning of the loop.. E- Q ?! @* C; Z. e! R
, T( y- W9 n! h% T: J
// This is a simple schedule, with only one action that is
& U; L# F% S; J" E# ^- {) h // just repeated every time. See jmousetrap for more7 R( r' \! \0 N, [& p/ \
// complicated schedules.7 `' l. ?6 S9 C6 U: z
& m% v0 V$ k$ D0 t- d
modelSchedule = new ScheduleImpl (getZone (), 1);+ T* N/ a7 k2 V) s& h! k
modelSchedule.at$createAction (0, modelActions);
1 l/ a& ]( Y: m1 j4 B * W/ [! g- b# P
return this;
6 `* b3 n( S8 x( X. ?8 a$ d3 L# C } |