HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. T" H" x# S" g7 w8 ?# e; A5 p
* D u! x N. ]. _4 Y4 g6 Q public Object buildActions () {7 ^0 s# l6 U6 n# N) |6 w: r; Q
super.buildActions();+ v" ?7 \# H8 \% x+ t0 s H, n
; n/ M1 [1 T" v. d E% ?( g0 \ f
// Create the list of simulation actions. We put these in
1 W0 m8 @+ k; j9 F* ^* c* h2 P( ~ // an action group, because we want these actions to be
z: L) H: m% B( V8 n. L# {7 G // executed in a specific order, but these steps should
: ? V1 D8 r* G8 C3 m& A // take no (simulated) time. The M(foo) means "The message/ ~1 Z" u/ g; O6 y& y) N3 p1 \
// called <foo>". You can send a message To a particular
) U: D' t; T! a) O8 W' A // object, or ForEach object in a collection.( J" N5 J( X1 j ?+ _
/ f# _% R' W* b4 \1 v8 H1 z$ m // Note we update the heatspace in two phases: first run
3 `& C" g$ y9 E* e // diffusion, then run "updateWorld" to actually enact the
5 Z R. J- D4 R3 H% R& t // changes the heatbugs have made. The ordering here is
8 U4 T8 G# Y7 Y, G8 ~3 n) u/ H0 S // significant!8 N4 E, G% h2 S
- X! H1 m$ M- T
// Note also, that with the additional2 G6 F! z- w6 B9 j$ E
// `randomizeHeatbugUpdateOrder' Boolean flag we can7 X; W2 L6 {2 E0 i; _
// randomize the order in which the bugs actually run/ m" y& O! A+ U: h5 Q6 o. Y
// their step rule. This has the effect of removing any
0 J3 p0 i4 W+ P* X5 G! U // systematic bias in the iteration throught the heatbug
2 {0 r2 _) K2 G! I8 C$ r // list from timestep to timestep4 R/ }6 l% w4 n% P. _# _
( J( N" Z# D$ g5 ?+ E$ k# W3 W // By default, all `createActionForEach' modelActions have/ H$ H( V# h8 _% h3 A: [
// a default order of `Sequential', which means that the9 m) G- @0 p% M' {; _4 y
// order of iteration through the `heatbugList' will be, V* ]' }9 I/ T
// identical (assuming the list order is not changed
7 D+ u6 ]) p5 t; }5 p( w // indirectly by some other process)." t, R$ \/ s+ e; f; q% }9 i
+ [! V1 r c: ]# T modelActions = new ActionGroupImpl (getZone ());
. D8 R5 F& Y- J. {1 I1 T; R0 c |1 i) x& |* ?' Y
try {+ f! L2 c( k7 K1 z0 T0 X( x
modelActions.createActionTo$message
8 n$ x1 O. T M$ Q4 W5 k1 h( ~ (heat, new Selector (heat.getClass (), "stepRule", false));) M$ X/ U0 G, G9 c8 e5 E
} catch (Exception e) {
4 A- d6 _' X6 g9 R+ B4 s( C! q1 G7 ^ System.err.println ("Exception stepRule: " + e.getMessage ());' @6 ^$ S% \0 J! {
}
) N; B) Y2 Q+ V# U9 x, X3 g
4 t5 W7 k* D; J/ J S3 X try {1 q7 f G* t6 t3 r
Heatbug proto = (Heatbug) heatbugList.get (0);
7 ]1 S$ c3 J7 P8 Y5 u' G$ G3 \ Selector sel =
" h' x5 a3 S3 u6 p5 K) R% {. O new Selector (proto.getClass (), "heatbugStep", false);8 Z! ?0 x2 n: D; P1 O S
actionForEach =1 Y5 F5 {' G8 [+ a' v" ~
modelActions.createFActionForEachHomogeneous$call, t- W, ]3 v/ {& A R3 J
(heatbugList,
0 _, E. d4 H) Z- e new FCallImpl (this, proto, sel,
' }' S+ Z/ |- a1 ]# Q q( W new FArgumentsImpl (this, sel)));; o: C5 B+ O5 w: l
} catch (Exception e) {
# r+ K8 ], P; t. { e.printStackTrace (System.err);
. ~7 |/ W7 e5 e: I } }. y& X" f7 k9 O
# I2 W; P3 u: f& a syncUpdateOrder ();# t$ A2 s) g8 q7 r$ m* }+ T4 F( ]8 ]
8 m1 ~ P, A; h6 h/ ^* o
try {
1 @: M/ ~) t8 Q modelActions.createActionTo$message
# E* J; u& x- G (heat, new Selector (heat.getClass (), "updateLattice", false));
4 e9 b1 V2 v4 J } catch (Exception e) {3 }' F; v7 C. u( d3 q6 D9 p- h. u
System.err.println("Exception updateLattice: " + e.getMessage ());
" P( Z$ U: Z( k; S; {: A9 Y } p: d# c# ^5 l5 S8 u
3 Q1 { [0 b! i- O5 \( D3 ^4 @# [: d
// Then we create a schedule that executes the1 N2 V% y) W! v' R8 [" C' m
// modelActions. modelActions is an ActionGroup, by itself it
# t' [/ j0 e) m M1 D: J // has no notion of time. In order to have it executed in! o3 t8 S* q0 [- H3 b
// time, we create a Schedule that says to use the
! {# G i& U; i6 P8 }4 X H // modelActions ActionGroup at particular times. This
; Y! Q6 @0 j, g8 m4 k8 l! B( n // schedule has a repeat interval of 1, it will loop every6 m7 ^2 J9 p, j2 u& t4 f
// time step. The action is executed at time 0 relative to# v4 s) r$ ^! {" c& G- o- v6 Z% u D2 x
// the beginning of the loop.) T9 d% Q7 Y1 g& C" d* U+ C& ]
- M5 F& z5 l3 b9 ]! V+ ?$ { // This is a simple schedule, with only one action that is* _2 Q# r! {3 e4 p4 y d) \# a
// just repeated every time. See jmousetrap for more
( I4 M- o1 j: C0 B' R3 J1 k5 V- ] // complicated schedules.0 l5 J$ E! \- C$ D0 ]
8 |/ B3 s- w& x+ W/ u
modelSchedule = new ScheduleImpl (getZone (), 1);! @ D$ _1 R9 ~7 D- |
modelSchedule.at$createAction (0, modelActions);
- d- b& d0 U8 K; d* l! l' }0 P: v
% j W6 @" Q w3 y return this;
& z. q2 |$ O: _ } |