HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 \. P' W) c1 R$ l2 C+ f
# G0 _$ d& G6 o6 z( I- } public Object buildActions () {) A( Z% Y0 ~6 t; O* k9 {1 Q2 N# W
super.buildActions();; ?6 E8 k) c. I# V
1 Y9 @* J6 w0 d, w // Create the list of simulation actions. We put these in
9 S( K7 Z. a$ }+ R7 J2 K // an action group, because we want these actions to be
' }9 ]' v: s. k* [1 j# K! Q // executed in a specific order, but these steps should- A6 l# `" h0 N0 q7 \
// take no (simulated) time. The M(foo) means "The message* N/ m0 l9 w& b! e8 q& b- S
// called <foo>". You can send a message To a particular# a9 F# ^2 j. i, |6 _
// object, or ForEach object in a collection.
' r+ U a( |1 @% X, w& L 5 E' S* e8 H& U% g6 |# q8 G, @5 k
// Note we update the heatspace in two phases: first run* K, _" x A& t4 S. l# n
// diffusion, then run "updateWorld" to actually enact the
1 U. k$ d: z, p- e- P% ^$ o // changes the heatbugs have made. The ordering here is- h! B. a5 f: |; N7 N
// significant!
& ^; h2 I* t, V+ M! M& j 6 T, j. m/ \& m' B: z! h5 h+ S
// Note also, that with the additional
! f9 p* t4 i! D // `randomizeHeatbugUpdateOrder' Boolean flag we can' m) K7 M' b$ t# e, o) q6 {; u# j
// randomize the order in which the bugs actually run8 g! ~8 ?* R6 {7 [6 }0 t
// their step rule. This has the effect of removing any; T7 O: W1 W- N0 x0 \& s9 |
// systematic bias in the iteration throught the heatbug
- }; R+ e- U2 l: M! J // list from timestep to timestep2 |1 x0 M4 a" t4 d9 u" [& o& I
8 J7 ^% Y$ g8 E8 f' R% v; r // By default, all `createActionForEach' modelActions have$ E w( w; D v- T, ^1 f
// a default order of `Sequential', which means that the
2 [5 B2 T/ _( J // order of iteration through the `heatbugList' will be- v1 m0 `1 Q+ d2 J2 m) F
// identical (assuming the list order is not changed# p* A( W) d' R6 k+ E! O; n. _- @
// indirectly by some other process).
* p3 ^7 H. L; v0 E: \0 R
$ o4 M! ~. K" j0 _ modelActions = new ActionGroupImpl (getZone ());
. L0 ^, K8 _# m: y- G
9 E7 o- d) S3 n& B: _ try {
. i# T! @" `5 }1 L7 U5 F modelActions.createActionTo$message* n7 Z/ \/ k1 b9 E; F
(heat, new Selector (heat.getClass (), "stepRule", false));
+ Y- t r3 B+ D1 U/ t9 j } catch (Exception e) {
5 L$ d; {. T1 X' v0 ` System.err.println ("Exception stepRule: " + e.getMessage ());8 F1 P3 S0 |, R$ E, c7 {% `
}
/ G: s$ F5 _" x1 t1 C5 u% w; |/ O, |
try {
! w5 f8 C8 a; T* A8 n' F Heatbug proto = (Heatbug) heatbugList.get (0);
# Z0 T/ h! U+ h. |/ [. E5 N. u. a5 D0 L Selector sel =
$ @/ L9 T7 T- k0 u0 j. d: J new Selector (proto.getClass (), "heatbugStep", false);$ h( Q" U9 R/ N% f5 O
actionForEach =
% c$ ` \& h; n: e, ] modelActions.createFActionForEachHomogeneous$call
) G4 f& k# U. Y5 w9 f (heatbugList,, m( C; T4 u$ c, d2 K8 \3 j/ W
new FCallImpl (this, proto, sel,; W% Y6 }/ I, z4 @
new FArgumentsImpl (this, sel)));' @( @* p# `; s3 O1 C& f- S
} catch (Exception e) {7 b# f- X% D8 H4 h$ P) n. `
e.printStackTrace (System.err);
. C" m% }8 F2 f/ i! P& G }% r- Z M# ]! A$ x( F& Z. r) V3 {- Q
0 `9 H( O2 U0 x3 v$ X5 M
syncUpdateOrder ();# Q0 c. a; N5 ~# i2 h
- d' o) M3 \8 I
try {
6 K* T* e0 E7 I/ x6 b modelActions.createActionTo$message
& _8 K+ o0 m$ O. T (heat, new Selector (heat.getClass (), "updateLattice", false));, o8 E4 H6 V! S
} catch (Exception e) {
' j8 g( b* `4 x System.err.println("Exception updateLattice: " + e.getMessage ());
/ O: e4 X- E" I) @7 e' e }/ Q. ^: m& n; m& M
) L% b0 d5 ]1 |
// Then we create a schedule that executes the6 {/ j i& |0 x, R( Y: O8 a
// modelActions. modelActions is an ActionGroup, by itself it: N- k# W* l5 L
// has no notion of time. In order to have it executed in9 _% }( z. J/ w q* Z
// time, we create a Schedule that says to use the7 T: @7 l3 f. @
// modelActions ActionGroup at particular times. This S ]) z# A/ a2 l. Q+ _
// schedule has a repeat interval of 1, it will loop every
$ ^" f) P7 |3 r# l' a // time step. The action is executed at time 0 relative to
7 @ Q# \* i6 O' X. t3 Z // the beginning of the loop.# f, ^4 E: ~- p1 N$ q; J( ^" I
" T+ Y2 |( z, g9 S8 C7 ?4 ~) m // This is a simple schedule, with only one action that is! j/ ]! A" E& m$ _
// just repeated every time. See jmousetrap for more- U; c- A8 H3 X6 ^/ R2 M
// complicated schedules.
1 {$ ?& [, |+ M( ]( k" n ?0 r
6 B+ O+ s/ x: k modelSchedule = new ScheduleImpl (getZone (), 1);, z. t3 C; m( t4 @- W
modelSchedule.at$createAction (0, modelActions);
( R) A3 @, ?4 [$ M; m6 \7 k ( G1 U6 \/ c. `8 w7 H7 X* S- Y
return this;; u0 K V' O* g* W6 z( z: o
} |