HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ n2 R* W# z2 P! ]" ?" n
8 n4 }5 R7 c' W public Object buildActions () {
0 E* _/ @- q( b5 @6 d' i8 T! |& @ super.buildActions();
1 K, d; J. w5 d: n$ F4 M " i1 X' Q! f: U
// Create the list of simulation actions. We put these in
9 n$ Z2 e# U# p# v // an action group, because we want these actions to be
+ w' Q2 Z" m& q8 r1 g7 c // executed in a specific order, but these steps should3 E5 A. n" E% x+ b! p6 c
// take no (simulated) time. The M(foo) means "The message
1 z) ~# @8 T* B // called <foo>". You can send a message To a particular* w0 f7 e. T# M) b5 {, V U& L, D* g: D
// object, or ForEach object in a collection.
8 c9 c g/ r; k" y , Z- h' H# Q8 v* t% C
// Note we update the heatspace in two phases: first run
' I" Z7 _) }/ | // diffusion, then run "updateWorld" to actually enact the- h3 @; t& g& B' ~) z
// changes the heatbugs have made. The ordering here is
" q# c3 s$ a3 p1 m- z // significant!2 Q' v& T5 x8 X1 W# D; p/ w
0 j4 l, M+ {+ E* `7 g* L // Note also, that with the additional: J' W2 Z! }7 u+ [& ~3 i
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 J9 R* g& |( b- w
// randomize the order in which the bugs actually run
0 I) y( o& @" N* v5 ~5 w1 f5 R // their step rule. This has the effect of removing any% e7 L( c( B9 R
// systematic bias in the iteration throught the heatbug- d+ a* `, t1 i
// list from timestep to timestep
, {( d) l* Q+ l/ R9 p$ d G. s9 O2 \0 G1 k3 q* @3 e
// By default, all `createActionForEach' modelActions have0 B) ~9 C! F+ ?+ n0 {
// a default order of `Sequential', which means that the
% n' |% b( Y& w. } // order of iteration through the `heatbugList' will be
4 B. s1 o, t% @) A0 o6 `6 C) V, X // identical (assuming the list order is not changed
8 x# h) L* X$ G8 O- P2 K7 Y8 S/ n // indirectly by some other process).0 I0 i5 n) V* x: m
- d2 y! ?5 \$ z( e( G' ~
modelActions = new ActionGroupImpl (getZone ());
9 \: O# m! h6 c6 v, }' [/ c& t. `; d8 F( c$ F6 |( p; K( p
try {
$ ?& b, A' j) q modelActions.createActionTo$message
6 a' K3 G& ^4 h, B/ K3 X8 j (heat, new Selector (heat.getClass (), "stepRule", false));
* m2 p$ C ]2 x, P4 | } catch (Exception e) {
* B7 W4 ?& Q1 i+ ?2 [* a System.err.println ("Exception stepRule: " + e.getMessage ());
]* q2 P+ ~) G" X J }
1 t* u z, `1 `0 ?+ ?
5 \% V+ G, ?5 K& g# \- t try {: v. V# v) C! {1 v& _/ {
Heatbug proto = (Heatbug) heatbugList.get (0);- i ^; d( O1 h2 e5 l$ U
Selector sel =
) ~3 c: W8 c& M+ I, c* B new Selector (proto.getClass (), "heatbugStep", false);
1 I( Q) B; t. d s6 S$ ? actionForEach =
! q# E3 E3 f( E. D modelActions.createFActionForEachHomogeneous$call
6 w, B5 G/ z) A. u+ y: q (heatbugList, F2 y' O! Q+ Z" v, t
new FCallImpl (this, proto, sel,$ M9 l" v3 Y% h J+ g1 s E
new FArgumentsImpl (this, sel)));
6 W6 b; f) s( \. m7 A& U } catch (Exception e) {' ?' k# e9 t4 r$ c1 t) w" i+ N1 [
e.printStackTrace (System.err);8 K( _ }* H+ f5 X+ x
}5 q5 n6 Y; F5 z8 M
# K4 F7 k: J- A! h6 s% d2 N# U syncUpdateOrder ();+ q. N8 ^+ u7 {" ?1 @6 Y+ N$ m
: f$ @: s4 |' O/ {) ^+ g( z. E try {
! B, b5 g a( s. R4 X2 [6 }3 G modelActions.createActionTo$message 9 Q4 n* E/ I9 K3 v& d8 f( U6 z
(heat, new Selector (heat.getClass (), "updateLattice", false)); [% O" ^, c( \
} catch (Exception e) {
0 l; J3 _5 N5 a, W, I6 i8 A" ? System.err.println("Exception updateLattice: " + e.getMessage ());/ o# ]( p% B4 T# k, s5 J" g& R
}
) q( O' E- t8 s% D; x , [+ G$ K! D$ K! t
// Then we create a schedule that executes the
3 i: w! f& T5 K, i // modelActions. modelActions is an ActionGroup, by itself it
' X- K8 D( l* O) m // has no notion of time. In order to have it executed in3 U1 k! g- [& P
// time, we create a Schedule that says to use the. u) }) s/ B" C' T2 a
// modelActions ActionGroup at particular times. This
y* l; |6 y, v, h // schedule has a repeat interval of 1, it will loop every2 T" U* p5 P8 U" Q
// time step. The action is executed at time 0 relative to9 s+ a9 j, d! E/ e: H
// the beginning of the loop.
4 H* Q) f! |% n* x j
5 B2 F- B7 y# X# Y // This is a simple schedule, with only one action that is) s8 _, M6 w0 R+ p9 }3 Q
// just repeated every time. See jmousetrap for more" z9 t% O" l% k& V3 B+ m
// complicated schedules.
: X, E* W: q( l; T
; @/ Q$ e5 H9 i$ P, D( w modelSchedule = new ScheduleImpl (getZone (), 1);
: T3 A" h+ z( J+ E" B* Z9 }/ Y modelSchedule.at$createAction (0, modelActions);, Z0 m3 N) a5 m4 e0 Q+ I
, L2 h. p4 I/ W! u$ M# } u* E& }5 d return this;
* _2 }- Q1 X' Z: o1 d } |