HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, o+ d b4 S, H+ I5 B! a- q
5 R. f' \1 \" V7 z public Object buildActions () {
/ A" D0 x m3 e6 C7 j# H) D super.buildActions();
$ T% _# f' J' n, Q. F
- ~- Y1 ~6 k" H& |7 `! C4 k, M3 p // Create the list of simulation actions. We put these in
) P, t/ q& b. h // an action group, because we want these actions to be
7 o. R# h% V: d' J6 x // executed in a specific order, but these steps should
: x9 N6 s$ a& p* u) M! @' U // take no (simulated) time. The M(foo) means "The message& C6 Z8 T- v( J/ J8 H& T& S
// called <foo>". You can send a message To a particular
4 {, S# l2 ]# |$ R# `5 o // object, or ForEach object in a collection.
% T; p$ ^: u9 [# J. ?7 n% s( l
' J6 ?: m1 E# b // Note we update the heatspace in two phases: first run/ @" j; T5 B7 J6 i1 s% ?4 m
// diffusion, then run "updateWorld" to actually enact the* R+ A, C% h2 c7 n6 h, `' N& S
// changes the heatbugs have made. The ordering here is
& t/ Y1 N: F. P5 s" i9 {6 {+ Y/ w // significant!0 c2 f: n% M: w+ Y4 [
5 o( H( \/ }1 g" A // Note also, that with the additional9 A! \- D& E2 t. O/ o
// `randomizeHeatbugUpdateOrder' Boolean flag we can
5 i) i3 \9 C0 g9 y$ _! {) P' Z; R4 J // randomize the order in which the bugs actually run" d% z6 G/ _. K7 r7 m5 `
// their step rule. This has the effect of removing any
; a2 D: }2 `) S // systematic bias in the iteration throught the heatbug
}, A) y5 I0 I4 V6 P0 u4 y. q // list from timestep to timestep
- c. e" K& v8 [6 c" }
9 p& p- W3 x% u6 h8 O& h# j5 [ // By default, all `createActionForEach' modelActions have
2 Q* ^2 L: f: Y, Y1 U // a default order of `Sequential', which means that the
# @& E$ Q- g! n5 Q+ v' R% r // order of iteration through the `heatbugList' will be
' H2 N8 @' _' F( w // identical (assuming the list order is not changed
$ L% P* B. Q4 N% Z% W // indirectly by some other process).
2 `9 K5 d, I) L5 q9 t7 b ( ^% B c" N' u" u. L
modelActions = new ActionGroupImpl (getZone ());" F6 N. n4 v% M# j5 U! `
% N, V2 G2 m5 J- ~; T try {6 Q g$ ^2 `$ |9 O& q9 I: v9 ]
modelActions.createActionTo$message: z/ b+ t; x* B& a! I3 w
(heat, new Selector (heat.getClass (), "stepRule", false));, c8 T5 ]/ q/ a. U
} catch (Exception e) {
) |- n1 W* h2 ^+ \3 a2 F2 F* A System.err.println ("Exception stepRule: " + e.getMessage ());/ B3 `9 G$ g' L1 l
}1 g$ H7 b3 X: B! p+ ^
% c8 l; j5 k9 L" g$ Z try {
: h5 r# p/ e+ y! c' p) v Heatbug proto = (Heatbug) heatbugList.get (0);0 c5 t1 i/ J7 a
Selector sel =
" B) ^' R% x9 r& U) ^6 g new Selector (proto.getClass (), "heatbugStep", false);8 h1 I' M; D$ d% e5 y
actionForEach =0 G5 b8 N0 u/ ^* f- H6 O" @( q$ i
modelActions.createFActionForEachHomogeneous$call
& H% V, ~1 s, ^) V (heatbugList,
; p9 H4 s* u3 U3 j new FCallImpl (this, proto, sel,; Y1 e# ?- f( x6 D! `
new FArgumentsImpl (this, sel)));
" M3 E( a6 G' J+ K3 M. s } catch (Exception e) {
6 `" ?" s5 B0 @$ `" T" N/ O e.printStackTrace (System.err);- V, |- O/ P! t+ p+ p
}
; b( d+ K' ]' T# Z. ^1 \+ E
* B' Y! L( `) u( X; h; @6 G syncUpdateOrder ();) k1 B: _! Q1 M
. I# h, H" j4 ` f
try {' \' w5 ]8 g. _7 K4 Y* d; M3 q" q5 Z
modelActions.createActionTo$message
8 j! Z4 M! i2 j) m, L3 g (heat, new Selector (heat.getClass (), "updateLattice", false));
& I- [8 s8 {) X" M } catch (Exception e) {
- \+ A( b, K& k4 x+ Z' x System.err.println("Exception updateLattice: " + e.getMessage ());2 O# Z: ~/ n) @
}
* ~ ?- F. [8 \' X 2 K! B$ T6 l' q: p( j
// Then we create a schedule that executes the
1 x/ [4 S4 W0 X7 z' q( h7 t // modelActions. modelActions is an ActionGroup, by itself it" O' I" v9 w# H
// has no notion of time. In order to have it executed in
0 {2 W- }9 `# f: L. T // time, we create a Schedule that says to use the0 h6 `8 @+ g4 J
// modelActions ActionGroup at particular times. This
! h ^7 V. y H# q# I // schedule has a repeat interval of 1, it will loop every. h. j1 ]; ^* t' U) w# H( G A
// time step. The action is executed at time 0 relative to
% Q3 g/ {9 d. I% U; P- b' s // the beginning of the loop.
; d- u. Q( x# t% J/ ~4 g* M
8 w2 R8 Q4 Q8 H; s1 q // This is a simple schedule, with only one action that is
1 a5 v, p( z1 N/ l" t U B // just repeated every time. See jmousetrap for more
5 ^5 t' S4 m5 z# }; v/ U // complicated schedules.
4 B K6 `9 \# p" S0 a, Q
) n, W0 O# _- G1 _4 x modelSchedule = new ScheduleImpl (getZone (), 1);
5 d" _# a0 _0 b Z modelSchedule.at$createAction (0, modelActions);8 j* y! ~0 y# D# B& ^1 [" q
2 g" n% _; p2 O0 }9 ~8 ] return this;$ @" F; P) |3 h# a/ L
} |