HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- ?. i$ ?/ }6 j. k# q% d
+ ~, J# G% X1 {& J9 R5 @ public Object buildActions () {+ |8 H4 D9 o+ ?9 I" A8 ~
super.buildActions();
9 c) m9 p6 T$ Q7 b% I+ {% |
- Y: [& E/ P' P, i2 E$ D! |, { // Create the list of simulation actions. We put these in
3 a) h7 N: V$ R9 J# } // an action group, because we want these actions to be, ^" y( I% z; W8 w4 R
// executed in a specific order, but these steps should
8 z. A8 }: @8 w, | // take no (simulated) time. The M(foo) means "The message1 M# d0 I6 [8 J: r. |
// called <foo>". You can send a message To a particular
1 |/ z# I, ]9 R9 ?, Y // object, or ForEach object in a collection.8 }2 y( s: Q ~0 E
, Y$ K1 ~' Q8 b; t* W m5 D
// Note we update the heatspace in two phases: first run
2 y' n: ?) T' C Q# y // diffusion, then run "updateWorld" to actually enact the
0 ?) U+ q& g5 } \2 O // changes the heatbugs have made. The ordering here is
7 c- H7 s$ @4 w // significant!. d5 x* i% G% i5 h; A* G/ |/ j# U
; t6 D+ i( ]0 r
// Note also, that with the additional8 a( K( j, M0 _ ?8 h
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 _/ Q% n! i* [' d# R! P4 S. C# n
// randomize the order in which the bugs actually run
* K: h6 i: @: b j5 ` // their step rule. This has the effect of removing any" j# _2 f9 O- g4 ]; p
// systematic bias in the iteration throught the heatbug5 m4 b* B' B# y4 g8 q& n$ @
// list from timestep to timestep0 r/ `4 {7 l. _3 |- y8 {; a6 h) l
7 |+ w* J: q% {/ L; A# P+ U // By default, all `createActionForEach' modelActions have* w3 E/ i3 b% z2 ]
// a default order of `Sequential', which means that the. ]( e# B9 u& k, w! M& A0 _( M6 T
// order of iteration through the `heatbugList' will be% z+ m1 [' @( O, K5 h2 u# O8 J
// identical (assuming the list order is not changed
; v- U1 W' w, Y6 N O/ F% O // indirectly by some other process).+ }8 k# ^1 ?' q7 i r3 o
" j- D8 _& }; Z! A+ C* j- ]* g
modelActions = new ActionGroupImpl (getZone ());7 c; s. q* e1 u9 J
% Q7 h+ ~/ E, H0 Q2 s6 E! g) f* C try {4 K) |3 `; C7 b; p4 k& S+ R5 O
modelActions.createActionTo$message' A, Q1 ]* d0 |
(heat, new Selector (heat.getClass (), "stepRule", false));+ Z/ U8 A7 s# b) J3 t% I
} catch (Exception e) {
3 R0 p$ Y; f3 a: Q ^+ F0 X# G( I System.err.println ("Exception stepRule: " + e.getMessage ());
9 o+ S, _0 G& E" Z2 [ }& U! p/ m$ O* m- S: E! X! V: i3 t
) N2 a" Y. R0 Y6 ]: @
try {
; E9 i; Z { e$ V* ~ Heatbug proto = (Heatbug) heatbugList.get (0);8 [7 A: s4 I$ ?
Selector sel =
" N1 a6 I6 |0 D. Y6 x4 r' W new Selector (proto.getClass (), "heatbugStep", false);" U/ V5 r( B9 g9 R% Q8 ~* @
actionForEach =, h5 z O' }" {9 P
modelActions.createFActionForEachHomogeneous$call, V' p5 d; R( {
(heatbugList,% y* I/ p; o0 X+ K. C+ g7 |
new FCallImpl (this, proto, sel,3 i+ F/ D& f$ {" t' a1 f+ K$ J0 x
new FArgumentsImpl (this, sel)));
0 W' B6 ]& S0 |. n2 y, B! e7 E } catch (Exception e) {, J& r" q; n7 u, P" ?
e.printStackTrace (System.err);) M; S2 c6 a/ E$ i3 R2 g1 y8 d( Q
}& X0 w4 W( v% q1 m. u7 e
! V' [! B7 ?2 s$ V
syncUpdateOrder ();
" T! T6 W# a6 F; Y0 F4 r& c0 d2 v: V1 N
try {3 z* D6 E: X; ~
modelActions.createActionTo$message 4 Y2 C' m" X$ l# s
(heat, new Selector (heat.getClass (), "updateLattice", false));
* P8 ?4 [1 \: @! ? ]7 E } catch (Exception e) {
3 G0 @% R8 I6 B# L: W0 m System.err.println("Exception updateLattice: " + e.getMessage ());3 x: b. ^9 K; e/ f3 ~$ M, W
}( m a9 @/ K9 q
. I% l5 e* G4 H+ h1 w5 j // Then we create a schedule that executes the
% ?* j( b/ W' S6 `8 H4 o // modelActions. modelActions is an ActionGroup, by itself it- ?3 F& `9 Z3 v, W% \# f) r( `
// has no notion of time. In order to have it executed in! @. l Z8 g0 z. Y3 t
// time, we create a Schedule that says to use the
& j' M7 Y6 D6 ?; M9 \ D' y S O' X // modelActions ActionGroup at particular times. This# u, W6 I* f' R2 T# r; v
// schedule has a repeat interval of 1, it will loop every
* g+ _: R1 X) a0 } L) S3 } K // time step. The action is executed at time 0 relative to" N; q0 E" ` @0 n+ e' Y
// the beginning of the loop.4 |! x& H Q1 U4 Z3 f" A" l
* l$ h# _+ T+ k$ c4 y
// This is a simple schedule, with only one action that is! ~8 ^9 K# U' o$ F5 f! Y
// just repeated every time. See jmousetrap for more
9 m9 w0 I: v, O // complicated schedules.
k. |% O) P e2 \ ) Z* h# |$ [7 o' X4 [* } B4 L
modelSchedule = new ScheduleImpl (getZone (), 1);
4 h% k% E/ T. z; y+ C modelSchedule.at$createAction (0, modelActions);
( T% y% W6 P' A* t* I# S" l % l) [* ?" D. Y! {7 x
return this;* g4 Y0 R9 Z v; c( j
} |