HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* U" A3 d5 I1 t, H, s+ O0 q, m3 N
public Object buildActions () {
# Z' n9 h: b& w+ ]4 J; ^ super.buildActions(); e/ d1 @" g( n% _1 Z" J$ M4 j, b, o
7 E; g) q. c: v& J2 P$ A
// Create the list of simulation actions. We put these in2 O$ \( K; H2 m. I; N1 B1 H% S" E
// an action group, because we want these actions to be, _! Q8 y2 n; d. d3 [. D
// executed in a specific order, but these steps should5 x4 o* j+ r: {: t: s4 h9 [
// take no (simulated) time. The M(foo) means "The message
- G) r) H! B9 w9 i7 T // called <foo>". You can send a message To a particular
" f/ m) ]+ M- m/ s // object, or ForEach object in a collection.9 Y) h. S0 F8 K) T# l4 E$ q
+ y& Z8 N$ u, K) K# K
// Note we update the heatspace in two phases: first run
$ O" q. c) P, L& m8 O0 u: m // diffusion, then run "updateWorld" to actually enact the! v9 J: h7 K0 B, ?; Z/ w+ I
// changes the heatbugs have made. The ordering here is! u' Y0 V4 w! L7 u' W2 _
// significant!) T' r0 c/ i) d6 Z) `
! \& H6 z, X, _9 {9 i0 `/ F
// Note also, that with the additional
2 \) K. ^* y. C; z // `randomizeHeatbugUpdateOrder' Boolean flag we can
: ] ~& i1 @2 u0 a // randomize the order in which the bugs actually run
/ h2 j c) Y* p1 H, n // their step rule. This has the effect of removing any
+ S& I+ @) \+ Y" {1 Y // systematic bias in the iteration throught the heatbug2 G$ ~2 C# g# ?1 P
// list from timestep to timestep0 |( N$ i& ?+ s) ?
' O4 Y0 ~" O5 o0 y+ \3 _
// By default, all `createActionForEach' modelActions have
, P' [3 l5 m8 _' s- W // a default order of `Sequential', which means that the
' m; i$ B0 t7 i( O7 C& i // order of iteration through the `heatbugList' will be
) j/ U4 B( U4 r. ^0 r // identical (assuming the list order is not changed) B+ w8 C, h5 r$ }4 O
// indirectly by some other process).
0 W9 t& T$ C* Y) {" f% @ - _' V5 t! K9 [2 l% n2 x
modelActions = new ActionGroupImpl (getZone ());
9 ^/ }& M* g% {, i
7 k* S* M' [, J9 Y try {7 Y$ Z' K9 x& h5 k" E, F* [, }
modelActions.createActionTo$message
8 I, d2 j3 `! b/ I& F8 W! t (heat, new Selector (heat.getClass (), "stepRule", false));' n* V. S# f7 F; z, F
} catch (Exception e) {1 k( }% p3 Y2 A% r+ u
System.err.println ("Exception stepRule: " + e.getMessage ());
' o+ X5 w2 ^5 k! g( @2 H* v. l }0 ^; a9 K9 M& w
1 T0 A1 E. m# ~; ~; L1 {0 ~+ O, s
try {
; b+ |! m+ A5 C: k6 k% d7 o6 D Heatbug proto = (Heatbug) heatbugList.get (0);
6 m) r8 o7 G8 D( g Selector sel =
& H3 T5 s# \; }! {/ i new Selector (proto.getClass (), "heatbugStep", false);
1 W' h. s3 W1 a* X. n actionForEach =8 M5 f) S+ U$ j
modelActions.createFActionForEachHomogeneous$call
7 w( V) W9 W W' J! E (heatbugList,! Q% [8 ~7 b4 g3 y f# g' h
new FCallImpl (this, proto, sel,; x9 G) {) M' p3 z
new FArgumentsImpl (this, sel)));3 h3 p4 l1 ^5 O! W) |
} catch (Exception e) {
, a! ?: \% h3 x( | ~! j e.printStackTrace (System.err);; c8 _6 k6 G, v! e9 |
}9 F' a: z: O- k9 _7 m
! S' u+ h ]1 F" D" F
syncUpdateOrder ();1 _8 I6 T* R( G; e) e6 e
% t6 k+ l/ f4 X5 I
try {
$ O$ [$ `, p$ O modelActions.createActionTo$message
) b. E0 s- I) s8 u9 _$ { (heat, new Selector (heat.getClass (), "updateLattice", false));3 x( @ B/ t4 Q# E$ R$ U# n
} catch (Exception e) {
$ Z! A7 `: P0 R0 M System.err.println("Exception updateLattice: " + e.getMessage ());" T. Q) S* b- u8 y; L8 `
}5 r$ F1 ?3 m3 S9 S* i
( W7 J( u+ ^0 [0 S6 b: T // Then we create a schedule that executes the
3 S# W; t7 I ^/ ^" U // modelActions. modelActions is an ActionGroup, by itself it
" i) X' e l1 r6 }: t // has no notion of time. In order to have it executed in, U: E& l; [- [9 ]( I9 @# ?
// time, we create a Schedule that says to use the; G1 J P- m% o( u1 P
// modelActions ActionGroup at particular times. This
' y1 A( y1 p( G" m* m, {6 w! ` // schedule has a repeat interval of 1, it will loop every2 h5 t( i7 Y# i$ _
// time step. The action is executed at time 0 relative to
% M9 }7 a6 o1 i# K! y C1 ]% z // the beginning of the loop.
! L) Q* }7 l% E: Y/ a; {) E; m. h2 D- W4 j6 t$ f
// This is a simple schedule, with only one action that is
' @; f2 @6 f; B( n7 y3 f/ ^ // just repeated every time. See jmousetrap for more* u) ]6 d" t; h2 C" _7 Q' Z
// complicated schedules.
1 d: M# d: N( p4 K q$ G3 K. Z1 n+ c7 t% f
d/ O1 ^: r: X6 H" g: q9 B modelSchedule = new ScheduleImpl (getZone (), 1);
. d V3 Z5 Q- I, T- P1 r modelSchedule.at$createAction (0, modelActions);. F# V) {. H! p6 t, [; L
- K+ y d# _. S6 h) r& n8 k. C
return this;+ W5 [5 t P9 F9 S- w( I
} |