HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 T2 M C$ R- p1 r
4 Y1 G' W- e' K$ M7 T7 S, J5 h public Object buildActions () {% i4 N7 q, R* s% s8 F. U
super.buildActions();7 B8 j" T' v1 {) G# M
/ Y: c1 ~! _5 A, i' a, |; g+ L // Create the list of simulation actions. We put these in* e9 I' M+ Z' U' ?4 O
// an action group, because we want these actions to be# o# c: W0 L" L( ?: j
// executed in a specific order, but these steps should
1 w8 X% w+ G3 w) U) G/ I. Z // take no (simulated) time. The M(foo) means "The message% s3 E: j4 Y$ E! F. D9 c i$ _
// called <foo>". You can send a message To a particular* @% Y3 l* n. i) j/ ]& C) o5 j
// object, or ForEach object in a collection.7 {; y( G# z% b: ~
# V0 p1 }. K' Z
// Note we update the heatspace in two phases: first run' w8 W1 [, O" H2 `
// diffusion, then run "updateWorld" to actually enact the
; ^4 O& u9 N8 n // changes the heatbugs have made. The ordering here is, k) z9 u5 ]0 |1 r
// significant!
7 v/ P. T P2 p3 {: T& ^6 k % [. {. D; ` \3 x% x- K: [' t
// Note also, that with the additional8 w6 A, S8 \( J
// `randomizeHeatbugUpdateOrder' Boolean flag we can" |# r( f' _ a+ M5 f' B7 O
// randomize the order in which the bugs actually run
- Q" m- J' n. w. F // their step rule. This has the effect of removing any
- ~: r8 L, F9 e // systematic bias in the iteration throught the heatbug l# P* o( E2 {) g$ ]9 j8 K
// list from timestep to timestep. z' @; B. h7 \
8 n: Q8 R( C Q // By default, all `createActionForEach' modelActions have$ f9 F) m+ C' h6 c/ r
// a default order of `Sequential', which means that the
y' w1 ? N0 i& m$ d0 u // order of iteration through the `heatbugList' will be
+ o6 g# g) T3 V) v6 ^ // identical (assuming the list order is not changed
. t; v. o) ~& i& F2 v: } // indirectly by some other process).7 u" X4 J4 f& t6 B/ C
# @* d1 x8 I" ^+ [0 ^6 z) z2 w
modelActions = new ActionGroupImpl (getZone ());
: _! m1 C. b9 i8 N3 U! b& e/ t3 l
try {
) d i: r4 K4 X8 ? modelActions.createActionTo$message
9 y( q: G2 q9 M$ P7 s8 L) `5 \ (heat, new Selector (heat.getClass (), "stepRule", false));1 |3 d* O) x1 F" C
} catch (Exception e) {
4 m1 D e }3 x- ?) g2 g) H System.err.println ("Exception stepRule: " + e.getMessage ());; b+ L- _+ }$ ^& K
}) P, C" i' X% Z3 i5 D. {- X( l
" d. Y6 Z6 g* J c try {
) ?" C+ p, k y& z0 j Heatbug proto = (Heatbug) heatbugList.get (0);7 T& w8 D; Y% F7 i1 |8 a2 {
Selector sel = $ M3 ]8 I; k' n \0 r( o
new Selector (proto.getClass (), "heatbugStep", false);
5 M0 v. u, X1 j* V2 C0 d actionForEach = p3 R% C: i5 [ W8 d
modelActions.createFActionForEachHomogeneous$call$ M# \' o1 g0 a' A
(heatbugList,1 \2 v' X) ]! F7 z! R; B
new FCallImpl (this, proto, sel,& u A7 L' K- d1 Q9 \2 s9 t' r
new FArgumentsImpl (this, sel)));9 ]3 a' l) W Q2 @
} catch (Exception e) {
# y; T' s( h0 Q4 S! m$ a: G$ H; c( | e.printStackTrace (System.err);' X: A+ f$ h' a4 T. L
}
8 o2 }2 k8 o6 G7 j
( z$ O* V1 x5 W5 T syncUpdateOrder ();% m) C4 ?8 f* h( |
% R6 U- O* q9 I4 p, |- W
try {5 U+ y& M! Q: O/ f& ?6 S
modelActions.createActionTo$message . M& r5 ?8 o4 D9 s
(heat, new Selector (heat.getClass (), "updateLattice", false));
, W1 ~2 U% J4 D- ^3 e } catch (Exception e) {
V) V# g8 n0 `1 |, E1 E( `7 ~* E) C System.err.println("Exception updateLattice: " + e.getMessage ());
9 c6 ]: y5 h, L5 D0 F' L# ~ } }
8 r) Y# o( n4 l' D+ j
# h% s) B! M+ R7 J // Then we create a schedule that executes the
* D% f7 S& [/ k# T" t% N // modelActions. modelActions is an ActionGroup, by itself it
( H- W6 S/ w8 c) Y# ]# C0 q // has no notion of time. In order to have it executed in
. Q1 p2 p2 y; H5 }; n/ B // time, we create a Schedule that says to use the
/ W: o! Q8 ~+ x8 c' I! b+ g // modelActions ActionGroup at particular times. This0 ?5 U( y) P: J3 q, w: P$ B; G6 |
// schedule has a repeat interval of 1, it will loop every7 p' {( E: B8 {1 h1 |
// time step. The action is executed at time 0 relative to& b7 ]) M3 U# p
// the beginning of the loop.
# s+ S+ O" K$ ?" y2 [. K' a Z7 J
7 h8 J3 e7 b& y. } // This is a simple schedule, with only one action that is
, c u7 R( V1 J8 p% H$ ^ // just repeated every time. See jmousetrap for more
1 n% c4 @% r6 X' T4 P* `* R* m5 y // complicated schedules.- i9 j# Q, Z& E O$ N1 {
, t$ [0 m) @; R" m6 W! M
modelSchedule = new ScheduleImpl (getZone (), 1);/ U( I/ _5 O. k7 h2 _, Z( I* _
modelSchedule.at$createAction (0, modelActions);' B. A' I7 ~) o5 Q/ Y* B9 t" O
" O9 z; {4 p9 _( R return this;
2 o0 L9 }$ E: w* \4 T8 N } |