HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ V/ m" ^( q' R, N3 x r/ {( ~+ o: j: Y. w1 w3 V! L7 e6 |7 k
public Object buildActions () {
2 Q }0 t. Q' i- P super.buildActions();9 C& h; M& w9 n% J$ x' ~
8 [! g9 E! _4 _ // Create the list of simulation actions. We put these in4 I( X* K7 z. J% H
// an action group, because we want these actions to be" T3 P: x h) Q; _0 N( k0 e0 E
// executed in a specific order, but these steps should% T3 q6 A& E' B2 t$ E
// take no (simulated) time. The M(foo) means "The message: J: U" A9 f$ I
// called <foo>". You can send a message To a particular7 F, c: A1 {- I v2 a5 d3 p
// object, or ForEach object in a collection.1 i7 o/ W/ O9 _0 T
4 I% U0 e# u& @4 u I. u& }
// Note we update the heatspace in two phases: first run" H; F% S) v' x5 y5 K+ H; M, |
// diffusion, then run "updateWorld" to actually enact the
* j! ^1 h8 H2 Q" V$ v4 \3 S // changes the heatbugs have made. The ordering here is* u1 R! w _- h; ?8 w
// significant!& d; ^: R( j. f/ O
' `7 k# u/ _7 @& h: Y7 ?& x+ P$ {
// Note also, that with the additional" I6 |1 |5 ^( k9 y* r! X
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ t7 S. g x; I, [) [6 P( L% d1 d& Y
// randomize the order in which the bugs actually run$ e# \" U* X4 l
// their step rule. This has the effect of removing any
0 y6 j! U7 Q1 d; T z/ b // systematic bias in the iteration throught the heatbug
4 Y1 ~) t; D5 z/ z9 T // list from timestep to timestep
7 {( y3 |) K' R3 R
+ M6 ] l+ r% X( b) d // By default, all `createActionForEach' modelActions have& Q1 l; O# c9 k' O) Q n$ I
// a default order of `Sequential', which means that the8 X- S0 q& ^/ n7 _
// order of iteration through the `heatbugList' will be3 F1 n9 p+ K! g
// identical (assuming the list order is not changed
$ W+ b& y0 v# L; [& ~ // indirectly by some other process).3 u* F" E3 S5 | t+ H0 n& K9 ^
/ O& u, k" d: d6 N5 e
modelActions = new ActionGroupImpl (getZone ());
& ]9 b5 z' z' c P. ^; T- m- l, B$ s0 @8 }+ s: r2 i" H; q
try {8 T6 ]$ x7 p9 p/ y7 h
modelActions.createActionTo$message1 z2 t) e; \ A/ j2 A9 t/ W
(heat, new Selector (heat.getClass (), "stepRule", false));
; q/ _4 ^0 P% C2 A! B" U" y } catch (Exception e) {, R( O ^' g5 s- \# Y6 i
System.err.println ("Exception stepRule: " + e.getMessage ());
; Q& [# w3 ?" x }
* u' f! e b( v& N7 H% K/ g
1 I! W# ~3 P5 C; n6 u4 p5 i try {7 i' A. @$ x6 e. }/ n& B
Heatbug proto = (Heatbug) heatbugList.get (0);
3 g$ `& D; F) g9 ~ Selector sel =
8 E& ?3 s* ]! o new Selector (proto.getClass (), "heatbugStep", false);& t8 d/ U/ {/ ^
actionForEach =
" D+ }! V0 b6 u2 ], T; J+ A modelActions.createFActionForEachHomogeneous$call) s! [0 |4 c) X+ Y3 B/ [, b' V4 |
(heatbugList,
; @, l4 I R* O. j- v9 t! y, | new FCallImpl (this, proto, sel,
/ K1 X6 F0 I) Y new FArgumentsImpl (this, sel)));3 E/ m0 H/ Z' m
} catch (Exception e) {
/ A$ X. H% f3 \; _4 {" T& j8 F e.printStackTrace (System.err);% I d- w; \: \9 E
}
0 p$ h* _& U# D: g# X & P) F: e3 R- {. e' R3 C, Z3 G3 Y
syncUpdateOrder ();
: X! S- h6 ]- o, M8 g& G
/ V- g6 S5 m! C5 P; n4 d! N5 m( i try {' D; @& n8 G5 n4 j( l' C( F1 G' Q8 ^
modelActions.createActionTo$message " U. i+ t# H# N9 I" b3 x
(heat, new Selector (heat.getClass (), "updateLattice", false));, L8 v: X( K$ f
} catch (Exception e) {
2 [; v, `$ b7 V) r# R' [) I System.err.println("Exception updateLattice: " + e.getMessage ());
% [8 Q* x# v* z1 x- V4 a }$ d z2 Y+ O1 \0 A. D# W
1 b X- P2 z$ s" E& ]! Z3 V5 X# s // Then we create a schedule that executes the4 s& _0 t6 C6 \; h
// modelActions. modelActions is an ActionGroup, by itself it
! B2 q" j( x/ ]) V* o: B$ Z8 F // has no notion of time. In order to have it executed in9 a# e+ @# | N% Y
// time, we create a Schedule that says to use the
' r ^& { t2 K2 h% ]. w // modelActions ActionGroup at particular times. This( ^# E( l1 M1 A! w( V2 u
// schedule has a repeat interval of 1, it will loop every
. G+ V( m" T8 u0 U* R! M // time step. The action is executed at time 0 relative to/ I8 q2 M' d* L0 w9 I4 ^- j' V
// the beginning of the loop.* `" g4 U& b2 a9 ]2 y1 H& Q* P
6 @% @0 ~2 T( n: e$ D // This is a simple schedule, with only one action that is
( ]1 O4 o. A( R* f1 R // just repeated every time. See jmousetrap for more9 E9 T" f% g( D; U ?) X
// complicated schedules.
6 ^- V# o; W/ W5 [! C4 ] % _( `. `, M$ C3 o4 S) Y
modelSchedule = new ScheduleImpl (getZone (), 1);
) p$ V; Q" M: C* j5 l modelSchedule.at$createAction (0, modelActions);
; D) m& @* n6 N / a/ e5 x$ Q6 c- K
return this;! @/ V6 r8 g/ B* B% Z
} |