HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' ~$ S Q. W L5 | z+ ~6 D% {: Y
public Object buildActions () {
y; _1 i6 i/ j7 ]% A8 X super.buildActions();; M0 `( h5 r2 a. Q# w
6 w* j1 _; L9 K; f9 C% B
// Create the list of simulation actions. We put these in
C) ^" o: o& ~( o // an action group, because we want these actions to be
" Y, Y" C) Y0 w: _& U+ ^ // executed in a specific order, but these steps should- d; C/ B3 e" K. V4 s$ d
// take no (simulated) time. The M(foo) means "The message' [" k, ~0 V1 _4 ?4 [
// called <foo>". You can send a message To a particular% z/ K/ y/ {" v6 M& D4 x! p" D" o
// object, or ForEach object in a collection.
, D2 h: \! G- i# o7 e$ I9 H2 l& G
2 }& K# O; s. s // Note we update the heatspace in two phases: first run
8 N& h8 e+ T, f1 h" M: D3 Q3 e! E" _+ ~, R // diffusion, then run "updateWorld" to actually enact the* a7 }8 j7 _- r1 @
// changes the heatbugs have made. The ordering here is
8 _% \ C+ N3 W$ d" e. k // significant!: h( o& E! d8 O2 w2 t8 C
0 w- V2 P+ ]$ \$ Y8 h# I
// Note also, that with the additional
. C, C# J G& ]6 A" s% p2 t$ z* _ // `randomizeHeatbugUpdateOrder' Boolean flag we can; f3 }5 k3 d* U
// randomize the order in which the bugs actually run. P1 C4 \" E4 ~* a( D( Q) w
// their step rule. This has the effect of removing any% s0 e6 i+ ?! n$ P( W1 x8 X2 }
// systematic bias in the iteration throught the heatbug
9 d( {+ t$ \) u) b // list from timestep to timestep' s/ x3 A% G. @! W
# T- t; q$ {1 F5 F( m# w6 {: [ // By default, all `createActionForEach' modelActions have
6 B) K/ U0 p7 c // a default order of `Sequential', which means that the3 z" `/ d: u' V, y' k* p
// order of iteration through the `heatbugList' will be
7 M X0 R6 S) U4 {3 b5 E! a // identical (assuming the list order is not changed
, J; J, a, o3 @1 ]+ c // indirectly by some other process).
: O4 K/ U( U) F! u, I5 d9 P- { " a; w' Q- N) q2 a
modelActions = new ActionGroupImpl (getZone ());
1 O/ i% e/ y7 }3 ~
/ y6 g' Y8 j5 R2 z6 U3 A: ` try {7 @' Q, ?/ W( w- j5 s
modelActions.createActionTo$message- v. n( z) b7 K. H$ x- m
(heat, new Selector (heat.getClass (), "stepRule", false));( G' O+ T. ~. f8 M
} catch (Exception e) {6 E( r& L) B- [1 E6 Z
System.err.println ("Exception stepRule: " + e.getMessage ());5 |) q1 C/ J# N' S2 a
}
" S$ u4 H8 m1 e4 j
- n6 T- a2 q7 r. l4 u try {
/ w) d* Q6 d" {5 _& n; q* ^ Heatbug proto = (Heatbug) heatbugList.get (0);! O) V E( X. }
Selector sel =
" u( j7 Z8 E0 y7 W new Selector (proto.getClass (), "heatbugStep", false);
3 R+ Q$ P/ V" ? m$ F actionForEach =' w! _" x5 o; R2 c$ W
modelActions.createFActionForEachHomogeneous$call
2 M8 H, S& u* U! Y" M$ H" n (heatbugList,1 `) l! {# {8 y
new FCallImpl (this, proto, sel, G9 J: P8 J/ S
new FArgumentsImpl (this, sel)));
8 r% O9 W# s6 Q2 Z) o } catch (Exception e) {( C( u9 K, X1 U& b+ O6 F* Z7 k1 w
e.printStackTrace (System.err);
8 j* {/ C% Y3 L \" n }
% _: u! C. [6 g
% p$ f$ _9 f; j. q syncUpdateOrder ();! k& C5 [; ~4 o" ^5 \
2 ^0 j+ z I: X( O. Q2 ]* F try {! ^; V Q: X, Q2 e
modelActions.createActionTo$message * P, R- Q( t* g
(heat, new Selector (heat.getClass (), "updateLattice", false));
0 D# p! F- U5 E } catch (Exception e) {
0 ~3 U: O* M/ _. Q+ ? System.err.println("Exception updateLattice: " + e.getMessage ());
( w2 p: o7 {2 E2 J. r2 B1 _* u4 N& L/ r }5 Z5 ]+ O: H7 M" M
' E, x0 n: k$ d! w' H // Then we create a schedule that executes the
3 S/ U q1 K3 o% i6 }& B. V // modelActions. modelActions is an ActionGroup, by itself it
+ E j) p- T: ~+ ^( C& g# u0 v6 g // has no notion of time. In order to have it executed in! D4 {' R" u0 q. ^) d
// time, we create a Schedule that says to use the
! q1 J' H1 H! R& O // modelActions ActionGroup at particular times. This
; z _( ]! U3 M) g( l9 ?" R5 h // schedule has a repeat interval of 1, it will loop every# @& }+ [. k1 n. s' O2 S2 x
// time step. The action is executed at time 0 relative to- H1 Z# m3 z" V+ z3 M+ ?1 `
// the beginning of the loop.
8 f `! m* E; }; {! ?; b/ U/ z% \1 Y6 N: u4 x- \& M* _- Y
// This is a simple schedule, with only one action that is/ V! v, i8 a/ M* u% m3 `
// just repeated every time. See jmousetrap for more/ I: E" \, }0 c$ V7 F+ h, Y: H
// complicated schedules.
- w: F/ u# E% l# U; i! W
$ K# B; N! m3 ]5 u* D. y2 p; ] modelSchedule = new ScheduleImpl (getZone (), 1);+ W$ {0 G$ o, X A. Q
modelSchedule.at$createAction (0, modelActions);- T8 \: K5 y' X/ X( p. x- M* j0 E- Q! G
4 M" u) G, F* ^% y return this;
$ p8 ]! o* U8 { } |