HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- c$ X& X# C. X+ f7 i9 C. _8 e4 Y/ E/ E1 A$ A; ?$ I
public Object buildActions () {
3 }! H' T0 q1 Y super.buildActions();1 x5 _4 M1 v+ J7 l r
, Z# _/ S6 F) o) C5 m
// Create the list of simulation actions. We put these in
1 _" _/ U6 S2 F! X R/ B // an action group, because we want these actions to be8 V* i0 b/ z# J: b. b/ e" y h, u8 Q
// executed in a specific order, but these steps should
& |2 z* R# d9 P4 }/ Y // take no (simulated) time. The M(foo) means "The message
% {0 T; b& v, h: w% d // called <foo>". You can send a message To a particular0 Q+ d4 d6 q; M' [6 g8 _% l9 P
// object, or ForEach object in a collection.' O! X P0 j( {* |5 q3 Z
' T4 P2 w$ y( _; m! l // Note we update the heatspace in two phases: first run: d2 g$ ^) s; k" p# p, j6 s0 a
// diffusion, then run "updateWorld" to actually enact the
" @/ z# Q( @/ V; c4 Y% D- A" I // changes the heatbugs have made. The ordering here is
' J$ w; j6 Z, h; h4 F, X // significant!
# P3 U; W0 J) g, s7 X! t
+ R/ J2 N8 K# w' U. o. u // Note also, that with the additional
3 m0 \2 s! k3 r/ O+ ]: X // `randomizeHeatbugUpdateOrder' Boolean flag we can
; A( Z0 ~ e4 j // randomize the order in which the bugs actually run
8 W" Q* ?& ?9 C // their step rule. This has the effect of removing any
( D7 l% s0 U) m( z8 S, { // systematic bias in the iteration throught the heatbug) Z. X8 v: Z, ]" m. I0 e4 ]
// list from timestep to timestep
' x: h) L; s* [- g6 a7 t, w( g
0 s4 J( U) l4 X' b // By default, all `createActionForEach' modelActions have% B$ o. O: ~3 ?& p
// a default order of `Sequential', which means that the4 L/ t) n: ^, b( l3 v
// order of iteration through the `heatbugList' will be
% C7 [8 s- t6 r8 |# R, q: x // identical (assuming the list order is not changed3 u2 \0 ^3 Y) W8 n% A5 \3 Z' g
// indirectly by some other process).
) y. k5 e) u, r' p& X1 c
& j5 |% {1 A! b modelActions = new ActionGroupImpl (getZone ());0 a4 W) O7 X- o1 s! `
" V& a2 x+ L0 } try {
% r: S" p( y: ?" q$ n( r- h modelActions.createActionTo$message1 \+ O- b! q5 j7 {; ^
(heat, new Selector (heat.getClass (), "stepRule", false));8 j' h, v3 O& g
} catch (Exception e) {
5 O# v5 Y) S7 z8 y- [ System.err.println ("Exception stepRule: " + e.getMessage ());
/ j2 ?6 |4 C' l }) u& C. Y% `1 `- \3 ^" s+ G0 h d+ o! s
7 \- c4 [* Z& O try {
9 R$ ^% C# ]! E Heatbug proto = (Heatbug) heatbugList.get (0);
( h, @7 `0 @5 [) y. u Selector sel =
' |, U) H7 r$ g6 O0 v: [5 z new Selector (proto.getClass (), "heatbugStep", false);
0 C5 M1 h! n# ] H# ^: h actionForEach =: @* k& j, `, t4 b
modelActions.createFActionForEachHomogeneous$call
4 B. r/ b& Y2 O; n) `/ z2 j (heatbugList,
- d. m4 B, f, ] A. b% x& }: H new FCallImpl (this, proto, sel,
+ o2 k: J S) X, G( h# i, F new FArgumentsImpl (this, sel)));( Z y# d, n. ? |, A
} catch (Exception e) {
' L' X) c, B) A ] e.printStackTrace (System.err);
5 U3 P' J1 _: i( z. |# F* b5 z# S }
0 B9 k! ^8 g9 d) U F) v4 f
# u* o4 f# N, \4 C0 M syncUpdateOrder ();
8 I" `' L' W7 |9 e1 ?1 f! S! ^) V$ _" [" D5 ?. Q E f* j7 |
try {; I4 q: {$ B' C6 v
modelActions.createActionTo$message & I0 {5 j0 {0 ~0 }3 d- D
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 \8 ?; U3 A( H7 I8 W } catch (Exception e) {
/ f) X0 V" R5 q. C1 H System.err.println("Exception updateLattice: " + e.getMessage ());' A& @, }8 q# d* N0 c& K. s, k
}0 D& v! m, [: D/ ^: h; {
, @ f: a. M' m, x$ w* o // Then we create a schedule that executes the
V! r1 N) W6 x$ S8 Q7 e // modelActions. modelActions is an ActionGroup, by itself it( w+ h" g6 r* M1 a* C
// has no notion of time. In order to have it executed in, \* `3 v4 X, q
// time, we create a Schedule that says to use the
5 f8 \5 Z% Q- \' A1 V: l2 y // modelActions ActionGroup at particular times. This- c( J2 h- M% F! w5 l
// schedule has a repeat interval of 1, it will loop every, ~+ y. @" Q4 ?+ T
// time step. The action is executed at time 0 relative to
3 Y! ^. b+ P, h9 k3 A+ P // the beginning of the loop.& M" z5 [; b+ ]+ O N3 @
! |; P- o3 G* c$ B) e& l7 ^ // This is a simple schedule, with only one action that is
3 G2 w: j4 A* g // just repeated every time. See jmousetrap for more
4 Z9 b- O2 n9 Q* b; y // complicated schedules.
/ h7 {7 J; Y4 T. u' ~5 \/ I7 M/ p ! C9 G, g; s9 r) [
modelSchedule = new ScheduleImpl (getZone (), 1);+ }' {5 {. ?" [! \9 d, h, j- c
modelSchedule.at$createAction (0, modelActions);& v1 {& G/ I/ Z! d; O; `9 P
3 X6 M* R+ u; [3 l" y return this;
2 d6 I( E4 z+ L- L s5 A } |