HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 j' {3 R/ B I, a( P: w e! I H; i3 Z1 k! n2 I, z% a
public Object buildActions () {9 z4 V0 h( w0 @) {
super.buildActions();6 e$ K4 ^0 n" B0 W- _% x
2 H& ]. ~5 B' P. }+ K# R // Create the list of simulation actions. We put these in
; i& g: p! M3 ?- w5 a2 W // an action group, because we want these actions to be# J0 l6 E$ W1 @7 L+ Y% m/ G
// executed in a specific order, but these steps should7 z; F1 V7 ?' [/ ]
// take no (simulated) time. The M(foo) means "The message
q% }* Q4 k5 ^" z: _ // called <foo>". You can send a message To a particular X6 c: b S& i) y6 u3 \
// object, or ForEach object in a collection.! m# v; B3 L9 A! E
) |# H& B2 I$ ~7 x3 T7 a6 n // Note we update the heatspace in two phases: first run5 |6 r' D* F1 f) B6 u9 I# i
// diffusion, then run "updateWorld" to actually enact the
& f ^8 \! ^3 \5 V // changes the heatbugs have made. The ordering here is7 E9 R$ S- O0 n( z9 u
// significant!' \4 a& m- R+ ^, [
$ f1 T# _/ ~# \3 U // Note also, that with the additional
`/ t, _) L" A+ G // `randomizeHeatbugUpdateOrder' Boolean flag we can: G2 M2 l2 B! w
// randomize the order in which the bugs actually run( ]& E, ^! o* r& K
// their step rule. This has the effect of removing any# v* ]: A0 l/ h$ L2 s
// systematic bias in the iteration throught the heatbug" M; O+ T+ V- q: X
// list from timestep to timestep# A# \* B8 c# U7 @+ E7 c: j
. E" f4 ^- Z3 w
// By default, all `createActionForEach' modelActions have
- Z% B- D, k0 n. s/ ] // a default order of `Sequential', which means that the6 j% D( W3 B3 I
// order of iteration through the `heatbugList' will be
3 H2 e4 o- i" H# \& T4 v // identical (assuming the list order is not changed
3 ?8 ^9 _* u2 I8 @$ k! o // indirectly by some other process).
! i% m& e0 O$ ]
J# x. O& K$ a2 r' | modelActions = new ActionGroupImpl (getZone ()); G$ W5 H$ [: \! K* V/ O; w3 ?
* D5 s6 h" k; h1 ^% d, w try {6 X6 y9 c, j/ {; }# S' _/ |. I' I
modelActions.createActionTo$message
9 [6 P3 f# E/ Q1 e" E7 P (heat, new Selector (heat.getClass (), "stepRule", false));# ]; A. k+ n5 p" F
} catch (Exception e) {6 V; ^# v) H$ Y) |& S
System.err.println ("Exception stepRule: " + e.getMessage ());- e! t9 ~" A q2 C) V' L& g
}
/ e) h4 {, q" T$ C/ V6 o2 T2 ~; S) o% V- f2 `# n' I
try {: Q5 }) |+ \: Y- [9 ]
Heatbug proto = (Heatbug) heatbugList.get (0);
( @/ K1 V1 l' Q3 J7 k Selector sel = ' u* B6 R. u+ V; u
new Selector (proto.getClass (), "heatbugStep", false);
2 Y Y# k& @6 X5 d actionForEach = N3 K; K1 R# e, X* }
modelActions.createFActionForEachHomogeneous$call* V! {- m+ O2 V* _; q( o9 K" j
(heatbugList,
: h6 h5 t$ r' h new FCallImpl (this, proto, sel,
: D; z0 ~* c% b( [& u new FArgumentsImpl (this, sel)));$ V( L4 f& b( W ^* o* }5 C
} catch (Exception e) {1 F1 y) V# r' M- R& R/ K' m
e.printStackTrace (System.err);7 K3 {& p9 [9 I3 U( q: O9 o
}) Y7 t7 U9 N% A5 `9 ?* l% ~" B
9 r9 ~4 `- J: Q( K3 i
syncUpdateOrder ();0 h" Y4 d7 |) u' e/ w6 [
/ f% z6 y+ [; o' u' o2 l try {
' J. \" w- U, W! F+ u5 T modelActions.createActionTo$message # \$ @7 |3 E# p8 i) N+ f
(heat, new Selector (heat.getClass (), "updateLattice", false));4 P" p3 m+ e4 ~1 Y5 Z! h
} catch (Exception e) {
: U6 R4 Y9 X2 `1 m+ z System.err.println("Exception updateLattice: " + e.getMessage ());: o; U+ B- R0 t7 G- P( Y$ d% ]: b
}5 @- `2 L F" |1 M. I
. U v. K% W9 m+ r
// Then we create a schedule that executes the
4 |( a/ `; u: e d* x) O: E // modelActions. modelActions is an ActionGroup, by itself it
5 j8 S4 m' T/ D // has no notion of time. In order to have it executed in/ Y" }3 O! B/ A- f7 D0 K
// time, we create a Schedule that says to use the
: u0 q, F' N+ j: f2 u // modelActions ActionGroup at particular times. This
( f& `+ J" W# q; A3 \ // schedule has a repeat interval of 1, it will loop every
8 N+ G8 d0 u9 X7 p; {3 @/ ~ // time step. The action is executed at time 0 relative to7 `% d) m. z; v$ x% @8 _/ e; z( b
// the beginning of the loop.. r% W( p! t/ b" H0 P' a( M" ]
& {9 w! O4 N- { // This is a simple schedule, with only one action that is1 _1 w( e4 }& F% w8 g( Z
// just repeated every time. See jmousetrap for more
N; ?/ Q# s8 j // complicated schedules.
( n! ~+ g' W4 k4 { & {$ } R7 R+ [) P5 ]6 z$ u E4 N- I
modelSchedule = new ScheduleImpl (getZone (), 1);
2 t& [7 @. t$ r6 z8 {9 t3 a$ m modelSchedule.at$createAction (0, modelActions);* e( E) S+ e- I' F1 a1 R, N4 c' z
: J& X; y1 G4 b$ B9 O. q return this;
* r, N% p8 F# ~' Y* d s# l } |