HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' Q7 D& R x4 ^8 J* H
# U4 d' C D0 b2 D" t3 J: A public Object buildActions () {5 n2 @* x2 p; y8 j
super.buildActions();4 d3 t7 p' H( D6 S# _6 O0 C
+ [. k$ J# C% P# k // Create the list of simulation actions. We put these in/ O- L" ~7 ]# ^" C1 k
// an action group, because we want these actions to be
' v5 p0 H1 l' y7 s4 u2 M g$ o // executed in a specific order, but these steps should
' J+ K5 k i+ `" n; F, O0 j // take no (simulated) time. The M(foo) means "The message
5 t# A( G! ?; [6 `/ ` // called <foo>". You can send a message To a particular
/ }' e# |/ q- p) j" L) l0 t // object, or ForEach object in a collection.
+ z, t l+ n/ K, k5 @* K: e/ ~
/ ^, H7 ?2 l! `4 O% S+ M& t // Note we update the heatspace in two phases: first run q. e- E, g# }/ U K; Z
// diffusion, then run "updateWorld" to actually enact the2 |; {# p% i4 w5 a \% q* L% g
// changes the heatbugs have made. The ordering here is
1 g" {- I( C, ~9 s" N0 t8 g // significant!& a8 R7 D, u- i& y* H4 C( u
4 `5 F/ d, p) Z- O3 t" s7 I // Note also, that with the additional$ H' |6 f S4 L* B3 o( f1 [
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- I. D" b* k; b // randomize the order in which the bugs actually run
9 I M/ M2 q* ?2 ^! p- y0 J. m. u( F6 J // their step rule. This has the effect of removing any- F$ m4 ]/ z" t8 W x5 y |9 @
// systematic bias in the iteration throught the heatbug
4 h9 @8 Y g' R8 _' l4 `( r6 { // list from timestep to timestep' R& K% q1 C7 O9 v/ E$ b1 l
9 A7 }( K* p& W/ D. V c // By default, all `createActionForEach' modelActions have
3 e& R! @) K8 Y. m' G; k! p' y // a default order of `Sequential', which means that the
' P8 ~5 P8 B( v8 l8 f // order of iteration through the `heatbugList' will be
" }) h; M4 U$ ~; ?! d5 q$ g // identical (assuming the list order is not changed
( d' V# I0 Y) b- I2 Z // indirectly by some other process).
" i, B: ]0 ? I. K/ p
* j& L5 ]! ~( _9 o1 A" o* F modelActions = new ActionGroupImpl (getZone ());: S/ v3 l+ _8 V) M3 T
' |( u! G, Y' [: M8 u
try {
& Q' t5 }: ]; J$ ~, b! g1 L& ^6 i modelActions.createActionTo$message
, ~' A4 N ]" w9 ]9 ? (heat, new Selector (heat.getClass (), "stepRule", false));
; ^# l. X; d0 H' D } catch (Exception e) {% k: r5 ?% x, u/ U% ?
System.err.println ("Exception stepRule: " + e.getMessage ());3 Z% R4 M4 I3 Y
}
1 o$ F3 K s, F! E1 D
3 |8 q4 ~% C& @) K6 v4 `. [ try {0 Q; G* B w: g1 K( I/ s9 e# o
Heatbug proto = (Heatbug) heatbugList.get (0);
* x7 H/ S2 k# R, U; X" A' J2 S1 J Selector sel = + C; E( v% B7 x* ^$ ^
new Selector (proto.getClass (), "heatbugStep", false);$ o D# Q6 k7 h
actionForEach =/ h$ t5 p8 {" ~, [
modelActions.createFActionForEachHomogeneous$call; P: O# z- ?8 k! a' M
(heatbugList,- O7 O4 t1 N( e6 W
new FCallImpl (this, proto, sel,
5 a! ~/ S+ o7 @9 Y' c7 T new FArgumentsImpl (this, sel)));' x, t2 [" [4 A4 v: i
} catch (Exception e) {
1 B/ p3 q: {% ?0 p e.printStackTrace (System.err);0 u8 }3 \' }6 c0 H7 T0 |
}
* a: g5 E8 b' ]& X; h$ i! S( \6 g
7 `: {; M2 V' t" v syncUpdateOrder ();
% T: Y8 \3 V: b. C' `: m4 d* V4 j: f9 X: s u* `1 C9 H2 H1 ?* P
try {
$ l' n% I9 S( E" q6 _ modelActions.createActionTo$message
; ^' E1 `9 f& ]. a Y4 \ (heat, new Selector (heat.getClass (), "updateLattice", false));
5 d7 G3 V5 L4 G& D' c, w } catch (Exception e) {
3 a6 N' w9 J5 Y( X System.err.println("Exception updateLattice: " + e.getMessage ());& P+ c- g$ ?0 n
}7 B" O& d! s& g! W* a
0 B8 @' O' [* | // Then we create a schedule that executes the0 E9 i; |3 N& C4 _
// modelActions. modelActions is an ActionGroup, by itself it- b0 J+ S& @8 ~0 p7 B
// has no notion of time. In order to have it executed in
2 P' v4 l8 }! P // time, we create a Schedule that says to use the: A% ^( h5 Q& E% ~( f8 ]* y6 c
// modelActions ActionGroup at particular times. This
5 Z' N& M7 Q( ^( D8 o0 Z // schedule has a repeat interval of 1, it will loop every2 z5 \# M0 S4 L7 ?% V* C
// time step. The action is executed at time 0 relative to+ l+ ^; n! D$ V- o5 D5 E4 j5 H/ m
// the beginning of the loop.; l$ m6 B9 i# l& l5 t
. _$ N) F8 U. \7 i: ^
// This is a simple schedule, with only one action that is
- A$ ~( r# N3 d: u# Y/ L // just repeated every time. See jmousetrap for more
) v) o3 T' ~" E# ^. i0 x // complicated schedules.$ W* R* E& g- m' O8 n# `
7 [- f! r, o/ x w modelSchedule = new ScheduleImpl (getZone (), 1);' M/ A' B3 ?. |- C. [
modelSchedule.at$createAction (0, modelActions);
) w" w6 r9 X4 d/ W5 p# U# v ~6 w 4 ]' E/ a( n! Y! v9 @# {
return this;2 D" c6 U Y3 p2 T
} |