HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& q5 u5 Z( G0 X& t. a1 T2 F& v% @8 d/ l( _+ `& f
public Object buildActions () {/ ~4 a3 B/ D) `
super.buildActions();
) \; P) L/ q: }" n 6 u) E& k1 x0 f" i; r( t) |
// Create the list of simulation actions. We put these in# m7 D9 a6 Q( l
// an action group, because we want these actions to be! l' `0 D; `4 q! A9 @% O9 Q7 I! y# `
// executed in a specific order, but these steps should$ w/ e# N4 s% K' Q+ J7 _0 @
// take no (simulated) time. The M(foo) means "The message( e9 \& n: Y9 |" w1 ~9 ]
// called <foo>". You can send a message To a particular, V1 n. s# e- @# c* w" w3 W
// object, or ForEach object in a collection.
m6 ~. s* g5 s! G. C. H
0 }0 [: f2 s! b3 o // Note we update the heatspace in two phases: first run" e2 Q3 o' X3 [3 w R% ^
// diffusion, then run "updateWorld" to actually enact the
3 |- m& k6 `+ N( N5 B // changes the heatbugs have made. The ordering here is
* F( W' i* J, N9 p7 `: g // significant!
, }. U- v% q7 _
) c) ~' B2 c( y. C$ H' l // Note also, that with the additional! r/ B, z: b. Y+ C F
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 I& `6 U! m2 ?5 Z) o) B' t$ Z' I
// randomize the order in which the bugs actually run
+ m1 a) V8 c* U8 [ // their step rule. This has the effect of removing any/ @6 d! ~' h$ c
// systematic bias in the iteration throught the heatbug
f9 p1 V0 i% ? // list from timestep to timestep
3 c, T4 n+ y+ h' r+ [( z: e6 {" ]
) k$ S/ @+ A# z5 `- S. | // By default, all `createActionForEach' modelActions have
7 ?8 _6 N K, Y7 Y* V2 g // a default order of `Sequential', which means that the0 F# c4 S7 f( m1 e
// order of iteration through the `heatbugList' will be
- E4 \( J, r, _: _% c' L$ `5 x // identical (assuming the list order is not changed2 _1 D5 v; K& o: r
// indirectly by some other process).5 O$ u- z" r% k9 O7 D& \
5 a, Q/ Z& b2 j& q$ J, X" D modelActions = new ActionGroupImpl (getZone ());
" k1 q; ~* B0 j1 h/ S3 A; v; \& L8 u% |* c1 s$ Z) D* k$ f
try {
' _) J2 x3 Q- A) I$ l$ _ modelActions.createActionTo$message/ |1 n* ]! b' m
(heat, new Selector (heat.getClass (), "stepRule", false));
5 J* f2 W o, [3 X# p: m } catch (Exception e) {
; Z; F/ e3 v% ]. P, T System.err.println ("Exception stepRule: " + e.getMessage ());
6 t$ q/ ]6 t* D0 y; |5 N }
+ E# ]. p- r4 M# ^' B% \
) ^1 a9 L% n0 J+ b try {
! Q( j5 g+ Q4 | Heatbug proto = (Heatbug) heatbugList.get (0);
7 r( i$ Z( t: Z8 F2 U$ @0 P- V6 M Selector sel = 4 }9 Q6 a& q. j% @ E, ~2 P
new Selector (proto.getClass (), "heatbugStep", false);
- t$ ^8 L9 {5 f6 j actionForEach =
8 s* z: w# y: g5 ^ O @5 N ? modelActions.createFActionForEachHomogeneous$call4 A/ v, b( L" b! J# p f+ b2 n
(heatbugList,
0 m2 e) T3 p8 S; t, g new FCallImpl (this, proto, sel,
+ I% _8 n c$ \ new FArgumentsImpl (this, sel)));& F2 H, f0 s% E! s/ W+ H
} catch (Exception e) {- H8 [* _' D& g/ @2 ?2 v
e.printStackTrace (System.err);! O! J: \- M4 N3 d2 n
}
. E" {8 t, s9 y/ Z' k1 Y
( G8 i! z. ]* z3 v syncUpdateOrder ();
: a4 c# |7 n) K% a( P% H% u$ R K9 {4 Z
try {
6 H* [( R \4 F5 a) a+ I3 K9 a3 ]. J modelActions.createActionTo$message $ M) r" u4 n* g* `1 r$ n3 h
(heat, new Selector (heat.getClass (), "updateLattice", false));
* M" H2 k2 I- C! h } catch (Exception e) {
( i4 C* p7 {5 K! w/ B2 T% G System.err.println("Exception updateLattice: " + e.getMessage ());
) J: X* k4 e# L4 [8 g }
) R7 C. ?, N4 ?4 q
- v- t. ]9 _ K7 h+ W5 B' |+ ?; s/ E // Then we create a schedule that executes the( H2 h9 }: v. x: ~$ a% W2 u. ?
// modelActions. modelActions is an ActionGroup, by itself it+ @, i5 t* r+ O# e1 |
// has no notion of time. In order to have it executed in
- d( r, t ?6 n" t! {4 D // time, we create a Schedule that says to use the7 `: t! c% X8 z6 b/ }
// modelActions ActionGroup at particular times. This* h. ~$ q9 G8 l% v: F5 l
// schedule has a repeat interval of 1, it will loop every& z1 R9 l% P# Q( m6 S( z, x8 F( B! S
// time step. The action is executed at time 0 relative to6 T- R$ s3 O0 l
// the beginning of the loop.
8 g8 Q6 C2 y) ^( H. O3 J
; N$ g5 E O3 Z3 m3 ^/ A // This is a simple schedule, with only one action that is
/ g4 v0 H; \2 C0 a3 y // just repeated every time. See jmousetrap for more
' m) n% v, Y8 O* r+ r3 K& V3 t // complicated schedules.
$ c7 S( }" ]3 M& X1 V% J/ F9 z % n0 g4 _- u: Y; H
modelSchedule = new ScheduleImpl (getZone (), 1);
7 U" |/ v- r5 L% t+ ~ modelSchedule.at$createAction (0, modelActions);
; g+ Z. y+ W H& |" p
; f9 s. a1 B; r5 v8 O% k return this;# O, f) y) d ?0 j/ m& Y3 V
} |