HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 C& l1 i% d/ {" I1 L
7 T% D2 b3 } a3 \; M5 Z: t
public Object buildActions () {
. G& a% c$ b5 R* _6 R |/ U! W9 D0 h& e super.buildActions();, `" S1 e- [& E4 w' A* I
0 C3 K5 k5 t* z' p* B/ B6 Q- q
// Create the list of simulation actions. We put these in
( k- X M& W3 L4 a# j5 P: }& [ // an action group, because we want these actions to be
+ c/ }' ?3 a: k% q8 { // executed in a specific order, but these steps should
* S# F% D& G5 h- E1 x+ ~ e8 G // take no (simulated) time. The M(foo) means "The message
* z, Y# S$ r' m5 [ // called <foo>". You can send a message To a particular
" G8 R3 M( M' z$ j# K // object, or ForEach object in a collection.5 c* c' R2 [ G2 ~- P8 Y1 x
: e/ I! P% X0 w3 y' J2 D // Note we update the heatspace in two phases: first run" r8 R) t+ R; S Y, F7 H0 ^5 |/ a
// diffusion, then run "updateWorld" to actually enact the
9 d9 h3 ?' s5 V1 b, j // changes the heatbugs have made. The ordering here is
' m; Q& }, w6 e P // significant!
) V/ x2 N. h" Q7 g ' R a- g1 [( L3 c! Y2 a; o, w
// Note also, that with the additional
" W( X; J8 ^7 I8 |. [ // `randomizeHeatbugUpdateOrder' Boolean flag we can0 h; A2 ~6 {3 W4 |6 F+ z0 n9 c; h$ h
// randomize the order in which the bugs actually run
9 N6 ^# I# g# m- p: H // their step rule. This has the effect of removing any
$ i: M. G/ |* ?! Y( l" K, o$ I/ \ // systematic bias in the iteration throught the heatbug
2 H# u7 \) J' y // list from timestep to timestep, J y9 k+ u; @: O- f
- ?- E* @+ a( C0 n: d // By default, all `createActionForEach' modelActions have3 p& Y( t2 A5 Q1 r+ K2 ]
// a default order of `Sequential', which means that the
4 f, a1 T/ A7 X8 e7 s4 E* w+ u // order of iteration through the `heatbugList' will be
, e" L5 X6 R4 _5 C$ g // identical (assuming the list order is not changed& l0 r5 s" t+ {- x' b1 g
// indirectly by some other process).
& b; M6 p1 ]0 p+ `% T . I0 N, ^& X* C. p$ C. b9 o
modelActions = new ActionGroupImpl (getZone ());
( ]! q, h0 F/ N$ i2 j( F
9 _" @3 i) A" {/ ~9 c# v% F. B5 t try {
$ r5 d# v9 S* [# m2 \: S6 E* w modelActions.createActionTo$message5 O) v6 ]/ y& ^# t( M) t j% }; s
(heat, new Selector (heat.getClass (), "stepRule", false));% ]3 N9 b: X$ `2 I9 w
} catch (Exception e) {
* J0 a4 O) x4 T- F System.err.println ("Exception stepRule: " + e.getMessage ());
# u7 I7 q. }8 B! t/ F }
: n& C# ?, l% Q6 q/ y& p( X, j$ x3 ?
try {
F9 [* R; r) ~7 e8 E0 H4 r Heatbug proto = (Heatbug) heatbugList.get (0);
4 H4 n& k3 l$ U( l0 v+ q Selector sel = 5 d9 i$ j5 B8 F1 N w( c7 f
new Selector (proto.getClass (), "heatbugStep", false);, f! J. N8 m0 J5 ?" V
actionForEach =" G# t- G0 k v" P; F5 {: K
modelActions.createFActionForEachHomogeneous$call
, p6 T* q# \- r (heatbugList,3 Q7 _ H2 L" |( v& l0 Z1 O/ X
new FCallImpl (this, proto, sel,
8 z# `, h. |$ X* {- e" } new FArgumentsImpl (this, sel)));
5 y5 h5 q# W. ?; C0 H' ?7 f6 y; V } catch (Exception e) {
* O. ]4 R9 N% `6 N e.printStackTrace (System.err);
6 T( g4 ^' A5 Z; B/ } }
& ^6 }+ [2 }/ [ H v
C2 h# ?' S$ W# `1 ?9 m. i syncUpdateOrder ();
9 Y1 C& o* k' l5 @3 d0 W. D, h, Y' Z
try {
% A5 u* x7 ]' q1 [% } modelActions.createActionTo$message
* ^: {' _( Y! `3 r% N k# w (heat, new Selector (heat.getClass (), "updateLattice", false));
/ d/ [3 |' t3 v } catch (Exception e) {+ ~ s* D" I1 i1 u1 _
System.err.println("Exception updateLattice: " + e.getMessage ());! k' {. ]5 n% S. {& T& D6 L* m. O
}9 u% a2 y M, J# J* `* H
2 v) |& A" R4 b2 U // Then we create a schedule that executes the
0 N1 |6 j7 o J8 b6 L. I // modelActions. modelActions is an ActionGroup, by itself it
" F' }& G0 h8 U8 ^: \ // has no notion of time. In order to have it executed in
* g1 ~8 x1 G( d. F: r( M; m( x // time, we create a Schedule that says to use the
8 X* W4 y4 G! m+ u% m2 } // modelActions ActionGroup at particular times. This
8 o. x) x' h9 G7 z // schedule has a repeat interval of 1, it will loop every
1 r. x) W4 l# q) R+ b // time step. The action is executed at time 0 relative to
5 R, e0 z: U" F$ x9 ]3 g' H6 r // the beginning of the loop.0 M/ p* @' ]: F
1 x& N1 M7 S9 H; n# h7 _
// This is a simple schedule, with only one action that is) D& a5 o- O) b; h9 U1 `7 e6 |( B
// just repeated every time. See jmousetrap for more! w3 a' k, P) P" o. G' {
// complicated schedules.
2 W. w1 q" v% v# L X " p" \7 m6 u6 k, b8 e
modelSchedule = new ScheduleImpl (getZone (), 1);
! e/ Z: Q2 ^" d: J4 F" f modelSchedule.at$createAction (0, modelActions);3 |" m) a" ~( o7 o8 ~+ z2 w2 B* k, P
# Z/ n' j* v0 R6 k return this;
& k. {2 k3 V& F } |