HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ z) T9 R+ H" L/ d# |! O3 H
1 H' X( I8 Z& `" p3 S8 Y public Object buildActions () {6 F% T; L3 n, X1 l7 s5 f
super.buildActions();. k) F8 n. Q6 h6 _2 Y
% Z5 p. B4 k0 d& O% [
// Create the list of simulation actions. We put these in
$ v4 O$ R: B4 o // an action group, because we want these actions to be7 Z" \' Z/ E# `- D, a$ |: W
// executed in a specific order, but these steps should9 U m$ y( v$ `% [+ c) ~1 ~
// take no (simulated) time. The M(foo) means "The message
+ j) d+ X; C- T! d, U9 l7 T // called <foo>". You can send a message To a particular
# Y: Z0 ^) N3 d1 s1 K' U6 A4 ] // object, or ForEach object in a collection.9 {. |8 e6 P* H" R8 ~
0 k7 [6 @0 j8 l, u; J
// Note we update the heatspace in two phases: first run1 n$ l9 w! X+ p) g8 g! H9 P' g
// diffusion, then run "updateWorld" to actually enact the
# p* g: `0 G$ A, k: B. J // changes the heatbugs have made. The ordering here is5 V. ^0 R. _4 m) f9 I% q Q
// significant!
3 v$ }% {! l6 O; M6 M
; @% I n7 d# l3 Y; F( w // Note also, that with the additional
' ^/ \* ]( O c2 o' X6 w' v // `randomizeHeatbugUpdateOrder' Boolean flag we can( S' z' D" r8 s( n
// randomize the order in which the bugs actually run* q; o* V+ N4 E$ R+ ^
// their step rule. This has the effect of removing any
7 ]; |/ z3 H* t // systematic bias in the iteration throught the heatbug
% ` ]- O2 B# c6 m // list from timestep to timestep
+ m/ | e: l$ U' B ; P# n- I! s5 ^
// By default, all `createActionForEach' modelActions have
+ M+ Y! x, D( K6 h // a default order of `Sequential', which means that the& Z7 ^* `2 C. b4 I. `% c
// order of iteration through the `heatbugList' will be
: |; G/ ~5 g2 i! y2 n. C // identical (assuming the list order is not changed
1 d, J( U: }: d F; T // indirectly by some other process)./ M* a# c, \8 O/ H5 H
8 B6 k/ o& N$ W2 G0 A
modelActions = new ActionGroupImpl (getZone ());0 ?4 @7 n& @3 b. H! D- q4 \
( u3 ~" A/ ^" c- Y; h7 g3 l
try {
* @5 t. M& X' R% H modelActions.createActionTo$message6 B$ W4 e0 S$ B8 p
(heat, new Selector (heat.getClass (), "stepRule", false));
# P1 f( }* `; s6 `8 X" V6 S% ?# u( X } catch (Exception e) {
( r/ m5 u5 A3 a System.err.println ("Exception stepRule: " + e.getMessage ());
- C) ? a2 U3 H; K$ A) k% | `; z }0 S8 ~( I+ T) i9 Z' D6 j% _
6 b( I" L" v' L% Z, I2 E% b- @5 _ try {
( _ [" Z3 {+ W# n. M5 I! Z# h+ f Heatbug proto = (Heatbug) heatbugList.get (0);
( ^, @" @: c* k9 y Selector sel =
/ i9 D9 n, i V- w new Selector (proto.getClass (), "heatbugStep", false);/ g# n, t! h7 L9 W: [ c, Y
actionForEach =/ y) L! T- m0 w5 v& V7 [3 w; _
modelActions.createFActionForEachHomogeneous$call
" M) ?% M c$ j) q' [4 M (heatbugList,: ]6 i3 m! H4 s0 p% g3 c3 c
new FCallImpl (this, proto, sel,
8 g$ {! d# O8 ^8 e new FArgumentsImpl (this, sel)));# X3 ?+ D# d. A9 Y: Y- j; @
} catch (Exception e) {
7 t6 V8 ^0 Q7 ^4 h/ D" l e.printStackTrace (System.err);
$ }9 u' U- o. a( ~* C4 y# _ }: D6 E! o$ I. r w- n$ G
; p8 ?6 h% y/ [: H3 r6 O' ?3 z syncUpdateOrder ();7 f- |8 ?& u) m% C
! A+ y( f/ k! j( e) _1 t try {. |6 M! |$ [0 X, s
modelActions.createActionTo$message " m4 Q5 G$ P* V% Q, h
(heat, new Selector (heat.getClass (), "updateLattice", false));2 |# K% D. q$ j, l( i7 ~4 E+ C% v) p/ d
} catch (Exception e) {$ _$ n0 Z- N6 m$ x: e7 n
System.err.println("Exception updateLattice: " + e.getMessage ());; y) ?# t- q+ X Z6 a
}
. D* V' ^) c: d; d* v9 q1 z 3 I! {7 d1 X# A) V3 C$ g4 |" N5 q
// Then we create a schedule that executes the
# P. u% p. B, ?/ G6 M- | // modelActions. modelActions is an ActionGroup, by itself it
$ y+ c U& L- ]- I* O# E // has no notion of time. In order to have it executed in# v$ F/ _' J. u) k8 ^6 H' z- T! ?
// time, we create a Schedule that says to use the
: P8 U0 f% X1 v _/ C n: G$ o- r // modelActions ActionGroup at particular times. This3 Q4 s6 w& e' v& k
// schedule has a repeat interval of 1, it will loop every
+ u K* k& {% T$ S3 @* t+ z // time step. The action is executed at time 0 relative to- x0 j: b6 _$ H: V7 P4 {; ?9 \* n
// the beginning of the loop.
: I; \$ B: M! i! e. E# R1 R3 C
! b2 i) N9 h1 _: H7 p // This is a simple schedule, with only one action that is8 d: j2 Y" P5 P
// just repeated every time. See jmousetrap for more% r/ M2 N2 }8 W; ?3 j" p6 c
// complicated schedules.
! P" z2 n, ]2 L& U# @
: y' I. g$ M7 y: z& y/ e modelSchedule = new ScheduleImpl (getZone (), 1);
7 d0 L4 `: [' B' [ modelSchedule.at$createAction (0, modelActions);" ]& A! [! [/ o8 R
5 j8 z2 i! j+ Q9 b- {8 ^9 W! ^
return this;9 W' l, h. r2 L9 q# b) Z0 a
} |