HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 I+ E& c3 `3 b2 T x
3 p# v' C, A# A$ S9 ?
public Object buildActions () {
6 A$ f* s* @7 J) c) B super.buildActions();
8 h! i! o9 e* W3 |, E/ n6 S $ Q8 d7 e8 L% g# F) F3 W
// Create the list of simulation actions. We put these in
+ D& R' \; R) E4 j9 @ // an action group, because we want these actions to be
4 F2 G9 V0 W5 L& t0 r% W // executed in a specific order, but these steps should4 D/ h+ \6 P6 V7 x9 z
// take no (simulated) time. The M(foo) means "The message
$ z, E1 P2 t: j) W // called <foo>". You can send a message To a particular
6 m8 F& ]0 m. `; A C // object, or ForEach object in a collection.
- e0 Z+ D9 ?# b5 U0 Q, z9 o# x
' y1 r: J) w3 r3 p" I; p // Note we update the heatspace in two phases: first run1 C7 T$ k6 [4 c4 k% J4 H3 @
// diffusion, then run "updateWorld" to actually enact the4 k6 l6 F+ \* Z. |( w: _) I
// changes the heatbugs have made. The ordering here is
% X [6 y8 m& O$ E) Z, A // significant!
" u8 j# t! M! n' L! n7 l/ c
: c5 x, L2 q) N1 k+ W // Note also, that with the additional/ p+ Z$ _- ^' r
// `randomizeHeatbugUpdateOrder' Boolean flag we can
) W1 A/ c) A& R C+ z8 N9 F. c // randomize the order in which the bugs actually run
. b; @' ^& o: i2 G8 j- Q8 ?9 G // their step rule. This has the effect of removing any
+ [ f( Y5 D" O // systematic bias in the iteration throught the heatbug0 p: b% v- k, W; ?+ x R
// list from timestep to timestep2 t/ w' {" ^) ] j8 f
( I4 V" G1 c. `' ~* J // By default, all `createActionForEach' modelActions have# D9 F1 C- x. n; t% U, M" m
// a default order of `Sequential', which means that the
# Y8 I* e: V4 n' k& Q1 W // order of iteration through the `heatbugList' will be! C9 C. o3 h! v% _8 e- r8 H
// identical (assuming the list order is not changed
% k, }* w: h" U' W( { // indirectly by some other process)., E3 t; f, \. c8 F+ J
' F+ |6 {8 W1 b$ w; O modelActions = new ActionGroupImpl (getZone ());( H! ^8 s! j! p: g4 E! J
# n# T* e+ I8 p, T
try {
* _8 M5 q" O/ n4 g/ p, l modelActions.createActionTo$message d; m1 E4 V9 ~
(heat, new Selector (heat.getClass (), "stepRule", false));
% \$ d* |1 s9 o) i6 { } catch (Exception e) {
e2 a, G9 o4 a: Z- A( G/ \8 v System.err.println ("Exception stepRule: " + e.getMessage ());, v2 p7 H" t. l9 y* X8 \5 j' F2 R, I
}
* V$ M h: {, g
* ]% [$ C4 f! c W( }, ^* w try {
/ j% C' @. B- Y$ j! h$ g2 ]1 H Heatbug proto = (Heatbug) heatbugList.get (0);$ o$ i' P# j: V( Z% k3 B4 q* o: T
Selector sel =
6 w+ p% M/ k. T new Selector (proto.getClass (), "heatbugStep", false);
' L$ M& a8 U* ~* N, G' F' M( a$ i actionForEach =
' Q2 U, z% H5 J& R2 r( j, W/ t modelActions.createFActionForEachHomogeneous$call' a4 h8 ^& e2 q5 {0 J
(heatbugList,' @( Z: s, @ `
new FCallImpl (this, proto, sel,
0 z; c& Z& \9 `* F8 M new FArgumentsImpl (this, sel)));, x7 r% y7 i4 x! s3 ?
} catch (Exception e) {
9 E( I- k- K; d, t' I e.printStackTrace (System.err);0 w4 E8 {' C% W- [
}
) t+ M% l+ }" L# h+ c" ` , m3 |- M" f/ R% j5 Z( i
syncUpdateOrder ();: N0 |5 T, z5 T/ I
0 a( `4 X& K- F& T m6 }( j% O try {6 g* V9 u+ p' _' T, Z, n
modelActions.createActionTo$message
/ n: f- L+ g* q7 O (heat, new Selector (heat.getClass (), "updateLattice", false));
: i! a9 ]1 i8 N- y } catch (Exception e) {
7 s9 O1 A4 U6 m* }# B System.err.println("Exception updateLattice: " + e.getMessage ());
2 B3 E6 H5 p! `6 ~. j }$ S- q3 t W" p4 {/ x+ X' X' L. E5 h' ~
- G8 p6 u5 I4 p" ?& s8 p' v: S. H
// Then we create a schedule that executes the
2 x3 G( j6 P* |# R: I; s, c // modelActions. modelActions is an ActionGroup, by itself it0 ~8 J5 `9 \! {7 }2 ^, }) L. ]
// has no notion of time. In order to have it executed in6 ~; o/ m2 e- C6 S$ o
// time, we create a Schedule that says to use the" u) H7 J4 N5 E# `$ K, v
// modelActions ActionGroup at particular times. This
( n( O" s5 ^. ]6 m: p // schedule has a repeat interval of 1, it will loop every: c! T5 f& a# P7 u/ f4 Y H
// time step. The action is executed at time 0 relative to, R& p7 e1 V9 } M7 N; J
// the beginning of the loop.
. T$ N- L/ N- b% S/ ~ U% ^' P$ S# j0 M( M$ Q
// This is a simple schedule, with only one action that is
" o0 u5 M, T+ V9 q9 c" G // just repeated every time. See jmousetrap for more
$ c. h* r+ t% ~+ ?2 G // complicated schedules.
) e0 _, G0 e# D2 W2 t: C8 I
0 ?0 @. ?: D# h& C' {* T/ a' x modelSchedule = new ScheduleImpl (getZone (), 1);/ g8 I9 C) x- a; R/ \7 d* T
modelSchedule.at$createAction (0, modelActions);
& \4 ]) c8 A2 `! n6 o 6 D$ Z* \* d+ k
return this; Z; p" T0 [5 b# k7 b5 z8 \5 n
} |