HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ ]! b* H$ l% m" f6 y. _
( o7 J) L% `4 j public Object buildActions () {
- ~9 A, m {; r2 ~# P0 o- d super.buildActions();
) C- W: a4 d5 t# `$ R1 j 0 Z; T) S! l- N; {
// Create the list of simulation actions. We put these in: S6 d/ b2 \! c/ d. o; _
// an action group, because we want these actions to be5 @3 B4 A k- g) x. G
// executed in a specific order, but these steps should& i$ o K! [' A6 }( P% S
// take no (simulated) time. The M(foo) means "The message
4 Y) X5 x& v1 U: j* x4 W5 @6 g // called <foo>". You can send a message To a particular. ~2 h7 a: J+ p" Q, i" S2 d
// object, or ForEach object in a collection." C* m! R4 @* }3 Q. ]4 U) g: ]
) o- O5 `3 k4 |& T, [7 [+ R# p6 ?' [ // Note we update the heatspace in two phases: first run
* h/ U! I, x3 G' e0 I' X // diffusion, then run "updateWorld" to actually enact the( @4 X7 Z- A Y) X! u$ X
// changes the heatbugs have made. The ordering here is' O: t- J1 n) r. h; {4 x6 o
// significant!% Q/ C! i5 g. I: M$ X6 m7 c
" t, S* F1 P9 c0 z- i
// Note also, that with the additional
5 P3 Y% c! n, c, |7 ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can
8 F$ Z. J# ]4 {2 P // randomize the order in which the bugs actually run$ ?2 ~6 }' C/ L
// their step rule. This has the effect of removing any- m( E2 Y+ q3 t% Q$ |
// systematic bias in the iteration throught the heatbug
8 L" N' o- c# U3 I7 b* A# h9 G // list from timestep to timestep
8 X- @3 F( j' i4 K! O. A5 s
2 M" g! l& I5 N // By default, all `createActionForEach' modelActions have
* c- h* M; k/ z // a default order of `Sequential', which means that the6 t' M' R. P+ P$ h( D7 e
// order of iteration through the `heatbugList' will be# b9 }* P G- X B! j$ D* ?& o
// identical (assuming the list order is not changed
5 `0 ~ L" @% [7 ~ // indirectly by some other process).
$ C( c }, e e4 E # R; E7 r* B" k9 S5 T+ o
modelActions = new ActionGroupImpl (getZone ());
u+ n! }' E: [3 x z/ J
}( t' Q) N1 X+ u! ~; Q try {
) p) H$ }8 E- d9 A* h modelActions.createActionTo$message
# ~/ y2 F% r" q; n. U (heat, new Selector (heat.getClass (), "stepRule", false));
) J" g% t* y! N% R2 X. B& x1 s0 e } catch (Exception e) {
* Z. }) ]4 o, _ o; E System.err.println ("Exception stepRule: " + e.getMessage ());! a) i' }4 R, ~0 \2 M4 E
}
* }5 ^1 I0 ?. _, l9 [; R
0 P) c# ]( p. ?" u% }7 H try {
! }9 X( L+ T0 m9 Z+ u Heatbug proto = (Heatbug) heatbugList.get (0);
% S( L3 Z" s2 e; Q Selector sel = + y8 v- L% g1 C( s( M5 O
new Selector (proto.getClass (), "heatbugStep", false);
2 `. l6 i9 m" A, S" N. g* o actionForEach =
* K' H' L8 K! H( g1 H2 g+ R- s modelActions.createFActionForEachHomogeneous$call
1 z# D0 `# b5 Z2 s (heatbugList,
* {$ }1 n2 m1 D: Y) I5 L new FCallImpl (this, proto, sel,, u/ j4 |* N5 m$ U, I; y8 t
new FArgumentsImpl (this, sel)));8 K2 T1 n! A& Q8 D# a9 ^) L! e7 u
} catch (Exception e) {* u* D1 F7 [' x/ H9 o# n6 B4 \
e.printStackTrace (System.err);
) A; r3 t/ o) `2 Q }
}% [: R: e# M
4 A4 k3 s0 l/ w7 w syncUpdateOrder ();% s5 q& S* [7 w( W7 N
! T4 s/ D Z2 x5 k try {' y6 S- o$ Q' s t
modelActions.createActionTo$message 1 }' {: o e p8 f' u' M
(heat, new Selector (heat.getClass (), "updateLattice", false));
6 C; R6 Z! y0 _! D } catch (Exception e) {
9 s9 }. C/ W* @) @2 ^* O4 H System.err.println("Exception updateLattice: " + e.getMessage ());; w, g7 r4 h8 f F+ ^
}/ v4 J0 D. U& I/ @ `8 G
: [. Z- D5 K2 G- u3 K% H, `
// Then we create a schedule that executes the3 U/ g E' w0 s& S
// modelActions. modelActions is an ActionGroup, by itself it6 [# z- A# `- \ {7 q+ z( C; }
// has no notion of time. In order to have it executed in- ~3 c5 Y1 n! C2 q
// time, we create a Schedule that says to use the" _* T' u( n" U; L" E
// modelActions ActionGroup at particular times. This
& H: o# g( c8 ?$ E/ J( J // schedule has a repeat interval of 1, it will loop every
* f0 Y& q8 J5 _4 a U! ~8 z // time step. The action is executed at time 0 relative to- @. F! ^9 ]0 x
// the beginning of the loop.! y/ c6 f9 F' |: \: a9 u. l9 n; e
# c3 y: p4 F; F* Q T9 L1 d: G // This is a simple schedule, with only one action that is# u4 n7 Y/ \: w6 u. i; [
// just repeated every time. See jmousetrap for more) Q" @1 ~ @- L& T$ ]
// complicated schedules.( N2 e4 U# o- E9 a4 T+ `; v' r1 F$ q
* ~4 @% o: X2 D! h; X; C
modelSchedule = new ScheduleImpl (getZone (), 1);# d: V8 ?9 R6 C. C( u% [
modelSchedule.at$createAction (0, modelActions);$ _( [8 E/ |5 L6 O! I
6 J' _1 ]4 t0 J# F+ c
return this; t8 Q& c& j" p2 g* K' D( m, M
} |