HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( T3 Q" l# W1 Q' U5 O9 m7 Y5 r. l+ b: V) f" K
public Object buildActions () {
# t- @1 ]. t" J- F: U super.buildActions();
* A/ _* j3 ^0 P) H * M# X, ~& L' g: A9 l) Y9 }3 y
// Create the list of simulation actions. We put these in2 w Q+ k+ h A( \. P4 O
// an action group, because we want these actions to be
1 {9 `) c$ }& C9 p5 N // executed in a specific order, but these steps should4 q: F, S" b/ p- }3 A3 p
// take no (simulated) time. The M(foo) means "The message9 ^6 z, x2 \$ w& y) G& z
// called <foo>". You can send a message To a particular
5 p/ E0 B' t3 P, M5 L // object, or ForEach object in a collection.
3 t& _: B( d7 o" M$ c! I4 e" F . R+ g$ G# T( e" [+ E
// Note we update the heatspace in two phases: first run# M+ N" W' f8 E5 i+ k! d$ e" L) B
// diffusion, then run "updateWorld" to actually enact the
4 q! n! h* Z7 C9 P- m // changes the heatbugs have made. The ordering here is% c5 B3 p7 D% t2 x9 L
// significant!
* r: j7 a; }* M _6 E. o; Y - n3 S2 h/ P- u+ z% D8 ~+ [
// Note also, that with the additional {4 B! _4 p+ i* e3 u1 q8 o- ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can
+ o) F! Y2 c" z0 g( F: `! z, p // randomize the order in which the bugs actually run
, i, E& R+ Q. P! M' J @ // their step rule. This has the effect of removing any2 j& Y0 ^! |9 t( ]9 f# A1 X
// systematic bias in the iteration throught the heatbug1 l* v* ], H A6 q; y" o2 ~
// list from timestep to timestep
+ V8 v$ q. { n9 k
2 u- Z9 T3 b* A& t' K0 v // By default, all `createActionForEach' modelActions have5 j E9 N( O1 b- y
// a default order of `Sequential', which means that the
8 t8 M: _. q" M: [# F // order of iteration through the `heatbugList' will be$ M( V% e( t+ q( L! ^
// identical (assuming the list order is not changed8 h1 @) Q# m3 ~) s
// indirectly by some other process).
5 d# @4 a" e! U8 ~2 [: S . v) z2 S. i+ B6 V
modelActions = new ActionGroupImpl (getZone ());
7 Q$ r# I- p! L) f
5 W- m' N( O' [1 v b try {; c, \( a. h7 ^$ v+ v
modelActions.createActionTo$message j2 V5 }. q5 C$ o) f8 q0 R
(heat, new Selector (heat.getClass (), "stepRule", false));' e5 E: l, r' H( z) c
} catch (Exception e) {6 J. ^% d7 r5 Y9 j
System.err.println ("Exception stepRule: " + e.getMessage ());
t: @0 @5 N5 U0 }! D }) X: a0 A i9 n
' X% E% ?4 |) ^, ?
try {
e7 Y7 ~* V- b1 J. {: k/ o Heatbug proto = (Heatbug) heatbugList.get (0);
7 O$ B, f) R* k" R Selector sel = & k; w4 x# T! M% i5 i
new Selector (proto.getClass (), "heatbugStep", false);
# t# }! X! C2 }; k actionForEach =7 k$ G1 z: E" G9 q! }0 a; r3 S; q
modelActions.createFActionForEachHomogeneous$call
" y& E& N- ^) {. D. ? (heatbugList,
3 G, w$ D) ]8 s X new FCallImpl (this, proto, sel,$ W3 ?6 B `+ C4 I' t0 v5 v
new FArgumentsImpl (this, sel)));
9 B' n2 g3 ~9 K) y5 P# G } catch (Exception e) {% p- S" W0 h- K- g1 g. Q, D
e.printStackTrace (System.err);! ^5 g$ g5 D/ X N
}4 t$ `. Q9 s1 `
! h9 T" k/ u2 S g. w, Z
syncUpdateOrder ();
. K3 _( b0 o( C% t# L {( r$ N4 j g, s* M2 k2 E
try {
! }" y& a: c% P. i5 H0 ^/ s modelActions.createActionTo$message . ?$ o! E( g* @, A. X# ?& G' C
(heat, new Selector (heat.getClass (), "updateLattice", false));
q0 \; }' L+ t" k$ q* m' }: |4 U } catch (Exception e) {
/ o3 ~ d) k0 H0 b System.err.println("Exception updateLattice: " + e.getMessage ());$ ~; d3 h2 Y! d8 k; @
}
1 t" P' K9 k6 ?7 ~' C. c* {. E
1 @" F+ v0 e2 u% V // Then we create a schedule that executes the
6 s) ?# c* u3 N! }- T' i // modelActions. modelActions is an ActionGroup, by itself it5 @2 o7 n- o9 F- }" f3 [ C
// has no notion of time. In order to have it executed in$ Y2 i& D4 X, m- ?7 {2 h
// time, we create a Schedule that says to use the
6 P$ v, P7 C" W( B! } // modelActions ActionGroup at particular times. This
9 m5 l! n: q9 s3 F, F) k6 l' C // schedule has a repeat interval of 1, it will loop every
) @0 Y+ r. e1 {2 q' F! f // time step. The action is executed at time 0 relative to
- e% X/ k+ J& B // the beginning of the loop.9 Q0 o& d1 H( M
: g* ]: x0 H3 h. Y7 Y // This is a simple schedule, with only one action that is
2 o: w- q. O$ j0 H. W // just repeated every time. See jmousetrap for more e& u$ [/ h/ g* `. f+ J
// complicated schedules.
: v& X! t% l" R. @9 ^9 @ w
( W1 a3 T* z4 U modelSchedule = new ScheduleImpl (getZone (), 1);& V3 x# y, I' Y! ^- k$ V4 {/ S
modelSchedule.at$createAction (0, modelActions);+ x N$ g9 U H4 F' }6 I" b
5 K2 H- f) f8 ]/ m2 E return this;! y& g" z4 e5 ^+ d+ B5 h
} |