HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* B% N- I$ \3 O- v6 u
# P2 i. r6 D3 W+ ]
public Object buildActions () {
& I; m) ?* R3 x: k- C/ X' s# N( k super.buildActions();
7 C" o# C( V. V8 T' z: p! d, Y
+ |2 a; G8 H- Z: ~# T3 H5 c // Create the list of simulation actions. We put these in
3 p4 e0 K) l" D // an action group, because we want these actions to be
2 q: I9 h' }0 B // executed in a specific order, but these steps should
5 f) R% l& t- C // take no (simulated) time. The M(foo) means "The message# x$ i0 o+ `) O7 ]
// called <foo>". You can send a message To a particular* f8 o1 z8 @* [
// object, or ForEach object in a collection.4 o7 i: s# J. `7 `9 w' p8 w
0 ? d: }, T! H- e' p& {
// Note we update the heatspace in two phases: first run4 p: ]# t. _( B: R' }. t/ z1 @
// diffusion, then run "updateWorld" to actually enact the
8 A5 t# Q9 F# ~$ H, U; j // changes the heatbugs have made. The ordering here is
) x3 F! Q# o+ d6 Y+ e* {- f8 C // significant!
/ S! [8 z+ G, S J" v- G( u9 D
8 _ o! v8 |8 h# g // Note also, that with the additional
# o9 j* z" @. K7 f8 |! H; U1 t7 k // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 [) G6 h$ a7 C4 k) a // randomize the order in which the bugs actually run
" ^" _/ @' y( g+ l // their step rule. This has the effect of removing any5 p2 |: | o8 @( v
// systematic bias in the iteration throught the heatbug
# Z! T& R' i8 a& Q // list from timestep to timestep3 t& J- ?# a0 _; A2 ?% z: n' [; U: }
9 B; x6 b: ]9 D# s" } // By default, all `createActionForEach' modelActions have
/ W5 l+ V `' n% j+ u, `1 q# O // a default order of `Sequential', which means that the
! h4 M5 _: h, Q, b# q2 j // order of iteration through the `heatbugList' will be
' |: f3 B. q' v) a4 R2 ` // identical (assuming the list order is not changed @8 D: P2 {1 Q* C' h
// indirectly by some other process).
: ]$ p; d- E5 h2 Q9 h/ V0 s 1 J. t8 C* A. l
modelActions = new ActionGroupImpl (getZone ());
7 _3 E& Y6 k4 N9 e# O, V
" q a( a6 r: b# A try {
, E8 X* C! `) a- y2 y1 h modelActions.createActionTo$message
: m8 P+ \1 ~ U: ` (heat, new Selector (heat.getClass (), "stepRule", false));
+ j) u' _1 x; g' D } catch (Exception e) {
4 r' k2 Z' K4 b5 q/ j% `( l System.err.println ("Exception stepRule: " + e.getMessage ());
( u) D( D# S& r$ y; K/ J) S$ ] }% I+ [' g* g4 K" i
) f+ e) u1 o; Y Q1 k try {: Z C; y0 [: T: F/ p! r( p! [4 m
Heatbug proto = (Heatbug) heatbugList.get (0);
6 @8 g9 ^9 \/ Q$ ` Y/ D Selector sel =
3 ^/ [0 e7 w- V new Selector (proto.getClass (), "heatbugStep", false);5 U _2 s2 S' P" p2 I
actionForEach =
9 ~. O" D* \( W3 H t modelActions.createFActionForEachHomogeneous$call
* O, r% n! _" x: ]2 Q+ P (heatbugList,4 M% C2 f: ~5 M5 N5 L; L) b
new FCallImpl (this, proto, sel,
8 S0 J; j D# l9 n new FArgumentsImpl (this, sel)));+ I+ O. n) `8 c( ]8 e
} catch (Exception e) {& t; t% V9 y' E
e.printStackTrace (System.err);+ ~) O% p* {; T1 U
}! D& V- o) u5 s' ]6 }/ A% ], z
$ t) I' L4 }3 y
syncUpdateOrder ();
5 J% y. P! p6 k S. A$ D: Q ?/ b& a" |% O7 A2 \
try {
0 l, o j6 F2 ^3 ?6 e modelActions.createActionTo$message 3 ?4 @# z& O& ~% U5 \* Q) f
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ h/ z0 |8 ~: P0 U0 u } catch (Exception e) {+ G! ^3 e* I+ z, N8 J
System.err.println("Exception updateLattice: " + e.getMessage ());
2 m3 N; q- r6 n+ h' P Y }
5 K1 t! P( i1 c- v3 b4 _, q( c 8 j# `/ x; _/ `0 `4 ~6 M1 b! ]
// Then we create a schedule that executes the- N7 h5 ?5 [5 D" j$ V8 o: y4 z
// modelActions. modelActions is an ActionGroup, by itself it! C* c. w; m( e* d5 q+ R
// has no notion of time. In order to have it executed in6 O. z# k" O, n( {7 _# U v# b
// time, we create a Schedule that says to use the
' F9 F2 {8 P5 y8 |7 F* \9 g6 N9 _ // modelActions ActionGroup at particular times. This
+ |+ d- ?- O& a4 t // schedule has a repeat interval of 1, it will loop every' p1 [! l5 y/ m G& R
// time step. The action is executed at time 0 relative to
% j. o! ]; h% d2 ?. x* l // the beginning of the loop.
; z7 _, B( o2 D; w5 t0 `0 f# P" m" b. Z+ B* H$ s
// This is a simple schedule, with only one action that is2 ^) w# `* _( A R
// just repeated every time. See jmousetrap for more
( x8 V5 S1 J3 f* a0 d( R+ W3 | // complicated schedules.
" n" ^4 P( u& A" i' w$ E9 Y1 H ( U; u1 o) J6 f0 A2 G" t; d
modelSchedule = new ScheduleImpl (getZone (), 1);
; t' T# r8 V e" W* P; y modelSchedule.at$createAction (0, modelActions);7 I. c$ O" M F- t9 W d7 |
! x; N$ X2 O5 m) t' P3 {& m# |
return this;: X* z6 t: p0 D% A+ W& g
} |