HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 S% m9 g# {! ~# T0 k
8 \, @, F8 F* l- m public Object buildActions () {9 F# Y1 B+ p3 }1 S, C0 e( C- `1 s
super.buildActions();
1 O% Q" t0 \# l5 } g
+ F# ?. S7 X# M // Create the list of simulation actions. We put these in1 o% c. o; U6 L- |
// an action group, because we want these actions to be; e5 d3 ^+ Q6 n+ w$ P; r
// executed in a specific order, but these steps should: B2 j7 J( \4 k. Y2 L1 h& J+ }
// take no (simulated) time. The M(foo) means "The message
& ]. n- z9 N' V v$ z4 Y5 ] // called <foo>". You can send a message To a particular
& K) h' L) N8 h1 @% I // object, or ForEach object in a collection.' I8 g9 ^/ g3 G5 A" C% Q
4 ` ] ^% V/ i6 V( J' z+ A6 Y // Note we update the heatspace in two phases: first run
& Y# J1 l/ J1 o# {" g% ?2 H% `0 I0 M // diffusion, then run "updateWorld" to actually enact the
/ @ u+ R; p/ \2 i$ @& \ // changes the heatbugs have made. The ordering here is
. E9 q! a0 W C. H // significant!
2 H2 a) A! q# u% |9 ~7 [' r
, \0 b" h6 j& _/ T C8 Z! k // Note also, that with the additional2 p) _. y( z7 l9 K) x/ Q
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. N9 Q$ }. L' \, s& h // randomize the order in which the bugs actually run# d! x; j. a$ @! g3 k4 O
// their step rule. This has the effect of removing any
% G9 B9 c5 _7 [# N# A3 j // systematic bias in the iteration throught the heatbug
/ F0 n5 @- W6 N# X" `7 T& r& d // list from timestep to timestep0 B6 J4 l& N- Y4 S, r1 B
- r q( C- \4 t& X6 a // By default, all `createActionForEach' modelActions have' V; c0 m4 W/ O0 U) a
// a default order of `Sequential', which means that the. B7 |' P5 G0 `+ \3 t) l
// order of iteration through the `heatbugList' will be7 n4 `4 G: v: l( j
// identical (assuming the list order is not changed5 g4 X! O1 U, w! h
// indirectly by some other process).0 Q( ?* c3 D2 I2 v) N5 h, ^
' ?9 R" v4 j W- y0 m, M( Y" _
modelActions = new ActionGroupImpl (getZone ());
s1 E$ K- T. g; a* `" H5 Q8 u7 [& D1 B. b; h( H1 R
try {
& f! h* N* T" E$ o/ D: j modelActions.createActionTo$message
, Z9 s3 l/ ^# F0 p P/ v( b (heat, new Selector (heat.getClass (), "stepRule", false));$ j: ]9 d) _& u' m0 f6 D5 o
} catch (Exception e) {! z% w1 |, F: E3 x
System.err.println ("Exception stepRule: " + e.getMessage ());% v2 A9 I0 C2 l* [. R* _
}
h6 z$ A+ |4 E) o4 S( y ?; }# W6 U4 f
try {
( x: a% R5 K; o, p) x1 m) S Heatbug proto = (Heatbug) heatbugList.get (0);4 [8 V: L# E/ b) Q7 z" H
Selector sel =
P5 F: Z9 }* i* R new Selector (proto.getClass (), "heatbugStep", false);. J- Y4 O7 B% G/ V) q
actionForEach = O5 j2 x6 y8 O
modelActions.createFActionForEachHomogeneous$call, S& i4 M* {8 c3 c- c, S' K+ Z- x
(heatbugList,
" z& Z6 r" i0 F: E; T O6 J+ W new FCallImpl (this, proto, sel,
/ X/ J7 u9 Z! { new FArgumentsImpl (this, sel)));
) v9 V W6 A/ _0 X } catch (Exception e) {
( G+ x& J E1 e$ F1 D7 [7 v; ] e.printStackTrace (System.err);
' M2 c# H4 [6 @, J- k/ T }
$ [6 }9 n+ Q9 T# C9 N. Z+ b$ `
- r8 W3 W* Q; F8 R7 N7 R3 v& T* R syncUpdateOrder ();
9 r8 D6 D# s, k g, ~, ]6 J- x1 ?- ~0 X$ g \
try {
/ F% F7 ~+ N; A1 D B! t8 T2 D modelActions.createActionTo$message T# W! }4 V2 a+ {, Y
(heat, new Selector (heat.getClass (), "updateLattice", false));
, M2 K: B2 [. ~$ T1 Y+ E" F } catch (Exception e) {7 f6 y( b3 \5 p! N: k: m
System.err.println("Exception updateLattice: " + e.getMessage ());
! J# f! ?' Q% P* r( S. Q$ X } `' ~9 k" B$ A6 M: S1 R# _# P
& P+ x2 T+ A2 \7 L9 G // Then we create a schedule that executes the3 h# h7 e" s/ G1 Q# Z
// modelActions. modelActions is an ActionGroup, by itself it* V: X$ }% x2 G9 A; w" t0 {9 g
// has no notion of time. In order to have it executed in, { V5 W2 Q% q& u
// time, we create a Schedule that says to use the% }7 p0 ]$ F# C4 ~. b$ e3 p* i
// modelActions ActionGroup at particular times. This
. g5 c$ k4 a. y! ^/ q // schedule has a repeat interval of 1, it will loop every. x* J/ \0 J+ i3 ?. R. z
// time step. The action is executed at time 0 relative to+ ?/ I$ s" X' h9 @8 u0 f
// the beginning of the loop.. \# S. G; B) M* T
, r. C J) Q# J% R: W
// This is a simple schedule, with only one action that is
" r( s4 E) E2 |$ C // just repeated every time. See jmousetrap for more
2 ~& p6 t9 v. k% T // complicated schedules.; H4 D% C( V2 ?, H/ Z
: E- p E$ Q0 I1 n modelSchedule = new ScheduleImpl (getZone (), 1);* ~- j! w$ q9 ^7 L
modelSchedule.at$createAction (0, modelActions);# q3 X, z, y6 u" S8 W/ w
5 j" {/ q+ h( T3 k7 @; c return this;. s0 Q6 f2 z* K {% O- S/ ^4 F
} |