HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 d$ ]' I2 y! m! ^) h7 l0 A( _& h% K# e# _$ F8 k: ^
public Object buildActions () {
& k: A. W' `1 G super.buildActions();
# d9 C1 G7 i$ Z$ |( [
$ `$ w+ {8 Z F4 A1 C. l // Create the list of simulation actions. We put these in
# |$ x! x. D4 q, k9 A: A) y1 R8 P // an action group, because we want these actions to be& \% } }6 X! V, h2 u3 ]% R- e
// executed in a specific order, but these steps should
8 @* ?8 P) g5 R& | // take no (simulated) time. The M(foo) means "The message! M& I1 b0 Y+ _/ ] L
// called <foo>". You can send a message To a particular9 n; y- h& l$ T' a; R0 `% N5 R, M
// object, or ForEach object in a collection.8 ?" r( x( s; _1 t
" E3 _3 U1 g+ Z7 ^/ m // Note we update the heatspace in two phases: first run9 u4 h& K% ~8 S; o; \3 Y
// diffusion, then run "updateWorld" to actually enact the8 x" J1 @' t1 a' Q
// changes the heatbugs have made. The ordering here is
) t% ^! i7 j$ D! Y // significant!
" P' M9 A, z( g4 J, Z5 U+ B1 o ! d7 J a) l3 |5 L: Q
// Note also, that with the additional B' n% p5 }; |9 y. `
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& |+ O0 a7 b( ]" b# h/ r // randomize the order in which the bugs actually run; D4 ~% n. g! {: f
// their step rule. This has the effect of removing any, w' U3 D6 x9 V' ^! \- l/ V+ o
// systematic bias in the iteration throught the heatbug0 _" V; b" {0 r% b
// list from timestep to timestep) m/ X+ L' `( t5 d1 P
! A; g+ r6 m7 \- I# \; G+ d# {: t
// By default, all `createActionForEach' modelActions have
& p; K1 ^% n2 n7 H // a default order of `Sequential', which means that the3 t& a8 d P4 n: P
// order of iteration through the `heatbugList' will be: J# w! s' t$ I6 K" E) i0 ^
// identical (assuming the list order is not changed7 M& r" _ R- h# i' X) j
// indirectly by some other process).$ Z6 [5 q6 a( Z5 k! t: f
/ C" y( }( h4 ~& l. b6 J modelActions = new ActionGroupImpl (getZone ());
8 I' X1 ^, ?6 z
& A: Z; R' j0 `0 g) R, [5 v6 l try {
1 `2 z. H: s0 F! p3 C- K! Y" T [ modelActions.createActionTo$message% E7 b( q- U% X9 z
(heat, new Selector (heat.getClass (), "stepRule", false));
F; z" m0 D4 C/ c C } catch (Exception e) {9 e5 A4 R! K" S( e& h( C
System.err.println ("Exception stepRule: " + e.getMessage ());
4 ?2 e! \) L. s: K0 r }
% S; _+ P! y6 e2 @# Q2 g' A D
/ z! R$ b1 V( r) @ try {
/ N) T2 G8 k# I Heatbug proto = (Heatbug) heatbugList.get (0);4 ^" I. E, f$ s2 p3 N, ^
Selector sel = $ y/ ^8 Z& x) F8 s6 P
new Selector (proto.getClass (), "heatbugStep", false);& e+ j9 Z' @* b% T, \
actionForEach =8 r6 R# i8 s$ b5 u2 G/ n- o+ F
modelActions.createFActionForEachHomogeneous$call7 K& G; W+ Z' p$ R
(heatbugList,
6 y8 ]* Q& i h5 m6 _# k new FCallImpl (this, proto, sel,6 X+ M0 n% A5 R8 }) `
new FArgumentsImpl (this, sel)));
& t" r& d7 U* q& o3 A2 D7 z% d" a } catch (Exception e) {( }5 L E/ m2 R8 F6 b# i
e.printStackTrace (System.err);
1 R& E- _, \) `* _ }
y) {6 t# u$ ]8 S
$ b" j! e r0 t( ]+ Z syncUpdateOrder ();
6 A' \3 g) `* t, O
. c! L: A: ^6 Z8 ?8 ] try {0 @1 ^# K2 J w! a7 o2 x8 I! Z
modelActions.createActionTo$message / t9 y, t' s' w$ g1 r8 v
(heat, new Selector (heat.getClass (), "updateLattice", false));: n3 U0 _2 V2 z3 K6 T/ F
} catch (Exception e) {
, s' ?% o+ ~, w' d5 d. D: H System.err.println("Exception updateLattice: " + e.getMessage ());2 C3 U) U. v. ?0 W" B
}
- D6 d$ D% B( n* G4 M7 A" E
- l! I- L6 c% ^ // Then we create a schedule that executes the
& a$ m- ^) J- U2 u // modelActions. modelActions is an ActionGroup, by itself it+ V" S& i" p% R5 L) b
// has no notion of time. In order to have it executed in
4 c/ N0 T& M9 j+ D: [+ j5 T // time, we create a Schedule that says to use the4 @) H( ?: R% C7 z4 v& U5 T
// modelActions ActionGroup at particular times. This
6 j/ p$ t6 q7 P6 | // schedule has a repeat interval of 1, it will loop every
8 u6 s g4 L. j2 L) a0 j2 x // time step. The action is executed at time 0 relative to6 O& D8 M; S; o- A# c" y
// the beginning of the loop.
$ D; K, c4 I* g9 p: W I d0 c3 A6 \# p. g$ \/ Y1 b
// This is a simple schedule, with only one action that is B" R8 g. W1 X$ e
// just repeated every time. See jmousetrap for more
9 x9 \6 P) u. H' |- P* f6 v, ? // complicated schedules.
6 M) h# `0 @$ s7 X4 H4 m. F/ h 7 ?5 P( g& Y; B, N: Q; ?
modelSchedule = new ScheduleImpl (getZone (), 1);' t0 H p& K& A8 ]$ l
modelSchedule.at$createAction (0, modelActions);/ o9 e: H' f' ]' U7 _( w6 z
: A+ I0 f% J0 a4 K3 `3 N" R1 {3 Z
return this;3 _' w9 K" Y0 R; ^: W& Q. k1 r
} |