HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, l% m' @+ N- Z' R4 y
2 w5 K R0 u# s$ J$ q$ b* Y+ m public Object buildActions () {" @/ l$ i/ f: H1 H9 R# v
super.buildActions();+ W8 V; b# L$ {% w# ~7 ~
1 z& q+ ~3 C& z( r, D: _: H
// Create the list of simulation actions. We put these in9 R7 r& |' V' {7 Q' j
// an action group, because we want these actions to be
, l& P( s; r. G; A/ G4 Q; x // executed in a specific order, but these steps should. |# |6 D( F! V X
// take no (simulated) time. The M(foo) means "The message( v& O, y$ J" R% h3 @
// called <foo>". You can send a message To a particular
8 r1 | S+ Z8 X2 z7 F // object, or ForEach object in a collection.
1 J: T: d. _& q# w" R$ P) B 9 Y9 t) R! p- |4 i% o
// Note we update the heatspace in two phases: first run
7 @0 D6 |! |; d$ H c) a3 v // diffusion, then run "updateWorld" to actually enact the4 L3 t" u) {: _+ L3 q3 T5 X
// changes the heatbugs have made. The ordering here is
: G- c& [- @1 t. \& S // significant!7 ?9 b( L0 B2 Q& c1 h R# W3 P% E
- \9 a3 m; o3 w- W
// Note also, that with the additional
; w$ j2 N: B/ Q: n: A! c% h. Z // `randomizeHeatbugUpdateOrder' Boolean flag we can
( l3 M+ \: h) [, W8 i. D q // randomize the order in which the bugs actually run7 e* Y& D# `" x4 B! \6 g
// their step rule. This has the effect of removing any# _' p, l# ]+ a! j: N
// systematic bias in the iteration throught the heatbug
" T: O" L H# |2 ?8 ` // list from timestep to timestep
* B% [9 U3 n& D; Q ! y9 p8 |8 }- Z/ H" ?
// By default, all `createActionForEach' modelActions have U. W# Z. C& t1 m& T
// a default order of `Sequential', which means that the
0 \5 l; |$ l& Z+ n // order of iteration through the `heatbugList' will be
; u k O& ~5 z // identical (assuming the list order is not changed
' P: U/ ~( N+ @/ m // indirectly by some other process).
6 a$ O% p' a I7 ] 4 q+ x0 ^. m& j
modelActions = new ActionGroupImpl (getZone ());- T, M0 _1 X) y/ Y5 {8 A
N; ^( A E7 q9 U6 T
try {
7 s9 i* S# T; h, S: \( i6 p, K modelActions.createActionTo$message0 T+ U5 t; t1 D
(heat, new Selector (heat.getClass (), "stepRule", false));
" ^8 Z% i% F7 h$ A o+ Z" y& c9 K } catch (Exception e) {5 @0 W. K1 P; ?) V$ k
System.err.println ("Exception stepRule: " + e.getMessage ());
, L4 O+ y4 |; h/ m* e }
3 A4 a: J S0 F2 I5 b. w( [2 `& g+ C
try {
+ w* f4 ]7 U% x8 {6 Z Heatbug proto = (Heatbug) heatbugList.get (0);: r: P! C' e+ z
Selector sel = + Z; |. S% W3 E! M* a, G
new Selector (proto.getClass (), "heatbugStep", false);' v2 s) i. T9 y6 v
actionForEach =
# X9 c: m$ M9 {) [4 Z. ^ e modelActions.createFActionForEachHomogeneous$call/ G% X. F, T d- }) z+ }
(heatbugList,2 V- ?6 V! U" |, Q( P4 v9 e
new FCallImpl (this, proto, sel,
" M5 U7 y2 W1 _ new FArgumentsImpl (this, sel)));. Z* M& M8 E$ k
} catch (Exception e) {( v( z, H' B3 j% R
e.printStackTrace (System.err);9 u) _" _. |5 G1 M9 s& m- m
}' `% X) O5 Z3 B- J" Q6 J) u; O S
2 C% Z$ n* q, `. [ syncUpdateOrder ();
7 H# W# w0 s) ?# k$ f. E3 u0 {9 ?' _6 X! d* |* o/ c
try {& d3 ^, V7 H- u( v
modelActions.createActionTo$message 0 d. {9 q$ S- i8 p$ i! [
(heat, new Selector (heat.getClass (), "updateLattice", false));6 l: ~2 D) F6 I7 T1 q
} catch (Exception e) {
0 v* @3 i! b4 S* m System.err.println("Exception updateLattice: " + e.getMessage ());
& ^# B8 r I* v9 C9 U" f2 v }. @$ @$ m" p% j0 Z
$ k+ O! Z4 X3 H$ D+ L |9 | // Then we create a schedule that executes the
[' W, M* _. M+ _/ y# T5 v // modelActions. modelActions is an ActionGroup, by itself it) o! p3 g# b+ S
// has no notion of time. In order to have it executed in- O, G2 [! N" H: e6 }3 u+ O) l
// time, we create a Schedule that says to use the
; M1 _* \% G6 b // modelActions ActionGroup at particular times. This
7 F9 D# G/ T x4 A# A4 g. O // schedule has a repeat interval of 1, it will loop every
* x/ v% G8 U) Y1 x; D( s/ G. @ // time step. The action is executed at time 0 relative to a2 w7 B: T% p9 ^" N& C
// the beginning of the loop.
" a; J" u0 _5 k, ?" G: w/ l. J& f. A- T8 |& x' V5 ]4 w7 a
// This is a simple schedule, with only one action that is
& W1 f `6 p( L' p+ d! `: } // just repeated every time. See jmousetrap for more$ t1 ]( p+ Q- }* e8 a+ F) \
// complicated schedules.
, V2 d7 C3 E2 v& U/ L! l2 l. s
' U4 Z+ H8 s3 p0 O3 p2 i modelSchedule = new ScheduleImpl (getZone (), 1);2 o7 i2 l" D: u, K t
modelSchedule.at$createAction (0, modelActions);
5 B0 Q& k5 [+ Y ; x) {, h, K6 n9 C! o5 B! p0 E
return this;: h, f2 e7 S% d- r' H$ D9 v
} |