HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- @1 ~, V1 c# n9 k X( ^. L! q' S6 S0 W8 a
public Object buildActions () {2 S& k" C# b6 B- Y* Q: c
super.buildActions();
$ G, X* z( k0 z! v8 X! E" C
& B) {: j0 _( K# e5 v- Q7 M6 F4 _ // Create the list of simulation actions. We put these in
$ k ]- X- ?, J4 u: U, U0 W' Y // an action group, because we want these actions to be
$ F p# T& h' i- d6 y8 _ // executed in a specific order, but these steps should0 L# ]! d F; F% g
// take no (simulated) time. The M(foo) means "The message1 Q8 {- y' L! V5 P- t5 H5 h: D. f
// called <foo>". You can send a message To a particular
: c- O% K" o; k: ]4 d$ D+ c6 j // object, or ForEach object in a collection.; j& x& E3 W9 @/ T* R' A
% }% u7 f& x( ?+ s3 w
// Note we update the heatspace in two phases: first run3 X' S) v( j1 J9 ~3 e& q' V
// diffusion, then run "updateWorld" to actually enact the
3 D( d1 S9 t0 a: h% | [5 i& E // changes the heatbugs have made. The ordering here is
1 e" y3 ], ]* g8 ?2 h- a // significant! x. Y0 C) b& R0 n6 G- z
1 v# F- [3 M! B; G& S I# w
// Note also, that with the additional
) W9 T8 }" f/ ~' ~! B // `randomizeHeatbugUpdateOrder' Boolean flag we can
- ^+ P H6 M' S7 j // randomize the order in which the bugs actually run$ x* k* E9 R: A# z
// their step rule. This has the effect of removing any) d) z6 N8 \9 J9 W# X# i
// systematic bias in the iteration throught the heatbug/ l% O' v% k; g! d# n" L
// list from timestep to timestep' x: X! E( s0 Z) W! T& {) @' n N( W
g8 \; ]" K; U! n! f% L // By default, all `createActionForEach' modelActions have
T% C! V0 x, N& G) U // a default order of `Sequential', which means that the; H& S. Y; k: i; Z3 R
// order of iteration through the `heatbugList' will be
" a% a: q6 T8 Z // identical (assuming the list order is not changed
7 V; c+ T4 A, @6 @0 I$ J // indirectly by some other process).
6 H" R: m! V5 w x! E5 Y+ Q) s2 n" K
modelActions = new ActionGroupImpl (getZone ());! a& Z B" ?2 ^
+ m1 A" o& n, ^9 a4 w: _ try {
* e4 Q, m3 u1 ^4 G1 h9 B modelActions.createActionTo$message3 x* U u: v! ?( b4 D, m
(heat, new Selector (heat.getClass (), "stepRule", false));
% p% ^0 i. x: H4 l, [9 c6 S2 |6 I; u } catch (Exception e) {
. N4 ]5 C3 j* \" p( j Y& {( O6 N* ` System.err.println ("Exception stepRule: " + e.getMessage ());
3 y; c) U* B! {# r0 _ }! t# j" M' m k1 T) s
. @. U" b+ j' Q @+ M try {! C( y8 D2 r. O y* i$ ^
Heatbug proto = (Heatbug) heatbugList.get (0);2 R, K! z u* J" X" k1 N; V, C
Selector sel = / C+ Q3 k0 v1 `, R$ `+ ^) X
new Selector (proto.getClass (), "heatbugStep", false);, N4 k7 e* f! t. B5 |6 w$ h
actionForEach =
i8 i" K4 q- R2 [, W modelActions.createFActionForEachHomogeneous$call2 ^8 ]: T: f, ~' x/ e$ G0 U, [! Y% P
(heatbugList,5 v" H$ N' v& I% Y! W `6 p
new FCallImpl (this, proto, sel, [& v U) S% x2 `8 Q
new FArgumentsImpl (this, sel)));
- D9 x! J- c& w8 H0 {2 `4 _ } catch (Exception e) {+ y" z% z; X" w' T5 E( F
e.printStackTrace (System.err);
3 G. ]! o! _+ {6 n! l }
0 S, P% l" v' f* }' U - g5 T e) x( u. q& }
syncUpdateOrder ();) n* K% {* n8 Z2 d% D" M- Y
% Y2 b# M8 O: b9 ^6 H: Z( ?
try {
5 I% \% c* E5 J* l7 I5 J* L5 X, ^ modelActions.createActionTo$message
6 K' z. d \0 w2 L (heat, new Selector (heat.getClass (), "updateLattice", false));/ d) [5 `# {1 Q
} catch (Exception e) {& v8 m8 [0 i6 R+ A9 _
System.err.println("Exception updateLattice: " + e.getMessage ());* w4 o* x; t! L$ v' |. {6 I% M
}* e) C2 ]# H$ p* o
& ]- a2 w& L+ t, t- w
// Then we create a schedule that executes the
: a' @8 o: O1 g k // modelActions. modelActions is an ActionGroup, by itself it
0 A( s. q, c. F) E! I // has no notion of time. In order to have it executed in
9 O* I& ~8 H" d, G // time, we create a Schedule that says to use the
1 ?) {. U" u0 _( o$ d" G // modelActions ActionGroup at particular times. This
5 ]7 n) m; R4 z* Y! J+ m8 T // schedule has a repeat interval of 1, it will loop every1 g" T+ a" ^3 Y' }' x
// time step. The action is executed at time 0 relative to
) N9 C8 j' s$ R1 X // the beginning of the loop. F8 M, N; {( U; J$ F
_4 B5 m# k3 O' x# F% t$ ]: F // This is a simple schedule, with only one action that is
, U: K, Y1 Y* h // just repeated every time. See jmousetrap for more
7 d, _+ A5 ^; B ?+ W // complicated schedules.
N& M: B4 K" U: |6 S 8 m' \3 B6 s" c; Y) ^ F1 M4 f
modelSchedule = new ScheduleImpl (getZone (), 1);
5 J C$ _( y) l+ c modelSchedule.at$createAction (0, modelActions);% ]' \% K; O' g5 C8 z0 @' G
D3 Y+ @$ U& Q4 O return this;$ G& e4 E8 t* b+ ~# s6 v
} |