HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: c+ ]2 s% c* y6 ?
4 w; k8 g! w) a3 K2 l8 f
public Object buildActions () {0 h; j* T) k/ l9 R* p7 U
super.buildActions();0 T: h% q/ ~. g
$ \) m) N0 D8 K) m' S6 v+ o7 s } // Create the list of simulation actions. We put these in
# u) q; j5 j1 M: j6 w. m // an action group, because we want these actions to be* p% c7 L- S4 G0 }/ K- Q
// executed in a specific order, but these steps should) Z/ g; d& @0 t
// take no (simulated) time. The M(foo) means "The message# Y) n1 E- j& u
// called <foo>". You can send a message To a particular
4 X9 s1 T) W& A- g2 Y* g // object, or ForEach object in a collection.4 y, Y- p5 p. U
* W; D* `: X: |8 S
// Note we update the heatspace in two phases: first run2 T ~' Q- d6 @+ Y& ?( `1 Z
// diffusion, then run "updateWorld" to actually enact the8 \, t$ \4 D2 t0 K6 U) h: Q
// changes the heatbugs have made. The ordering here is
4 t( Y- M% u# b, [ // significant!
( n; W, l+ \% r. D
2 U, w) w2 n1 c // Note also, that with the additional
n c+ {. m5 u; b: w // `randomizeHeatbugUpdateOrder' Boolean flag we can
) t/ O$ k% y( p) _ // randomize the order in which the bugs actually run1 d3 _1 e4 X/ a4 m1 w
// their step rule. This has the effect of removing any) Z3 P$ a9 E5 v3 `' ?, m
// systematic bias in the iteration throught the heatbug
5 d; G; r$ G: c; H // list from timestep to timestep' B! C6 r5 T$ Z4 @
8 v1 f6 X7 ]7 y# |, g; `5 E0 k // By default, all `createActionForEach' modelActions have( r1 U: w5 d4 U3 J1 n) u
// a default order of `Sequential', which means that the5 q( y1 v# x* f1 J3 o% ~; `
// order of iteration through the `heatbugList' will be
1 O4 h" g0 h( k- ?$ B. m3 h4 d) R // identical (assuming the list order is not changed" I" o+ W- J# p0 ^% `; C& n
// indirectly by some other process).
! M2 C. }& a: r0 A; G 4 x5 H5 Z; u, J& Q. C! ~
modelActions = new ActionGroupImpl (getZone ());
- a/ h, O: O8 k+ S9 b; {- A* @; ?2 d1 j; B& l: Z* o
try {
5 M; M" X: L L0 f$ v8 ]1 S modelActions.createActionTo$message `( z$ H: ?; O+ P
(heat, new Selector (heat.getClass (), "stepRule", false));
/ V0 ?8 g7 o" M! y: | } catch (Exception e) {
|; n& O, t2 {7 {& h5 V% ^ System.err.println ("Exception stepRule: " + e.getMessage ());
. N; s8 \" o+ R2 [8 z# u6 E2 ^" y N( N! O) s }4 {' [' U Y. w: [
% e. A# Q& P7 r try {7 c0 m' i1 l" N8 Q2 |! o" @
Heatbug proto = (Heatbug) heatbugList.get (0);- K! ^5 H' h8 a
Selector sel =
z1 z+ C! j9 Q- E( L0 d new Selector (proto.getClass (), "heatbugStep", false);
$ _2 ^1 s/ ?5 i8 c/ m4 q actionForEach =
l3 b- q! C/ ~% V+ I* \' | modelActions.createFActionForEachHomogeneous$call
# c. O8 P( C+ H: d( X7 K. m5 ] (heatbugList,
1 V4 }' a1 `+ P5 m) m9 O2 W new FCallImpl (this, proto, sel,+ u* Q7 L. K% d4 L
new FArgumentsImpl (this, sel)));8 C% A6 I9 {# e( x4 U) N. V9 i3 w
} catch (Exception e) {9 [. m/ g' p- e, y! b
e.printStackTrace (System.err);
% w; f; X+ J6 d }
$ A: A- q- ^3 x$ Y; z
; g8 A3 U8 q3 d- F syncUpdateOrder ();
, H: P2 o7 S0 n% s% @
3 Z0 s) K: o# T0 m try {
3 O2 F4 A9 i( J! a& l8 C( U modelActions.createActionTo$message
0 m! H: q, e T9 Z+ {( \ (heat, new Selector (heat.getClass (), "updateLattice", false));
E8 d. o6 f6 ~6 K3 f) l0 A9 f } catch (Exception e) {; ] c/ v6 H. U, g6 l
System.err.println("Exception updateLattice: " + e.getMessage ());
* L% c5 x9 W+ B% C- b5 k }
, `" o9 S! J: r8 ~7 c c 5 ], y/ j* Z$ X9 i/ Q9 N, T7 ]
// Then we create a schedule that executes the, p) ]. B- E8 ?5 j4 l
// modelActions. modelActions is an ActionGroup, by itself it
1 f6 i6 r$ f M: q5 @* Q // has no notion of time. In order to have it executed in) l$ G7 D- x0 u( G# d9 s
// time, we create a Schedule that says to use the
" X1 l6 ^' [( y8 l$ ] // modelActions ActionGroup at particular times. This
2 |$ a4 ?7 i! [ // schedule has a repeat interval of 1, it will loop every
% ^/ \& f8 a' N3 ~2 v3 @5 T9 v$ f // time step. The action is executed at time 0 relative to8 S0 C7 s0 x; E1 \; X
// the beginning of the loop.7 w$ K7 Y5 ]- s* {9 C; g! k
- \% I+ ~5 b1 i w // This is a simple schedule, with only one action that is
5 _. [! y! t: O // just repeated every time. See jmousetrap for more
& T# ]& v, k) @ // complicated schedules.6 Q0 X u7 ^, \7 C4 }; K
2 t+ J6 @. t4 L- o modelSchedule = new ScheduleImpl (getZone (), 1);6 X- T" @3 O x7 Y m, s# c+ o
modelSchedule.at$createAction (0, modelActions);
/ s, e0 K) b% y% J) K" x
& o+ @9 V# ?% \5 A return this;
5 {8 U) v. r! r! \% v- I* t: u0 A } |