HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' `# j' L- A* n/ [5 i
+ W/ P' e5 p4 ~0 ^/ u* ^& E { public Object buildActions () {) Y9 i6 c/ u: i1 H7 ^- }3 J) H
super.buildActions();
' i1 u$ T D9 b3 G. F4 r" Z 2 ]4 \* i3 w8 P7 l6 C
// Create the list of simulation actions. We put these in
0 t7 u& {0 A" d# L( I: { // an action group, because we want these actions to be/ _ P, N# i& p
// executed in a specific order, but these steps should1 ]6 N; \# H) s
// take no (simulated) time. The M(foo) means "The message
P) {7 `+ y* [9 S! J, C // called <foo>". You can send a message To a particular! I5 T; e2 J& Q V: g p
// object, or ForEach object in a collection.
8 }& c& X4 x5 n/ K 0 p7 A- W* D$ _$ @% ]) M" N
// Note we update the heatspace in two phases: first run
# G5 j2 y5 w# |' d' P7 B // diffusion, then run "updateWorld" to actually enact the
8 o7 z3 L+ @" D // changes the heatbugs have made. The ordering here is$ u3 L* \, `( q
// significant!8 r0 u* s: |. \! ^( }8 i; x
9 c& m1 ~# ?6 l
// Note also, that with the additional
7 M; ]+ L( I3 q! |; O, G // `randomizeHeatbugUpdateOrder' Boolean flag we can
0 D( L) w# L; b5 D) k% ~$ E( R // randomize the order in which the bugs actually run
) E& {5 j$ i8 X4 m+ K // their step rule. This has the effect of removing any
, X7 X: Q& Q) V, O // systematic bias in the iteration throught the heatbug7 S0 C1 I8 n2 L3 ^. K
// list from timestep to timestep, `# X& {1 A, q
, G! H: T1 t; ^8 w9 `1 V9 F; Y
// By default, all `createActionForEach' modelActions have( a5 `7 ^1 n; F* y5 n# ?7 B
// a default order of `Sequential', which means that the" r0 O9 h' v* z% D: d8 b9 P6 [
// order of iteration through the `heatbugList' will be6 A$ R- V$ l% g! ?5 o
// identical (assuming the list order is not changed1 l M v& G. M( j# N( S
// indirectly by some other process).
% @2 ~8 N/ a! y) U " W1 [# M. I6 {+ Z% g9 {4 I
modelActions = new ActionGroupImpl (getZone ());1 E6 s# @+ t, P! L' |
8 W9 c9 {* t& t/ k
try {
2 {( Q7 {& l! K; t8 I modelActions.createActionTo$message
4 L5 J/ H1 }& ]5 I6 M8 l$ B2 I/ y (heat, new Selector (heat.getClass (), "stepRule", false));
, e1 N2 {+ f* k7 f& r; z } catch (Exception e) {8 S. U' I, M7 R
System.err.println ("Exception stepRule: " + e.getMessage ());# {% q* }! T4 _! l! A# v3 H
}5 X. ?% S4 |( S! X; F) c
* ]8 J9 F1 O6 J0 z try {
; @- W E8 Q9 a g8 |( m) c1 w Heatbug proto = (Heatbug) heatbugList.get (0);
* p! d2 `$ P* g Selector sel =
/ U; A/ D8 x) r new Selector (proto.getClass (), "heatbugStep", false);
. i2 W2 i6 q; ] actionForEach =
6 X; E$ Y! Y/ p9 N. V2 |) o modelActions.createFActionForEachHomogeneous$call' a1 ^ X8 w8 ?6 U
(heatbugList,
, X* j5 ^, U4 W' B z: U new FCallImpl (this, proto, sel, r1 `1 H# V w9 L+ s
new FArgumentsImpl (this, sel))); h" H( w2 e- ]# D( N! U/ h w
} catch (Exception e) {4 z0 l' U0 s7 i
e.printStackTrace (System.err);
) `" m! O, @* D8 P+ f }
2 F- G% K5 Z" B# C9 r9 L) H / O/ g0 S* r. i4 v
syncUpdateOrder ();
+ w; ~7 G N9 I/ y# ^! P) A, k5 c- w7 M' `$ Z4 m1 j! S7 Q
try {: r+ T: O+ T r8 o) ^9 n$ T4 k
modelActions.createActionTo$message
6 A6 }4 Z- |: j1 r4 b3 Z (heat, new Selector (heat.getClass (), "updateLattice", false));1 R) j% p% ]8 Z# X! X
} catch (Exception e) {; t% z& o' \# K$ o) e! }9 o
System.err.println("Exception updateLattice: " + e.getMessage ());7 l, e( A* j9 i+ x' F/ O h
}5 o5 T) k; m! [: L% {$ |/ O, G
: k8 b5 l2 V2 f b9 [
// Then we create a schedule that executes the3 e, }& u* c- A
// modelActions. modelActions is an ActionGroup, by itself it
& Z5 |' S6 R+ j3 E3 |0 h* g // has no notion of time. In order to have it executed in
6 n! P1 ?0 N* [4 u8 w4 h4 ^ // time, we create a Schedule that says to use the" [& c2 [+ s1 Y2 e
// modelActions ActionGroup at particular times. This1 t3 t) \( V) h, Y; l2 ^: _
// schedule has a repeat interval of 1, it will loop every& \9 y+ R+ e/ b! i4 j! [' [
// time step. The action is executed at time 0 relative to- F# k, K' ~ O! G( w M
// the beginning of the loop.
2 z$ m y0 v; z7 d. m
2 w& A1 L5 F9 z0 m; Q& b // This is a simple schedule, with only one action that is
* t+ L. r0 U2 K' l! N$ r, h+ q // just repeated every time. See jmousetrap for more# }. J* [+ H; T+ s2 @5 n- s
// complicated schedules.
- N8 b4 ]$ o# p7 d+ u* L9 S W4 P6 I . b" ~- y2 w4 I
modelSchedule = new ScheduleImpl (getZone (), 1);: @' D5 _( M" t, S# |
modelSchedule.at$createAction (0, modelActions);
. ? z! z3 K5 A; U2 o6 { 5 t' t/ u& o. _3 \- k- l
return this;) V- p" Z; L# r3 i# Z3 O9 q* |
} |