HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# x2 v) A7 H* r& x2 L
, u5 ]2 G# w! H- Y* n! @2 D
public Object buildActions () {
; }6 r; F( n; L K super.buildActions();, I: J. F" M; o/ A" t. K1 N; e1 P; o8 h
, m W, {1 w8 E+ Y; l) z" e9 ?, Z // Create the list of simulation actions. We put these in$ m7 j2 d A& _% W' g* }) k
// an action group, because we want these actions to be
( q* ?& ~/ B5 {- }& M2 [2 l // executed in a specific order, but these steps should
~! c8 _# `1 r/ Z3 p+ L // take no (simulated) time. The M(foo) means "The message
: {: P$ ^7 u8 t+ @0 e, F4 Q" V // called <foo>". You can send a message To a particular. d u8 B& ]0 V! J5 _- l" V
// object, or ForEach object in a collection.
4 s2 [7 f, B5 ^2 X 4 q5 P1 b( |2 x# }# B* i0 q
// Note we update the heatspace in two phases: first run
7 y5 L. W3 b" D5 ~0 f // diffusion, then run "updateWorld" to actually enact the
. r! m ^* a1 M$ b( d% ` // changes the heatbugs have made. The ordering here is
# C" J+ }( a' d% A+ @2 G // significant!
0 I2 N# \8 Z2 S; k/ k* C% p7 ^& o
! S) q. J8 b7 k5 c" _ // Note also, that with the additional
, s0 e+ ]+ K( c, c; M // `randomizeHeatbugUpdateOrder' Boolean flag we can5 I3 I% C0 _! e9 v) r
// randomize the order in which the bugs actually run
r2 p/ P7 r- g% E& ~ // their step rule. This has the effect of removing any$ P0 {# }+ {( ^7 q, d; Y) W8 g2 g
// systematic bias in the iteration throught the heatbug$ o) j1 n, C* Z" L* C1 \& A
// list from timestep to timestep
8 ] z! w ?, [+ n 5 [8 L; |7 I( z8 e! _3 ?9 g
// By default, all `createActionForEach' modelActions have
1 ]7 @% t. N/ t$ L1 R7 U4 y // a default order of `Sequential', which means that the# `2 {. h. j3 O0 G: @) g7 I" S* q3 |
// order of iteration through the `heatbugList' will be
! M! i& v) g4 ~6 U' K& b* q // identical (assuming the list order is not changed& b; C& F _- t% n
// indirectly by some other process).
; X1 C( g; \8 F, u( ~; w9 M) t: ]; A
4 N# f+ g& T- S1 F+ j1 g modelActions = new ActionGroupImpl (getZone ());, e C1 Q) r+ }; U
: ?% o7 j+ ?; O3 W+ K/ m try {: w" j$ x7 i& \: K9 ]* R, Y
modelActions.createActionTo$message
' Q d( q+ U- D) { ^' t- T ? (heat, new Selector (heat.getClass (), "stepRule", false));
6 [1 z% b4 P: u, q1 h } catch (Exception e) {
% g3 G) `6 L) r l2 y System.err.println ("Exception stepRule: " + e.getMessage ());
8 U9 ? N4 Z7 B, a& K- L2 ~ }
5 T# [1 d( \, x8 N
7 Q" l$ v; V) ~: g$ n8 X P/ w. H" J try { ?& i, Z& a5 c
Heatbug proto = (Heatbug) heatbugList.get (0);
2 c9 \2 ]$ l& v }# D5 R6 l Selector sel =
3 H4 S, y" [; P8 q2 z i5 ^ new Selector (proto.getClass (), "heatbugStep", false);
; @" r3 D/ V6 H& ? actionForEach =1 T! q7 y! E8 ]/ s' f; a! i ]$ r; S+ P
modelActions.createFActionForEachHomogeneous$call+ @* p1 N0 J2 R: z, g: i1 L9 f
(heatbugList,
" g6 s3 j8 G. Z" K$ U new FCallImpl (this, proto, sel, k& S3 B6 F+ R
new FArgumentsImpl (this, sel)));' i6 o8 j$ U) G* ] |
} catch (Exception e) {
) Z) W# [* k7 j* Y e.printStackTrace (System.err);" V0 ?$ p( W4 Y' Y
}; Y- S' m& _! g& \4 ?6 y
5 K$ }' f1 A7 w% x9 [7 j
syncUpdateOrder ();$ C$ ~ j* B9 S7 N9 S: R) p
2 R2 `1 h# V1 t, V- A& P
try {; `) O; [0 b' ~9 E5 v; L1 g" g; }- W
modelActions.createActionTo$message
) m" c0 _1 S# x5 J/ Q6 ~ (heat, new Selector (heat.getClass (), "updateLattice", false));
; k& U1 S7 C1 r3 v! m } catch (Exception e) {
5 s, B$ y8 B( B" O9 M& G9 e4 { System.err.println("Exception updateLattice: " + e.getMessage ());* g- j$ H9 K0 J5 _. }9 x l
}
# a) f( X- k4 |5 {' ~0 H ( o- c1 O4 j! G7 y# [8 J7 d
// Then we create a schedule that executes the- R0 D$ C+ b7 Q! J8 N I$ o; e
// modelActions. modelActions is an ActionGroup, by itself it
" ~& e" _# E$ W7 q // has no notion of time. In order to have it executed in
$ H; U% r7 ]3 t$ I // time, we create a Schedule that says to use the
# ]2 d& }' @5 ]( `# E; D, p // modelActions ActionGroup at particular times. This
3 A8 y) f% a( F) S0 N* h$ s // schedule has a repeat interval of 1, it will loop every
3 M/ ]2 i4 t/ S7 D# p2 O" d // time step. The action is executed at time 0 relative to
9 P% K! \6 b1 e' [1 L8 Q // the beginning of the loop.
7 M5 _6 `+ x# u4 B! h4 M4 ]
( ^* m% e ]: |* k // This is a simple schedule, with only one action that is; D9 D8 d% b8 `6 u7 K# N
// just repeated every time. See jmousetrap for more
9 S/ @+ W: @ e // complicated schedules.+ e1 I+ |8 n$ `! N' W5 g
4 {1 m r+ h ]2 b6 P
modelSchedule = new ScheduleImpl (getZone (), 1);
/ y, O' d! D$ D! D: K; i& L1 N: j9 K modelSchedule.at$createAction (0, modelActions);
2 ]4 Y0 M) ~; }' v6 x2 z 2 v A$ Y. G) e5 |
return this;
5 y9 T) J6 R0 U, |8 D } |