HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, J- D9 p2 Q1 L
4 p, I/ U! E3 M& S) _, q9 a1 b' | public Object buildActions () {4 h" d4 Y% @- Z' i
super.buildActions();6 p* r. Y5 f9 z8 b! g" W
- p% ? X- r9 D" x1 R
// Create the list of simulation actions. We put these in5 _3 y# r. ]3 x" M" g$ c
// an action group, because we want these actions to be* h1 s, {. U, Q! B
// executed in a specific order, but these steps should
0 q9 ^% q+ W3 I1 Z @- R2 e: z1 ] // take no (simulated) time. The M(foo) means "The message% |( |! `' B) S
// called <foo>". You can send a message To a particular
" ]7 \( X) B( E$ I N // object, or ForEach object in a collection.0 t) V+ B; K, J$ r, O0 y
* P) K: P2 \1 @
// Note we update the heatspace in two phases: first run3 y* t1 e, s. B4 q: L
// diffusion, then run "updateWorld" to actually enact the" [5 Z' ]9 i; }7 M, T
// changes the heatbugs have made. The ordering here is
8 }# g0 n7 r- i; d/ V+ A4 p- E3 H& X // significant!
8 A2 `8 F. N& c, \% \ % I s* @0 ~- ?/ z' [
// Note also, that with the additional2 U& m0 r; P& Q+ c' F! o: I# Q/ R
// `randomizeHeatbugUpdateOrder' Boolean flag we can3 G) R0 F3 e- B6 [0 l
// randomize the order in which the bugs actually run
" z. \9 ~! y$ ]) {! p, | // their step rule. This has the effect of removing any
1 q: }- i1 P5 t2 Q: d/ r // systematic bias in the iteration throught the heatbug8 h* S9 w/ W2 y
// list from timestep to timestep) f- {: m4 e, q* T) {; W7 \/ t
8 ^8 j( C- Z! D* A" P4 `$ F
// By default, all `createActionForEach' modelActions have
5 F# y- Q- o u! K6 C& Y // a default order of `Sequential', which means that the* P9 V* H2 w+ U2 A3 k- R
// order of iteration through the `heatbugList' will be( V5 h# a# F- D' e7 N$ r
// identical (assuming the list order is not changed) M1 i' o7 c+ h
// indirectly by some other process).
$ y& H5 E& I% X# z9 D5 ~- l. _ 7 T5 R* g2 O' M& D' f
modelActions = new ActionGroupImpl (getZone ());( {. P& I% {3 e+ V' K$ t
, |( r5 s) ?9 d try {" \8 A/ z9 a7 D- `9 k" x. ]# Z
modelActions.createActionTo$message
. j2 d! q) K" `1 e/ v. n: D* R (heat, new Selector (heat.getClass (), "stepRule", false));
" U/ u1 _& V+ F9 a& j6 V2 a } catch (Exception e) { {. N/ t' Y/ O2 V2 g5 s
System.err.println ("Exception stepRule: " + e.getMessage ());
# Y+ \& n7 i4 M$ _7 A3 c1 G% g7 { }) d" e" I1 l* J9 s& E! q& `. _
" O$ _% d. p K. v. K; x
try {
' y8 l$ ^& Y& S( q' Z Heatbug proto = (Heatbug) heatbugList.get (0);% d3 e* K3 q6 r9 X! c5 h
Selector sel =
, B+ e% W- e. z2 V new Selector (proto.getClass (), "heatbugStep", false);
' N3 b. F9 p- M) U actionForEach =
3 g" @$ J7 U1 s) A( {5 @ modelActions.createFActionForEachHomogeneous$call
J; b6 b* f s0 K3 j1 @ (heatbugList,9 t$ \* L4 l- |/ Q1 v
new FCallImpl (this, proto, sel,
2 @: l3 `+ h8 \2 ?% Q5 d new FArgumentsImpl (this, sel)));+ O, f( U% Z3 O1 N
} catch (Exception e) {8 a6 Z5 B/ j& a- n: Q
e.printStackTrace (System.err);3 I# u% O. M% A% V, m
}8 Z ^6 A' p2 @# f9 T
: p+ X: u; ]0 t J2 _- _: c
syncUpdateOrder (); p7 f: m5 H4 d& ~" k; h/ X' Y$ R
$ A7 c8 d4 D8 o+ P try {6 ^+ \! [+ Z, r2 _" r# e$ g( V
modelActions.createActionTo$message
2 v% C' Y$ E& p (heat, new Selector (heat.getClass (), "updateLattice", false));% A' h6 R; w/ \( p5 [" n+ c
} catch (Exception e) {
9 o1 I4 R3 o3 S8 F6 W- S: o System.err.println("Exception updateLattice: " + e.getMessage ());0 V- E1 R" w7 ?
}3 h3 H3 m% I/ T8 Q1 [
. h( W% h) h p6 u, B! H D7 v // Then we create a schedule that executes the1 a3 p9 l( b3 g4 m" N. h
// modelActions. modelActions is an ActionGroup, by itself it: n$ R+ N3 u# G5 {- z8 n
// has no notion of time. In order to have it executed in
5 w- G9 q& k- c // time, we create a Schedule that says to use the& o4 S4 ]6 E) S) O/ o4 v, ~
// modelActions ActionGroup at particular times. This
( I2 T) D! k( [( p9 `; [' p5 `! z // schedule has a repeat interval of 1, it will loop every% a, u) m- Q! a Y. Z; u2 z5 b W
// time step. The action is executed at time 0 relative to
: _1 o: \- d/ ] // the beginning of the loop.: e7 R2 U/ V" i# [
: \( I2 B. m( A0 A0 ~' f // This is a simple schedule, with only one action that is& e' Z! u3 ~8 ~) [% M' @/ c. D$ j; N
// just repeated every time. See jmousetrap for more' @- j* W& O1 t. i0 z
// complicated schedules.
1 D9 X' O8 z/ g$ B+ {, ~8 y
; [6 O6 l& C" p% P' O5 L modelSchedule = new ScheduleImpl (getZone (), 1);
9 C+ q4 P( f0 N1 l. X+ e$ G9 _ modelSchedule.at$createAction (0, modelActions);8 i5 |; J5 V& Y- D/ n+ E& o
" M% Q3 B6 o( l( X1 H return this;3 S$ j& i$ D* Y0 X) A. {3 d/ S
} |