HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 T' s; p/ C6 \; S
1 G) p8 G( N( y/ E8 ~& E public Object buildActions () {
$ D) i: P4 W4 q( d* ^+ B super.buildActions();
. r( L2 A& i! Y, C
* G& p- s' j, a n/ A( F2 E // Create the list of simulation actions. We put these in! R3 Q) d: B: n' u) c
// an action group, because we want these actions to be z5 w* H( [! `; T; O. D4 n7 c) m6 ?
// executed in a specific order, but these steps should/ P& g' l0 v0 t6 P- w! y5 Q
// take no (simulated) time. The M(foo) means "The message
]$ e7 y5 }+ d+ `. W // called <foo>". You can send a message To a particular
) \% c5 k+ \4 X- h+ T2 `% X' M // object, or ForEach object in a collection./ J+ t' J7 V H4 {( |8 |
3 s! J Y5 f; U5 G* G5 W // Note we update the heatspace in two phases: first run# E4 l3 z8 u$ y6 _0 |9 t
// diffusion, then run "updateWorld" to actually enact the
' K4 x+ D# t6 @" v9 i/ p. s // changes the heatbugs have made. The ordering here is9 K8 z& W% ?& ?, I9 Y' P
// significant!& a+ S+ B, @0 t: s8 G6 Q7 N
* a1 O: H2 Y# o7 O9 A
// Note also, that with the additional8 [, S# ~; }+ ?6 g7 @: n+ D6 f
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- Q& ^. {( G7 P9 Y T+ n // randomize the order in which the bugs actually run
& h4 [. ]$ Y F8 R // their step rule. This has the effect of removing any8 R. c9 }0 A. e( t3 j
// systematic bias in the iteration throught the heatbug
# u( x9 J4 d9 S* c( y // list from timestep to timestep
( a& p7 r) c. d; ^
6 ^) L1 b3 X: V# J: M // By default, all `createActionForEach' modelActions have, U. i7 `* N, V, j ?" T6 W- X
// a default order of `Sequential', which means that the' v' y( p7 N5 U4 E% B, r: A6 C. Y
// order of iteration through the `heatbugList' will be
6 k6 t1 G5 K0 n% w! ` // identical (assuming the list order is not changed# X; ^0 o0 G3 r7 D" ~' [* X! i! ^ ?
// indirectly by some other process).0 x5 r) f9 z2 O/ }- n; h- x1 o7 u2 F
& B8 s( C% Z! d+ @+ f
modelActions = new ActionGroupImpl (getZone ());
3 R3 ?# V- \0 w' b5 f8 K3 ~
$ _' R9 [3 U3 |+ ] try {
, {4 f$ z6 A: l4 \ modelActions.createActionTo$message
2 w+ U* A8 D; ]( V( U& n" ?( G- Z (heat, new Selector (heat.getClass (), "stepRule", false));
$ U N4 x. f2 p" H3 T( |- c+ P } catch (Exception e) {
8 }) @2 p7 t7 }3 N" | System.err.println ("Exception stepRule: " + e.getMessage ());9 q" I1 k" a; ?' B
}# w% P1 y: F3 t3 T9 N
+ L2 G+ q# d- b. L( A try {
, r8 C+ L( ^$ G% B' k( _ Heatbug proto = (Heatbug) heatbugList.get (0);5 f S/ H1 m' {: e# o, b! C! g! I& X
Selector sel = 1 a* n: `: b8 u# ]" H
new Selector (proto.getClass (), "heatbugStep", false);1 j0 p0 E& d2 ~3 P
actionForEach =6 E6 b% g0 Y( h2 S
modelActions.createFActionForEachHomogeneous$call
" d" {* G/ U; _* F2 c (heatbugList,4 s5 c6 t: u1 ?# Q) H0 m
new FCallImpl (this, proto, sel,
( q; I; P7 G3 U: x new FArgumentsImpl (this, sel)));% j/ T- j. G# h+ o
} catch (Exception e) {
4 d1 u3 Z- m3 @* O9 W e.printStackTrace (System.err);
' s0 f% s# ]0 M- c: j0 S }4 d, ?! V4 H. z! }3 f& u
: p- `, U; q: f+ H- l- C5 |4 U+ L$ }
syncUpdateOrder ();
: \; h' h5 O$ Q/ A2 n% u& D' K
. U9 ~1 C5 W0 x8 ?# N try {
8 u7 P$ ~: d' z. B" E5 m/ w modelActions.createActionTo$message
) t, K1 }# M4 i# B" R. b$ L (heat, new Selector (heat.getClass (), "updateLattice", false));7 A6 g1 t9 w+ I+ W6 g
} catch (Exception e) {' x' M |! E& Z( c) ~, ^3 O
System.err.println("Exception updateLattice: " + e.getMessage ());
. @# I4 k* x1 p @- d3 b }
* k! |8 O" Q% u0 r
6 S4 k% t; h7 `' H // Then we create a schedule that executes the1 d2 t5 k; m0 o1 `2 e0 m
// modelActions. modelActions is an ActionGroup, by itself it
[0 I( _$ e" D O) Q+ M // has no notion of time. In order to have it executed in; Y/ w( Y! l- G7 Y
// time, we create a Schedule that says to use the% V: b- T) ? g J0 A
// modelActions ActionGroup at particular times. This: f- y; s _$ u# p, R& }- f! b9 Y4 h
// schedule has a repeat interval of 1, it will loop every `6 R# f" d: F1 u
// time step. The action is executed at time 0 relative to
$ ]" Y; S f* F5 v& H // the beginning of the loop.
* r+ S7 f7 C- k. M M; g" u/ ~3 M3 j
// This is a simple schedule, with only one action that is2 L! c7 e0 r0 O$ W) w; M
// just repeated every time. See jmousetrap for more
" r& ~: ?/ J, U; Q3 n+ T // complicated schedules.
: R" ?. F% c U$ c, \- W. I
% c: C- \9 V0 W modelSchedule = new ScheduleImpl (getZone (), 1);9 Q% s! ?3 e; C, J; R' T/ E0 Q
modelSchedule.at$createAction (0, modelActions);
4 B) ~8 C! Z3 ~ " K ~; t, p% L7 H% H
return this;
2 j9 { q6 h6 q) ^ } |