HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* @. x) u1 ~9 z* D! {+ z
) W/ ]. ~: `4 h+ f4 ?% J/ j" p public Object buildActions () {( j8 L& T- ~* v8 ^8 j
super.buildActions();$ j0 o. h% B: \, I
, D, y0 _2 W/ T% g0 A // Create the list of simulation actions. We put these in1 T8 r. d4 O4 ?' h7 Q. X* d d" P
// an action group, because we want these actions to be
R* Q9 ]9 C) T& j) l9 ^ // executed in a specific order, but these steps should
* G; j( {# z1 c1 G$ @ // take no (simulated) time. The M(foo) means "The message
! `* U7 G: F# F& o // called <foo>". You can send a message To a particular3 a2 J( f) x; q b k5 n
// object, or ForEach object in a collection.
7 e. X- V) I5 J& H9 S3 Z 7 A: S& y. S& l2 P
// Note we update the heatspace in two phases: first run
7 v% v8 g, g) z, c2 {$ X // diffusion, then run "updateWorld" to actually enact the8 D1 P# W, ~* R4 ?
// changes the heatbugs have made. The ordering here is. }( @$ {9 X! C; k. H
// significant! N" [+ ?- E+ J: L5 m$ h
# L: L* D2 Q4 l: i" i
// Note also, that with the additional
8 V7 M: z; t9 L, G& U" ? // `randomizeHeatbugUpdateOrder' Boolean flag we can X; B- V+ G3 u& F& }2 r
// randomize the order in which the bugs actually run
+ S- R6 Q5 g) X! z' [, E6 c' V# D+ l // their step rule. This has the effect of removing any
& E |, P1 H' U8 b/ }6 S // systematic bias in the iteration throught the heatbug
! K( n# W) X" M // list from timestep to timestep
z, b( Z" U) z% k* P# s3 R% p
0 S) y: J( \2 \/ Z // By default, all `createActionForEach' modelActions have0 @, C+ K8 k1 a4 W6 i. h6 A9 |' g
// a default order of `Sequential', which means that the
" |/ K* c" q! m6 [& o J // order of iteration through the `heatbugList' will be
; J- u; ]9 C0 g3 K+ n$ U // identical (assuming the list order is not changed9 \% p5 U# H8 n/ W3 t% t
// indirectly by some other process).% ?& U a* L% F9 Z& J2 D
4 s, J! u6 f* s V, n; U( g' y
modelActions = new ActionGroupImpl (getZone ());& T) D5 d: ]4 T. Q Y& k
' r1 F" X0 {4 k
try {+ @( I: S0 O7 Q7 ?$ ?. L
modelActions.createActionTo$message( F- p- Q5 j6 x) H3 H
(heat, new Selector (heat.getClass (), "stepRule", false));
% Z! h" r" A6 m# }- e& [( I( ? } catch (Exception e) {
- B4 r; O* k M- T6 a System.err.println ("Exception stepRule: " + e.getMessage ());0 @4 K$ @6 {/ ^( a) j+ G8 r
}
) {! J+ U) n3 o ^9 s9 i# |# N0 Z, l& E+ R" p/ K0 k
try {
- ^& o/ }" x7 T' G- `1 u& o/ } Heatbug proto = (Heatbug) heatbugList.get (0);2 M0 w. n: k4 E# {8 o& B
Selector sel = 1 R* o$ W- }5 v, ^
new Selector (proto.getClass (), "heatbugStep", false);$ s0 r4 i/ Q" U% z4 D
actionForEach =
. [+ q: z" }- K' j/ ] modelActions.createFActionForEachHomogeneous$call
* I8 M# A+ ] s1 Y7 y (heatbugList,
8 w, z; ^2 n& J3 F. }$ U9 W. f new FCallImpl (this, proto, sel,
' e$ r( K U5 R+ q+ v new FArgumentsImpl (this, sel)));
/ G# G" f8 A! f q6 O% s7 F } catch (Exception e) {/ K- g+ l2 i3 K: k4 R& i( U+ k
e.printStackTrace (System.err);! O g9 D1 E+ i0 `7 t* V
}# S7 J( o2 b( U$ Q
- q- K2 m2 i/ a9 j/ n9 ~ syncUpdateOrder ();
2 u! N: f _+ O+ T a/ Z/ q; o" q* Y9 ?* B% G
try {& J. v7 f$ C- p* P
modelActions.createActionTo$message
" Y) a% j) I6 Q: ?- ~' o (heat, new Selector (heat.getClass (), "updateLattice", false));
+ I' f4 ?1 @/ [# ]/ }( W0 L } catch (Exception e) {' `' U# Q$ D7 Y+ u* o2 n
System.err.println("Exception updateLattice: " + e.getMessage ());
$ G- P# X6 W! ?& f5 s0 j3 i# X }
" a( [* b5 l* e0 x# g% L + `$ r2 R/ S. s5 c% I
// Then we create a schedule that executes the
6 l. X, i2 p# c9 L1 N" m* @- X0 u // modelActions. modelActions is an ActionGroup, by itself it
3 ]! A8 J0 R) `4 M$ _$ X0 Q2 Y // has no notion of time. In order to have it executed in6 M" M* F' y0 m% ^3 f1 `. I
// time, we create a Schedule that says to use the) Q I: [* M6 {$ e/ f
// modelActions ActionGroup at particular times. This0 g0 q% y3 ~ f7 B: O- l( ?
// schedule has a repeat interval of 1, it will loop every) N8 t1 g* M A$ W2 w* |
// time step. The action is executed at time 0 relative to0 }# F4 N- K, X! m# c6 J0 y
// the beginning of the loop.
. c1 p( Q* V- @( r0 y% E' [: Y! @* k' }" Y' r' _& o$ u( Z
// This is a simple schedule, with only one action that is
2 \9 j8 V- l* q; @& j // just repeated every time. See jmousetrap for more0 f$ L, |; ^6 Y/ k
// complicated schedules.
3 P: F, A. K+ {: o, `! ~) T6 }
% {" l2 r3 n M6 ]1 D2 r1 w modelSchedule = new ScheduleImpl (getZone (), 1);
- W4 l) \/ G& L# @# O8 k modelSchedule.at$createAction (0, modelActions);& J( m# k% k, x% A7 J+ a5 P* x
+ J. V7 G% F. g* ^( S3 i4 Z
return this;, K" n/ ]" |4 ^" s
} |