HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
H0 X4 s5 K5 \; N8 z& b# e
?) U& d4 x" H1 h P- n public Object buildActions () {$ b2 E0 F6 t1 w7 K0 c
super.buildActions();* x4 d- Z3 l, h
f, r5 h O; r6 X5 \; n
// Create the list of simulation actions. We put these in1 k! `5 S- A& N* v: z, e7 e0 h2 h% [+ F
// an action group, because we want these actions to be# y9 s; d4 j7 _5 a
// executed in a specific order, but these steps should/ O8 |, T) v1 r( h
// take no (simulated) time. The M(foo) means "The message5 N' |, H# S# p x
// called <foo>". You can send a message To a particular+ z/ m! _* T5 c2 L6 K
// object, or ForEach object in a collection.
: b- J( O1 m( O1 T3 c 8 h' {+ \& x# V* R, W
// Note we update the heatspace in two phases: first run
7 D$ [# y+ {, }4 ~# P m // diffusion, then run "updateWorld" to actually enact the' U- a2 x, z6 @# X# @
// changes the heatbugs have made. The ordering here is8 b) }0 h8 Q, B1 V1 }2 c* a( `
// significant!& t& }6 V4 X3 S+ Z5 ^; L# x* I' F
, V- Z0 R9 X- M$ F% o
// Note also, that with the additional4 b7 Z" ^4 k7 u+ F+ J
// `randomizeHeatbugUpdateOrder' Boolean flag we can' R3 ?0 q6 E6 ?+ N* `* X
// randomize the order in which the bugs actually run
; B# h+ h& c2 h6 H: q+ I9 ` // their step rule. This has the effect of removing any
3 Z% G2 y- t0 O7 { // systematic bias in the iteration throught the heatbug
6 F2 d' T0 n; a5 Q. R // list from timestep to timestep
* B7 Z' d: o, y) c$ F# t8 Q
9 f+ z7 ]4 |6 S8 k1 O& f8 O1 A // By default, all `createActionForEach' modelActions have6 E8 i6 H' H7 s" c( ~+ d
// a default order of `Sequential', which means that the/ ^# G0 H/ Z( u* a
// order of iteration through the `heatbugList' will be4 v) h% o) b% A( l# X
// identical (assuming the list order is not changed
' I) w& {/ o# K( p" I // indirectly by some other process).
! r. h) b( t" a( I" `0 @ u
" R1 ]' Q7 o; i V- P( t F modelActions = new ActionGroupImpl (getZone ());5 T( f5 w, o2 L5 \/ u) p$ D9 G
6 v) {* _* Q6 V/ m5 ] r- w
try {
' O/ i+ R9 r3 k! B5 F3 C modelActions.createActionTo$message9 F* r; k* I3 K+ x
(heat, new Selector (heat.getClass (), "stepRule", false));& U9 m' a$ b2 }1 }" q
} catch (Exception e) {# p! [ R- V4 v/ E' j& `
System.err.println ("Exception stepRule: " + e.getMessage ());
' O& j8 ^! g" z0 D4 e! v! t6 s }
' C' l' \4 ~) Y! Y" S0 e* T" b, L# X% p% Q
try {! p" O' E9 k, h/ H
Heatbug proto = (Heatbug) heatbugList.get (0);
; A+ L6 m6 r d' e- k Y0 M; N1 D6 y Selector sel =
" R3 S: j& N4 ?( L. J new Selector (proto.getClass (), "heatbugStep", false);2 n5 u7 D- [) n+ p
actionForEach =) L! J, E' ], U% D6 M
modelActions.createFActionForEachHomogeneous$call% F1 n' J7 S# v. K
(heatbugList,% ~5 e0 c' F9 ~. v, s
new FCallImpl (this, proto, sel,
# V2 p" ?+ p1 q2 e new FArgumentsImpl (this, sel)));. o4 s7 H2 L6 Z6 c
} catch (Exception e) {) M5 K- E" ^% u, o9 E) }
e.printStackTrace (System.err);
9 V; N5 w* b8 |- I }
) Y: b3 ?/ `% S) s1 F
% k8 s3 @" F/ n syncUpdateOrder ();' V ~2 j2 H% k0 W% I, |: {
8 N& I/ r/ E8 M6 h7 ^ G% b- R& K) { try {1 a$ I8 O- r5 u1 P3 ^, X) f/ g# Z
modelActions.createActionTo$message
" o4 O) j B M1 R) a (heat, new Selector (heat.getClass (), "updateLattice", false));& ?" W( Z3 J1 @0 v) Q- L0 o4 I
} catch (Exception e) {
' q! C% ]! x* z; o3 } y- K System.err.println("Exception updateLattice: " + e.getMessage ());. q; h( G# b# }
}
7 i. K' ]' C4 u) c8 B
8 H: \2 w* H+ ]2 p$ K0 P0 R // Then we create a schedule that executes the; G3 L+ ~" u+ p+ T
// modelActions. modelActions is an ActionGroup, by itself it1 r( N' d0 b1 E1 [
// has no notion of time. In order to have it executed in% ^( T) `. _& I+ {. m# i" ~" Y
// time, we create a Schedule that says to use the. @! i1 a$ i4 v9 K8 j& q
// modelActions ActionGroup at particular times. This# }7 p; c; ?( j
// schedule has a repeat interval of 1, it will loop every" w+ z( x( J2 i( \$ b
// time step. The action is executed at time 0 relative to3 l2 j1 B# }/ i
// the beginning of the loop.1 r( w, N3 l' B) ^+ W6 D0 E6 `
/ D+ M0 D8 a8 B) X
// This is a simple schedule, with only one action that is
9 |! r+ k% ^2 N; a C // just repeated every time. See jmousetrap for more
% [3 e8 \- m) t t* j // complicated schedules.( p, ^/ P, ]$ F( L6 C/ G* s! h
! F. s% L; A8 n/ A9 s modelSchedule = new ScheduleImpl (getZone (), 1);
& z- }9 l1 Y* V7 y modelSchedule.at$createAction (0, modelActions);* E' G) h/ c+ D' x* w
+ S8 m# m1 \* Z t* t( x- B
return this;
5 p( r2 {: C! V5 T5 d- w; ~* c } |