HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 W: p! {2 Q! N V8 w$ X& T% L; z# U2 {; X# F
public Object buildActions () {
; q' I3 S* M8 \0 _: C+ x: X) k super.buildActions();1 v# O3 d' h; o( P
9 \/ G# ^$ x. z/ h3 O // Create the list of simulation actions. We put these in$ a* D' C6 b4 m- ?4 Y+ m2 B8 B& N
// an action group, because we want these actions to be2 m$ V' b1 @" ]/ r8 a( c E
// executed in a specific order, but these steps should
9 W* c7 G/ l5 m/ V* O0 z* ~ // take no (simulated) time. The M(foo) means "The message! U+ m% F+ j; \
// called <foo>". You can send a message To a particular
; r$ `' R9 y' O. U* N& t // object, or ForEach object in a collection.
# D2 C. x# p* H
5 w8 S8 v" U( @' f9 [2 v2 { // Note we update the heatspace in two phases: first run% V0 U& O, f) A7 i
// diffusion, then run "updateWorld" to actually enact the1 V5 _$ c& y6 v/ E/ H
// changes the heatbugs have made. The ordering here is4 D5 W9 z4 J* N0 W
// significant!: ], R6 V3 E- i
7 a3 _' h% O0 P5 L6 w3 h
// Note also, that with the additional
8 C$ m5 K- v0 Y" \" l' k // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 l+ v7 x# V3 Q# g: J2 V7 m* ? // randomize the order in which the bugs actually run& _# b( D- ]1 O O$ l* x
// their step rule. This has the effect of removing any, C# z! w9 |) `0 Q# m S
// systematic bias in the iteration throught the heatbug
; J; B" T/ I9 m/ S! n! f* l // list from timestep to timestep$ v& F. h! p) a$ [3 N
" Z, ~, }- n( L) ]2 P
// By default, all `createActionForEach' modelActions have
4 U# d6 m* @$ I, h6 ?2 f N // a default order of `Sequential', which means that the2 ^, |2 M2 Y3 o, K M, e( O
// order of iteration through the `heatbugList' will be, ~8 v: o: k8 ~- j" Y8 y
// identical (assuming the list order is not changed
/ ~5 Z% q) e, Q `: j // indirectly by some other process).2 |1 Q# W, ]! ~$ K
, h& G, {6 A/ V8 z: ^ modelActions = new ActionGroupImpl (getZone ());
9 n0 Z& |- j# A9 S5 l5 K9 l
. h' Z# ^% d! E try {
& u# Z: y+ J3 g2 F8 P: Q modelActions.createActionTo$message
~- U+ n7 n' V M% T+ R1 @# w) v (heat, new Selector (heat.getClass (), "stepRule", false));% D2 P3 F; W# O* h7 k! ?4 e
} catch (Exception e) {
0 |4 S/ z. W: Q. t6 `* C System.err.println ("Exception stepRule: " + e.getMessage ());
" t( E) q5 G5 i1 M. G/ ] }. D- ?% E' Y3 W. l8 |
( R6 W& a {. ]2 F I" T try {* i$ l8 n! R; @5 E" e8 W: Y
Heatbug proto = (Heatbug) heatbugList.get (0);9 X: I. ]" C- o: r8 [$ G: d
Selector sel = / H, V* m. U' m# a5 H0 v4 p
new Selector (proto.getClass (), "heatbugStep", false);4 L7 E: h7 s+ n2 z! u" R5 e
actionForEach =
# O* Z3 N* x# d3 z9 c$ ` modelActions.createFActionForEachHomogeneous$call
l; S; {/ U9 D: p2 X (heatbugList,
6 H" Y0 A/ c7 Y* b1 ? new FCallImpl (this, proto, sel,& H# [* W" ^4 P
new FArgumentsImpl (this, sel)));
/ ^/ m" A% T7 P9 v: V2 t } catch (Exception e) {! I! a5 k- C7 k
e.printStackTrace (System.err);
8 H' L( F: a* r- h' u }( r0 N* w, P# b
^& I( q+ X& c4 c syncUpdateOrder ();
8 }9 a: m. I( {$ S% ^/ q# a& k4 v1 y, d1 p3 x# P
try {
4 S9 B# \# w+ l+ x! { modelActions.createActionTo$message
, I; G" P8 Q- f. O. j; { (heat, new Selector (heat.getClass (), "updateLattice", false));
2 q* i& K6 c6 c5 x- k8 I9 Y) t } catch (Exception e) {
% Z5 d, C8 N" @* E* t* y System.err.println("Exception updateLattice: " + e.getMessage ());" {9 U1 `; j# ~
}
( p) @. {! M$ p' S
# [0 J" h. |3 d, o) O- y // Then we create a schedule that executes the3 s1 Q( f6 o5 L5 Z
// modelActions. modelActions is an ActionGroup, by itself it' Y7 I$ ~' o( d
// has no notion of time. In order to have it executed in) s9 r! U& o) K3 U8 S# {
// time, we create a Schedule that says to use the
- T; P- v2 q& x5 l- r+ `' q: E j // modelActions ActionGroup at particular times. This
, A f& K9 Q5 a$ n. G // schedule has a repeat interval of 1, it will loop every, X8 A& g8 y" K9 R6 ]
// time step. The action is executed at time 0 relative to; A+ ~% A9 f+ {/ E0 G! S* f
// the beginning of the loop.1 ~1 a; e4 W5 R1 y
9 A( Y" _" g) E* U0 j5 Z2 ~0 T$ } // This is a simple schedule, with only one action that is
. S# y% K; W# l5 ?9 s // just repeated every time. See jmousetrap for more
1 f# h$ Y: ~7 `& ] // complicated schedules., s5 Y# ^( J. X Y# a' i
- e- f- g1 s7 O& u: G# s modelSchedule = new ScheduleImpl (getZone (), 1);
2 G7 N+ K- {0 s8 } modelSchedule.at$createAction (0, modelActions);
0 f( W# p$ t Q8 }3 {) k
) K i2 Z% c/ v return this;/ B- L% `6 _0 e' d7 V1 G
} |