HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 k- ^& S' U5 a3 V
, E9 s8 I3 ^& g* L9 k9 e" D
public Object buildActions () {
' O8 B$ u3 Z. P0 a super.buildActions();2 S' l( P& J1 T1 n* s5 \
. ?9 [: ~8 W4 a+ A1 b
// Create the list of simulation actions. We put these in2 O" P; k$ h+ H& x4 p/ M5 l
// an action group, because we want these actions to be
) y* H5 S6 r4 c- h) Q9 E1 ] // executed in a specific order, but these steps should4 ^- W9 r/ C& n6 d! N
// take no (simulated) time. The M(foo) means "The message
8 D1 L9 @; n* \# C7 m0 Y // called <foo>". You can send a message To a particular
- m Z4 f/ r J/ i$ x2 n- i2 ^1 I; _ // object, or ForEach object in a collection.
* R# c, k2 x6 ~# w! |& K# Z& G
5 J% b2 k9 f) s4 g) w2 g // Note we update the heatspace in two phases: first run
9 d" O& X4 c ` // diffusion, then run "updateWorld" to actually enact the
1 z( q1 C! c- I: t3 Q // changes the heatbugs have made. The ordering here is
' a; |* F8 D( X8 @9 p9 s7 p8 _1 i // significant!, K) ]) z8 w2 e
& J& Q# G8 M8 x; `/ h // Note also, that with the additional
' P- Y# y8 N1 u5 I7 Q // `randomizeHeatbugUpdateOrder' Boolean flag we can
- I& |( O4 `( J, s // randomize the order in which the bugs actually run6 [' l2 S- v0 Z) W
// their step rule. This has the effect of removing any) ` L' q/ x2 H) ?# L g! N3 p* i
// systematic bias in the iteration throught the heatbug, v7 c$ `3 V% W; g
// list from timestep to timestep
9 p5 W3 q& l: ~ , c) j! s% D" z) C; t& F
// By default, all `createActionForEach' modelActions have
0 c: r$ K/ ]' ^2 o2 y2 { // a default order of `Sequential', which means that the$ e: D3 s8 ~/ A' Q. K
// order of iteration through the `heatbugList' will be% l* f0 V9 f( P9 r$ Y, Y- m
// identical (assuming the list order is not changed
4 [# [& X# m+ v. W$ ~- N! @2 n // indirectly by some other process).
/ \9 ^# l& A# M, e* Q% v
( j* w' V9 U$ k) j% r: w modelActions = new ActionGroupImpl (getZone ());
/ g) Q0 A, u; W i/ |' O! k. k5 {% R$ z0 |: ~# c3 G6 S8 H8 c
try {: b% i" [9 y1 `& h T- V
modelActions.createActionTo$message
* U* k U( o# C: J1 K2 z0 t ` @; M (heat, new Selector (heat.getClass (), "stepRule", false));
! H1 w! k+ Q8 \1 @ } catch (Exception e) {
5 X7 V: R( r" l) \" B8 |8 V2 W System.err.println ("Exception stepRule: " + e.getMessage ());
, Z6 ]' k4 W7 \' t" c$ | }6 Q+ y3 v% I" O
7 A% X" P3 J4 V0 Z/ t
try {
4 M4 c$ R- p3 a Heatbug proto = (Heatbug) heatbugList.get (0);' C) [6 B! n& s$ p
Selector sel = , e% q b8 H7 R+ g0 B
new Selector (proto.getClass (), "heatbugStep", false); |$ W* s0 J2 f) B) P" S( X3 _$ J
actionForEach =
4 P: L9 M3 b2 }' S i, O8 n3 x% ]* w modelActions.createFActionForEachHomogeneous$call
9 p& p1 J7 d( `- q* T+ i# W (heatbugList,
( R0 K* t9 p5 J# c2 ?5 ~ new FCallImpl (this, proto, sel,) {7 A7 F$ C% V% ]6 R
new FArgumentsImpl (this, sel)));8 D/ C; b) a }+ T' K5 v4 m
} catch (Exception e) {2 n6 M3 m; ~; y
e.printStackTrace (System.err);' q5 x1 H2 Y! }( h# B0 `
}
6 S4 X J, r9 y6 G
4 g, {: j( }2 Q7 A3 f( w- |: T' ? syncUpdateOrder ();/ y& Y# M$ [! r; E/ M5 E: F
6 p5 F1 O3 P3 A
try {6 ~4 a3 q% c4 o" I" t9 r, N I1 b
modelActions.createActionTo$message
/ L0 [7 b ]+ K7 a, Z (heat, new Selector (heat.getClass (), "updateLattice", false));
M8 i: |" f6 q' O* `( t. a; W } catch (Exception e) {6 Q7 m9 r! h3 j$ J8 a0 ? X0 W
System.err.println("Exception updateLattice: " + e.getMessage ());1 ~+ b5 B. J, \ m5 A/ J* p* T
}
y' [, P1 [: U# n) Q/ z1 \
5 D# e4 i& y- f! ]' p* ]2 p" r1 K% i/ G) ] // Then we create a schedule that executes the! b( S9 X7 l0 D/ s S) j
// modelActions. modelActions is an ActionGroup, by itself it6 J0 {9 ]% s2 _* ^1 l, r0 d
// has no notion of time. In order to have it executed in
( u t$ f8 U0 M X6 u // time, we create a Schedule that says to use the, K$ o7 \% e7 U [( ] U
// modelActions ActionGroup at particular times. This# F. G+ B% S* b; Y) ]0 K2 k
// schedule has a repeat interval of 1, it will loop every1 M2 G) q& r6 _0 b2 N- X/ |7 g
// time step. The action is executed at time 0 relative to
% c- w% f: e. d/ q // the beginning of the loop.4 @) \8 t. M8 y0 E# ]1 o8 Z
- b$ B( y7 c3 g& V L // This is a simple schedule, with only one action that is
$ m u" B- {8 F& b; H7 N$ W) ]1 S // just repeated every time. See jmousetrap for more
% Q5 U8 l) H8 U; i1 W5 m // complicated schedules.* b) b+ g5 K9 E4 Z
& k. \3 W, G& F' b+ T% E
modelSchedule = new ScheduleImpl (getZone (), 1);
; l# ?: [: D6 n. U modelSchedule.at$createAction (0, modelActions);
9 I5 q# m0 J1 I 9 | N6 W! |" P4 D/ d$ I4 {
return this;7 K& i3 v2 ?7 g Z9 i
} |