HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- R; j6 b: Q m
( ~3 c) M( A8 f public Object buildActions () {
1 {, t3 _2 W- m5 f' v, C super.buildActions();
( u! M" W [0 C; A# `/ Q4 C
0 Q7 h$ z) }: c // Create the list of simulation actions. We put these in
6 n8 O" |. m5 b // an action group, because we want these actions to be
Q$ e& E1 g, b# t6 ~. ? // executed in a specific order, but these steps should4 ~% H# a" d: h( J; Y3 ]/ k" m6 h
// take no (simulated) time. The M(foo) means "The message0 Q* @1 I) }' [ i
// called <foo>". You can send a message To a particular
6 F! s" S. o' O* k% i // object, or ForEach object in a collection.+ ] B% f6 `# ~4 _
0 ?# B8 N, F+ s* H0 z
// Note we update the heatspace in two phases: first run+ ~1 N/ B; J% \8 `7 |
// diffusion, then run "updateWorld" to actually enact the% K: C' b* C' ~6 O3 z) g* l7 C
// changes the heatbugs have made. The ordering here is: T# ?$ E! o/ Z( K+ N
// significant!
! [# o7 c) {8 _ U) O
A6 o* s3 A& n+ A( k0 j7 [" A // Note also, that with the additional( g1 w7 Q* v% Q+ g: C
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 r3 N. a& p% f' ^5 n
// randomize the order in which the bugs actually run n. Q; j) b6 y* P
// their step rule. This has the effect of removing any
" W9 g- i8 N( _. N6 W3 `3 c7 N // systematic bias in the iteration throught the heatbug
, Y6 q: c5 t0 L& P4 Q' q# G // list from timestep to timestep
' B9 ?1 B! r# B8 g: [) P( n9 l- V ; F9 D: N5 f- [ p
// By default, all `createActionForEach' modelActions have. p1 l) K3 L1 a2 Z3 x& v. |
// a default order of `Sequential', which means that the& X4 w$ W' g( |3 ]
// order of iteration through the `heatbugList' will be
0 S2 Y/ G/ q) e7 ~% H) E5 l) T; t* S // identical (assuming the list order is not changed, v# G% h& V: G, c" {, ?
// indirectly by some other process).
+ s% p" l+ c* F$ R, F1 N- p$ e' F
* d; \" H2 j2 x# Y6 S. n0 K( h modelActions = new ActionGroupImpl (getZone ());
$ \( f) {4 Z; Y/ r) m% r' l6 o+ @8 `) W, @5 s2 z* z0 J
try {
q$ _+ E8 i+ @" p, [ modelActions.createActionTo$message- _3 b4 s% f$ R+ |& ^
(heat, new Selector (heat.getClass (), "stepRule", false));
6 h, i# t$ M2 @& ?8 M } catch (Exception e) {
4 g5 J: b7 Y7 t6 n( W. `, P System.err.println ("Exception stepRule: " + e.getMessage ());7 Z7 l, ~8 R- ?4 Y" v7 [) w( F
}
2 Y% h4 Q9 ]" O& T
6 l& }: N* a+ E- W3 A try {- m R" B) Z8 [
Heatbug proto = (Heatbug) heatbugList.get (0);
, R- C- N3 O6 Y( H Selector sel = $ W+ v" V, V. I# S; q f$ L9 d
new Selector (proto.getClass (), "heatbugStep", false);
# r& U% X H0 b actionForEach =" N6 x# n; B, B5 t' d
modelActions.createFActionForEachHomogeneous$call
6 J$ W) R2 l7 H8 q5 n: S( E! I (heatbugList,
0 F& N# n4 j0 ?9 q/ z' l: R. H- O+ @ new FCallImpl (this, proto, sel,; z4 z- A1 t) o3 r6 H; l# @5 B0 I% h
new FArgumentsImpl (this, sel)));
- b$ y$ `' R3 P! s+ @. q } catch (Exception e) {
% a/ a U( f$ a e.printStackTrace (System.err);. l2 {+ h. n, N
}
5 b" O) y# v* M4 ]
1 X3 b5 Z" B5 o) G) M syncUpdateOrder ();% }! U. {( d: y% G
9 W+ S) ]& o0 q" N6 a6 ]# m
try {
4 i8 \2 `1 j% o# Y5 e6 B modelActions.createActionTo$message & Z* }+ T: m# [6 x- ]) j
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 D* {& m6 O2 k7 d8 p! ~1 A } catch (Exception e) {
/ ]' e, W; E6 O# Q System.err.println("Exception updateLattice: " + e.getMessage ());
: x% i) I6 T; L8 u9 x }1 [1 A# a6 H1 T% Z+ a
% J$ G( b6 f9 U5 m
// Then we create a schedule that executes the" J7 Q- O* ]' O2 U
// modelActions. modelActions is an ActionGroup, by itself it1 ]; Y- c' _# f5 `2 R
// has no notion of time. In order to have it executed in4 B8 _7 e7 }# Q1 v) W
// time, we create a Schedule that says to use the% e! s$ P0 Q- L8 g* Y
// modelActions ActionGroup at particular times. This
; Y! h( r) s. a e% c' X% Q; j# } // schedule has a repeat interval of 1, it will loop every8 |3 o: @0 y! J, O2 C" ?
// time step. The action is executed at time 0 relative to
9 K! Z5 m$ |7 d2 a // the beginning of the loop.- ^, I4 ^% K* i3 Y R2 s( Y
4 U$ ?0 }. e2 N- y5 U1 U // This is a simple schedule, with only one action that is0 E |3 N, n: m- e ?) z ~, e9 ?
// just repeated every time. See jmousetrap for more% L0 G% o3 m w' ?3 u, W" b. z6 ~
// complicated schedules.# C, `; Z& G. g+ c; B
6 [7 s' i" e8 O) k modelSchedule = new ScheduleImpl (getZone (), 1);5 L; R `0 H) J8 G2 J
modelSchedule.at$createAction (0, modelActions);
* L+ O+ A& P/ E( m3 @: v
A4 t3 o/ I2 M, j7 `: |5 U, O1 ? return this;
- Q( Y! H# w8 W } |