HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# ~3 w! h8 l" P) v; c: Z% J$ _6 p
& ~7 {2 b& f- Q% \0 w2 z public Object buildActions () {+ ^# s1 [* Z5 k, m
super.buildActions();8 t/ a0 o2 D) X6 x
# z, w# V$ n7 ^# W- t' g
// Create the list of simulation actions. We put these in
" }6 V4 S( [8 T6 X" T/ g // an action group, because we want these actions to be
, u- ?1 G$ T0 T8 T4 @$ u+ f0 F% e // executed in a specific order, but these steps should
8 o5 c$ E6 k4 V3 u9 W% G6 Q // take no (simulated) time. The M(foo) means "The message3 Y t% e9 a1 P* s3 U9 K5 m, O& s
// called <foo>". You can send a message To a particular9 r& A1 a% b8 ]
// object, or ForEach object in a collection.
" Y6 |4 i0 N& d
% u( R9 I7 C$ h% k }# p1 h // Note we update the heatspace in two phases: first run
6 M( q2 B. }" h* h8 h // diffusion, then run "updateWorld" to actually enact the% n% k: T. F4 o& f+ S% @ \
// changes the heatbugs have made. The ordering here is/ Q) J! O1 Q6 M3 B7 g. Z! ?; @/ v
// significant!
, y* V* p0 m; T' o7 H / h1 D1 _) }# f0 ]4 b
// Note also, that with the additional) I+ K5 z4 ~2 L
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. h2 f4 K: {' h z // randomize the order in which the bugs actually run; ], I7 Z3 ]4 E% m
// their step rule. This has the effect of removing any7 k5 ~6 u. c" O( L: y
// systematic bias in the iteration throught the heatbug4 Q- m/ }$ H4 \
// list from timestep to timestep4 k; ?/ b& ~9 b
! m) O! X! e: a- x& k9 _ // By default, all `createActionForEach' modelActions have5 U1 }: j3 {9 e( r. }
// a default order of `Sequential', which means that the
' o. s3 K; T. s // order of iteration through the `heatbugList' will be0 H# u. m8 q, f
// identical (assuming the list order is not changed# W1 W% i) [* E, b0 J0 E8 @* \% ^
// indirectly by some other process).
7 F$ J M7 B" F* P a, o1 \
: F9 ?1 x: \0 _/ u2 [. j7 ?. F modelActions = new ActionGroupImpl (getZone ());
6 R0 K' M+ V3 l+ s
- D* A9 W( X# y3 C) R, P try {
$ ~7 a1 F2 ?4 K modelActions.createActionTo$message
' }! g0 [ N* W& p (heat, new Selector (heat.getClass (), "stepRule", false));
' W- o/ g- n! g* [& d } catch (Exception e) {4 Q. f; H) G( H, X
System.err.println ("Exception stepRule: " + e.getMessage ());
! Q m! w A$ z- \; x8 d }& a# B+ C, O4 ~ i# |8 J
! @5 q1 p2 `8 ^; {; L$ t
try {* a" f" u9 q( }1 x7 F. `
Heatbug proto = (Heatbug) heatbugList.get (0);
; A) N$ a* o; u' C1 K% o Selector sel =
i8 C$ t: p/ A5 W new Selector (proto.getClass (), "heatbugStep", false);- [" e6 d& s! h& K% h/ [9 i; U% i
actionForEach =; o: O$ N( a8 ~9 ?! B
modelActions.createFActionForEachHomogeneous$call# ], \- }, o# A& b y+ c% q( g, X9 R
(heatbugList,( s% N4 V3 l# _) q/ Q
new FCallImpl (this, proto, sel,4 b q+ s% Y7 v( T( W3 d
new FArgumentsImpl (this, sel)));
; J# r% ^& {% E! J1 _( ? } catch (Exception e) {
. C( S3 q( F9 G e.printStackTrace (System.err);
' Q0 B) f# i& X- o7 w/ q } @2 f; o4 ]* M/ r
% z2 ]; o# `7 y6 F syncUpdateOrder ();
: _8 r3 @4 @+ r: g) n# e
1 m' p0 ~- [! J$ ~- e$ y% e# C, `* P try {
6 C# k! w. e* Y9 d) z modelActions.createActionTo$message 8 `7 A% J- m" M0 T
(heat, new Selector (heat.getClass (), "updateLattice", false));
* X. O- z8 ]* ~- e3 F" Q } catch (Exception e) {: `2 @5 T2 N" Y" \, I+ q: Q [- A) t; ~5 j
System.err.println("Exception updateLattice: " + e.getMessage ());
) g1 t% V& G5 u* B }
+ x S F/ e0 F% N3 d 8 m m8 i! d( y. e, p# a0 K
// Then we create a schedule that executes the' A) D2 }. c n% m+ C- [- z
// modelActions. modelActions is an ActionGroup, by itself it
9 Q( J4 g5 F2 h' y3 u2 q // has no notion of time. In order to have it executed in) U* @" z: S* L4 W ~
// time, we create a Schedule that says to use the
. E0 P$ N5 S3 P6 g // modelActions ActionGroup at particular times. This
$ d& Q, g! m1 V // schedule has a repeat interval of 1, it will loop every$ k; t# k: T- s! c6 g6 O' R2 Z
// time step. The action is executed at time 0 relative to& n( A3 V& Z- D, n" r. N0 E
// the beginning of the loop.
/ B( b) g/ ?# f- t( `- }7 O' X2 R! v, C0 Y( \/ z6 w
// This is a simple schedule, with only one action that is
) Y0 b2 T% y; F1 l& n# B) e: H // just repeated every time. See jmousetrap for more# q8 j- }6 B9 j7 N0 U4 }; `
// complicated schedules.
" \5 H2 Q$ [. a, [
& ~2 n' o( m+ D% N, L) h modelSchedule = new ScheduleImpl (getZone (), 1);
! t: m5 `1 R* P$ A# e0 a# O modelSchedule.at$createAction (0, modelActions);
7 T- m! `5 Q5 h1 S
/ Y* }# y/ d6 ^ return this;
% Q% W1 ?- X3 O: o) Q } |