HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- T' D8 @- C7 C2 j! I8 Y/ ]" F0 A3 J6 r' w. [& f
public Object buildActions () {
* }- S- f) j: S4 x* g" @ super.buildActions();
$ A0 @5 D1 {1 i& |
4 n& z- y! n/ g; n' i; k4 X9 _ // Create the list of simulation actions. We put these in
. s( A# q; ?# d+ t // an action group, because we want these actions to be
) P6 _6 d6 ^) X$ K0 z3 R$ ^# V3 R // executed in a specific order, but these steps should
& d" R4 p* X1 p# N" f // take no (simulated) time. The M(foo) means "The message
W$ `. ?6 Y( [; h4 [1 I7 P // called <foo>". You can send a message To a particular
9 C6 t" P% k! J3 d% X# L // object, or ForEach object in a collection.
, `$ A S0 C5 G4 L' `6 a/ h
- }* A! L. \7 k- A* [. e+ ]% F // Note we update the heatspace in two phases: first run9 T+ v% `: x% l/ a
// diffusion, then run "updateWorld" to actually enact the3 ^1 Y ~/ v, Y6 E- _+ ] b; G
// changes the heatbugs have made. The ordering here is
' s) Y' b a/ N5 h9 s t // significant!
- Q* r, j" U# W7 q7 B2 d+ v
3 S: W3 a' z% B- r8 P/ e // Note also, that with the additional
- V2 X- l/ _ j: k0 M% b // `randomizeHeatbugUpdateOrder' Boolean flag we can9 f# w1 O" U& ]9 L8 a4 ?0 h
// randomize the order in which the bugs actually run" Q/ P- X* C" ]! {0 s6 B! F o% r
// their step rule. This has the effect of removing any
% a+ ?1 I' c, O+ h: N8 w; E // systematic bias in the iteration throught the heatbug" t+ V' p% E$ I* l s2 Y$ s1 e
// list from timestep to timestep" x7 l; Q! D1 W; z
+ T- x+ g6 j, J+ Y Y6 d
// By default, all `createActionForEach' modelActions have
3 q& H( |7 x5 a: C/ G2 l* x' O, e // a default order of `Sequential', which means that the
- P. N' K& u! J3 d' w // order of iteration through the `heatbugList' will be
7 r+ B- b3 h8 o2 y // identical (assuming the list order is not changed: Z! j) c! ^: E/ `
// indirectly by some other process).6 t3 r& D0 R+ I8 |
4 R9 w1 e) T: B" D8 N modelActions = new ActionGroupImpl (getZone ());
1 S) x" y' U" g2 d, r
$ S2 X0 l5 K% G: G8 W( n4 ? try {
, z+ R) U( a7 p* a; d3 V2 X6 F modelActions.createActionTo$message4 c2 D4 c2 [8 t2 _- G3 T3 C! T% K
(heat, new Selector (heat.getClass (), "stepRule", false));' a0 b2 O8 [ {, a K
} catch (Exception e) {
& A7 v# ~/ V! W( _; Y System.err.println ("Exception stepRule: " + e.getMessage ());- i, s* m, O$ j# b. a8 D' p
}) k ^" n9 U" I) D3 C
. w4 F" a- z @. }4 L try {! n/ e1 t1 |+ @
Heatbug proto = (Heatbug) heatbugList.get (0);
% n1 I1 x! T+ Q" S! y Selector sel = ) s# M- L6 B5 W: U2 B4 u2 F J, r
new Selector (proto.getClass (), "heatbugStep", false);
Z' a) m" h1 e4 H3 _! } actionForEach =
3 w2 S2 H- k$ c y: e) T7 ^, x% `4 R modelActions.createFActionForEachHomogeneous$call8 g P) k5 ]. l6 l
(heatbugList,
7 B( Z0 f: \' \5 @/ e new FCallImpl (this, proto, sel,& k6 f: g* F% H1 M8 t q6 @
new FArgumentsImpl (this, sel)));
; R" C" y4 E9 B7 e* } } catch (Exception e) {, z$ H4 \9 b9 P. v. W% x) \( n
e.printStackTrace (System.err);8 F% c* Q& Q+ V3 n+ X
}
- B _8 X4 k, Y* i; X# Z- a
. i3 ~9 w) E a+ [) i syncUpdateOrder ();& o, e3 P2 ]& K" X5 w
# i& H- \: |! a% r {
try {' i9 G8 {, f5 n8 ^
modelActions.createActionTo$message
; {) q9 ^- g' a- Z& D- k (heat, new Selector (heat.getClass (), "updateLattice", false));
; f1 U- q) S# J5 }, D# o9 }4 n } catch (Exception e) {
. o) p+ k! X/ D# b5 ]* w$ d( R' ]9 y4 Y System.err.println("Exception updateLattice: " + e.getMessage ()); N: i" ?/ _9 |& |! e
}: G m: }& e5 g: B9 I5 Z2 U
8 S2 Q! U: B. D1 W6 ` // Then we create a schedule that executes the; x2 ]% j) p& v: [% j, d
// modelActions. modelActions is an ActionGroup, by itself it5 f' A+ u. e. |5 ~6 _4 j7 t) n* q
// has no notion of time. In order to have it executed in& \% L" r+ J- M3 _6 p
// time, we create a Schedule that says to use the
9 X# \9 }4 b& i // modelActions ActionGroup at particular times. This* [* L. c, E* M2 `+ b
// schedule has a repeat interval of 1, it will loop every
: `$ o9 Q/ I" f. @$ `" z, {$ m // time step. The action is executed at time 0 relative to" v+ ~" H8 O3 ]9 b
// the beginning of the loop.
) k( U( F U) @2 |* Y9 |/ o
5 z0 L5 C3 r; f. j' v1 h" D$ y" p // This is a simple schedule, with only one action that is9 B, E6 e1 i* o/ B
// just repeated every time. See jmousetrap for more$ o' V) A2 N; J& g3 \) _( @. C
// complicated schedules.
" _: X! P/ R V
! G, |4 U/ {' ^0 f" s8 v( T! l( R# Y modelSchedule = new ScheduleImpl (getZone (), 1);
9 k7 \0 r+ o: S; s$ h modelSchedule.at$createAction (0, modelActions);1 {$ M* l9 p. J- O0 e% j# U
@% Q+ _2 i! K) K return this;; s l- C" L* V+ Y1 [
} |