HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: |; Y8 [4 U' T4 _
- w/ a* g1 S Q+ R) A public Object buildActions () {
" K7 T( U0 H4 c. K8 ~. w super.buildActions();
+ P) v/ |/ X" D/ t K 2 m% i: c- ^3 b& f
// Create the list of simulation actions. We put these in; \! f* S- O7 u& t. B/ ^, T
// an action group, because we want these actions to be
' v. G$ {$ h) h! K" ^ { // executed in a specific order, but these steps should
, Q7 I) Y4 a- w6 S' E // take no (simulated) time. The M(foo) means "The message; {, V" f6 l2 @& k
// called <foo>". You can send a message To a particular/ I# @8 p* z6 \
// object, or ForEach object in a collection.
# b2 P4 u) m. m: }( U: L 4 h2 q* x6 p+ r' i
// Note we update the heatspace in two phases: first run1 m+ S: E; _; x
// diffusion, then run "updateWorld" to actually enact the
4 C Q; s8 J0 ?. W" \5 X% ~. } // changes the heatbugs have made. The ordering here is
. `! y6 X; |% M) O // significant!3 |9 C' e3 g I% U1 T+ X& A% o
) o6 m( r v" r+ b5 {& K) q0 d k" J7 E // Note also, that with the additional1 c" e! @+ x9 }* f" b3 e
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& d G; x3 p a. T // randomize the order in which the bugs actually run. D& \+ W+ R8 I. x
// their step rule. This has the effect of removing any& c0 {6 O; ^' L( s& K% _" R
// systematic bias in the iteration throught the heatbug5 i1 P$ \' ^$ U* [7 @' w
// list from timestep to timestep7 R7 b) a5 |2 @2 R+ O6 f
, @" G6 D; d% |0 T. N! k, |4 t
// By default, all `createActionForEach' modelActions have9 \( ^$ \0 b6 B% }" G
// a default order of `Sequential', which means that the: @0 G, N" w @$ P+ O8 Y; u
// order of iteration through the `heatbugList' will be
5 G9 K* V. h1 a+ g7 P. s' y, \ // identical (assuming the list order is not changed
! z/ w' X! `: R% X // indirectly by some other process).
( k. [0 w2 X% ~1 h9 V2 q% O ^ 3 i# n& ?9 g8 i Q6 s* T& v9 V
modelActions = new ActionGroupImpl (getZone ());
' m2 d3 T' |# S. D+ u9 x$ P+ m- P @( R8 b- l" T2 m
try {
0 u6 g( X; w, s2 R& d modelActions.createActionTo$message
: r9 g6 T Q; [: _$ E& r (heat, new Selector (heat.getClass (), "stepRule", false));
5 n% F9 d: k1 ^: i* x } catch (Exception e) {& M! |5 c% ^ b7 s3 k) \ _+ [
System.err.println ("Exception stepRule: " + e.getMessage ());2 }4 m( |3 L! A
}
: C- n1 `5 t' F- ?0 A( n+ L
6 J: I4 F9 P6 E0 r try {
& ]6 S1 R9 s$ m9 c Heatbug proto = (Heatbug) heatbugList.get (0);
9 m* D! ^% [ x$ Y" s# _0 i& x Selector sel = & {& ]1 o/ j. c6 x2 U
new Selector (proto.getClass (), "heatbugStep", false);
9 j9 {& y3 i2 |$ Y actionForEach =
2 F9 ~" |3 u% S4 ]' a! u2 J modelActions.createFActionForEachHomogeneous$call& ?5 i! D2 R9 y' T4 j7 z# m
(heatbugList,
. W( P$ p( e5 Y0 c# D8 a new FCallImpl (this, proto, sel,
$ N7 P5 r& S! E9 Q$ `- A- e Y' F& X new FArgumentsImpl (this, sel)));! j1 O6 ~* u7 D1 Q( b. `" N! y
} catch (Exception e) {
) a- B' `( E. g0 e* W w) @) q e.printStackTrace (System.err);* [, V- n1 _5 {
}9 z" V N0 f, M2 b5 U
( {+ {' x' _; q& k3 c3 \
syncUpdateOrder ();
. F, i* s2 b# U0 T6 D* w8 S- F+ V& \3 [# [+ R* ^
try {+ S5 f# H- s& H. p2 ^& s! M( Y
modelActions.createActionTo$message
# Z0 B+ Y* q+ i7 E0 Q (heat, new Selector (heat.getClass (), "updateLattice", false));' ~/ \3 a; Q$ g7 w
} catch (Exception e) {
9 c' n$ z8 u: o! V3 T2 B! Y0 [2 r System.err.println("Exception updateLattice: " + e.getMessage ());
2 ~; A# y0 s/ I }! }6 T( J- @. r4 j$ @
& C- G! S! ?" {4 a5 Y, c0 ]
// Then we create a schedule that executes the. \0 r3 g/ x9 P
// modelActions. modelActions is an ActionGroup, by itself it5 I% J2 S$ h1 Q+ j* v- P
// has no notion of time. In order to have it executed in4 ~& k& b& \" u
// time, we create a Schedule that says to use the
6 L. ?' w. ?* V( D7 q5 R7 x1 z // modelActions ActionGroup at particular times. This4 B( c8 H/ `8 A1 _$ S7 S: O0 w
// schedule has a repeat interval of 1, it will loop every: Q3 L% x5 a" q" _- X
// time step. The action is executed at time 0 relative to
4 T$ u) m5 ]8 K% T) n // the beginning of the loop.
' P( A' L4 |7 [8 V+ L
1 s0 [4 C" g8 K8 `0 @ // This is a simple schedule, with only one action that is
& v) v" w6 `" ]5 s* @$ i // just repeated every time. See jmousetrap for more( |+ V3 ]8 {% x7 g J. {
// complicated schedules.& G3 |; Q8 H, w5 H6 C$ o! ~
) ]* t4 I0 B9 ^" C ]! U! _% P. z
modelSchedule = new ScheduleImpl (getZone (), 1);
: n% X3 b7 G1 { {( Q1 D modelSchedule.at$createAction (0, modelActions);
$ X" p- k+ O3 \; Q- O. C8 D1 v 1 x R& `4 `. o; e
return this;) ~ q# f: G+ y0 e. x9 k
} |