HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 q% G4 T4 }& {
8 h& l$ e1 ?. o public Object buildActions () {
+ Q# }0 t$ U4 n+ A" K super.buildActions();
8 s- e8 ]; R8 S6 ~ / B! L* A/ Y0 [+ i
// Create the list of simulation actions. We put these in8 w- b* b% s; _6 L h& t
// an action group, because we want these actions to be
' ~' M2 Q0 F* x e" P5 k // executed in a specific order, but these steps should
$ n1 ^5 h5 X$ c // take no (simulated) time. The M(foo) means "The message: g& v8 f2 L" c/ Y) Y! e
// called <foo>". You can send a message To a particular
( [! ~% R# I6 D. o! U // object, or ForEach object in a collection.
J: f! y; S6 ^( [) g
' L. I4 D/ m7 D: K% N4 c2 o // Note we update the heatspace in two phases: first run
# c* u1 X0 h! L! x // diffusion, then run "updateWorld" to actually enact the
7 h% p' T: ]5 E. _* \: n2 U // changes the heatbugs have made. The ordering here is( v$ J4 v" I$ T9 a! m8 H
// significant!
& g7 p) h- m3 u0 @* f6 e, g6 N 6 t" s5 H5 |: I# Z( F4 y2 @) X( D
// Note also, that with the additional% T9 J' p9 V! e8 m+ u0 M
// `randomizeHeatbugUpdateOrder' Boolean flag we can; ]" Y! W9 E* x9 H( J1 M
// randomize the order in which the bugs actually run
# y5 D+ Q6 q6 G r7 o // their step rule. This has the effect of removing any9 X* ^: z7 w# H4 s2 @% T
// systematic bias in the iteration throught the heatbug8 z/ d% |, u( m% |" n( T( k
// list from timestep to timestep
9 W( N- B+ [& i- [) s + f" G2 I) b- O) \ q3 |
// By default, all `createActionForEach' modelActions have
* V- i4 T- o4 v' G% \4 _2 C // a default order of `Sequential', which means that the+ w) W5 Y# z' X/ r3 [# T9 I
// order of iteration through the `heatbugList' will be6 _/ g1 ] Y9 a. p9 V, u8 R0 F
// identical (assuming the list order is not changed
3 a4 _6 b4 e: \ // indirectly by some other process).
! F7 q' w$ z4 i1 p' U + ~/ P4 a+ }) [$ e8 {1 L% z+ c
modelActions = new ActionGroupImpl (getZone ());
4 M: N, d& o& k8 E- ]" _& Q* f! ]5 W4 P
try {
) _6 I0 V( G: y l( z9 e! C1 @6 y8 I/ X& s modelActions.createActionTo$message
+ X( o% d( T1 A$ l (heat, new Selector (heat.getClass (), "stepRule", false));8 K w( I( D: i6 i4 M) s x
} catch (Exception e) {
* M' W! _4 N! O U0 n System.err.println ("Exception stepRule: " + e.getMessage ());
: ?) K& `. k) \; k/ b! h }1 I+ `, ~% c8 _8 a) K5 B
; R& k0 D3 e) { ] try {
$ {' ]( c q0 z- N1 p3 F Heatbug proto = (Heatbug) heatbugList.get (0);: n5 K4 _ n9 h3 S* x9 w
Selector sel = . @2 U, h8 z! U" i) N
new Selector (proto.getClass (), "heatbugStep", false);
" J7 |: m' Q# V7 R$ e; W" K actionForEach =
e5 R' U+ I2 L4 x& x: |* m* l _ modelActions.createFActionForEachHomogeneous$call
# ~+ J1 X5 C& c o+ Z2 O! [# x9 u1 ~ (heatbugList,
5 n9 H: |: J8 M+ u' a4 \$ k new FCallImpl (this, proto, sel,
5 m2 ]! V8 |: x# j$ b+ j new FArgumentsImpl (this, sel)));5 V: x/ k, d) i5 t6 [
} catch (Exception e) {
3 p1 @& O' n& W3 [: T+ S& w e.printStackTrace (System.err);, b" Q' M% i0 T5 E& t% |5 ?# I) o0 g
}9 K% I9 D4 [9 p2 s
" o- i! _* l! t1 U! c, w! S" v( l syncUpdateOrder ();
. z" C* u0 ]7 `2 O! f5 n: g
- k: _# \: Z/ {" Y+ @0 N6 c- h* p try {+ T$ _; D. h0 M% [! X/ c$ C y
modelActions.createActionTo$message # z4 |! G2 _8 u* n- F/ {
(heat, new Selector (heat.getClass (), "updateLattice", false));
$ T9 Z5 R2 `/ l# Z0 d! ] } catch (Exception e) {+ i% e7 s, c Z* { L* h
System.err.println("Exception updateLattice: " + e.getMessage ());% r; S* x; }. P2 ? D1 y# d
}; ?- c1 S$ p+ l9 N. v0 ^
, x% C* _+ p# G // Then we create a schedule that executes the
" m8 A2 H2 d* z6 ^6 t // modelActions. modelActions is an ActionGroup, by itself it6 H( ~4 v( r: q: K
// has no notion of time. In order to have it executed in
: J- |% Q6 o- L5 [7 N // time, we create a Schedule that says to use the3 S# A" R: ^ d! h3 i$ I7 ]( C
// modelActions ActionGroup at particular times. This$ H4 q, z- f* l3 `! W
// schedule has a repeat interval of 1, it will loop every
8 C# Z# G4 [' O4 g // time step. The action is executed at time 0 relative to, x, a% H; y+ P* ^/ L3 f
// the beginning of the loop.+ S+ F7 g( |6 V: I
6 {) Q9 I* ]' j1 ` // This is a simple schedule, with only one action that is% {4 g7 `* a" f# d
// just repeated every time. See jmousetrap for more* V: g# }1 k* H% y7 n
// complicated schedules.
9 K1 C3 E5 `0 X; F9 n' q
% o& S. q# J+ B modelSchedule = new ScheduleImpl (getZone (), 1);
$ E0 [; g) b% O modelSchedule.at$createAction (0, modelActions);* m8 T% Y8 {2 _, J) c
) i. ~6 m$ f" J9 Q' u6 w return this;
) d+ ]" i% e l& d% r# r* t } |