HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
K8 c3 f- O) W( |7 k) `4 u/ M% c/ m3 j8 h8 B! s8 w7 c: \
public Object buildActions () {
! G9 k# H: m7 m$ r) S+ \6 B+ z super.buildActions();6 P8 P9 ~8 y4 P( [) \
' F) m4 Y/ f7 G$ R5 z
// Create the list of simulation actions. We put these in# _+ ~2 k% Z! ^8 m" N, ?
// an action group, because we want these actions to be
- ~' t* X( N, F/ T7 ^4 k4 S // executed in a specific order, but these steps should
K) n0 ?, Y9 B* v) R! b // take no (simulated) time. The M(foo) means "The message" p) |+ }" y7 N' K- W* d7 `( w% z
// called <foo>". You can send a message To a particular, u6 }' G, V4 d5 H2 h. S
// object, or ForEach object in a collection.. |# B u$ h: @
( l& c* I& D+ M; Y3 [. _' X, a7 ^ // Note we update the heatspace in two phases: first run
+ P9 h# a& X: e; s2 v // diffusion, then run "updateWorld" to actually enact the3 Z9 d+ Y; k/ q, O. {& E2 X9 [
// changes the heatbugs have made. The ordering here is6 ?) X f# R! a# L @! K
// significant!, h3 }' E7 g' {+ Z( X
+ c+ Y) n' g2 P$ `( [# o4 j
// Note also, that with the additional" L: x/ X: N. F5 J( I% R( h
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ G* A1 ^2 [/ {; ~- O& V. m/ a1 B
// randomize the order in which the bugs actually run
7 \1 G# V" ]% u/ a. }, b r // their step rule. This has the effect of removing any
2 s! [5 h& G4 C, S$ X7 `. ` // systematic bias in the iteration throught the heatbug
9 f _% S. H5 s$ w // list from timestep to timestep% g: S; m% |- d3 L6 \. @; z
- s0 P8 i$ z2 ^! U4 i
// By default, all `createActionForEach' modelActions have
" F4 y# u( f) @/ l! L/ V // a default order of `Sequential', which means that the
) y, K* H. w$ s: d! D // order of iteration through the `heatbugList' will be
% i% i, C% ~0 G; P7 d$ H // identical (assuming the list order is not changed! p# M8 z9 z2 K# O9 Y( l y8 O9 |6 B
// indirectly by some other process).
+ ?" t: P& d8 R; J6 h0 \6 d ( ^5 g" Y4 Z, w( Q4 T% Y' x- ^
modelActions = new ActionGroupImpl (getZone ());' {; W2 f3 t3 g
' ? y! y: N/ K$ S- H5 E5 K6 @
try {: }0 x# E/ h" V7 A3 [
modelActions.createActionTo$message5 ~0 d! k2 w* M9 N7 O( U
(heat, new Selector (heat.getClass (), "stepRule", false));! s' {) Q; f4 J+ h, j2 i
} catch (Exception e) {, L/ U3 j' ` ~+ W: i& l
System.err.println ("Exception stepRule: " + e.getMessage ());$ D* J- B1 |7 f* B* T! I
}5 W# ~) L) O r5 `. y& q6 ?
+ P8 E1 E, R0 z. v
try {
4 A4 n4 ?9 _2 {: @% C" ?& p Heatbug proto = (Heatbug) heatbugList.get (0);6 v- K( E/ I/ C; o g b
Selector sel =
0 }& ?0 j4 a9 y' c) A/ G6 s4 r8 @ new Selector (proto.getClass (), "heatbugStep", false);$ J8 |/ A9 S. W1 _4 U' \/ G# O! V
actionForEach =% [, T) U7 e. f3 Y
modelActions.createFActionForEachHomogeneous$call4 C( i2 s$ r. F7 m; i0 i$ Y& S: D- W
(heatbugList,
8 J% K# B7 m2 c& { new FCallImpl (this, proto, sel,
. D4 x) f5 J3 T) U new FArgumentsImpl (this, sel)));
6 Q! ?6 `& M" z' A0 c! ^ } catch (Exception e) {3 k$ V* j/ L. H ]9 i7 W1 G
e.printStackTrace (System.err);
$ q; l2 a9 h+ I9 _3 Z0 F }
+ Y7 R( S7 J$ D6 g" M. A $ A9 p, a2 A6 f4 [8 |% D2 ^5 M
syncUpdateOrder ();! |# B7 I* c$ l3 ^3 x
" E1 y' L# i6 Y* U @" I
try {2 t' g0 `2 q# R& A; F$ f
modelActions.createActionTo$message
9 ]- e" M+ p5 B9 z: J" a# g (heat, new Selector (heat.getClass (), "updateLattice", false));! F' u: o4 z& x2 t
} catch (Exception e) {
6 N: U: }* o6 \+ I) e# s System.err.println("Exception updateLattice: " + e.getMessage ());
* C% I% @6 K! [1 L" d }
- I3 S. q0 Y/ m
1 l& z' Z& W( J" A6 r0 z // Then we create a schedule that executes the- ?% t2 u5 K) K2 I: g1 s6 P; X2 ?% {
// modelActions. modelActions is an ActionGroup, by itself it
8 ?( c) t% X1 e+ ^( V8 Y8 | // has no notion of time. In order to have it executed in9 F3 _1 p* W$ |7 k7 R6 L6 m1 S+ u
// time, we create a Schedule that says to use the! U% A1 O6 K: `
// modelActions ActionGroup at particular times. This: T: L6 W, v* A; T7 v O/ [0 b( o8 l) m
// schedule has a repeat interval of 1, it will loop every" }) h+ b7 G+ P% ?* {3 F2 p3 J
// time step. The action is executed at time 0 relative to6 T8 P% r9 L) P
// the beginning of the loop.
, s) u, ~5 Y4 p3 g9 ?6 |
' g5 h! M! N' ~ M5 e$ i( n% d // This is a simple schedule, with only one action that is
: M, w1 K0 {' j, ]5 A& R$ D // just repeated every time. See jmousetrap for more8 t, a0 a& A% C8 h R2 C* k& i3 i M
// complicated schedules.
9 l- n, N, d% b. x! Z( \" S( E% G . N" U& _! V/ V+ Y! Y
modelSchedule = new ScheduleImpl (getZone (), 1);
; a. T, {8 R$ t8 D, W4 e modelSchedule.at$createAction (0, modelActions);* A, {2 c; Y+ C8 j$ A
* X( a* {. }& h8 e7 [% o
return this;$ Q# u$ y, f% T4 n
} |