HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 {, l) S/ G4 d/ W
( \! K$ p( @7 y6 i' a9 M% I1 A public Object buildActions () {' j) U3 Z, d: Q+ A7 L
super.buildActions();
z0 `+ Z3 G) I* u, }
" m1 l( V8 p U& D4 ^1 y. l // Create the list of simulation actions. We put these in4 r& m! b/ a4 Y7 F1 w; d# m
// an action group, because we want these actions to be
8 d. `1 }. ]# T y // executed in a specific order, but these steps should
: ?. g! n5 q, i6 M) _0 C. N# L6 R // take no (simulated) time. The M(foo) means "The message
0 B. X) c8 i$ [) b/ B/ I! u# { // called <foo>". You can send a message To a particular6 Y) J2 r0 n& V7 N3 Z# W& y- {0 k
// object, or ForEach object in a collection.; N! v# B3 M4 E7 X: h
" L, o( C- O( X1 }) \+ \2 ` // Note we update the heatspace in two phases: first run* q; Y' O8 R6 x5 [0 j
// diffusion, then run "updateWorld" to actually enact the! p$ l8 k3 x1 S, `, M# H
// changes the heatbugs have made. The ordering here is: J3 e! \1 T6 r) q9 x6 s8 t
// significant!( A0 i# r3 j1 z2 |8 j3 R# s
. {+ {# b5 P" K
// Note also, that with the additional' N( C3 \, Z2 u( H; [1 P
// `randomizeHeatbugUpdateOrder' Boolean flag we can) ~$ J. }! K8 X. y* k: a
// randomize the order in which the bugs actually run6 r* ]$ @: j8 l! A8 A4 f% L) w
// their step rule. This has the effect of removing any
- r6 B v; m# R* G$ @ // systematic bias in the iteration throught the heatbug% [: N' M' G; J- `* T9 r X
// list from timestep to timestep5 @' _2 v" i" J+ {6 T2 L* Y
+ a6 J ~: C! y9 e6 c! s8 O // By default, all `createActionForEach' modelActions have
, c0 C$ }% q: u; h" Q; a/ [ // a default order of `Sequential', which means that the
+ @0 |$ T0 x/ t% \& t* R // order of iteration through the `heatbugList' will be
: B3 W, Q) k3 F/ v // identical (assuming the list order is not changed& u, b. p) [/ Z0 m3 p& z. E
// indirectly by some other process).
% V& L( j3 {3 ~' K7 K' o
! {8 z7 H8 C. I" U+ Q4 z modelActions = new ActionGroupImpl (getZone ());+ G! @! d1 f" p' ?0 o; ~2 c
4 j8 N1 I) w! X# v
try {
. A) y. s5 U! u2 S2 } modelActions.createActionTo$message. ^. B( q. }' f
(heat, new Selector (heat.getClass (), "stepRule", false));. V0 \2 x2 g% U1 r
} catch (Exception e) {
4 m4 F+ a0 f/ ^! o System.err.println ("Exception stepRule: " + e.getMessage ());
6 F, P' r- J7 X. g3 E7 B: F }
+ V% J' ?/ X0 {! k7 `0 O) ]! V" J8 y/ E$ T' N
try {
0 D$ M: }6 [, y) P5 t Heatbug proto = (Heatbug) heatbugList.get (0);0 S$ A! J) E' a! H$ U
Selector sel = " @" k) @; Q6 {8 A
new Selector (proto.getClass (), "heatbugStep", false);
3 V9 m* J0 z: Z" T6 c5 F/ P actionForEach =
+ S9 I, ^" F$ U- _* b! Z/ ]$ O4 ] modelActions.createFActionForEachHomogeneous$call7 k, v" u# h, c- X
(heatbugList,! _# z2 y* D1 J# M! |# t% \
new FCallImpl (this, proto, sel,
: ~4 r: {% i$ @# w1 D new FArgumentsImpl (this, sel)));, j' v/ X+ c9 y# Y6 X
} catch (Exception e) {
" o( `$ {& ?+ `; m: L e.printStackTrace (System.err);3 S5 l) w: G* w7 f, B7 o8 a O
}+ w! ~) H8 e1 ?; X ~
$ A s5 m+ c# i1 O: S6 g7 P syncUpdateOrder ();
. b( w' `8 c, k' J# g7 l* q% H1 r" a
try {2 |1 X. X& d, x
modelActions.createActionTo$message 6 }. B7 M8 ^- u0 D7 k+ g# d
(heat, new Selector (heat.getClass (), "updateLattice", false));$ l1 \' V; h8 t$ }9 V; U( }0 @6 S
} catch (Exception e) {
6 I. Z2 h& j4 F H2 R% T6 }# I System.err.println("Exception updateLattice: " + e.getMessage ());
! W# d# T Z5 F+ a- P }: k8 I7 N8 V; f! i, P
$ ?6 i( A: A' A3 g: i
// Then we create a schedule that executes the D; K. z H, m' [
// modelActions. modelActions is an ActionGroup, by itself it
6 _. T# t p3 Y4 a- { // has no notion of time. In order to have it executed in% @/ }& d- g1 c' s# a+ I
// time, we create a Schedule that says to use the
: r) `" W4 ^ g3 I2 D) t% }0 W // modelActions ActionGroup at particular times. This1 Y. K6 w) [/ D9 b' N# u
// schedule has a repeat interval of 1, it will loop every
$ U3 y) q' U' ` // time step. The action is executed at time 0 relative to7 M" T2 J/ c) z6 H7 O2 ^
// the beginning of the loop.- v; h+ ^2 e1 S) [
& ^9 G; C" ~; Z8 H% | // This is a simple schedule, with only one action that is% }! C: _( i0 V8 Z( _
// just repeated every time. See jmousetrap for more- T8 h* \4 W; q3 \7 w
// complicated schedules.
5 h4 _; ?9 Z: ]4 i# \" e ( E2 x- S) _1 A1 i$ b% t5 c
modelSchedule = new ScheduleImpl (getZone (), 1);4 N9 [* p/ C& x# x9 F' J
modelSchedule.at$createAction (0, modelActions);
' p! O# X! C) r M. ?! ?3 F! C
; R: Y1 a8 p1 X5 I0 F, o return this;
* @2 ^$ i( [& n- ~* ~0 Z* ?# w } |