HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, R; _& p" i, i) G
- j9 Z; |5 r& ]1 Q; d) H
public Object buildActions () {
8 t0 w6 a, S# `7 j super.buildActions();
# @6 H) |$ w0 L6 A + ]: P5 t) C5 S2 ` x4 o. i
// Create the list of simulation actions. We put these in6 X+ f5 I3 r. t" }- A5 b- F
// an action group, because we want these actions to be
# y( m6 h' L1 I" B4 Y1 a0 B // executed in a specific order, but these steps should
) ^ A2 s& V% N1 Q" j // take no (simulated) time. The M(foo) means "The message' x& [& S1 S$ P& K! g2 B
// called <foo>". You can send a message To a particular5 P! B* ~4 K+ t- |2 Z5 z1 h3 |7 D
// object, or ForEach object in a collection.0 u; `2 b1 e$ x
) v0 [4 R0 W+ C) x% L$ [: ~* R
// Note we update the heatspace in two phases: first run- g& z! T; G* v4 u9 i
// diffusion, then run "updateWorld" to actually enact the
! i+ U/ P8 l- ` // changes the heatbugs have made. The ordering here is: P% E% E3 v# v5 N9 |5 e
// significant!4 C" ^4 ?9 W; O% B
( |# _9 C) Q7 v3 k8 R! i // Note also, that with the additional3 X6 j$ Q$ h p" s4 W
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 g3 Q: T2 D5 B! ?* H( Y+ v# m- b
// randomize the order in which the bugs actually run: U! A% x/ v/ ]7 y
// their step rule. This has the effect of removing any" l: B, d. A) g5 {4 O* ]: e
// systematic bias in the iteration throught the heatbug6 p R) T4 s" N# `- @* B
// list from timestep to timestep) i2 }8 E0 G* [& y
8 s" S( W& v7 B2 j/ F // By default, all `createActionForEach' modelActions have6 ~" }3 j, a* R- r% u$ C D
// a default order of `Sequential', which means that the
* E) K9 w1 z1 L& K0 `- l // order of iteration through the `heatbugList' will be
( Q7 }, z/ p m. [& F. r // identical (assuming the list order is not changed
0 o1 J, H6 q/ U- B1 K' t2 A // indirectly by some other process).* E4 O% a0 @; m" d. H
! A0 V( E. t- x: _* n modelActions = new ActionGroupImpl (getZone ());
5 v( c2 h# ]) e5 W* i1 V; t% c6 J2 `" x2 J! e& p
try { W1 a7 P. c* ]! N+ ~
modelActions.createActionTo$message
4 \# S+ k" p# F+ o8 N& f (heat, new Selector (heat.getClass (), "stepRule", false));
# h( D6 O9 u% K [5 o. i } catch (Exception e) {+ _4 h3 J& @) Y
System.err.println ("Exception stepRule: " + e.getMessage ());
% ?9 D: X z# J i" [ e5 [6 M" g% D }
n/ O/ R K5 j- Y
" N1 |9 x! U8 b, y4 r3 A8 t: W3 n try {0 J& \9 Z' [' `7 Y# }6 H/ Q
Heatbug proto = (Heatbug) heatbugList.get (0);
$ s' p, m. X: {4 ^; k% r Selector sel = % V* @- P7 R1 c
new Selector (proto.getClass (), "heatbugStep", false);' ~6 ], E. Z* e; A
actionForEach =# ~) K" Y8 ?8 {, i: c( M" j! M8 C
modelActions.createFActionForEachHomogeneous$call
) L. ^1 L. }# j! D5 f* { (heatbugList,
4 n1 K0 N: ]1 H1 |& j4 o new FCallImpl (this, proto, sel,
2 Y+ H7 T9 \1 X new FArgumentsImpl (this, sel)));
5 [3 p" e. M3 r# N } catch (Exception e) {
, `5 w. Z/ h: ~1 z2 W1 G/ k e.printStackTrace (System.err);7 T' h' l/ ~( B) V
}
, P: E2 |2 f7 h" [
- T' Q" }. P+ t6 S syncUpdateOrder ();
$ Z8 l0 Q1 m1 }, s4 k3 U
) c% q5 f% G1 Z, l try {
; X" y3 {1 W" Z" S& v* i modelActions.createActionTo$message 8 C. @6 I6 G6 Z" d$ C8 P
(heat, new Selector (heat.getClass (), "updateLattice", false));7 l7 Z" U$ B; L& I; `+ O
} catch (Exception e) {
5 h; ~, ]" w- N9 k7 F7 @: A System.err.println("Exception updateLattice: " + e.getMessage ());
8 Q4 F- E6 [. B }' C8 U1 E. w7 e' g* X" }( O1 j6 a
8 n0 r6 |2 V$ g4 O
// Then we create a schedule that executes the
( e& N5 s/ T- z& j; U. Y$ p2 H9 ` // modelActions. modelActions is an ActionGroup, by itself it
3 S; U9 e" \5 w( y$ V: g // has no notion of time. In order to have it executed in9 ?. E. o' S* M
// time, we create a Schedule that says to use the
. u s i, N# M! T0 ]2 N' S // modelActions ActionGroup at particular times. This/ X0 d$ j7 ~9 W# B. d$ I- E" M
// schedule has a repeat interval of 1, it will loop every, s' h \* L& w! l
// time step. The action is executed at time 0 relative to
8 g9 G) v4 ]4 H3 ~ // the beginning of the loop.4 _. ]6 E! c! [; q$ p1 m% X5 i
- A& Q+ g! x, h& d; P) f
// This is a simple schedule, with only one action that is
0 e# P& r; y) F4 w# {% a // just repeated every time. See jmousetrap for more4 w* V |) Z0 B/ P
// complicated schedules.
- f; R+ K: s- @ b- f $ S7 @' m9 |$ ^1 w4 _/ Q, a) t
modelSchedule = new ScheduleImpl (getZone (), 1);
7 U( O8 z+ j: s; s/ x7 Q modelSchedule.at$createAction (0, modelActions);& T7 H4 z; J$ f& G
3 f! z# m3 M% h return this; i$ F$ o0 T/ s: d; o7 N [
} |