HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, n" l5 Z2 Q) L7 }2 }/ k& D$ Z% K' a
public Object buildActions () {4 R' } P. F9 k
super.buildActions();
7 Z$ o; x6 h u
9 N [- V7 ] r+ H; c$ a // Create the list of simulation actions. We put these in' Q% d3 d6 `/ X0 J' u$ P4 I
// an action group, because we want these actions to be
( l4 k3 s0 I( k+ W; e9 w; p/ V- [ // executed in a specific order, but these steps should
, E) O* Q! u" J' ?5 k // take no (simulated) time. The M(foo) means "The message1 M) @% z( ~9 \6 a2 G( C8 D; [
// called <foo>". You can send a message To a particular
" t6 {$ v7 n$ @9 E# X! | // object, or ForEach object in a collection.
3 |2 U% h' d1 m4 {) z# l! M9 } / o2 F" l0 `+ Q8 i" S f/ N
// Note we update the heatspace in two phases: first run
4 S9 m! X8 m/ A6 X- K // diffusion, then run "updateWorld" to actually enact the
3 I4 Y c9 Y" _2 k# { // changes the heatbugs have made. The ordering here is
* _% G3 e9 g0 o- a7 V; @0 ?) J' } // significant!+ O* b* v( r; l! L z
- s0 Q' S; _2 R7 e // Note also, that with the additional8 k6 _0 I9 t- s: m1 I8 Y3 I
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# S; A8 a5 m, N$ u* O8 Y // randomize the order in which the bugs actually run2 A4 {" q0 G2 K I) T* x
// their step rule. This has the effect of removing any
( Q$ C$ l0 B2 U3 @9 w E0 K // systematic bias in the iteration throught the heatbug$ X. I7 ?* g" U! w
// list from timestep to timestep: S- z. w# T# g
8 ~3 @' V* M& s& ?- _ // By default, all `createActionForEach' modelActions have% A4 s+ C7 I. u$ l& a0 Q
// a default order of `Sequential', which means that the
) Q' _1 }5 j' p$ d1 l // order of iteration through the `heatbugList' will be
( {4 \% t1 i1 r8 p5 M" F: S // identical (assuming the list order is not changed- ^2 ]/ I5 V1 X, i Y0 w
// indirectly by some other process). x, F5 G) w4 N
* w, `8 m w* { modelActions = new ActionGroupImpl (getZone ());
! p \& ?# {6 _: r# Q
* v5 S8 K! U. f! @; j try {
# V$ p" u9 K. z9 L- f9 s! ^ modelActions.createActionTo$message
5 T& r- S9 o6 @ d1 g$ m (heat, new Selector (heat.getClass (), "stepRule", false));
/ ?9 {' s8 a( E& \! K e( g" U: s } catch (Exception e) {3 i; l, ^8 Y5 g6 K7 X6 C; W
System.err.println ("Exception stepRule: " + e.getMessage ());" v; N0 w* A) |7 T- B- S
}( l5 a W' C0 O+ O8 G
- a! O# d3 {) u6 V: n3 |
try {
' d' L' w' D, T2 T2 P' y C Heatbug proto = (Heatbug) heatbugList.get (0);
% K% x5 S! Z- }: ?7 G/ b Selector sel =
5 I$ i2 W d) a t8 o9 B new Selector (proto.getClass (), "heatbugStep", false);
7 h0 w2 T; a8 t# f! | actionForEach =" l% d: H0 {! f* P
modelActions.createFActionForEachHomogeneous$call
" A7 X1 X* ]% b/ W. J (heatbugList,5 G) g# b0 S$ m0 k0 o% v
new FCallImpl (this, proto, sel,, S1 f( L" }4 ]1 r
new FArgumentsImpl (this, sel)));
/ i2 p- s7 o8 }) u$ u# L( c9 B# S } catch (Exception e) {4 b7 @+ S9 q6 e5 R" u: Y
e.printStackTrace (System.err);5 A, ?8 |+ L1 n8 m9 ?. c1 ?
}
* y; A/ H" V* E. U- C & p1 F% `# n/ ?9 b$ k+ |+ [6 p0 }
syncUpdateOrder (); x2 Q( n% N, H8 M
9 i8 H/ `6 G7 B6 z* `/ S, w8 z try {$ r7 j7 y+ ?3 E# B+ k1 o2 ?% H
modelActions.createActionTo$message
* C/ S* V6 O* e0 x$ C0 ?$ B+ ]$ ] (heat, new Selector (heat.getClass (), "updateLattice", false));
- E# P( P3 s8 [ } catch (Exception e) {6 }- l7 R0 e- ^' {$ Z& v- Y
System.err.println("Exception updateLattice: " + e.getMessage ());8 ?2 x% _+ O5 S8 Q4 `
}
5 N% Q1 o- R0 J
O5 C0 B' ?: b2 k7 j // Then we create a schedule that executes the* j3 R6 g5 [$ o! g% n7 `# W
// modelActions. modelActions is an ActionGroup, by itself it
7 }! \' I* @2 I/ P/ n$ D5 u6 M // has no notion of time. In order to have it executed in
4 t8 _3 \' L0 g" M- [! c3 j // time, we create a Schedule that says to use the
, |- h5 D! s" B" z* I // modelActions ActionGroup at particular times. This
) J7 G* b8 l2 T( ?# G // schedule has a repeat interval of 1, it will loop every
; R- U0 O5 ^% ~* K4 y0 j- d- v // time step. The action is executed at time 0 relative to$ ^, O7 j* t0 u5 `6 P* ~0 m, M, V
// the beginning of the loop.4 V/ Z6 S* ]( Z. l* o7 }4 H5 i5 L
; b( L1 U8 @+ n4 z // This is a simple schedule, with only one action that is
0 M+ U9 ~9 [, k2 G# Z // just repeated every time. See jmousetrap for more
& m; D+ w) w) ~- c" V6 ^ // complicated schedules.+ \5 y( d7 x7 n1 C0 S( `; H& |
: `7 T# V$ X: A% O8 \5 V
modelSchedule = new ScheduleImpl (getZone (), 1);
9 ? J3 J, q) T; k modelSchedule.at$createAction (0, modelActions);3 p- V8 |% A7 A. Y. ~$ K6 \
, e4 h6 T6 E3 w* A+ L$ h$ ~
return this;
5 E) k% m1 d6 k Y } |