HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* E0 F3 z$ c: I* r: c( ~# w. D
" h( D/ u8 {: V$ i- f; } public Object buildActions () {
" h8 @$ R* }( Q7 C8 Y9 r super.buildActions();
- I( |- q+ t5 ^# Y& m 7 w) X/ h" ^7 ]( N( B" W4 ?3 ?0 P" d. I$ X3 g
// Create the list of simulation actions. We put these in3 M& ^$ z d9 N1 ?; _" Z
// an action group, because we want these actions to be' T6 ?! p: n, W8 ]+ U y4 U% R
// executed in a specific order, but these steps should
) ~- E( Z( {. l0 @1 Y, y // take no (simulated) time. The M(foo) means "The message0 F1 x9 \# T; q- m. g% q" o
// called <foo>". You can send a message To a particular
a5 t& Q7 Y `; G1 K( J5 z // object, or ForEach object in a collection.
/ R0 i( J( _3 N, v0 Q) Z $ ^' }" L# s. i( A6 j' c! g/ b
// Note we update the heatspace in two phases: first run1 K0 O4 S# s9 J
// diffusion, then run "updateWorld" to actually enact the
5 r* P& ?! p0 c, [, [; ~! I, @* ` // changes the heatbugs have made. The ordering here is3 Z+ A: S4 G* N, I' u; T5 s0 Y, ~
// significant!
; X* [0 S6 Y; D
* I6 F! Z/ f) l- b7 [ // Note also, that with the additional
! t0 u' N: S# { // `randomizeHeatbugUpdateOrder' Boolean flag we can
% E3 ?* b' ?& ?& m# t // randomize the order in which the bugs actually run
7 e3 s" N, E6 D; D: s3 J! ] // their step rule. This has the effect of removing any
2 d+ X* X9 E1 L, X+ ?& G. ] @( b // systematic bias in the iteration throught the heatbug: a( o8 v# d6 m# [: N
// list from timestep to timestep
# L& {: k. `7 F: [+ l v/ i- U5 l9 i0 G1 U. A+ }8 Y
// By default, all `createActionForEach' modelActions have1 j6 N# `' _2 t
// a default order of `Sequential', which means that the# k9 B/ x3 N$ ?% W
// order of iteration through the `heatbugList' will be
3 H+ B. {$ x" Q& l0 U // identical (assuming the list order is not changed
) B: C- r& a: i* G# S // indirectly by some other process).
& d/ w- \9 {0 p( R; d+ Z
" g) {" F3 C y( [9 Z$ v modelActions = new ActionGroupImpl (getZone ());
. E4 z9 H* K# ]& j4 y- |# v; S( L" L6 P8 Z
try {# V+ r: O4 ?. }5 S4 V, ], S
modelActions.createActionTo$message
" @3 M0 ~9 ^/ z2 `' e6 ? (heat, new Selector (heat.getClass (), "stepRule", false));6 H* b) l1 W2 R, ]
} catch (Exception e) {
$ o1 ~( E J# H' T3 m$ A System.err.println ("Exception stepRule: " + e.getMessage ());) P# R& ]2 T2 ~
}4 a( l' O) F, c% u
9 L# G* @5 w9 `8 m try {
, c$ H2 N9 A8 k. }/ | w" J Heatbug proto = (Heatbug) heatbugList.get (0);) Y' O; Y* m+ O& ]) Q% M
Selector sel =
. Z6 ?$ W; M& K: s new Selector (proto.getClass (), "heatbugStep", false);
( m8 J+ j+ t& |3 p% ^ actionForEach =4 R6 j2 G3 J( a5 f7 _: E+ b
modelActions.createFActionForEachHomogeneous$call8 [3 `6 T# p3 V) G! N
(heatbugList,- u. U1 y: g' ?8 ]2 D
new FCallImpl (this, proto, sel,
$ Z# s5 t* h9 K9 W) Q$ y new FArgumentsImpl (this, sel)));% q) J% ?+ m* r. ?* |* j: u
} catch (Exception e) {
, I G& \' H( \5 o) p1 V4 n e.printStackTrace (System.err);& ~3 P- ^! X+ k- h, P- `
}
- W4 X! T: q* s / p* i2 H' z9 F5 X' e( n. _9 y
syncUpdateOrder ();
4 \8 @2 ?$ R! j* [9 W1 O: {6 r$ p
try {
, U& M* g' L) p modelActions.createActionTo$message 4 W7 g2 R- ?% X- z. X% a# ^
(heat, new Selector (heat.getClass (), "updateLattice", false)); I8 a2 g3 [1 i2 I1 F
} catch (Exception e) {0 P0 p9 ~1 z" d
System.err.println("Exception updateLattice: " + e.getMessage ());
$ @9 I, p2 u& |5 g Q; n- | }* r- l7 |- M7 C/ I ^7 \
5 J3 F0 ?! P8 @
// Then we create a schedule that executes the
8 S- D) S( C. |2 {5 T( ~ ~ // modelActions. modelActions is an ActionGroup, by itself it" ]0 t" T& v* V. e v m
// has no notion of time. In order to have it executed in
- {9 N4 Y) J0 ^# o. o6 F* m$ B // time, we create a Schedule that says to use the
: }1 m! E! [$ s# F. S // modelActions ActionGroup at particular times. This; x+ h; p u4 R7 Q* X; z
// schedule has a repeat interval of 1, it will loop every
; r; g2 v1 o/ [. q9 | // time step. The action is executed at time 0 relative to, A4 q* {3 E: j. ^/ H+ l$ d) |1 }
// the beginning of the loop.
; `* `3 J! ~, d7 N/ H& g% }) o
# }/ G' h0 f7 B7 x: J* b+ w3 S5 m7 c // This is a simple schedule, with only one action that is
8 i. \0 {8 I5 Q // just repeated every time. See jmousetrap for more2 l2 d# n) K' E% S/ Z
// complicated schedules.- R" U6 X4 B+ |1 ]5 W( z
# D3 ]2 s( u: y( J- V# n% k8 U B
modelSchedule = new ScheduleImpl (getZone (), 1);
1 A" @/ I; t, n: Q modelSchedule.at$createAction (0, modelActions);
" ~! F; X2 ~* m9 ~9 C" u$ B 3 w, f& f9 L9 X$ F& _: ]- T! P
return this;0 ~8 U/ Z6 b; n8 f8 F
} |