HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 l3 \6 {8 ]* }1 K7 F$ w2 N
1 p9 R# T6 `0 M0 J public Object buildActions () {
: [" {- \" r' Q. C% Y super.buildActions();8 l9 P0 O# i$ O+ g1 d; z
3 B& c- o! ?) V# y$ w2 `
// Create the list of simulation actions. We put these in: P8 x, S! ?! H; X6 ^
// an action group, because we want these actions to be2 z# F. |7 s7 W8 {( @' G
// executed in a specific order, but these steps should8 W% {- k V- c8 c% V1 q$ r
// take no (simulated) time. The M(foo) means "The message) e3 g0 Y% p2 k( }7 \6 o& E) h/ v
// called <foo>". You can send a message To a particular
+ `3 d/ q% X$ d9 k // object, or ForEach object in a collection.
- @2 F5 q v7 B" y0 H+ C + M% l" q: S2 F, Z3 B
// Note we update the heatspace in two phases: first run# v8 E7 U, _. U H, a
// diffusion, then run "updateWorld" to actually enact the
8 M, u. G9 j. t. B/ c4 i" g // changes the heatbugs have made. The ordering here is
: [$ F* R9 E# d9 c( J' O // significant!
% r' z( ^, @0 ~3 {( S L0 w8 t' p( e
4 D- ^- V! F$ x; S // Note also, that with the additional
/ q/ }% ^- a+ ^: R! |+ D7 L" v) c // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 ~2 J1 ?: K$ G* z) e2 D5 ?6 ] // randomize the order in which the bugs actually run$ o& {, V7 x: U! o6 o
// their step rule. This has the effect of removing any! C8 |* ?( v4 [9 H1 ~: w; B
// systematic bias in the iteration throught the heatbug4 m9 x2 D0 R8 v$ L1 k4 K8 W' K" Z
// list from timestep to timestep# d6 Y6 f: ~4 g7 `8 v6 G
; h6 T- d3 @+ w& X& ~/ y1 M, A
// By default, all `createActionForEach' modelActions have
9 [1 B* y( x9 e // a default order of `Sequential', which means that the Z% h: G6 [" f0 b$ F) B' O
// order of iteration through the `heatbugList' will be
( N o/ o6 Y, E! A/ M* N // identical (assuming the list order is not changed- {# h! ~& x2 p9 C* t
// indirectly by some other process).$ F) Q/ f: i$ z- Z7 E! B% J& g! L
4 s, g' J4 f+ h3 v
modelActions = new ActionGroupImpl (getZone ());
2 d( V* {& x/ a9 `
4 c6 h A; M; x6 E try {
* n( ]4 g% `8 v4 q& H; X modelActions.createActionTo$message. Y0 [9 b0 N; }' i1 w
(heat, new Selector (heat.getClass (), "stepRule", false));
% l; R/ h% p- Y, c4 I$ W } catch (Exception e) {
* p8 i8 ^7 f2 H6 D- w( i System.err.println ("Exception stepRule: " + e.getMessage ());; n7 l3 j* ^+ O. R2 H
}- v2 H0 L: g7 G1 c' n/ C
2 D, m# | O/ M' Y try {4 @' R7 R0 ~, }) c$ P3 o' z6 j+ r% m
Heatbug proto = (Heatbug) heatbugList.get (0);
0 {: L4 {" j1 t V Selector sel = - k" r! R1 L/ M
new Selector (proto.getClass (), "heatbugStep", false);8 G9 J4 a- Y7 O" ]8 X
actionForEach =, G d$ ]! K5 u% h3 a
modelActions.createFActionForEachHomogeneous$call
- }5 J3 p' s" i1 V0 m% b" P (heatbugList," J( U* n8 g1 I
new FCallImpl (this, proto, sel,
" Z# o' ?3 c6 A3 F new FArgumentsImpl (this, sel)));. b+ ]6 w ?' k; C5 l9 F9 H- l
} catch (Exception e) {. `; n) a4 _6 o& x2 T% Q/ y
e.printStackTrace (System.err);2 x6 h/ n! z- L; W w
}
8 y" T: H; h( S5 a: i$ J) W8 n " K) K, d/ t7 R1 Z
syncUpdateOrder ();
8 G9 [; `( L. d6 |2 s0 b0 J. Q" @# x! s
try { u! C6 h& g' S0 N/ N" g
modelActions.createActionTo$message 7 V* b% k/ t/ r! k0 P6 B2 B! V) v
(heat, new Selector (heat.getClass (), "updateLattice", false));
6 H& K/ |4 s/ M# s } catch (Exception e) {& c- c5 {& Z8 f ^
System.err.println("Exception updateLattice: " + e.getMessage ());% U% ~$ h/ S, C+ c# | ^1 R7 y: _1 A1 Q
}- N, Y8 {! F- R$ C1 C, ?2 _( R
: q6 t+ g4 e+ r" o
// Then we create a schedule that executes the
6 I. A' L( }* C% \% j e( z // modelActions. modelActions is an ActionGroup, by itself it
0 U3 ~! z& H/ P6 t h6 m% Y9 ^ V* [ // has no notion of time. In order to have it executed in. P5 b# \7 ~) D; B
// time, we create a Schedule that says to use the
1 G3 M- A4 R, {4 q3 e7 F6 y, M // modelActions ActionGroup at particular times. This4 p: y% o0 o2 w
// schedule has a repeat interval of 1, it will loop every
3 i0 n# z+ @2 }( K // time step. The action is executed at time 0 relative to
1 O9 S1 |+ H( ^+ z // the beginning of the loop.
% H" W% t' |! M# a0 q' P
, h3 ~. i' T$ _- O7 z, C5 H // This is a simple schedule, with only one action that is- m/ W% ]4 p4 M6 Q8 F* M
// just repeated every time. See jmousetrap for more4 z; h/ B% @* }0 o/ ]( V3 x; ?; y- U% `
// complicated schedules.
% j( s. c' C0 l6 f" U
; A7 a, T# c* o# o. o5 X- h+ V modelSchedule = new ScheduleImpl (getZone (), 1);4 ~8 m/ M* S6 |# Z2 {
modelSchedule.at$createAction (0, modelActions);7 M1 D( P5 b( ^3 c6 J
1 Z/ l3 c. l; h8 v
return this;
: M- q- K% }7 A; }6 H } |