HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ t; \' o, I! Z; F( {; R
+ `2 }0 Z) x [0 E5 J$ y" J
public Object buildActions () {
7 L- w/ t( G! E* Z1 J super.buildActions();
( Z0 `# h' d/ D/ P3 }3 H 8 U" ?0 A0 X9 c% i0 i) z6 W
// Create the list of simulation actions. We put these in5 V; @4 v3 b$ c6 l& o( ~$ s+ m
// an action group, because we want these actions to be; n+ A& _. @3 w( Q: v! y
// executed in a specific order, but these steps should
1 R" z! A/ w% @0 _3 p b' _% K- W // take no (simulated) time. The M(foo) means "The message9 G _- F- `+ S" `" |5 o
// called <foo>". You can send a message To a particular3 w, h* s$ S' ?0 @
// object, or ForEach object in a collection.
) v& M, O, g% u. D$ ^! w! U
! Q v U3 A; p! v9 g // Note we update the heatspace in two phases: first run
/ e) N; q) x% r& C4 @& t$ s // diffusion, then run "updateWorld" to actually enact the
2 E' ^! X( x' } // changes the heatbugs have made. The ordering here is
- o2 D! _5 j1 d: @ // significant!6 V: }9 J( [) d1 h+ g, S
9 m: g8 ?" S0 h- N, T' k5 }
// Note also, that with the additional
2 p+ Y' m; _3 V- J% L // `randomizeHeatbugUpdateOrder' Boolean flag we can' U7 ?- V0 w# V! s2 ^$ l4 h
// randomize the order in which the bugs actually run
/ v- y5 d( E( u+ o' A // their step rule. This has the effect of removing any$ J( K) `. V) n6 @( S& V* S
// systematic bias in the iteration throught the heatbug$ x4 t( a, T6 N" `; D, D: k; V* t
// list from timestep to timestep
% G6 N1 g1 f& g5 ^/ t: P- [4 T6 { 8 c$ v2 L9 j( _
// By default, all `createActionForEach' modelActions have3 U# P6 K( @/ V2 Y! l
// a default order of `Sequential', which means that the( W( ~8 Q3 B4 o8 }4 @8 A
// order of iteration through the `heatbugList' will be
- a+ R$ J0 z" {- \ // identical (assuming the list order is not changed
/ a7 F" I7 H* t+ ^. h f8 M // indirectly by some other process).
K/ I8 Q- w3 E) W- Y! P 6 q: S5 K' z- ]" Y
modelActions = new ActionGroupImpl (getZone ());
* n ^) t3 {5 W
1 n! j9 P% p& Z; s' r try {
( i! X4 J# T" i( r modelActions.createActionTo$message
. R# H2 \% D; t7 g% Z8 Q (heat, new Selector (heat.getClass (), "stepRule", false));
9 {0 y* X1 h, E+ X) ` } catch (Exception e) {3 `7 \9 i3 G: n' f. o
System.err.println ("Exception stepRule: " + e.getMessage ());
% M: {" w3 ^! ]1 e3 l3 f- Y- w2 u }
0 T% {, K' i* C; Y+ X* o& [* g3 {# V8 w
try {5 [) w0 Q ~/ [- R- [0 @
Heatbug proto = (Heatbug) heatbugList.get (0);
8 Y, P* `7 l. Z2 k. @3 h Selector sel =
# F# o+ G0 h, s% A) B8 t9 V, a# Y2 t new Selector (proto.getClass (), "heatbugStep", false);
7 u( k, U. J$ |4 ?+ ~ actionForEach =
) D* _& z' x* N2 }% ^! X& m modelActions.createFActionForEachHomogeneous$call% a+ A/ J: F6 Q
(heatbugList,6 E% B: q9 N0 R- @ b
new FCallImpl (this, proto, sel,
F P, l4 s& f new FArgumentsImpl (this, sel)));* ]7 n. c/ L) D7 P9 v% u' M
} catch (Exception e) {
$ P. v8 d; b# u- a- k e.printStackTrace (System.err);& S! n( f3 U- e+ o/ C5 J" e: Q
}
# ]) p# c+ M/ @8 L* v! k3 @# Y; B# ?
# I: P8 h! ?; o. w syncUpdateOrder ();
" ?/ U# {) _ F) D) J3 K( _8 d! U2 N% n
try {" A7 N7 s0 L# M; r; b/ K
modelActions.createActionTo$message ! N P% l, O Y( P- r0 f: [9 p- s' P
(heat, new Selector (heat.getClass (), "updateLattice", false));* T1 ], m# k0 j4 S- g+ ?
} catch (Exception e) {( U3 B& G: I' u
System.err.println("Exception updateLattice: " + e.getMessage ());
0 H2 ?( v' d6 ^( Q: }/ w2 C }
% \/ K% ?/ |! r/ |2 Z
$ x& B3 r# U+ K, G // Then we create a schedule that executes the
. \1 Z$ w/ q- ^: p' H% ] // modelActions. modelActions is an ActionGroup, by itself it
! `$ f/ Q. p1 C // has no notion of time. In order to have it executed in
: L' M1 y. Z; y" g3 s- G // time, we create a Schedule that says to use the0 K$ }* H. J8 `
// modelActions ActionGroup at particular times. This
9 z" [0 U$ ?" r" a: w m, n // schedule has a repeat interval of 1, it will loop every
1 d9 I- r5 o9 }' a // time step. The action is executed at time 0 relative to
$ w7 [( J/ w3 ^ // the beginning of the loop.
1 Z7 P: O& e: k" z/ b0 S6 u+ ^4 Y. `8 e! u8 h: h, o9 l( ~
// This is a simple schedule, with only one action that is E! R& ?: _* r
// just repeated every time. See jmousetrap for more' }" {6 M9 n# @% |! R
// complicated schedules." a# E! R1 z$ A3 S& k, w. G7 H
( V8 l9 F( k/ W2 H* g
modelSchedule = new ScheduleImpl (getZone (), 1);3 M! n& J, |; C- f- Q2 Q4 t; _4 W
modelSchedule.at$createAction (0, modelActions);
( A5 a' K7 Y* ^" [$ `. E" M
8 \7 C/ [1 q- U return this;# x2 C/ m i }. C2 P6 _7 X1 A
} |