HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) w2 Y5 n' j/ i2 Z5 l4 l& K5 ~$ R% A6 ~' t+ ~
public Object buildActions () {
2 H0 w8 @" X0 V T: m/ j super.buildActions();, f7 N; q6 l& e( a, B. Y5 m' O2 j6 i
7 p1 A" C+ `( X9 q' X // Create the list of simulation actions. We put these in
8 q8 A/ O5 w, M% t+ k0 V1 c // an action group, because we want these actions to be
$ H8 Y5 r! g# o7 V3 s9 l% t' r // executed in a specific order, but these steps should
4 B& a: ~& i T4 q! | // take no (simulated) time. The M(foo) means "The message
7 J$ a' l/ u8 v // called <foo>". You can send a message To a particular
& g" e( C) c# {( e F- y- T // object, or ForEach object in a collection.2 ^6 Q$ C7 }. j$ e1 M
* p1 N! K$ e8 E4 D6 q6 c
// Note we update the heatspace in two phases: first run
$ X7 D* a- Y& p# V" K3 s3 J5 E$ ~ // diffusion, then run "updateWorld" to actually enact the$ O* F+ o4 d$ P9 J- u% |2 w- {
// changes the heatbugs have made. The ordering here is, J M4 x+ I' ?* E) q
// significant!
+ a5 X4 S1 D( k% B8 [, e( Q $ |, L: x! ?9 \; Q
// Note also, that with the additional. E" E, J+ q6 }" X" t
// `randomizeHeatbugUpdateOrder' Boolean flag we can% Z! n: i0 Y0 L0 ?
// randomize the order in which the bugs actually run
; v6 ~' E: G. p7 R+ F7 s5 z // their step rule. This has the effect of removing any+ F8 Y3 {7 I/ m9 u4 V
// systematic bias in the iteration throught the heatbug
$ y# h+ ]2 W2 h+ G& l% R // list from timestep to timestep
& w- x0 E2 A4 K+ E% Y! j% n1 k
: F/ ^8 ~! d8 A* I // By default, all `createActionForEach' modelActions have
* I- |, B5 z0 v4 N0 f3 N // a default order of `Sequential', which means that the
/ a: [$ v2 G: ~/ f // order of iteration through the `heatbugList' will be {6 c$ U) H! z1 G
// identical (assuming the list order is not changed2 A0 g% z' m# j2 \9 E
// indirectly by some other process).; @9 Q2 Z$ c9 Y% I w+ q
6 e) R' v% P8 [* [ t, y
modelActions = new ActionGroupImpl (getZone ());
8 Z0 ~6 x8 ~7 J" W: m2 C7 C# j9 ^$ q4 {4 g- {/ g
try {0 @# i# X# {( H9 R$ d
modelActions.createActionTo$message
( B( ^ \; Q. \* z (heat, new Selector (heat.getClass (), "stepRule", false));
* W- r1 \2 B# b1 S" s } catch (Exception e) {7 F! d3 f/ p) L: s; A
System.err.println ("Exception stepRule: " + e.getMessage ());
0 E# U) M/ j" Z; ^1 O- q }; T, a$ N; b, Y* Y' E+ A$ h
/ A2 t" @& `' U* P0 K/ \ try {" }: z6 @: u1 i+ M/ m/ P/ c
Heatbug proto = (Heatbug) heatbugList.get (0);
( O8 S, G& T) r( F& s) p( K4 d5 Q Selector sel = 1 y3 T% q& X- K }/ Z# \( i# p
new Selector (proto.getClass (), "heatbugStep", false);
' ?. E/ V1 J: `/ g& Z! X actionForEach =
5 C) f. T& r; R4 s. @' h modelActions.createFActionForEachHomogeneous$call7 [, t4 O7 ^) C7 G% q0 E9 V
(heatbugList,( j9 x0 n" e$ n' d: p
new FCallImpl (this, proto, sel,! A( O6 \0 S: k" w3 |0 m; v+ h. e
new FArgumentsImpl (this, sel)));
2 p" i" [/ ?; v6 R9 h0 d } catch (Exception e) {( r) ?' f) d2 x$ s; p2 O* o
e.printStackTrace (System.err);% q. e9 |# d2 J$ C1 s7 z5 B
}, h( |4 g! V) o1 c& X: N5 _2 c, j
6 q1 ^6 I7 e4 l" h$ H9 y4 j0 ~ syncUpdateOrder ();7 ^8 x! |& l4 { {6 J z# ]5 w
5 p+ u2 N9 b$ Z5 J$ x, e6 O
try {. B$ r% ]# m, [- p. `# Q+ X
modelActions.createActionTo$message " k/ J* r9 w1 ^; K
(heat, new Selector (heat.getClass (), "updateLattice", false));9 ]' ]- |! C! H% H
} catch (Exception e) {' }* u4 u& g2 n) T1 M: U3 y
System.err.println("Exception updateLattice: " + e.getMessage ());; M4 ?( f2 F2 m+ J$ d. z! `
}% B0 c$ ~# E& L9 C1 Y
# X2 ~( m3 Y* Y4 v! w# \9 L' _ // Then we create a schedule that executes the2 R4 x0 i5 U6 _9 p$ O7 j% A" }/ x0 S! C
// modelActions. modelActions is an ActionGroup, by itself it4 H. ?5 M% P; F8 a
// has no notion of time. In order to have it executed in
6 Z( ~8 @- x+ ]$ A( K# ? // time, we create a Schedule that says to use the
9 |2 _4 }0 L; M ?+ y // modelActions ActionGroup at particular times. This
# r1 |' f& s! G2 Z // schedule has a repeat interval of 1, it will loop every5 b0 R* y( k$ H1 }3 T
// time step. The action is executed at time 0 relative to
" z! K: x' e) V4 N // the beginning of the loop.# ]) W5 h1 e* b3 L
/ \8 c5 E, H' U1 u) z+ x5 _" ?. s
// This is a simple schedule, with only one action that is& V. B' W+ v+ e( k2 m% I1 r
// just repeated every time. See jmousetrap for more
1 {" I/ ^6 B# Z3 t // complicated schedules.: Q0 u& {8 v6 V0 `# V) u1 T, X
7 I& c9 |: i; M8 U) _; | modelSchedule = new ScheduleImpl (getZone (), 1);
6 x9 K2 G2 n5 Z5 t$ S/ J& E modelSchedule.at$createAction (0, modelActions);
' r8 @6 [2 I* S
9 n' ]9 _( p. k$ C: O( l" z return this;7 x" S2 @7 e" o0 c9 O: Z
} |