HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 z6 ~" m( z. }6 T; G. T c4 j* C o$ C' d' W `: w
public Object buildActions () {; s6 f# W, k" K
super.buildActions();" I6 G# C# \4 ?/ x1 D2 Y' a1 v' I6 E
. p, A- s! G- h) ^0 x1 H
// Create the list of simulation actions. We put these in- o" R" Y& W0 }, m: J0 s) r
// an action group, because we want these actions to be1 \& w6 E) A7 r7 `3 a/ x3 \
// executed in a specific order, but these steps should7 h5 Z: e+ T& M( |( H
// take no (simulated) time. The M(foo) means "The message
# d; n4 m* O# f& R0 G // called <foo>". You can send a message To a particular2 a. d8 e& y/ s5 g( g: o5 ]
// object, or ForEach object in a collection.
6 h4 b5 }! g# U+ L+ d3 q
% K- g6 A) @. E9 b- M9 z6 x // Note we update the heatspace in two phases: first run
# y0 Y2 Y2 M. j# D# f- g* p" P // diffusion, then run "updateWorld" to actually enact the8 S. S, d7 v! t) S$ @+ J, _$ A
// changes the heatbugs have made. The ordering here is3 ?9 g' X! j" D q% S
// significant!
( N8 @1 @" W; Y% F, v6 g/ ~
. T3 j9 W! p; l: [" ?: L // Note also, that with the additional
- e# w4 B# R- W& J8 U! C // `randomizeHeatbugUpdateOrder' Boolean flag we can
0 @0 w" R6 t& o X // randomize the order in which the bugs actually run
4 @2 k' x: i6 D# f7 p3 { // their step rule. This has the effect of removing any' Z5 A8 X) X# J$ s+ M1 x
// systematic bias in the iteration throught the heatbug( R8 \2 Q' K' }. U( ~
// list from timestep to timestep
2 w5 `7 p9 w: w% S2 R( S
8 m, }+ N7 \- l* J$ Z. u // By default, all `createActionForEach' modelActions have
; u5 E, ~1 m0 N( U c8 K! R // a default order of `Sequential', which means that the- I+ b5 n3 e; @' K% H
// order of iteration through the `heatbugList' will be
, n* F$ Q- ^3 v. ?* r8 T5 C# i6 Q // identical (assuming the list order is not changed5 l- S4 P4 t7 L) g- a
// indirectly by some other process).
9 |- L/ G+ J, s , b( C# \' U, m3 _
modelActions = new ActionGroupImpl (getZone ());
9 |8 m Z4 D* {" b4 R5 p% `, c! J3 D. p8 N
try {
+ }# H) L& X9 c( O9 W& c8 j modelActions.createActionTo$message) W0 X& M$ A6 o
(heat, new Selector (heat.getClass (), "stepRule", false));3 o2 m& m6 p8 ^) c! i3 K, t: |
} catch (Exception e) {: k) l. A/ T% w9 Z) x( Y, @7 g' g
System.err.println ("Exception stepRule: " + e.getMessage ());
9 } P8 z# R t) Q7 ] }7 n v1 `; n2 |6 E4 H) J) ]
' i! L0 g* u" S
try {0 V; G7 K" ^7 O$ O0 H* a* D5 }
Heatbug proto = (Heatbug) heatbugList.get (0);& v& @0 ` W2 I& n$ j
Selector sel = & o& ^# P% G5 J/ ^( U; i
new Selector (proto.getClass (), "heatbugStep", false);
) w% |; R4 }8 n" ?+ a actionForEach =8 Q4 q# D- l$ P( v
modelActions.createFActionForEachHomogeneous$call9 I4 H" O4 T2 r# j* P, Y% c% D
(heatbugList,
+ f- C5 a1 G+ {, [ new FCallImpl (this, proto, sel,% Q# s8 q; K% H& `: v
new FArgumentsImpl (this, sel)));% a+ k" @$ E x8 u* o$ K& S1 |
} catch (Exception e) {
- W3 J6 W) U+ L. c1 X, F$ h e.printStackTrace (System.err);
. L& l3 L) ^. x }) m# x- y1 h% w7 Z" H% @" g
* q: w/ [; ?4 z% S9 L- D syncUpdateOrder ();
0 Q* R1 s8 `4 U Z9 @4 q3 c% u. Q! i2 Z! r: a5 ^: b, v
try {
: q3 Y1 g9 a$ S* q5 Q6 U8 t modelActions.createActionTo$message 1 ]+ @, b/ \1 c: m5 J: x# h' i
(heat, new Selector (heat.getClass (), "updateLattice", false));! W2 _4 M8 k7 s4 g
} catch (Exception e) {% F8 w) t5 J' l% U- K+ W1 t1 Z
System.err.println("Exception updateLattice: " + e.getMessage ());
( y4 A) M: C2 J }
. ?7 f# F2 X" \) ~7 Q" y7 L! o
5 r6 ~1 ~; d- c& i // Then we create a schedule that executes the6 O# j4 {) R: x; ]* j/ ?7 j
// modelActions. modelActions is an ActionGroup, by itself it$ E! _4 m" \# D9 k
// has no notion of time. In order to have it executed in! j7 }) Z! b8 u1 l
// time, we create a Schedule that says to use the
! I5 [ I; {. u7 F // modelActions ActionGroup at particular times. This
. c4 H0 W% F2 B3 s+ a7 k1 F0 D/ n( U. [% F // schedule has a repeat interval of 1, it will loop every7 a1 O' ?$ c( B9 J" e7 ?
// time step. The action is executed at time 0 relative to- P& C& g$ E& p3 j* f4 h
// the beginning of the loop. O/ g! m8 R. J$ f
. c* M' X v: l9 r // This is a simple schedule, with only one action that is8 f$ J1 O. Q6 C- B+ P/ a; z
// just repeated every time. See jmousetrap for more
6 A: m! d. t" R. h // complicated schedules.6 J+ b, e1 T1 ~. {" u
4 }6 e$ r: I9 P7 \
modelSchedule = new ScheduleImpl (getZone (), 1);
$ i ?. v1 x7 L$ W# v' b. C& u- a1 E modelSchedule.at$createAction (0, modelActions);
% e- ]" z3 c( h3 l * A2 m, d$ G! `
return this;
$ D8 G& a3 n: V2 i } |