HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- x) n$ V$ Q! P) Y$ h! i$ y l2 A9 b
4 g/ p- v& h/ ^# M! M& E% z. Y public Object buildActions () {
$ z( }( [) k# n+ F1 ~ super.buildActions();* v" H5 U, k0 V2 S" u. W B5 f& Q
: }# h" I2 P- n% G5 l; b) {
// Create the list of simulation actions. We put these in
! M1 H, V5 E) S8 H // an action group, because we want these actions to be' ?& R2 F, k G6 ^3 a
// executed in a specific order, but these steps should" T$ ]# p0 Z$ [* y
// take no (simulated) time. The M(foo) means "The message* @8 ] ]4 N t
// called <foo>". You can send a message To a particular s% p; \; ], ]; n
// object, or ForEach object in a collection.
5 s1 \ X4 E/ b: Y ' i# p/ z( } g% ?% m% B% U
// Note we update the heatspace in two phases: first run* M5 ]- b) G2 B- R6 y5 k" c0 @
// diffusion, then run "updateWorld" to actually enact the C8 r( W5 o+ w- \$ D; `
// changes the heatbugs have made. The ordering here is
( D8 j+ c5 r$ m. L1 O* f // significant!
. ?; B) z& p8 c
, J2 H* n' W# y* d+ S: r; [ // Note also, that with the additional6 h) z7 `9 _1 \1 u" I5 e
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 g" r9 r( l8 z# W1 x n
// randomize the order in which the bugs actually run0 X2 ?% M& ]- P! F8 O
// their step rule. This has the effect of removing any
1 @: @/ C9 Q7 X% Y // systematic bias in the iteration throught the heatbug
& w1 E5 D1 m9 } y$ K& [( @ // list from timestep to timestep
2 M( E3 H/ }8 Q 9 ?' u( m/ }+ M& b2 X
// By default, all `createActionForEach' modelActions have5 m. x3 x. W/ D% O ~& ^2 b: @
// a default order of `Sequential', which means that the
: y3 ^ f/ n* p9 v/ a5 y: c // order of iteration through the `heatbugList' will be
8 P5 P7 O k8 h' Y% _& Q% } // identical (assuming the list order is not changed
$ F) ~3 n, k4 K% E6 P4 N4 R // indirectly by some other process).% \; a+ E$ E9 A( \) y! i1 l
, S4 c" @: P n
modelActions = new ActionGroupImpl (getZone ());
" t# H+ V: i( @; n1 a! M4 P( c2 s$ n" N! x( U3 X% c' |7 [
try {
8 A. S9 m6 O Z. k: Q& _/ V" Z8 H modelActions.createActionTo$message" _* r( K2 Z% d# M; {
(heat, new Selector (heat.getClass (), "stepRule", false));
) \* \4 O5 v1 S4 k' I- Q3 M } catch (Exception e) {
p2 x0 H: K2 A. i System.err.println ("Exception stepRule: " + e.getMessage ());
6 Q/ p7 k9 X% R! i }
& E+ o b f$ l7 X" Q" K! n. N, v
try {% b" c7 p( `* z2 {' [
Heatbug proto = (Heatbug) heatbugList.get (0);
4 S$ ^' v( j e* C, e, F$ z( Q Selector sel =
6 j8 z6 J( Z; v* B C new Selector (proto.getClass (), "heatbugStep", false);
2 k* K. b* H. @0 B6 i& ] actionForEach =
9 @& B* z6 r+ D6 S# A modelActions.createFActionForEachHomogeneous$call' j2 y8 @0 K0 O! ]! T* {
(heatbugList," M7 x" y9 T9 Q
new FCallImpl (this, proto, sel,
. c) B3 L& K$ d) q3 m5 J: K+ Q3 n2 J new FArgumentsImpl (this, sel)));
! X+ L* P* o" {1 c5 ?& g$ m } catch (Exception e) {
$ `. t- S$ V# e) K( E. U/ j2 N) h e.printStackTrace (System.err);. P. g6 j! r! }' z: t. u& c F# w
}
0 a1 ]- z% W4 b( R# r s7 m. E) n7 p7 y! j
syncUpdateOrder ();! \6 \/ ]) }) e2 Z
1 y( u% ?) ^. L2 Z
try {1 W" j% C1 \8 ?# x8 [
modelActions.createActionTo$message $ R# n& {$ m- _( s0 ]% z
(heat, new Selector (heat.getClass (), "updateLattice", false));
1 e' R$ f) D3 D# [( Z } catch (Exception e) {7 _% K! D) W! v1 |; T9 K& n
System.err.println("Exception updateLattice: " + e.getMessage ());
8 l" P' A3 n5 `6 c! [% O5 ~ }; o9 P! z5 B3 d6 A6 O( c1 d$ u
9 z1 O9 ]5 p% [& q
// Then we create a schedule that executes the* w' l' y0 d) e/ I- V# `
// modelActions. modelActions is an ActionGroup, by itself it4 H1 s- l b+ B7 K1 W
// has no notion of time. In order to have it executed in; `) p j4 f) a$ u5 o9 I
// time, we create a Schedule that says to use the
( U4 X {0 Y" p6 E* _! P // modelActions ActionGroup at particular times. This
: ^ l4 C6 K2 ~! z9 p( K // schedule has a repeat interval of 1, it will loop every: u, W0 N; z+ [ g
// time step. The action is executed at time 0 relative to
% x7 s$ l. u. U5 `% A2 { // the beginning of the loop." t2 V% `; L. P5 w0 R2 a4 z4 H
# @* T* `: V+ A; v+ c( {$ M // This is a simple schedule, with only one action that is
0 K" Z4 R( V- d1 P6 L) i- D // just repeated every time. See jmousetrap for more
/ R+ z) A/ `9 K' h$ A! v' b( O5 ] // complicated schedules.' I" h- }& e4 e- ?: ?
+ f# Z L! f1 G x8 X' N7 e( n* T# M
modelSchedule = new ScheduleImpl (getZone (), 1);
8 s* u6 X4 S0 f' M3 h& G modelSchedule.at$createAction (0, modelActions);
4 L1 i* n0 V: ^. C+ {( O
/ O( B, |6 A, b' N return this;
6 F' D5 o3 y g! ]5 h3 u' K. q } |