HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, v4 ?* G9 J, J& y e5 ?: }% f$ E* Q- E( R
public Object buildActions () {
+ V3 M$ d/ ]8 Y+ {' c/ `. d, y super.buildActions();
- N0 \- _4 z8 w- ^2 T4 x+ m + A& V, h# ?+ I
// Create the list of simulation actions. We put these in
- P( `# w! ?' S- k% y6 l- f, a& O // an action group, because we want these actions to be+ b% L# b4 q. {& k. G: S
// executed in a specific order, but these steps should
" N) a! V3 _) X7 ]$ d2 }% E // take no (simulated) time. The M(foo) means "The message; C4 L4 j* T7 U
// called <foo>". You can send a message To a particular
2 h6 p4 D4 @- M // object, or ForEach object in a collection.( s- Y' E( o" f5 ?( M- ]7 P
{: W3 ]( M# x& @5 l/ x% k! A // Note we update the heatspace in two phases: first run/ k- l2 J3 Q# X1 A/ n: q
// diffusion, then run "updateWorld" to actually enact the$ `7 }5 {5 ^2 k; ?# ~
// changes the heatbugs have made. The ordering here is
& V S9 h3 f9 K } // significant!
3 H1 l0 E* G1 P4 t: r( E " J8 D1 L- g& E) `1 f- Y, }; l
// Note also, that with the additional. o- N$ d6 R, l6 c4 O5 R: M" j
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 C/ Q- {6 T, w3 g. o
// randomize the order in which the bugs actually run
1 g1 C. W2 X- O1 a // their step rule. This has the effect of removing any
; U% U: u( D3 p: n7 ` // systematic bias in the iteration throught the heatbug
0 \- @2 }' [1 W9 n; x r, ^ // list from timestep to timestep
- ^' j0 t+ C0 f! N; r$ ?: f ; ]$ {) h: G$ i3 Y$ B' X6 e" e
// By default, all `createActionForEach' modelActions have
% }7 K4 W8 V' g3 l8 ~$ B // a default order of `Sequential', which means that the. M b+ @ Y1 d$ u. L9 n
// order of iteration through the `heatbugList' will be
( p% ~ K8 B4 A4 B" v4 o2 v // identical (assuming the list order is not changed
9 l8 V6 y6 m) r% A5 U( H // indirectly by some other process).: O5 E6 T1 {* S+ g
( d i3 \- R, {, ?' O6 m9 D
modelActions = new ActionGroupImpl (getZone ());7 b* h' R' d6 u) u! | e( u
" f* J/ h8 K# I2 h
try {
* K' C/ C3 \6 v0 F+ d, P modelActions.createActionTo$message
6 w: R d* v; F/ V (heat, new Selector (heat.getClass (), "stepRule", false));4 T K6 m( G& ~7 x4 Z! P
} catch (Exception e) {% Q+ I0 b a Q; i
System.err.println ("Exception stepRule: " + e.getMessage ());
: v9 W" `" k4 A. a/ P! |3 f }, L# O" \ i5 [+ P7 h. o0 O. V8 E
{( V6 K4 a+ i try {
2 w$ J& \5 }% {: }) l% K; {) S Heatbug proto = (Heatbug) heatbugList.get (0);
" a9 {5 u }5 q# F1 v( t/ k Selector sel =
+ M' o" i7 Y2 j) m7 }2 f new Selector (proto.getClass (), "heatbugStep", false);' i+ q8 H1 v: ~* M! W: e
actionForEach =
, E4 r+ X3 u* V2 p6 v modelActions.createFActionForEachHomogeneous$call& a: N2 N* W( Y) H' ^. |4 e; j1 j
(heatbugList,
{! b( V$ J5 Q6 ~) s new FCallImpl (this, proto, sel,
% T- W; P, w2 W$ m5 [ new FArgumentsImpl (this, sel)));; v3 |( t3 J1 }8 U
} catch (Exception e) {
! |. a& ~: i4 p: R; \& v! t e.printStackTrace (System.err);: O6 f( ]/ A* h) k# X- ~
} n8 N2 a) U2 @" `/ ?* Q
0 p1 ]" Q4 d* E9 }$ q0 J. f
syncUpdateOrder (); f7 k1 r2 p6 n3 W# {5 c' a1 y5 U
e: N' \; l3 \7 x% H0 w8 M
try {, ?& Z7 b, Q# V4 \( c! d
modelActions.createActionTo$message _* g1 ~" n) U# M+ y
(heat, new Selector (heat.getClass (), "updateLattice", false));% T7 C$ n% S$ h9 E9 P; c& A
} catch (Exception e) { c1 X5 { {6 J2 Q; I- n! v
System.err.println("Exception updateLattice: " + e.getMessage ());
, M9 m6 q! k' s }
/ t& I1 I/ a) k4 s% y
, m3 H {, u, } p" ^; |1 [: Z // Then we create a schedule that executes the% s9 P5 [/ `: \! ~9 p: W% a
// modelActions. modelActions is an ActionGroup, by itself it7 D6 O' Z8 c7 }7 U/ g
// has no notion of time. In order to have it executed in
2 [5 i+ z( W: p/ K% J: s' v // time, we create a Schedule that says to use the4 X9 d+ x& I& e9 q
// modelActions ActionGroup at particular times. This
9 H( {, C4 L* [. y // schedule has a repeat interval of 1, it will loop every
/ H/ O$ _2 e3 B# K0 m/ n // time step. The action is executed at time 0 relative to
2 U1 f+ P" r# ^; d7 f' X( d // the beginning of the loop.
2 a' Z7 v: b1 Q" M- l5 B, `* l) e- h1 w0 P
// This is a simple schedule, with only one action that is
- b6 w! y5 {( o! r2 j$ Y: } // just repeated every time. See jmousetrap for more
' g9 @; a' e) V$ p/ g1 ` // complicated schedules.
8 u0 o+ U3 ?; G - A, ]$ z8 }! K) C+ b& V
modelSchedule = new ScheduleImpl (getZone (), 1);
) l4 `& s' J6 H3 [ modelSchedule.at$createAction (0, modelActions);
* n7 {. F( {1 g0 a# }
) z% |; ~1 ]( a& N1 t* B% G return this;: C1 y/ l& t+ m+ C* I8 q' d& v
} |