HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 I& ~) a1 m' k9 u* W. `
0 M0 v+ n' {$ K5 G# f; f
public Object buildActions () {
3 F6 |8 s' y% `9 s+ } super.buildActions();" u8 [% b0 V+ g1 t u( v
9 g/ a6 m3 R% h$ |+ U1 I
// Create the list of simulation actions. We put these in0 u- p- u# E3 M% @9 F
// an action group, because we want these actions to be4 e8 B' u- @( V- a# f4 P) S
// executed in a specific order, but these steps should0 W# ?7 `" e0 u5 f) J
// take no (simulated) time. The M(foo) means "The message
! R# }- {; Q0 v8 N. L2 H# X& P // called <foo>". You can send a message To a particular% \+ e. Y) T1 o& ~ P
// object, or ForEach object in a collection.* ?+ k5 \6 _7 a9 n; e0 X# r0 i( _
, o4 v+ m2 {. X
// Note we update the heatspace in two phases: first run
% q! g6 v7 U3 ]) Y // diffusion, then run "updateWorld" to actually enact the
3 J4 h/ k8 t% g# Q+ E2 s! q // changes the heatbugs have made. The ordering here is" R% E' N+ L4 U: |
// significant!
% Z. _ H* A! J9 Y$ p . A$ k# ~7 G+ ^7 q' m
// Note also, that with the additional8 g4 \- k' m# @: U q
// `randomizeHeatbugUpdateOrder' Boolean flag we can) d1 H' |& D& e; M% _; P& j
// randomize the order in which the bugs actually run! G3 F( M: C. g6 H' E
// their step rule. This has the effect of removing any% y3 j9 n- ?' L6 z+ @- Z9 A# V
// systematic bias in the iteration throught the heatbug
* N1 g- r4 O! y R" E7 a. L // list from timestep to timestep
. u( f% a, J1 J ' Q. k' Y" S9 O( O
// By default, all `createActionForEach' modelActions have8 y: Y7 [3 l& C* h0 O
// a default order of `Sequential', which means that the) F# D. v5 T M8 D+ J" _
// order of iteration through the `heatbugList' will be
% Z" E. i7 D. u" q9 s // identical (assuming the list order is not changed- O2 [( H" k# U- O# c
// indirectly by some other process).% b2 t* j( g" A' M# _: l6 e
2 s3 I* O* h. | modelActions = new ActionGroupImpl (getZone ());* U' t, @/ U8 M' B. \- M4 Q
4 K$ Z( R D" S8 D; [
try {6 s5 n# g* d% X& V" v& M7 O( u+ _
modelActions.createActionTo$message, @: B) L. v# b, v0 C0 j
(heat, new Selector (heat.getClass (), "stepRule", false));6 `* Y- c+ d( u Y9 V
} catch (Exception e) {, c% |- X2 }0 N5 l
System.err.println ("Exception stepRule: " + e.getMessage ());# R4 O; A! J* R. S$ B. n, Y2 W7 i
}
" S0 L) L6 H4 v5 e$ {- f# O# q+ j
try {
* `5 l) B0 p# q" c0 ] Heatbug proto = (Heatbug) heatbugList.get (0);& X& c- g- B( u# B; V
Selector sel = $ ]" N% r* L( L$ C
new Selector (proto.getClass (), "heatbugStep", false);
1 l& F% o; S# f1 A actionForEach =
) F7 v7 P1 v5 g \4 ?; ^( {6 \ modelActions.createFActionForEachHomogeneous$call
9 L c$ H) l4 a/ Q (heatbugList,* W; w$ P7 _, J; Z
new FCallImpl (this, proto, sel,1 U6 Y+ n0 t0 a4 E. P& m
new FArgumentsImpl (this, sel)));
- R& d: A9 W) X, @2 O' K } catch (Exception e) {8 c) S5 V0 u" _0 @; @5 D, ^* E
e.printStackTrace (System.err);. ~9 x* [! O, _, q4 @
}) h* N: c1 W, m
5 `2 Z: Z% |, ]0 |' ^! L# j6 d syncUpdateOrder ();
. Y$ [ ?) Y9 O1 ~, K: r- [! ~: ]7 _
try {& @* V& T" Y' L1 P: [+ s
modelActions.createActionTo$message $ W7 X: e7 P2 x- x8 M" d$ |
(heat, new Selector (heat.getClass (), "updateLattice", false));
3 @: L" C% P7 I- k4 D. [7 H; g" C+ f } catch (Exception e) {, E, p( L" S5 V. m7 \
System.err.println("Exception updateLattice: " + e.getMessage ());
2 P, n, E3 C7 a( D/ K. f( W2 D }5 u, F( v4 C3 I' T; }( L5 e
6 z8 y4 _4 V: Y* T8 c. K' \
// Then we create a schedule that executes the3 J* a5 f6 K# A, E* i6 Z
// modelActions. modelActions is an ActionGroup, by itself it/ I4 O# E( l% u# M% D
// has no notion of time. In order to have it executed in3 G* ]! z# z& t8 N
// time, we create a Schedule that says to use the
8 g4 x5 ]; i" g* o) n2 } // modelActions ActionGroup at particular times. This* ~! s5 W. }6 K4 g1 ]$ j& X5 l
// schedule has a repeat interval of 1, it will loop every
8 H( z( y8 B$ z& i' U3 k; m9 |7 a // time step. The action is executed at time 0 relative to5 y4 Z6 [! g- L9 C( k- l/ G
// the beginning of the loop.
' P: {, g! Q& M% r7 K( }: l
$ J5 \5 b1 x" W2 d, F4 ] // This is a simple schedule, with only one action that is% f3 ~( n2 C+ {. r
// just repeated every time. See jmousetrap for more
1 p* B# A- K- q: N. i // complicated schedules.
1 H _/ W5 G* c1 ?1 O . y& D9 i" B, i- Q! \3 P0 F( f p$ m
modelSchedule = new ScheduleImpl (getZone (), 1);
3 e3 [0 k1 y. d$ v7 S5 l: ?; F8 H modelSchedule.at$createAction (0, modelActions);
$ i6 w+ z0 a( B: u( N* Q
7 P1 [- B9 A8 u% z) {* R return this;% z3 r) A5 K9 v( j9 B
} |