HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
j1 G- m- @: N. E# |8 q' ]. g4 m: A) T
public Object buildActions () {. p" W9 w* x- [' `
super.buildActions();) G, f4 p. F* J6 C
2 T! x: s2 I- N! u. ] // Create the list of simulation actions. We put these in" P/ m) g- G& u- P7 ^, ~
// an action group, because we want these actions to be
! x/ a! t1 D+ F7 M& h // executed in a specific order, but these steps should
. t: a' u& ]8 w. }5 R: @# ]7 N) L8 Q // take no (simulated) time. The M(foo) means "The message
. m7 O* v: ?0 O // called <foo>". You can send a message To a particular
3 I' Y0 G. U- ~7 t. _; W // object, or ForEach object in a collection.
; ~) f4 P- ?- z' A. U4 C
& d* g: U' M9 r: u // Note we update the heatspace in two phases: first run
2 Q/ @. z+ {5 i; M, z5 n8 R" s // diffusion, then run "updateWorld" to actually enact the
7 j* r3 s) G# C$ W) I; j // changes the heatbugs have made. The ordering here is
, s4 i' `. {" E& Q% }! r! S // significant!* z- t3 `( r! W& i3 j+ K
5 \' {( h' ]( p, S // Note also, that with the additional
/ l. A& y/ R0 V9 j& T5 m5 J4 n1 ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can
& j: u' o+ y) M: e2 Y // randomize the order in which the bugs actually run& _& y/ \( l4 f4 W/ |
// their step rule. This has the effect of removing any+ @- k1 U+ C! `
// systematic bias in the iteration throught the heatbug
) w; C6 ?2 Z+ W0 f( E- s. m // list from timestep to timestep5 z' \, v0 O( Z; M1 v/ H# H r ]
2 K1 ?; G; H& y! F
// By default, all `createActionForEach' modelActions have
1 F) m: g) B2 l1 _ // a default order of `Sequential', which means that the0 m7 a; N1 `/ }# {2 q8 k$ ~, n7 q
// order of iteration through the `heatbugList' will be
; \# l/ }8 v' z% w! G // identical (assuming the list order is not changed
6 C. c7 z2 G0 O! |# P" B // indirectly by some other process).& }! P: g4 W& r( b% y6 i
! |8 R8 l V9 l4 k. D6 ^- s modelActions = new ActionGroupImpl (getZone ());
+ W' U+ t2 {1 X0 I: E" C1 m
- |, M0 N6 L$ S/ f try {% J( Q! k; f" p2 N: ~% k
modelActions.createActionTo$message& |- r' j8 |6 }9 U. W
(heat, new Selector (heat.getClass (), "stepRule", false));
) P; I" r1 E7 O. `2 a* ] } catch (Exception e) {+ ]6 l+ \( z/ c! N/ g' Q
System.err.println ("Exception stepRule: " + e.getMessage ());) g2 b: H- H/ j1 X2 Q% f
}
# {" ?2 H3 s- o% q5 O7 W/ f( |( d, L) N' D0 X- ~& y( o
try {
2 o( m7 \/ y- D1 @ Heatbug proto = (Heatbug) heatbugList.get (0);! A1 p: {8 T- v0 W, C
Selector sel =
6 l' y! M7 O6 A8 M new Selector (proto.getClass (), "heatbugStep", false);
& R& k* x1 _& l% p1 Z actionForEach =
1 R( o0 E0 P9 t1 g$ \. q modelActions.createFActionForEachHomogeneous$call E& ~6 y7 u- u6 T( n
(heatbugList,8 \ v( [; X) G
new FCallImpl (this, proto, sel,
/ E- B7 l& s3 [( A8 h+ f; K$ Y! f$ C new FArgumentsImpl (this, sel)));
x; G: m2 Z; I# k4 M# f6 e } catch (Exception e) {
2 A0 {2 N/ [% p' p1 e+ t e.printStackTrace (System.err);1 v) G; ]3 U3 l+ Y
}9 K, M: L7 t) _/ Y
1 v3 M7 e$ y4 o8 G% G
syncUpdateOrder ();
0 B9 `9 x! i$ ^5 h( q
2 d6 P* j2 X3 s) i& q- k3 t6 c try {' x5 _* Y& A- Q* t& e3 }' ]
modelActions.createActionTo$message
* I% f% O, a3 j (heat, new Selector (heat.getClass (), "updateLattice", false));
$ C' U+ d* b) Z9 _+ e6 g+ v! X } catch (Exception e) {
+ @' |- U0 x+ X" d9 L* e. c System.err.println("Exception updateLattice: " + e.getMessage ());
* Y5 n/ M5 I# ~* t9 J }. g2 x4 l- T* r `8 l8 W
4 |/ |2 ?6 M1 B
// Then we create a schedule that executes the
+ R! ~. d) L8 C# G5 S // modelActions. modelActions is an ActionGroup, by itself it! @9 r- W$ l/ T$ w
// has no notion of time. In order to have it executed in
, A, m: k1 \& o3 O) r3 [/ F( Z( m! z+ C$ _ // time, we create a Schedule that says to use the
3 ~4 H# v' I9 C% c# `. ~5 G // modelActions ActionGroup at particular times. This
# {% @8 N& f; k# _& Z* Q // schedule has a repeat interval of 1, it will loop every
; G0 V% Q8 X. K2 l+ N4 X! y2 ^ // time step. The action is executed at time 0 relative to
$ V! c2 c6 e6 |$ ^' J) t // the beginning of the loop.
, u& U1 c% e% |! i5 {1 t6 d; @; N9 e( h% z- {
// This is a simple schedule, with only one action that is
$ W4 C& O8 o% G1 } // just repeated every time. See jmousetrap for more
5 d+ V& j! F: \* m1 Q // complicated schedules.3 b$ f8 _2 g7 G! S
. c. y _% o* S% r modelSchedule = new ScheduleImpl (getZone (), 1);/ i# a% n( i+ A6 B2 p
modelSchedule.at$createAction (0, modelActions);
" g) z8 G3 z# g* A) _6 I # G: X( W) N+ Y
return this;
2 {) q1 D7 U& e8 t: k J) m } |