HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, l8 d: u+ h, O" Z
, B* T: l+ W" U7 F6 q9 e7 o public Object buildActions () {
5 c+ I" g/ F1 _1 F super.buildActions();% E4 v4 Q+ s' i1 I) n6 E w) ~7 W# U
@; B4 d0 q) x; n0 R+ c // Create the list of simulation actions. We put these in
8 F% Y" U( }6 K% b2 ~, Q$ o // an action group, because we want these actions to be& v( x4 K6 o, t( y. B, A
// executed in a specific order, but these steps should# d7 [% A: E8 i# m( I# r. L7 g
// take no (simulated) time. The M(foo) means "The message
, d' P" { t- {/ x6 y // called <foo>". You can send a message To a particular
* _" k6 C. ^ b/ ?4 w // object, or ForEach object in a collection.
, w7 Q4 T7 b' R 8 s6 d0 j% k/ W) Z
// Note we update the heatspace in two phases: first run) B' \& f6 ^: r; x
// diffusion, then run "updateWorld" to actually enact the% {: r" X( D2 X+ F4 p0 f( u; A
// changes the heatbugs have made. The ordering here is& t! h, n* {' Z5 T" \: p- Q6 S
// significant!
8 i- L: A6 q, N5 S( |/ y
( y1 o) e2 `$ U1 \, ^7 J) C // Note also, that with the additional0 {2 ?! f$ P4 P
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 Z; [1 C8 m; c" q9 I
// randomize the order in which the bugs actually run
& ^" F8 m) e' Q) O. W% { // their step rule. This has the effect of removing any. o- e" e+ F+ E7 G: W: I
// systematic bias in the iteration throught the heatbug
b; y5 e- ^* v: F( E2 K- {6 _ // list from timestep to timestep- }, |$ ]7 o* P
$ V( @6 K6 b: R# ~, w/ K
// By default, all `createActionForEach' modelActions have
3 {3 A% R+ f2 x: q- N // a default order of `Sequential', which means that the; g" y3 n( Y9 F2 i4 s: z3 H
// order of iteration through the `heatbugList' will be
6 \' X- s; D( y // identical (assuming the list order is not changed1 [; G! c* e% h8 U" \4 J5 I) [
// indirectly by some other process)., j& X& ~ `1 C9 L
: b$ c: [: M9 e9 t8 C, ]# C6 Z
modelActions = new ActionGroupImpl (getZone ());3 j7 Q: O% i( c2 g
3 g) d7 m7 c' W; p9 ^6 @ try {
" F8 S. u2 h" J; }5 I: \ modelActions.createActionTo$message
9 q3 }; k9 s9 B: b f# M8 H+ Z" O (heat, new Selector (heat.getClass (), "stepRule", false));
/ U6 m( t2 ?* ?% S9 B( D. P } catch (Exception e) {
+ W# s; ^2 K+ ^; [ System.err.println ("Exception stepRule: " + e.getMessage ());2 ?& }/ X8 n* t
}
& A/ w0 w6 ]1 q) @, i9 V% f1 q' f5 q) Y6 Z; [" k$ \0 o
try {
3 A9 M _! q4 u% u/ z Heatbug proto = (Heatbug) heatbugList.get (0);
" d+ H. i- A. ?$ p: j$ U Selector sel = 3 z5 k' E) K9 f/ C+ ]& G: V
new Selector (proto.getClass (), "heatbugStep", false);- H1 p( J V1 i
actionForEach =
3 x. ` F( s8 g- h$ k/ q modelActions.createFActionForEachHomogeneous$call) f' z7 o" x; P: b. Z4 b
(heatbugList,
: z7 M5 I0 i. K% l$ i- @) Y new FCallImpl (this, proto, sel,; T& k$ g0 p5 s! @0 U. z7 P; T
new FArgumentsImpl (this, sel)));3 Y, a0 T M2 K2 W( e5 ^; R4 |! p
} catch (Exception e) {
) _6 L- l. p4 _, Q( y e.printStackTrace (System.err);
* M$ T& I8 o( q T8 W$ h V5 G9 \ }
& z: o& _" _/ R I5 S/ G 1 E8 ~! X- ?7 S" |
syncUpdateOrder ();
; ^8 x' Z! f$ L: D6 x! w& s( Q: O# v- q9 G
try {# w) a4 s5 p, B2 A. c n U) S: [
modelActions.createActionTo$message
6 s- z" E- I" E _8 | l3 R$ g- K, S (heat, new Selector (heat.getClass (), "updateLattice", false));
4 o" X2 B7 ~" U3 h; y; M } catch (Exception e) {
/ g3 J' i. M, R# t, k System.err.println("Exception updateLattice: " + e.getMessage ());1 ? V+ Q7 [8 `& c& P0 K' w$ L! G
}
* Y2 `" r3 T% F2 `( D / x; B* N- B7 l# ]
// Then we create a schedule that executes the
! M9 F( f9 w8 \$ K5 m& o4 V V0 W% b; R // modelActions. modelActions is an ActionGroup, by itself it
5 h. m6 x. Q- X5 q // has no notion of time. In order to have it executed in
" g. @5 r/ F1 j6 B1 p/ T // time, we create a Schedule that says to use the
8 N$ ]3 w, Y% A // modelActions ActionGroup at particular times. This, @) a: O! }2 x+ L1 g
// schedule has a repeat interval of 1, it will loop every
1 j8 P; A5 ~$ W. D# M // time step. The action is executed at time 0 relative to
3 j4 H/ t J8 F/ P: S // the beginning of the loop.; |8 g) z! b) P! R
. M1 W; M. d" C // This is a simple schedule, with only one action that is
& v) z* R. e8 ]1 v, T6 E // just repeated every time. See jmousetrap for more$ |. o4 ?" M* w3 M+ U9 @
// complicated schedules.: E; v; e% s4 ], `9 T+ b) B
' M4 J: f- T( E: R0 o) R( X
modelSchedule = new ScheduleImpl (getZone (), 1);
0 Z- ^2 M; c* {5 @+ y6 R modelSchedule.at$createAction (0, modelActions);2 n# Y1 Q9 V+ R+ B* T9 L
1 r5 W2 e% r: H) j4 V7 O2 Y' m return this;4 Z0 Q. P/ N; p I. v' ~
} |