HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ Z. E" s1 @0 o, }( X D" J, n
4 o$ n& O ]/ { b( V1 y3 ^ public Object buildActions () {$ l' D G8 U# M8 a& g: T' z( z- h
super.buildActions();
z* z$ O9 D. v 2 f. Z. z( e: H6 y2 V
// Create the list of simulation actions. We put these in4 y/ T. f3 q, X1 W
// an action group, because we want these actions to be
, u0 j5 n8 c. s4 W; S/ {' ^7 Q Q4 W // executed in a specific order, but these steps should
' I6 i) y" v+ w. M // take no (simulated) time. The M(foo) means "The message: M, A9 X- F, ^ {, n Y
// called <foo>". You can send a message To a particular7 G* I8 N' y& ?' x$ Q
// object, or ForEach object in a collection.0 m; t0 Y! m! ~9 b6 \" Y! U
7 P: ^$ V1 V* T5 {
// Note we update the heatspace in two phases: first run# G( g+ Z4 p3 m2 N0 v
// diffusion, then run "updateWorld" to actually enact the
1 j) U( p: W: n1 m, E3 g1 Q // changes the heatbugs have made. The ordering here is
( D9 C6 H4 p9 |) [6 M* V // significant!
3 G1 _6 l# ?$ W5 k; N4 ]
! l: Z' J( }" I2 k8 q/ K: N // Note also, that with the additional
( y$ r# U3 F9 S. o2 l // `randomizeHeatbugUpdateOrder' Boolean flag we can/ a1 v) X- w) X. T! i, I
// randomize the order in which the bugs actually run6 C, R* R* c" a* `0 N: w7 v( j
// their step rule. This has the effect of removing any
N' W c+ b% c- M: a; ?8 { // systematic bias in the iteration throught the heatbug1 M" M3 J) r! z- i: v) l
// list from timestep to timestep
: u5 J9 J/ \: q1 w q N1 L9 p) F& A
' J. e8 I% t( @2 C/ \ // By default, all `createActionForEach' modelActions have a) E0 I; v( |8 `; I% C& b! m
// a default order of `Sequential', which means that the
' Y; Z: R+ m( J* ^# [" D // order of iteration through the `heatbugList' will be
7 l% i% }6 i x9 q! ?2 ], w: ^ // identical (assuming the list order is not changed/ r4 ~3 q$ y4 Y6 _" l6 _
// indirectly by some other process).- Z; N# S! l7 N! f
3 t" s" k% E$ Z2 ?9 A* N modelActions = new ActionGroupImpl (getZone ());
# \7 t) G: a# u. s! l, X7 V* E* B2 S9 G; t H# s' ?
try {
1 W$ b( T" g4 F; M modelActions.createActionTo$message7 r7 l$ n) c1 E- V8 G/ F n. h0 ]
(heat, new Selector (heat.getClass (), "stepRule", false));
+ B' j% z, P% ^; Q+ ~ } catch (Exception e) {; m! K) t! C" H; T2 `& r
System.err.println ("Exception stepRule: " + e.getMessage ());4 } ?) U, e+ G( Q2 L/ @
}
8 g; z, H0 x) A+ z6 d% e8 c- ~/ m9 ^% T. e3 E! x' X, @, ~$ U
try {/ E6 G% n& y7 |; K/ g# B' c* N# c
Heatbug proto = (Heatbug) heatbugList.get (0);
4 A8 l( k/ x; O" V* B" F6 S5 C( [7 y0 | Selector sel = " v" c1 z2 o$ c, o
new Selector (proto.getClass (), "heatbugStep", false);/ C! r+ x* U# t7 ?6 p9 h O; n% b A4 g
actionForEach =8 k2 |) G3 @5 Q; v1 h* ]$ l% N6 d
modelActions.createFActionForEachHomogeneous$call* e* M! ^) d3 O! `% j
(heatbugList,
$ E0 m% P; T7 x new FCallImpl (this, proto, sel,3 r. [4 d2 c: D7 h ^
new FArgumentsImpl (this, sel)));
4 |& Z' `) h+ U, D; K. R6 S } catch (Exception e) {8 a$ t8 V5 p9 W' R: V
e.printStackTrace (System.err);
. `( V8 i7 x; E& L% c }
- ]" {$ p) a: w w4 u5 q1 U( ^6 \* ~ 2 d4 y2 y0 {. s2 K$ H( @
syncUpdateOrder ();+ \+ z0 C: i7 w1 y) R
* p P$ \3 C3 x
try { f3 N0 K! I) P G/ \
modelActions.createActionTo$message
- r+ d- c8 K3 z% b9 ?5 E9 v/ N9 C (heat, new Selector (heat.getClass (), "updateLattice", false));. r5 c5 A3 ]( i: K! J
} catch (Exception e) {* \: p( }4 r5 f7 b0 D* C, S
System.err.println("Exception updateLattice: " + e.getMessage ());
5 g; o2 H% v6 q9 `5 ]( X# ` }
3 D5 f7 Z! O8 ?1 l, M* Y0 F 7 ^: L" I. C$ c; o4 d( ^9 P
// Then we create a schedule that executes the
" I$ H* `% Q0 \! W1 p9 F# P // modelActions. modelActions is an ActionGroup, by itself it
1 }* [& G# i8 \# x, h; B, h$ S // has no notion of time. In order to have it executed in
. l6 J& I/ ~7 O& |9 a+ A& W // time, we create a Schedule that says to use the
7 l5 z. T, ]6 [; C2 T7 ^6 a a* C4 F: g // modelActions ActionGroup at particular times. This
2 \' F% }7 z5 J! a9 C. h // schedule has a repeat interval of 1, it will loop every b0 v' u, l& M2 ]3 l5 h7 L- U ^2 `- k& w
// time step. The action is executed at time 0 relative to" y2 U( O8 `' n: ]! \9 H; _+ z
// the beginning of the loop.6 p! N8 H4 H v, V
3 ^! O2 @6 K7 S7 W& w% ]9 ? // This is a simple schedule, with only one action that is
3 k( z& Y0 P; { // just repeated every time. See jmousetrap for more+ f/ Q6 \& H1 I, ]8 @4 f: P% H8 s
// complicated schedules.( W+ \$ A) o0 J3 `8 N
* Y; E9 o2 m5 @- g f) n0 Q modelSchedule = new ScheduleImpl (getZone (), 1);1 U; C0 s0 p# h+ \: N- _
modelSchedule.at$createAction (0, modelActions);: T8 M1 q) x/ c! V' Q ~% o
) u0 u0 G4 g- \* O. i7 }5 m% ^5 ~# m- W
return this;- i7 ~7 m6 R8 i3 k* c$ I# y; o
} |