HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& @ A G, ?( Z, M
3 l) Z3 w4 F" @ public Object buildActions () {( V# @0 |- J, q) e# H4 y
super.buildActions(); G) h, {( S0 u7 o7 f( z
2 {+ ?/ V7 Z" R" M3 S8 c
// Create the list of simulation actions. We put these in
1 w2 e" Q# a$ G4 h6 r% ] // an action group, because we want these actions to be r- T+ p a" ^; k2 S! p
// executed in a specific order, but these steps should+ s6 g; z& }6 e
// take no (simulated) time. The M(foo) means "The message( b0 x" A- Y d$ k
// called <foo>". You can send a message To a particular: M4 X; u; J! u, p7 v1 n& |
// object, or ForEach object in a collection.1 V& y1 s4 q! X- `* O# p
Z* {8 P! Q/ u7 _/ f // Note we update the heatspace in two phases: first run8 b5 n$ H J6 M0 b0 a# a% i' N
// diffusion, then run "updateWorld" to actually enact the
, L& }. ?' b4 L* Y& x" ` g+ t) j // changes the heatbugs have made. The ordering here is( \: T* h0 ?8 G5 g; s' b& w7 _/ A
// significant!
6 U Y$ K. n# m/ Y% C" j/ W, p
' p5 a ]1 I! a8 J // Note also, that with the additional6 V3 ?4 {5 {' n, ]4 E- i. q
// `randomizeHeatbugUpdateOrder' Boolean flag we can
, O+ P/ X- g. a, C6 e: j // randomize the order in which the bugs actually run0 q3 P4 p7 E# L Y, l
// their step rule. This has the effect of removing any
, Q# V5 k* \# R0 R T. Y: A // systematic bias in the iteration throught the heatbug+ z- ^. A- F9 S# K1 ^/ }! S
// list from timestep to timestep
: Q1 r; `, r. l 6 i* A5 A# |+ @+ O9 F" ?
// By default, all `createActionForEach' modelActions have1 ]+ G* \. [% Z+ M
// a default order of `Sequential', which means that the! N# M D8 e* L6 ?
// order of iteration through the `heatbugList' will be9 z' u C9 ^- W1 D( G8 B/ g
// identical (assuming the list order is not changed
. ^2 {& d' t# ^ // indirectly by some other process).: v- ^. m: s0 ~% k3 K- ?! ^
2 i1 Z2 U- Q; T! z8 w; h H" {' S modelActions = new ActionGroupImpl (getZone ());$ l( u2 C/ }6 P+ s' `
/ C, u; S2 A$ }9 d0 W/ @5 U
try {
2 {7 {% w) F1 K$ r& g. u/ B$ v modelActions.createActionTo$message
2 ?% l B# g7 A/ O4 N (heat, new Selector (heat.getClass (), "stepRule", false));7 \ G* J" H2 d8 d
} catch (Exception e) {0 e( F6 {8 S. \& F5 [7 H
System.err.println ("Exception stepRule: " + e.getMessage ());
. D3 K( v4 ~! W7 A }+ K2 z8 s6 u0 C7 m8 l6 n8 {- o
) h ~+ O- v2 ~5 E: H( C try {
2 B% g3 u' o0 M8 ^4 O, p0 }, o Heatbug proto = (Heatbug) heatbugList.get (0);9 w4 m, p. p$ R; k
Selector sel = 1 ^/ _1 c; E& i1 W7 X
new Selector (proto.getClass (), "heatbugStep", false);9 S, [. d( V, W- ]4 E
actionForEach =
# a1 L5 d0 s/ t, b. v) A modelActions.createFActionForEachHomogeneous$call
8 `3 g" N' `7 h3 {+ ~ (heatbugList,& Q, `7 p: ~% Z" a
new FCallImpl (this, proto, sel,# C" _! H3 n( }3 C
new FArgumentsImpl (this, sel)));
- N' U7 ^) \0 n- M } catch (Exception e) {3 j- U* h. z4 a" e
e.printStackTrace (System.err);/ k4 A1 @- t* g5 M; Y' {
}
/ D. R2 a) ?. |0 h+ U/ L) b * |% k( E n. a; V+ I. i9 }( `4 u
syncUpdateOrder ();
5 Y- u' c$ w3 Y" I& F ]( n$ b5 q3 Y; Z
try {) ?+ a* f' a/ q- U& Q$ v
modelActions.createActionTo$message
/ v5 L$ m( X0 w2 R) R" ]$ e (heat, new Selector (heat.getClass (), "updateLattice", false));8 y5 d! b2 Q, E( }
} catch (Exception e) {6 t8 N# |, u5 j. h0 _
System.err.println("Exception updateLattice: " + e.getMessage ());
4 ]- G& f/ M- ^ }
4 l6 H K$ e6 x$ Z3 l , Z9 ?) ?2 y) y: j2 h& q( A
// Then we create a schedule that executes the/ N" p& N) Z9 C3 A; X
// modelActions. modelActions is an ActionGroup, by itself it; j6 u9 ?# }9 o9 [6 N( d9 z
// has no notion of time. In order to have it executed in% d6 |7 e5 L3 n2 q
// time, we create a Schedule that says to use the: i3 m' a6 F2 G* d
// modelActions ActionGroup at particular times. This8 \- q" r F. i, Z- g% O
// schedule has a repeat interval of 1, it will loop every) u0 T6 n. V( Z& ?8 }# u
// time step. The action is executed at time 0 relative to
' c& l j- ` r) W3 w // the beginning of the loop.
: ?- Z7 E1 U$ t6 r0 D7 U; [5 _/ i' p0 X7 ^8 z x _
// This is a simple schedule, with only one action that is
( T; r$ R8 c6 G u4 _' K, x' a // just repeated every time. See jmousetrap for more* p1 a2 r9 A) O% e, j% g
// complicated schedules.
" [0 d! C3 B% A) N; U7 P! x
$ R' V: G u4 K* U modelSchedule = new ScheduleImpl (getZone (), 1);
! R' x; i0 s. d5 Z+ x* I modelSchedule.at$createAction (0, modelActions);/ A" H3 {6 o6 b/ F, `0 K# {4 B
8 U7 `2 m* U% z2 o. l8 B
return this;
6 o: e' K- K: }# n, G% i } |