HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( L' n2 R; L- n6 f# w
. x$ q$ |' A+ S- e1 E A+ T3 [ public Object buildActions () {' {4 v' L9 d, f8 e3 [6 F
super.buildActions();( @. X7 R/ \1 m7 K+ x) C3 {6 Z
{0 w; H( Q2 G% u% I
// Create the list of simulation actions. We put these in
" {( z# o7 n( d6 U // an action group, because we want these actions to be
# Z% j7 R: s! N2 ^ // executed in a specific order, but these steps should" H( [- |: G1 C& r' d
// take no (simulated) time. The M(foo) means "The message* A1 ?9 ]% m6 j
// called <foo>". You can send a message To a particular
3 v8 H6 F; H( a6 v; b% T // object, or ForEach object in a collection.6 \5 H2 y8 p$ D
6 _# K" y; f# f: W6 f& P7 l
// Note we update the heatspace in two phases: first run/ t; Y; K! R K+ {" x3 C
// diffusion, then run "updateWorld" to actually enact the
9 q' [( I7 u. m, r- v1 i6 m7 I# `/ L // changes the heatbugs have made. The ordering here is
& O3 s3 V. H- A) M' ] // significant!, x0 K8 f1 T+ w9 r. C; g& j, ]
9 v. |. v0 E9 w% T0 D3 q. w // Note also, that with the additional% e8 I9 K6 d( f0 E4 B) D) ]4 O! {
// `randomizeHeatbugUpdateOrder' Boolean flag we can- K4 O" h! j# t" C8 P
// randomize the order in which the bugs actually run
+ d* S) A8 E( V0 r // their step rule. This has the effect of removing any$ Q& j# l# {- I9 u. A
// systematic bias in the iteration throught the heatbug6 b$ {0 H E5 z" L. b
// list from timestep to timestep( \8 _' j/ y2 o5 @& U: T
: W# T. T' P% G7 ^1 y // By default, all `createActionForEach' modelActions have
5 G7 I0 f+ R* e& d6 A // a default order of `Sequential', which means that the2 U m: V! K2 z6 C
// order of iteration through the `heatbugList' will be# d" `; `- {3 K: A2 |# L
// identical (assuming the list order is not changed* T2 F! p3 b& E6 X& s. V$ D2 W
// indirectly by some other process).
9 I' C# l, |+ S * v$ }7 G: r7 A# G4 g
modelActions = new ActionGroupImpl (getZone ());0 B/ f9 d/ j2 q4 M, s K
7 u$ ]1 s* J7 ~; p6 L) M try {) J( y- \: l a( {$ X
modelActions.createActionTo$message. Y% K* ?1 F h+ ~) e
(heat, new Selector (heat.getClass (), "stepRule", false));
' S) \( r( }2 }( C2 A8 N } catch (Exception e) {$ Z4 J3 b; B/ t3 A- f6 u
System.err.println ("Exception stepRule: " + e.getMessage ());% ~9 ~, p2 E% z6 `6 p
}
% U" r7 v, A, Q+ z7 O' E
; n1 D: n4 o; f$ x try {+ Q* s0 ^. Y+ t D
Heatbug proto = (Heatbug) heatbugList.get (0);
8 M* z0 O6 K& B( c' j Selector sel =
1 Z N) ~: Y: a/ a1 M# y new Selector (proto.getClass (), "heatbugStep", false);
1 w. e/ M7 G1 c, \7 ~- d actionForEach =
: ~" H( Q! i* q8 u- q$ _ modelActions.createFActionForEachHomogeneous$call( ~1 i6 F7 C3 X
(heatbugList,7 A$ k" s! o2 h3 s
new FCallImpl (this, proto, sel,
/ B+ u4 U- `2 w7 Z% H& b8 v" J ^- C new FArgumentsImpl (this, sel)));% l( w9 J, x7 |3 i
} catch (Exception e) {
! W8 k8 `. E* X9 M# Y e.printStackTrace (System.err);/ [% d6 c' }# X' y, a: @0 L$ ]
}/ x1 G' z! s Y
& c1 M6 T( S( G3 n syncUpdateOrder ();) w! q/ v$ D; g, F
' v+ x/ e9 \' W6 {( w try {/ r: d& a" u5 K# g% \2 a
modelActions.createActionTo$message 4 q# d3 n* [/ V/ j0 m2 D" p
(heat, new Selector (heat.getClass (), "updateLattice", false));
# W4 a/ T) ^! s& b } catch (Exception e) {% H! ?" B4 ^# r* T( F
System.err.println("Exception updateLattice: " + e.getMessage ());3 v# @) v8 i' X: X y& K5 S8 O
}
; m* q: L, w+ Z4 d* ^. I
* c) I. `, R* I1 f3 a // Then we create a schedule that executes the2 h, H2 X& ?5 ?
// modelActions. modelActions is an ActionGroup, by itself it0 F* f' O4 I+ x' {2 e
// has no notion of time. In order to have it executed in3 e- s- [% R. b, A
// time, we create a Schedule that says to use the
4 y0 b0 l! O9 a* T& w // modelActions ActionGroup at particular times. This
* G+ ^6 Q" c) P4 G // schedule has a repeat interval of 1, it will loop every/ M! F' e9 I( I# A# i
// time step. The action is executed at time 0 relative to
6 Q/ Q$ B0 S1 Z2 p; q; _ // the beginning of the loop.
; ~" I. y0 U5 s( _* G* h
$ x3 ?) _+ n4 ]9 d8 c // This is a simple schedule, with only one action that is
4 y8 _" F- i7 y+ ~. ~ // just repeated every time. See jmousetrap for more
) v3 b$ c. H! Z: j // complicated schedules.
+ T0 S- i4 U/ e. Z
' l- n* e0 b8 q3 I; z( \' R modelSchedule = new ScheduleImpl (getZone (), 1);' Q2 B' Z4 Q3 N. F+ ^; q+ {. G3 Y
modelSchedule.at$createAction (0, modelActions);
& n$ z- }; |8 ] L/ V$ X1 o9 _
$ Z+ h; Z6 m. p. L1 B- l) y$ t return this;2 Z6 p% R E+ f9 P! ^! m
} |