HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% Y7 ~* j) g& g3 I
* s3 [% }2 n4 d. J) Z public Object buildActions () {' b$ X! ?" p8 ?- I8 T) Q0 m+ X0 y
super.buildActions();
w- M9 i: M$ V4 v! f
* \4 a- e5 s3 D // Create the list of simulation actions. We put these in5 F. b h A6 E8 D9 x$ v" b" I* |
// an action group, because we want these actions to be7 f7 G1 D, S( a0 Y6 j
// executed in a specific order, but these steps should
# T5 e( p* m1 p. A, ?1 i // take no (simulated) time. The M(foo) means "The message/ k4 i! E( F/ w/ ~
// called <foo>". You can send a message To a particular
8 j7 j( a7 ]: }3 _% |* w // object, or ForEach object in a collection.
. e5 y1 y3 i- t( | H( m $ R( D( n4 f3 H! w! y
// Note we update the heatspace in two phases: first run
( B/ g" [5 x. H l* W // diffusion, then run "updateWorld" to actually enact the
' {: I* M0 H" L: q3 O& C. r // changes the heatbugs have made. The ordering here is: v6 o1 L& z: W; X% [0 l
// significant!
) e5 Z( y7 m0 N8 s
( j- K7 E( G6 j+ d // Note also, that with the additional+ j* N% B+ S* u0 G* P) ]/ s7 J
// `randomizeHeatbugUpdateOrder' Boolean flag we can" V+ m2 ~( m9 X* X o8 o$ f& R. N
// randomize the order in which the bugs actually run/ {1 f4 Z# g7 z
// their step rule. This has the effect of removing any; J! l* J1 a" b& n% g m
// systematic bias in the iteration throught the heatbug
2 ~, n8 ?; ?/ b/ F // list from timestep to timestep
& W( g0 H# ]3 D * J. G& Y2 D& \5 X# G
// By default, all `createActionForEach' modelActions have
) b8 [; F2 ^9 F // a default order of `Sequential', which means that the
2 _ Y9 z) x, W/ _ // order of iteration through the `heatbugList' will be
9 i$ W# b7 Q, J/ l // identical (assuming the list order is not changed
2 v+ d; w# u( b6 |9 Y4 l // indirectly by some other process).) Z1 R; b' r, `: j! t& x
4 ?. F5 r% T8 q+ }, ?2 ^& S- B0 h modelActions = new ActionGroupImpl (getZone ());
9 K2 d; R- W" f4 m K& V! T& C! j# @. t* D' J1 U
try {+ ~- O, S% M7 H" e$ G$ N2 I
modelActions.createActionTo$message
j; G/ P) z* { (heat, new Selector (heat.getClass (), "stepRule", false));
2 k. ~7 f' d8 p& V" k9 X } catch (Exception e) {% G) l" j" V/ Q* A
System.err.println ("Exception stepRule: " + e.getMessage ());7 n/ O M9 [8 P& }8 Z; P9 s
}
( w! q$ @# Y/ F& l- F' ]+ f0 e6 ]: R& u% e$ c
try {" M: v* Y' |+ }$ o" S1 {
Heatbug proto = (Heatbug) heatbugList.get (0);
" ~) N: l1 v, a Selector sel =
. a# Q- j* s2 ?( S* U new Selector (proto.getClass (), "heatbugStep", false);
4 T! I7 [$ _8 y4 k6 E actionForEach =1 m V* L4 z9 a0 b
modelActions.createFActionForEachHomogeneous$call
4 D: i# i. g$ h/ T3 k/ |) Z' A) @ (heatbugList,0 O: G/ F4 F3 S1 F" L
new FCallImpl (this, proto, sel,
+ b8 Y+ \1 X& S" h. [ new FArgumentsImpl (this, sel)));
. x2 l& M8 d4 k( k5 Q. V' U$ Z } catch (Exception e) {: T. Z* X/ k; D" J+ f
e.printStackTrace (System.err);: T9 \. ?8 U6 C. i, b& P6 H$ ]
}
6 E8 Z9 f( ?/ Z3 P/ G0 ~ ; B. [( @, K/ h4 x9 [* ]
syncUpdateOrder ();1 }1 s* N( D( o
1 k' ~: i8 h# f0 p% E3 c try {" ?( e# _ x2 N" u: d& _& N
modelActions.createActionTo$message 7 C7 T& E: |& g% a8 I7 a
(heat, new Selector (heat.getClass (), "updateLattice", false));
+ i" d5 u1 p5 O8 {: ? } catch (Exception e) {, J0 z3 `/ O4 g9 b& Z5 L
System.err.println("Exception updateLattice: " + e.getMessage ());
1 k$ B0 g( Q1 _& R3 \# t }
7 u( C: z" p3 P* {+ j9 R5 F
/ S1 m1 U8 S' \2 a6 v) M // Then we create a schedule that executes the! a! e& U" q+ |$ m" S7 P" Z
// modelActions. modelActions is an ActionGroup, by itself it
* [( l" }( v% v' i/ ~& E // has no notion of time. In order to have it executed in2 ^! ^( e, t/ \% D- \) D
// time, we create a Schedule that says to use the) U* G, f9 a$ H4 ]% O9 Y4 U3 p; j# {
// modelActions ActionGroup at particular times. This; F+ C& Y g) t
// schedule has a repeat interval of 1, it will loop every# y* @" @7 a u9 G$ `0 _/ C
// time step. The action is executed at time 0 relative to
$ G. h5 P4 y4 q/ W1 \ // the beginning of the loop.# m( F# [7 t8 [" Z7 W3 Z3 f1 A, K. H
8 @" X4 i$ \9 ?7 S
// This is a simple schedule, with only one action that is1 M2 v# _% y6 L, A4 o1 B
// just repeated every time. See jmousetrap for more# l9 }; S6 `" D1 r2 F( O
// complicated schedules.+ ~' c$ Y' F. i5 t
& K* e4 A; E5 R modelSchedule = new ScheduleImpl (getZone (), 1);
. V$ M# W1 n1 Y7 r$ J6 {! X modelSchedule.at$createAction (0, modelActions);
o5 k+ A. k2 R - }- |3 o. V* _( b5 e5 D6 V) v
return this;
; W( T: e/ p8 @* O# Y ^ } |