HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' f# A# N8 N. k: `6 e! a( M0 [, v
* N9 x' J0 z3 Y; P; b/ C/ E
public Object buildActions () {
5 R$ r" x; ~% \; r/ p super.buildActions();0 g; R* f' A# O# l8 m( }) l) ?4 I
+ z0 \& [ a, X% m- X' U
// Create the list of simulation actions. We put these in
1 D7 Y) A7 O2 _4 [0 m, s5 _5 G* D // an action group, because we want these actions to be; G& B1 m, Q, ?" n0 w$ W4 j: ^
// executed in a specific order, but these steps should0 P4 a+ p8 }2 V- t( u. ?
// take no (simulated) time. The M(foo) means "The message
) z' n0 Y8 }& _2 v4 V // called <foo>". You can send a message To a particular/ c" b+ G/ [6 j
// object, or ForEach object in a collection.( W4 F/ Q4 X' |9 S7 o5 L
2 z* Y: G) z ~' B( J6 g r // Note we update the heatspace in two phases: first run# o, k% u) N, ^, w. J) A7 U
// diffusion, then run "updateWorld" to actually enact the
' l' F) B6 R' f; d# N1 k // changes the heatbugs have made. The ordering here is- y9 L- r* n6 B
// significant!% t# M& R* J2 m( {) \- e
+ |. D, @$ Q- C* G) W; N // Note also, that with the additional
3 ]- O- F% h6 s5 i8 u1 j // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ r7 v* k+ C! p6 f( u, Y+ G // randomize the order in which the bugs actually run
) R" u" H9 x2 K. h% y$ W // their step rule. This has the effect of removing any3 Y( _- T4 M8 Q7 v% Y* A) J- l
// systematic bias in the iteration throught the heatbug
D4 i8 ?: g. s E# w // list from timestep to timestep9 w% K7 l- a6 t: Q2 D4 m
' b' B: O4 \, U4 P: R& E
// By default, all `createActionForEach' modelActions have1 M6 [) R% W7 g) p6 E
// a default order of `Sequential', which means that the2 r7 [! l3 W1 w& C; v' }
// order of iteration through the `heatbugList' will be& ~/ l2 {- b. a* v
// identical (assuming the list order is not changed& m- }. q' s$ i; {. Y2 U4 N
// indirectly by some other process).
- k! `# }& H+ h5 S, h6 J- d ( ?& w' N5 I& }0 V& f" K7 y. q
modelActions = new ActionGroupImpl (getZone ());
. _; T7 r# d6 p; @* a# v4 @0 z
. E e. I& |- _# Q try {) g- g* k8 M% U/ M) @
modelActions.createActionTo$message) w, n* e& g3 E
(heat, new Selector (heat.getClass (), "stepRule", false));
; ~+ v. W$ H' @. _, v# ?& ? } catch (Exception e) {
, f( d+ A% k3 A- E% n: S' U System.err.println ("Exception stepRule: " + e.getMessage ());
1 {" M' B/ U' c; B4 D# g; Y }
' q. y! m7 |/ e2 ]- s& L$ j; s V8 R( R( J
try {
2 I+ z) P, W+ T" Y# E Heatbug proto = (Heatbug) heatbugList.get (0);: a. h0 p' d, V1 H
Selector sel = 7 _% h) u2 V1 A! y4 G# R
new Selector (proto.getClass (), "heatbugStep", false);
( B i5 a b) @ actionForEach =3 j" D) G( [% P1 F( \$ g$ ~5 Z
modelActions.createFActionForEachHomogeneous$call
2 H$ A% a9 H! v# J1 }, ?# ^ (heatbugList,
8 A* k# H3 H! U2 Z* j) I% c/ _/ Y new FCallImpl (this, proto, sel,5 E( {/ f" R# a/ K# q
new FArgumentsImpl (this, sel)));
1 g8 ?/ H0 @4 J/ w } catch (Exception e) {. C/ b' D9 e! y5 m. t6 r
e.printStackTrace (System.err);% W0 L6 L6 B; b& y
}
+ N% C G$ { g _* d7 `
9 z+ {0 K8 }! J+ d# } syncUpdateOrder ();$ D4 C/ ^5 P8 E
7 S/ x/ @+ z) ?. G$ a
try {
3 g$ Q/ n9 N6 J2 p# T modelActions.createActionTo$message
: ~! H6 ?( ?6 B- N# F% q8 F (heat, new Selector (heat.getClass (), "updateLattice", false));# g% z- \- ^! ^1 J J+ h
} catch (Exception e) {$ M( c. C7 ~2 g' v! h4 |
System.err.println("Exception updateLattice: " + e.getMessage ());9 J+ u: V: E1 Y1 g: C1 @. M1 j
}
2 v# `2 j, ?; T# u, d- E 1 Y2 [) A8 s8 ~( l$ L+ I4 p
// Then we create a schedule that executes the; } P: [0 R) }& E3 d8 I( I' n
// modelActions. modelActions is an ActionGroup, by itself it. x, f5 V) Z% E( A8 C+ K9 ?
// has no notion of time. In order to have it executed in
, K: p7 T; g+ X7 e! C // time, we create a Schedule that says to use the9 c5 u+ h: t7 ^$ {
// modelActions ActionGroup at particular times. This
% J- z' G, X& _# G# h# w/ G, B // schedule has a repeat interval of 1, it will loop every2 O' k$ \7 N5 p6 u* F
// time step. The action is executed at time 0 relative to
3 j, L8 O# U2 x7 M5 ~, x // the beginning of the loop.
7 `/ t0 L* {: S6 ]
" \5 _8 c) W% e7 A: C- @ // This is a simple schedule, with only one action that is4 m! k/ Y5 ?' J' C* `( f& b4 z# {
// just repeated every time. See jmousetrap for more
% \& t7 u" R5 ~' ~: F4 x0 k // complicated schedules.
0 y' i/ R+ \5 t( o8 {1 e
* b3 j& G1 }6 M+ E/ X# h modelSchedule = new ScheduleImpl (getZone (), 1);+ w# E a4 c5 J7 i( ?4 g
modelSchedule.at$createAction (0, modelActions);# h a `2 d8 N) x8 N# B" H
3 B: ^' j2 W# |/ a# O- l
return this;5 f7 i+ g# x' i$ I @; h
} |