HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 T- O4 f6 n" j6 U3 V a4 F8 _0 r# L$ A% Z
public Object buildActions () {
' u2 Q7 @" C1 E7 Z- \9 X super.buildActions();
- j0 V: U1 f% `! Q2 h/ a
0 F x9 y1 y* ^+ O; N9 B; v // Create the list of simulation actions. We put these in1 U1 a. r' H9 U5 Q6 Q0 E' J
// an action group, because we want these actions to be }' C; u+ q t$ H3 z
// executed in a specific order, but these steps should
2 \5 G! \# [6 k // take no (simulated) time. The M(foo) means "The message I& @+ ?+ u" u0 m$ R G5 B
// called <foo>". You can send a message To a particular
: r+ D7 y( ]3 @( Q, h // object, or ForEach object in a collection.
6 q& H/ c/ i. T k # }( S/ m1 _1 ?5 E, N7 e
// Note we update the heatspace in two phases: first run. ~3 K* L7 |+ y1 Y ^' _% d
// diffusion, then run "updateWorld" to actually enact the
# L4 L; }8 z# K, Z' Z8 [# e // changes the heatbugs have made. The ordering here is
5 m, u7 P1 z' v7 Z/ d1 W // significant!+ \8 t$ V1 [8 I
_: S* F+ A5 a0 c
// Note also, that with the additional
- o# u- }; f8 a+ p4 H // `randomizeHeatbugUpdateOrder' Boolean flag we can: w% l! l! \ M' k) H/ @7 [
// randomize the order in which the bugs actually run
/ L7 t# @ L, Q // their step rule. This has the effect of removing any D& j* `" O' U" u# d6 |$ ^4 p3 Z
// systematic bias in the iteration throught the heatbug& H/ Q7 B. Q0 d6 Q' O
// list from timestep to timestep! @% Y( @8 a3 o
/ h2 G( g G0 D // By default, all `createActionForEach' modelActions have
; N( g" z2 [" O* l$ R // a default order of `Sequential', which means that the/ x% N4 Y2 U2 p& l
// order of iteration through the `heatbugList' will be
! q( q& L/ f% x# A9 F0 P7 j+ y // identical (assuming the list order is not changed+ A; A3 M. Y- ?6 B+ m1 Q
// indirectly by some other process).
# G& s, q% Q; w $ \8 h6 d% r; Q" e
modelActions = new ActionGroupImpl (getZone ());0 {9 Z# k, L3 t( p% {/ }4 D. A
( w1 F0 s6 l6 W3 F! r. R try {
H6 W' _0 V- o2 J' W9 A modelActions.createActionTo$message
5 ?( |9 z9 N) f- z) {. [1 u# E# ~: q, i (heat, new Selector (heat.getClass (), "stepRule", false));
! S6 j! A# s! Y0 _5 H, b) o } catch (Exception e) {
! y9 k1 @/ Q9 ` System.err.println ("Exception stepRule: " + e.getMessage ());. Y" B F3 N0 I# [% @
} |' m8 l. R$ B6 ?
% K, S! v" E) @, P8 M9 P* U try {
1 v" E& J" u3 Y4 G+ m8 e Heatbug proto = (Heatbug) heatbugList.get (0);
$ f) l+ ~8 Z& {- @0 L4 p Selector sel =
' K# ~& O, j; z. M new Selector (proto.getClass (), "heatbugStep", false);% w) z1 T; F: b4 E$ x
actionForEach =/ \9 W1 J. o$ ~1 q0 I0 S
modelActions.createFActionForEachHomogeneous$call
5 \/ x/ n. {! d( o (heatbugList,
; L7 n$ Z& Q4 x, `/ @* w+ C new FCallImpl (this, proto, sel,
+ D. w; C) D N, j1 ?2 l new FArgumentsImpl (this, sel)));3 D4 N, U/ c7 c/ {
} catch (Exception e) {
8 N! ?# ^: {( R5 ~: [- y e.printStackTrace (System.err);: I' a' s; E8 p# D! s, E
}/ }0 i8 `/ u4 l- t4 o ?
+ L4 t' y2 E- T; |4 C6 }1 V0 o2 f syncUpdateOrder ();
4 h. h6 j; I+ t) B4 i6 U; q' f! O3 e A% ?# d0 e% w, Y, M3 x, d
try {
) i: n5 h6 \, K; }! f2 s& S modelActions.createActionTo$message
5 Z) i3 P+ ~4 ]/ T: n. t6 t (heat, new Selector (heat.getClass (), "updateLattice", false));7 E- t9 L. B# {; x6 R
} catch (Exception e) {8 J' k7 `; T0 {& j
System.err.println("Exception updateLattice: " + e.getMessage ());
K& U- b8 ]. _9 R- x }+ V+ n& o% }* {4 O( K5 X0 b1 e3 {# v1 U
9 |* N" h( A7 u$ } o
// Then we create a schedule that executes the1 G% M' a' s5 F# s7 z; W
// modelActions. modelActions is an ActionGroup, by itself it0 f% `$ V" H% q2 Z: m) L2 `5 n
// has no notion of time. In order to have it executed in
! F- y% h* n+ G" X" L8 e // time, we create a Schedule that says to use the
" d/ S! c1 B) R6 J4 L \$ N1 ` // modelActions ActionGroup at particular times. This' u0 F; h( I( `
// schedule has a repeat interval of 1, it will loop every
' V- N& ?9 H2 C$ B // time step. The action is executed at time 0 relative to5 @- A' }' C) ~1 d) s2 O6 C
// the beginning of the loop.- o( K9 R+ x, z! E
. z/ B7 }4 j; u$ u& j // This is a simple schedule, with only one action that is; s3 H; A% O$ l: V K: ^
// just repeated every time. See jmousetrap for more0 p, i) I1 S. c# W& d; c" D# A
// complicated schedules.% B; |% v! N0 a4 z( B
: C. G5 Z' N% F" L modelSchedule = new ScheduleImpl (getZone (), 1);
( `: a. r0 n9 @% u4 R% Q modelSchedule.at$createAction (0, modelActions);
L, ?0 ~& g' w2 a. `3 B" l: D: T ( S1 y( v7 d6 w' s: G* {
return this;
- y/ E+ C' H( [4 U1 }4 b } |