HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( J8 E- V3 Z2 M3 j0 N* W) t1 i! P* R5 b; x! r% K' l- e+ A' J
public Object buildActions () {# @. W9 c8 v7 J6 G' Q; J: \8 D1 X% ^7 ^
super.buildActions();% t* I: K" q6 @4 c- [- X
5 C* d d3 W! l/ u/ J7 y8 ? // Create the list of simulation actions. We put these in/ \$ h+ b# {3 m# @3 N
// an action group, because we want these actions to be2 v! B6 h% k1 `5 P
// executed in a specific order, but these steps should
5 b. {, B! l* B7 C/ W // take no (simulated) time. The M(foo) means "The message( M$ i0 H, T0 C4 Q1 z2 U. l
// called <foo>". You can send a message To a particular
- \+ J. v# m& ]/ g& R // object, or ForEach object in a collection.
) A7 p8 g; a1 Q6 G v( ~5 N# i Z ( w' P& T5 v5 E& O: u
// Note we update the heatspace in two phases: first run
( I1 i y& U3 Q3 q // diffusion, then run "updateWorld" to actually enact the
+ h, j3 |$ ?6 d" q4 c& g // changes the heatbugs have made. The ordering here is
^4 f' o: g2 Q3 {! j! i5 E // significant!/ F& {$ W$ z6 }& A/ q& |* P
+ |4 `3 T \6 R, b // Note also, that with the additional
+ ]5 e0 {- i! p. W: k // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 x! S$ z3 m- g // randomize the order in which the bugs actually run* f$ I v. o/ O4 Q
// their step rule. This has the effect of removing any; F8 Z1 E5 T! ~) K2 _0 l: U- ^
// systematic bias in the iteration throught the heatbug
+ @' f+ k& x3 F' ]6 @+ d' J // list from timestep to timestep
4 R. ~% V3 `9 R, t0 [) I
3 }7 k( B( |- `" O( g8 @7 E // By default, all `createActionForEach' modelActions have
! U+ r3 s# \" i // a default order of `Sequential', which means that the! m) I7 h! Z* E" M+ G
// order of iteration through the `heatbugList' will be
8 D& Y( b$ |' F // identical (assuming the list order is not changed
! H: `$ i) { ]! g( O // indirectly by some other process).
" D2 v1 l; o' {6 i/ ]/ D$ n
% q/ I1 O( e7 p% _ modelActions = new ActionGroupImpl (getZone ());
6 M) M2 {/ M. w0 f, |+ s7 }3 ~+ K5 H. {
try {% C# j; }9 X. Y/ ~0 W, {1 u
modelActions.createActionTo$message
5 S8 r d9 E# s2 H( R3 ~ (heat, new Selector (heat.getClass (), "stepRule", false));
( {- H: q! Y- g" h2 H } catch (Exception e) {
' T/ s; z* n) _4 C! T" A4 s0 X System.err.println ("Exception stepRule: " + e.getMessage ());7 f2 [) t$ R- B" `& S
}1 U6 ^/ y- y/ G* U
$ ?( ?# a/ `# x) W: X; M' r$ ^% T$ P try {
" T$ [& r5 @( ? Heatbug proto = (Heatbug) heatbugList.get (0);# \6 T$ R" W) B# d* k1 F8 J; W
Selector sel = , C" [+ w3 y3 Y A' g: W- v
new Selector (proto.getClass (), "heatbugStep", false);' B7 G$ q4 B- i$ o. D" i- C
actionForEach =! v2 |/ t8 B6 n, C' Y$ j
modelActions.createFActionForEachHomogeneous$call2 z3 @2 h1 b; U) A8 L! L: g, l
(heatbugList,7 y, w; h6 V% @7 Z5 c
new FCallImpl (this, proto, sel,6 ]% ~0 K4 e& }# Z3 ~/ U2 k
new FArgumentsImpl (this, sel)));
, R1 j. x/ R! _1 D7 V7 \ } catch (Exception e) {
5 e N& z; O- H$ b1 C2 h e.printStackTrace (System.err);
! B0 e3 C6 I0 L% F; P M8 _* j7 J( L }4 Z- z: |6 p( x
$ r! j: j$ q; ^! T; s7 B3 S. r* Z
syncUpdateOrder ();
! w2 ]3 z6 T5 d( u
8 ]5 P0 t$ Z: ~5 c5 J try {
4 C4 y9 X) b5 n+ O1 R! n modelActions.createActionTo$message
5 d+ N7 q/ ?( S' ?3 ]( M (heat, new Selector (heat.getClass (), "updateLattice", false));) ? \% k5 F$ m) Y2 u' y9 c# K
} catch (Exception e) {" x9 i% J" y. F0 Q+ ^2 J2 q
System.err.println("Exception updateLattice: " + e.getMessage ());
/ o6 M# K- |6 L0 z* {1 _: M& C& x }! e$ _# z5 N7 ?2 u' G1 B
0 K7 X, c6 G5 W* c: Z' H // Then we create a schedule that executes the
# f& i0 w0 P8 h& b0 _7 V0 x // modelActions. modelActions is an ActionGroup, by itself it- T( e; a1 B4 F: `, e$ E( g
// has no notion of time. In order to have it executed in' \, q" E( g+ \$ r
// time, we create a Schedule that says to use the8 i! g7 x: ]$ I$ ^* V* q6 {
// modelActions ActionGroup at particular times. This l6 [; R# J" Z
// schedule has a repeat interval of 1, it will loop every Y0 m% V6 D6 Q D
// time step. The action is executed at time 0 relative to
* G) } _1 k6 g/ s% I+ b // the beginning of the loop.$ @( U0 l$ U4 T$ B) G0 n: B9 c
$ M" u6 }9 e H$ M- c. l // This is a simple schedule, with only one action that is+ c1 R3 ~( s9 F% g* @) \8 ~4 Q7 V$ F9 h
// just repeated every time. See jmousetrap for more
0 F8 T d; u, {5 q [2 y // complicated schedules.$ V# A, i0 @! c; l7 c" [
/ ?$ M/ d- k& h7 G6 ? A$ T
modelSchedule = new ScheduleImpl (getZone (), 1);
5 k8 L: F; p% H# u* _. t* v modelSchedule.at$createAction (0, modelActions);
* p7 l: z- t x3 e
/ y+ k ^- G+ i8 J. R return this;2 P a6 J( W$ |5 y
} |