HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" P' ? @8 F# o- |9 m+ Y# j
' ]' {+ R9 y0 E# V public Object buildActions () {
% S& h: y8 \* ?1 _% p0 m4 h super.buildActions();& \+ F2 u V9 @1 ]- m6 d' o U7 E8 h5 z5 O
" o4 ^* N; i! r2 i
// Create the list of simulation actions. We put these in
5 @3 B) R* Y# x) G // an action group, because we want these actions to be( i- B; G' p9 k
// executed in a specific order, but these steps should
6 p- [/ f2 r! `/ e8 _5 ~ // take no (simulated) time. The M(foo) means "The message# E, G/ L8 _9 v) \5 L3 F7 M+ z
// called <foo>". You can send a message To a particular; `+ y) G7 L3 |/ N; i& B2 W
// object, or ForEach object in a collection.
! @+ \$ v0 D. i6 h
/ }- A% r2 d3 _& J4 _0 Z y // Note we update the heatspace in two phases: first run
- K- N5 ]) K* `& G' M# D // diffusion, then run "updateWorld" to actually enact the8 [: V: Q. u( ?/ P
// changes the heatbugs have made. The ordering here is
( \# n/ F9 r9 v' l$ Q // significant!
, k6 R6 a) z/ }7 x4 J! k
5 a$ z2 ~/ E) N! F7 S, i; w // Note also, that with the additional
) O( p% ~: [* s- y) Q, P // `randomizeHeatbugUpdateOrder' Boolean flag we can" j; z, M( l6 x- X$ U( r2 H
// randomize the order in which the bugs actually run$ }' y6 J: ~9 ?2 c4 m: d' y; \
// their step rule. This has the effect of removing any
0 n' L2 A% Q* O! q* K5 ~ // systematic bias in the iteration throught the heatbug
0 b$ M4 O: S1 `3 ]$ S) ^ // list from timestep to timestep
- V7 O! b% b* { D
6 A$ j0 l% a9 n% s$ N // By default, all `createActionForEach' modelActions have8 T3 g9 I, V5 \. |- j
// a default order of `Sequential', which means that the. H2 c6 n' g& `) l9 h9 \
// order of iteration through the `heatbugList' will be6 u+ P) z# {+ `2 P8 t2 c( B# N
// identical (assuming the list order is not changed2 ^; j5 c; q8 Y$ U5 R) {
// indirectly by some other process).9 l8 w/ @/ I. x
6 ^. @1 D3 U0 c6 y) U modelActions = new ActionGroupImpl (getZone ());
, ]- e3 b9 S; V$ @) i) j) f
6 s7 u7 Q+ t1 b# K. \" f7 M$ O try {" Z) Z0 k8 E. e$ Y" ~2 _7 e7 C* t
modelActions.createActionTo$message8 q" h9 _& p. X. ~+ o$ a
(heat, new Selector (heat.getClass (), "stepRule", false));
* Z+ T! _1 `8 m+ m. U } catch (Exception e) {- p: O% K$ u1 Z! c) t0 K
System.err.println ("Exception stepRule: " + e.getMessage ());* P& d, f u K. i# Q3 H
}
7 D/ C. N4 v1 O8 o5 \1 M6 p) }4 |! h
try {
$ b6 t& k8 ]$ C' V% Z! C& G: O Heatbug proto = (Heatbug) heatbugList.get (0);* a2 Y0 C1 E7 p$ @" S) ?* @
Selector sel = . g; d% M5 @& G. }0 t9 Q
new Selector (proto.getClass (), "heatbugStep", false);2 ^ V, n/ J6 q. w4 X- [; m. q }" h
actionForEach =
8 \, @! i$ g$ ^4 N modelActions.createFActionForEachHomogeneous$call' ?* I+ D0 Q E* x$ H6 m# b& h; ^6 _
(heatbugList,
8 i4 u9 D0 H( }3 h new FCallImpl (this, proto, sel,+ b/ W0 ?, L A! O9 D* K$ b
new FArgumentsImpl (this, sel)));
$ J" d: e' E6 B; Q( d } catch (Exception e) {
; b- ]5 ~, L' Z- L+ U" P8 f0 q# k2 W7 _ e.printStackTrace (System.err);- @2 Q( r3 e7 H* \: o
}3 B% a, ]' Z g6 Z* e# W
, V! n! H4 Q! M# Y, y8 K syncUpdateOrder ();
5 J9 E2 v1 T" @1 D, W a5 R% g7 Q1 X0 m" N% `3 ^, [
try {0 R* f$ _+ \+ ^3 Y- R9 }8 K _
modelActions.createActionTo$message , b. V- G1 o& L2 w" Y& z/ k0 m
(heat, new Selector (heat.getClass (), "updateLattice", false));5 z& U1 u: f: }* w
} catch (Exception e) {
$ W4 X6 b" E, f' H: L: q* Q8 d9 b System.err.println("Exception updateLattice: " + e.getMessage ()); X) M3 x1 h' S# H( m5 ~( }
}; X# |; s+ s( W
" B( I' |! R9 t: @: b- f0 H // Then we create a schedule that executes the
5 _4 ]5 H1 ?' j$ F3 w' W/ H! d // modelActions. modelActions is an ActionGroup, by itself it$ Q' C+ G e3 Z) x" W
// has no notion of time. In order to have it executed in: N! C6 y* }( P
// time, we create a Schedule that says to use the5 a' n; z( G0 ~* b! ~" E; G- j: l
// modelActions ActionGroup at particular times. This
% F; W1 w6 D* m. B: ~( h( d // schedule has a repeat interval of 1, it will loop every
- G* O5 _: D5 w* t, C# p' B/ R! s // time step. The action is executed at time 0 relative to
; K. a6 u+ b) S& J/ p3 ? // the beginning of the loop.
+ r/ o* O( \& O/ r* K# v; y- T* o' j- f2 J" L* w4 M
// This is a simple schedule, with only one action that is3 G q6 a! x% u3 A0 Y' }: H
// just repeated every time. See jmousetrap for more
7 [4 u; t6 y) i+ N // complicated schedules.
% j; Y% ^7 f7 L: F
- [2 \2 A( R( e7 ~ modelSchedule = new ScheduleImpl (getZone (), 1);) e# T. e/ u; ?/ [1 H
modelSchedule.at$createAction (0, modelActions);/ z1 u% ?: }0 p% H& n( a
9 K+ Q5 ^2 c* s" @6 z; ] return this;
! F( }$ D) G) g% w1 v7 h } |