HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 i, ~ \# J5 Z$ m
; `/ F3 y/ @6 G% S) c7 W2 U2 l
public Object buildActions () {0 ]9 ^/ w% F% D) k& n, P9 {
super.buildActions(); C0 x0 v" a0 E5 v' L8 f3 Z
+ q i8 A' t2 i& p, | // Create the list of simulation actions. We put these in
( ~' B2 k7 n5 g. z _- P7 ~2 Y // an action group, because we want these actions to be
1 m M( E* } h7 d$ w* |* k, N // executed in a specific order, but these steps should
9 ^& w% _$ y1 v$ a( E8 w' Q // take no (simulated) time. The M(foo) means "The message
2 p( f% Q1 Q& V# @ // called <foo>". You can send a message To a particular
H" I6 v2 {% r7 w1 t // object, or ForEach object in a collection.
9 M9 b K) ]- [% o* v% t( Q % T* W; S: ]. T( A. v* @
// Note we update the heatspace in two phases: first run
- J( f5 Y) O5 [. n, V7 Q // diffusion, then run "updateWorld" to actually enact the
0 V# J& {. _5 \+ C8 W9 T- h // changes the heatbugs have made. The ordering here is
% p: |- ~% O0 x // significant!
! |+ [; w, j6 E
* T* q+ s* v. L& d; B% D( A' ]6 C R // Note also, that with the additional
3 L: D- R5 c! R l( B& q; K // `randomizeHeatbugUpdateOrder' Boolean flag we can) p4 t6 s4 \; k
// randomize the order in which the bugs actually run8 H# H6 c) l" s6 w5 R" I* e
// their step rule. This has the effect of removing any
* A: X+ ^+ y. x6 C // systematic bias in the iteration throught the heatbug( `. |7 w; g; h* u' p* _( i9 i
// list from timestep to timestep
2 j5 K4 A, ?. C2 r' [% ? 6 U1 k3 b3 F' |; l" m, r
// By default, all `createActionForEach' modelActions have
( l( E* A. b, W4 R& k0 E // a default order of `Sequential', which means that the( W& y$ H) j- K' l- b+ H' ]" l" U
// order of iteration through the `heatbugList' will be
/ n6 w1 X5 I& A- y% v // identical (assuming the list order is not changed
( L0 J) O( ~: P! \ // indirectly by some other process).( t" I0 X/ y4 b6 |/ V0 \# E
' a+ H( T7 G+ k( N- f
modelActions = new ActionGroupImpl (getZone ());+ |+ d4 b* Q) m
# {7 j9 w) f4 A$ w! N X' Q try {
& f/ {/ c" o w. v# ^ modelActions.createActionTo$message
& O4 ^, h& e/ H) a (heat, new Selector (heat.getClass (), "stepRule", false));
, F! N9 G8 {+ j* Z, F `( Q } catch (Exception e) {
* E: s" c6 B; F4 ?0 O, F0 ? System.err.println ("Exception stepRule: " + e.getMessage ());
5 Z# X, C7 C7 X! m- u# p }2 j7 Z8 q# i2 o2 ^# ^6 R- Q: I
, t2 \9 X; x# Q6 Y) p
try {2 E, Y( p' R* H8 P0 J
Heatbug proto = (Heatbug) heatbugList.get (0);$ M! @) f) Q# Y7 S
Selector sel = - _# B4 x+ t& ?( c7 x) R9 K
new Selector (proto.getClass (), "heatbugStep", false);
; c2 ]' \3 V8 z8 j- h actionForEach =
8 t8 x1 x" \5 K K( s7 v, _: V modelActions.createFActionForEachHomogeneous$call
& P4 j) _& n- ]: u2 U+ Q( g (heatbugList,* I$ G Q' }; c! ]; c+ Z
new FCallImpl (this, proto, sel,
% t* W5 W M/ v, D/ b1 T new FArgumentsImpl (this, sel)));9 y% d6 ?9 |* U
} catch (Exception e) {) F( L' e: e* y7 s
e.printStackTrace (System.err);4 {! B7 | g7 ^: C4 h0 ^
}
' r. I* b- T8 e' A# |) u$ J' B
* I9 C7 L8 x/ O* \ H$ b; f4 J syncUpdateOrder ();
7 ?: T# ~8 v8 \2 a4 c n- V2 h( ` h4 Z6 B# B
try {9 K0 I* `! _/ s% H# e; h1 O
modelActions.createActionTo$message
* h0 C( D5 g3 K8 S+ s (heat, new Selector (heat.getClass (), "updateLattice", false));: E4 r7 p! N1 K( a
} catch (Exception e) {
& e4 E+ Z H" S4 ?+ J! ~2 c! v System.err.println("Exception updateLattice: " + e.getMessage ());( ^8 ]* S8 P- A: x7 K% y
}7 `$ u C5 c" j3 y( C% P
( m: A# b0 {. U) k( w; f# @1 M
// Then we create a schedule that executes the4 P3 [' Y& Y, o+ N
// modelActions. modelActions is an ActionGroup, by itself it
* g9 F6 _' O9 A( S9 s2 p3 ?, K; M$ d // has no notion of time. In order to have it executed in; M, }2 Z, h: c5 M4 _
// time, we create a Schedule that says to use the
) C; B$ J% J1 u5 X7 X // modelActions ActionGroup at particular times. This4 U0 s, J5 d7 p- B) v v/ e, S5 z* J
// schedule has a repeat interval of 1, it will loop every
, ^4 c: U7 h9 T5 Q2 }1 [& E // time step. The action is executed at time 0 relative to
8 _. O* n0 _, y! e% U# z; S2 \% C // the beginning of the loop.6 G# P1 c2 t0 s
) F6 O( O5 m( n" l. [8 ?
// This is a simple schedule, with only one action that is
4 U% H2 f$ w$ U // just repeated every time. See jmousetrap for more! y, m1 l2 b1 Q# T4 E' t
// complicated schedules., f3 X5 H) E _6 A' }2 A
4 z9 N# M' s" H9 z1 P" f modelSchedule = new ScheduleImpl (getZone (), 1);4 E' c {- o) {4 [' H# k( h( A
modelSchedule.at$createAction (0, modelActions);
& Z2 T* t4 S7 M7 H' W8 }1 N ' j! ]4 F, N1 P* @/ m5 w }# w7 d
return this;
2 H P5 S+ c* ]0 b9 `# C0 v } |