HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: Q* {$ W, R! D5 w0 U, S5 d, X5 @6 a
" M, R( W Z2 `% Q public Object buildActions () {: q( w* y5 U- c x3 {7 c9 P
super.buildActions();3 [( N D6 ?9 K# A5 Y' e
8 Y/ s& Z8 s5 p+ K9 Z+ [
// Create the list of simulation actions. We put these in
0 J h2 K/ y0 y // an action group, because we want these actions to be& W5 c' M3 s: K/ S% [! S" @& w
// executed in a specific order, but these steps should
4 J. }( p0 |; |9 W% h1 F1 m1 z // take no (simulated) time. The M(foo) means "The message
& i8 l8 H( d/ [+ E7 p5 N/ U // called <foo>". You can send a message To a particular* y( E0 [6 ` Q @
// object, or ForEach object in a collection.
( H" l: g( j, c: q: q/ s" M0 }
5 ]. P h% Q* Y) G/ n( `/ I // Note we update the heatspace in two phases: first run
# L4 i3 I. f4 |# K1 [- s0 a // diffusion, then run "updateWorld" to actually enact the( k, w; e, ^2 m" N' J
// changes the heatbugs have made. The ordering here is3 H) J# t5 A- M5 t& u9 N1 Q
// significant!* M+ l- [; \5 S0 U
- {" j* J& A3 D3 [
// Note also, that with the additional2 e" P# R# v/ @( k
// `randomizeHeatbugUpdateOrder' Boolean flag we can
) i1 ?0 b$ t8 _0 X // randomize the order in which the bugs actually run& o- i4 Q1 z3 G! t& ]3 O: z2 V0 s
// their step rule. This has the effect of removing any
5 _# q1 b$ P2 f- M2 s O- R // systematic bias in the iteration throught the heatbug% a$ H9 D5 @9 N6 O$ r
// list from timestep to timestep
' v0 K! n. d$ f; k! O+ o y" b, U1 [7 b, I
// By default, all `createActionForEach' modelActions have6 n8 \3 i4 I( Z/ C! l/ J4 b) M, D
// a default order of `Sequential', which means that the F, u6 _1 z9 x& L
// order of iteration through the `heatbugList' will be
1 P# g) |) m$ ~' |' [& e // identical (assuming the list order is not changed
R0 a( [$ \# ?1 ?3 k // indirectly by some other process).. d2 J5 K0 _8 M! M9 s
8 Y2 m2 V0 u% F; ]; Y) i
modelActions = new ActionGroupImpl (getZone ());
# B- ~% M/ g& N* o% [6 g7 w3 w! O5 `; w' O4 O; |; Q3 j* D9 y. Y9 u
try {) c* I% ^) P O+ L) N ~7 y9 r
modelActions.createActionTo$message; P" L; q, F* }6 s" [
(heat, new Selector (heat.getClass (), "stepRule", false));2 ~1 A6 V% A \
} catch (Exception e) {
0 x/ _. F. I. \3 N/ ]& ^ System.err.println ("Exception stepRule: " + e.getMessage ());) w# t; ]8 `% p. M* E& z1 {4 A8 y, X
}# D. U f' b# n4 t
( e+ g3 H8 `" F4 z3 Q: I, o try {1 r3 K7 z K7 V- d# Y
Heatbug proto = (Heatbug) heatbugList.get (0);
) x4 r; |: t. r* L _3 n Selector sel = , z5 e+ ~9 p& L4 R2 Y! V
new Selector (proto.getClass (), "heatbugStep", false);
8 U U. y- j7 j6 h! S. m actionForEach =( G! p: F0 Q; s$ T1 Y- H) V x
modelActions.createFActionForEachHomogeneous$call
* B w0 D0 i7 l9 V* g+ g' V (heatbugList,
9 ^! _# E/ O; ~ new FCallImpl (this, proto, sel,
) d% W, G$ T1 @6 S, ]. Y5 R- K new FArgumentsImpl (this, sel)));
8 C' V: }9 F1 E7 F* g } catch (Exception e) { S" u/ ?6 W9 i8 S
e.printStackTrace (System.err);
8 c8 o# A* N5 _( u! r }
( ~' x7 v; B3 g# a7 z6 a! {
$ P: K, L* o. v* }1 B+ w syncUpdateOrder ();
5 m, }5 B% z0 c5 ~9 r
3 x( l9 M' V# C4 w5 z try {
; Z& ^8 a! F" _" u# l modelActions.createActionTo$message ( ?# H5 E" @3 ^& f0 J* y8 y
(heat, new Selector (heat.getClass (), "updateLattice", false));
0 E H0 c/ Q1 j } catch (Exception e) {( }# V& u; Y3 T8 G5 k+ O) F
System.err.println("Exception updateLattice: " + e.getMessage ());2 f; @; z" a6 g2 `
}
- s3 }# P$ f/ f$ }) k4 g 4 t: X+ L3 {" I- |. X# w1 Z/ E" ^
// Then we create a schedule that executes the
" Q6 L, ]1 {7 O9 z! l Y* W( Z; _ // modelActions. modelActions is an ActionGroup, by itself it
7 U0 j; z: x, e; o) ^5 D5 y // has no notion of time. In order to have it executed in
6 z1 S( o& }, ]% G) O. i: _ // time, we create a Schedule that says to use the( K D7 w2 s! A3 g U; x! S
// modelActions ActionGroup at particular times. This
# S, h6 p. U) [ // schedule has a repeat interval of 1, it will loop every( a$ a1 W0 E9 l1 x: `" _
// time step. The action is executed at time 0 relative to0 e: @& M( v% u0 _
// the beginning of the loop.
; O8 J3 r1 d) j2 R# {0 g8 k8 b* p3 R) Z: G
// This is a simple schedule, with only one action that is
1 m: P" b2 d( R // just repeated every time. See jmousetrap for more5 _8 C4 `4 M4 @9 v1 y
// complicated schedules.& [" @; L$ f n2 q* S
/ l7 y: U) d6 r e& v8 u; @7 c modelSchedule = new ScheduleImpl (getZone (), 1);
) k+ F/ t' k. ^( D. b! b1 }: M modelSchedule.at$createAction (0, modelActions);* x6 O' t9 g& v5 I$ `0 G' Y
_) x1 ?2 {, q. T; ] J% N) a# V8 S return this;
: x. q M3 x' \% a; P' g% E1 U; S } |