HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 I; O y7 i3 z- u! m+ X; c5 X" W' {3 b4 R2 C
public Object buildActions () {& Y( z( I1 Q7 z4 W. k
super.buildActions();' k# k. B* s+ O5 C
% {# ~( F; U+ N" a: c, r // Create the list of simulation actions. We put these in
% @, O5 x) M8 M, ~2 ?$ K- h6 ~ // an action group, because we want these actions to be
' d% }9 B! w1 j9 v+ t) | // executed in a specific order, but these steps should7 H# ~5 I/ g8 x; g& V9 V
// take no (simulated) time. The M(foo) means "The message, I: x# S5 y4 d! @) @4 } z; a
// called <foo>". You can send a message To a particular, A# a; g6 P' V& K5 U9 T# Q
// object, or ForEach object in a collection.3 T1 `9 ]' `$ X& |
0 l- d6 F+ I7 O
// Note we update the heatspace in two phases: first run9 @8 g# l1 @5 B0 l4 E9 w
// diffusion, then run "updateWorld" to actually enact the4 ~2 \$ K% i6 Q
// changes the heatbugs have made. The ordering here is. M+ E6 e$ P0 Z8 z( D0 H, E
// significant!
. S- o' M2 {$ O; P1 o; b & r4 @/ p& `4 D; K T! S. \- W( t
// Note also, that with the additional3 C# ]9 ]6 p2 [/ |$ S% m
// `randomizeHeatbugUpdateOrder' Boolean flag we can. q) w: K/ G1 U1 L
// randomize the order in which the bugs actually run U, T; e+ M2 s, p" U0 O0 T
// their step rule. This has the effect of removing any
2 d1 i/ Y1 t; p: ]/ L& X H1 T1 v+ | // systematic bias in the iteration throught the heatbug
! p4 A- P: k5 ~2 C8 ? // list from timestep to timestep
: h6 h& o2 a! ]9 {
5 B2 r/ R, N; r% q // By default, all `createActionForEach' modelActions have; K) Z# k a( x+ {+ u! w
// a default order of `Sequential', which means that the
2 U6 Y, e+ H/ I9 } // order of iteration through the `heatbugList' will be
( }6 e$ P: `- z5 o9 | // identical (assuming the list order is not changed
+ ]+ m1 |* O0 k4 h4 K& H6 g4 P3 k4 g // indirectly by some other process).$ k. s7 c1 t9 q3 F3 Q* Z4 o) Z
5 ^0 n3 h: L$ E. C! o
modelActions = new ActionGroupImpl (getZone ());/ X0 U' C+ i( ~$ z+ C V
! J& T' y! b" U$ `/ O* L0 A4 K
try {
+ [6 f8 S+ p0 F modelActions.createActionTo$message
( x1 P9 Q' b- R9 `: M) U" M (heat, new Selector (heat.getClass (), "stepRule", false));. q3 r: o. y0 ?3 I" u {4 N
} catch (Exception e) {
. J3 S6 Y; u _6 H& I& I. Y' G System.err.println ("Exception stepRule: " + e.getMessage ());% L$ d5 G/ j4 q e: o( N
}
- w7 f& F5 m }: K& K# X$ ~- t: @9 s9 q
try {
3 }5 [2 V8 Q. @% B Heatbug proto = (Heatbug) heatbugList.get (0);4 Y& q0 k: I- o+ @; t" x
Selector sel = $ }5 P. B; p4 r* }! @% d, z
new Selector (proto.getClass (), "heatbugStep", false);
2 ?& g% G3 t! O) f! | actionForEach =
% Y' k$ J" D- T2 e& S% ^ }& w2 q6 O modelActions.createFActionForEachHomogeneous$call
3 p! G# a* A% o; U3 c (heatbugList,
! P" R2 n" f6 O9 q( e new FCallImpl (this, proto, sel,
( `+ b2 D c. M/ d new FArgumentsImpl (this, sel)));* d/ [% g: m% |" c/ r9 u
} catch (Exception e) {
l6 |4 U) p3 L3 ?8 ]+ O e.printStackTrace (System.err);: V8 ^& d2 o0 e9 [8 ~# p
}7 {# ~( {+ R* @# @" T6 M
; q. X: L, r% X% ]$ Q syncUpdateOrder ();
# W* l+ [2 e; D& g3 u5 ~" S' @6 @; F1 f# F! w! p& d
try {7 O, K% M' r7 T/ a& a
modelActions.createActionTo$message # W: ~6 V8 Y0 a" d5 ~6 v. N" M
(heat, new Selector (heat.getClass (), "updateLattice", false));5 D' S/ L7 i: Z* O+ _
} catch (Exception e) {( n j5 [- h8 v+ d
System.err.println("Exception updateLattice: " + e.getMessage ());& ^ w1 M5 Q* ?0 w6 r2 p, ]
}; g4 _* C7 l/ U
. H" r: {2 Q6 A4 T _. d
// Then we create a schedule that executes the8 r0 ]* I! d" x3 o' l! K. _3 y+ _
// modelActions. modelActions is an ActionGroup, by itself it
0 x1 D }+ B! Z% P4 q: ?/ |% Q // has no notion of time. In order to have it executed in7 V c- F3 |% y+ [3 w# j2 W! o3 \
// time, we create a Schedule that says to use the+ P4 X" o6 P" z" h8 j- q$ U
// modelActions ActionGroup at particular times. This; A/ s8 a* w+ X% B2 b- r4 D
// schedule has a repeat interval of 1, it will loop every! e5 D" W" g1 W* B8 E! A2 X
// time step. The action is executed at time 0 relative to0 P2 n/ C. X/ R; {5 `
// the beginning of the loop.
0 J7 ^, w& n$ j2 I9 w0 {9 ?
& G2 f2 I) F* f7 |& {& B7 K // This is a simple schedule, with only one action that is* U* H8 K: H! U# D2 q% A& I7 M
// just repeated every time. See jmousetrap for more
& s: u, ^" N' T" @- ], M3 \ // complicated schedules.$ s& f, z) B5 S3 c& X, g
; {- M1 ` q2 j, G" Y k1 T1 W
modelSchedule = new ScheduleImpl (getZone (), 1);" L7 i7 S! D' G1 I$ ?
modelSchedule.at$createAction (0, modelActions);
2 s" i9 d+ `# y5 n: T7 M
; v: I& Y. o5 q; a; A5 E: S return this;
& j8 g' M( ~! p9 M0 {+ D } |