HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, W1 B _/ c& ?1 l
. W; s& u3 V3 M
public Object buildActions () {
( d! R* O* I1 \& m; g# S0 j2 ]% p super.buildActions();
5 U# x% ~* d; n6 n# h. S! s
% H3 N b( j( M- ?5 N6 y) a. s# } // Create the list of simulation actions. We put these in
7 I, @* \/ H, S. s( O // an action group, because we want these actions to be1 o- z# S* c9 X3 T8 l( I
// executed in a specific order, but these steps should' x. Z# I* a5 E8 a8 Q0 x$ l
// take no (simulated) time. The M(foo) means "The message
+ b" M6 R% e5 ^ // called <foo>". You can send a message To a particular% O: z* _0 _+ K7 U0 D7 ~7 [% z
// object, or ForEach object in a collection.# m; l+ o: ~& S9 V$ k
. u! P2 _# B5 d% V
// Note we update the heatspace in two phases: first run
# T; I. |4 k) W3 u // diffusion, then run "updateWorld" to actually enact the
! H. g& {3 g$ K4 M // changes the heatbugs have made. The ordering here is
" Z; S* k# o/ E% h. q' @+ V- @* n3 | // significant!9 [) k3 I7 p! A) d- O4 w
- j5 i8 ]0 j6 `- W- P // Note also, that with the additional
2 n# P% q7 p$ C; y% r // `randomizeHeatbugUpdateOrder' Boolean flag we can
) G, ^8 I( D! X% H // randomize the order in which the bugs actually run
9 }9 e- i4 o- ~3 R Z$ Q // their step rule. This has the effect of removing any
; x' a' {) }% X Q! ~5 q; b // systematic bias in the iteration throught the heatbug
; q% r# N% B7 j- i, d. m: s e6 u // list from timestep to timestep+ s- N! r3 \6 m2 e6 ~" ?7 W
. R$ j+ }2 y( g0 m // By default, all `createActionForEach' modelActions have0 c1 F% Z @: @3 W
// a default order of `Sequential', which means that the9 l2 a3 ]" U2 _
// order of iteration through the `heatbugList' will be
8 i6 A6 U, E! t% A+ `) D0 s) y3 ? // identical (assuming the list order is not changed
0 U, e9 V4 b4 A. f8 V // indirectly by some other process).
! U, v5 R( R8 g, x * P% e- r2 J6 i1 a; w
modelActions = new ActionGroupImpl (getZone ());: m! ?' X5 Q ~" Z9 G# h( N" x
, X- @* M& ?0 @, I1 d1 e& Q7 u" E
try {
: p6 S0 @6 a6 W! y i modelActions.createActionTo$message
q! @! T) @% Q# w8 Q (heat, new Selector (heat.getClass (), "stepRule", false));
, H0 H" D. \$ N9 {# Q$ @/ J B } catch (Exception e) {
5 o8 p) L) `$ \8 R2 t; P) ]* l) a System.err.println ("Exception stepRule: " + e.getMessage ());
% m" F* D' i2 G% N; B% X. ?1 I }
/ a8 b+ H. N1 p1 _* K
" X& J; G2 C& c3 M0 X5 { try {3 l3 B! z9 {, ]' |
Heatbug proto = (Heatbug) heatbugList.get (0);
5 k3 B- V. O) _& k& v9 a0 p Selector sel =
8 A5 i/ d6 l" k! _' i( b new Selector (proto.getClass (), "heatbugStep", false);" T& k B/ ^% K0 T$ i
actionForEach =( X' f, V/ c/ S
modelActions.createFActionForEachHomogeneous$call S9 H; [" @/ `/ ?3 r5 G
(heatbugList,2 } W4 w6 b0 v' }; \
new FCallImpl (this, proto, sel,/ _' D/ ^* d, s8 w# E
new FArgumentsImpl (this, sel)));
8 n# x# h/ [4 ?6 U } catch (Exception e) {( B0 [* A4 s& ~4 f% j* a( [$ G
e.printStackTrace (System.err);
; p5 o3 f. ^2 C" J2 [ }( C& b+ C. X& f; N5 i8 k
* F1 w6 M2 T4 o' O0 n; s" y
syncUpdateOrder (); Y6 l, i9 s9 s' w7 N1 R' z8 a! k
( r4 b5 r: h7 y4 v1 {! ] N0 E( p
try {: h, R$ _! a s5 W
modelActions.createActionTo$message
% K' I3 F" W, n( s# g3 a; d (heat, new Selector (heat.getClass (), "updateLattice", false));% Y( w. W: p2 d0 s4 _; J' H3 g
} catch (Exception e) {- j. G' u0 ]. @4 t" S
System.err.println("Exception updateLattice: " + e.getMessage ());
; K# @ b2 u. X* A& \ }
* S$ |: M, p+ V! `* P! ]( Z N( K4 v/ Q, l3 _9 o5 q/ q
// Then we create a schedule that executes the
: K3 b: a! C0 b `7 j // modelActions. modelActions is an ActionGroup, by itself it
4 ~, Z+ p- d2 q: ]6 `4 t8 S // has no notion of time. In order to have it executed in A/ N9 C/ }3 C4 a
// time, we create a Schedule that says to use the! c8 g! h4 W) M* Y0 R# N: @
// modelActions ActionGroup at particular times. This
3 \- n$ ~6 w# y8 f8 S // schedule has a repeat interval of 1, it will loop every
" U: c% i" K: `- O // time step. The action is executed at time 0 relative to
5 O! T1 _0 @/ W' h' a // the beginning of the loop.! H' E; M( }7 M3 |! n! e* C
' I8 l& S8 J+ [) v
// This is a simple schedule, with only one action that is
( \% U& w, B, a1 ^ // just repeated every time. See jmousetrap for more6 r& N) C: L& o: o6 g9 k% e
// complicated schedules.
0 \. r' W. ^1 J: x' I( k1 z % k( P" j+ p- u7 T8 k1 q
modelSchedule = new ScheduleImpl (getZone (), 1);
& c# b9 D/ |1 E! s modelSchedule.at$createAction (0, modelActions);
' m# y' @! n( t" N( j # ?9 ~) v* J1 s( \" }
return this;7 x1 r S/ J6 R
} |