HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 W0 X9 F7 \) Y( S. I/ K/ b- d$ L3 J, j/ n
public Object buildActions () {- ~6 ?- E/ L) g% E
super.buildActions();4 {6 I0 c7 O0 Z$ ?9 i% N
V% {" u6 W$ ~' h; j, f
// Create the list of simulation actions. We put these in' m) T4 J8 _6 _3 \, L. ~: ]
// an action group, because we want these actions to be
" t1 \" E% g) S. D // executed in a specific order, but these steps should
9 }5 s2 _" M- P5 e) ?9 F! g. x // take no (simulated) time. The M(foo) means "The message7 r2 X- |4 n _
// called <foo>". You can send a message To a particular
! }* }2 R7 w/ R // object, or ForEach object in a collection.
K) M2 T! I1 G+ k" V
+ U' ?; ]8 k6 d- d // Note we update the heatspace in two phases: first run" A; |9 [' x2 V# l( V
// diffusion, then run "updateWorld" to actually enact the
* Y `5 B- |/ \3 s+ w. G8 P // changes the heatbugs have made. The ordering here is6 l# y' O' j6 J. _ ?, I
// significant!1 O; R4 M, I; p, Q2 p7 b
! q# P3 W9 a- J5 F( I2 L" s% C5 q. U // Note also, that with the additional
% T& J, x5 _$ v* Q! O/ I // `randomizeHeatbugUpdateOrder' Boolean flag we can1 w) Q5 d* U" ?$ r ~( T
// randomize the order in which the bugs actually run
* Z! [* _* `" b) L // their step rule. This has the effect of removing any
5 ~/ u8 }2 ~' ]6 G- i7 u // systematic bias in the iteration throught the heatbug, R- J7 w! [, [0 Y$ H
// list from timestep to timestep C. s% A" Q4 h6 ~ P' g2 x
* \% i+ s# F7 ?" D
// By default, all `createActionForEach' modelActions have' a) f+ F+ w( m4 U X
// a default order of `Sequential', which means that the
9 S9 [3 ^- d7 N* k" x // order of iteration through the `heatbugList' will be! S- V' G" N4 s1 c
// identical (assuming the list order is not changed6 t+ V g( U( F' S8 R) @3 H
// indirectly by some other process).
7 g8 K z7 y6 n3 z
, [1 n0 {) r" N5 X modelActions = new ActionGroupImpl (getZone ());
9 F" M) D. R; v8 u" s1 ~, _( D! [2 v3 ?1 R# u' W7 y% n
try {; x/ r2 t- z' S* A+ j& E Q
modelActions.createActionTo$message$ a6 h. i" _2 ?: T8 k
(heat, new Selector (heat.getClass (), "stepRule", false));1 P( N- L' Z& m
} catch (Exception e) {/ K/ `, D1 S$ d; g1 B" Z( w) e/ T2 c
System.err.println ("Exception stepRule: " + e.getMessage ());
" `8 r/ ]: i. A/ @2 J2 i }
" ]$ v3 L7 Y' f) q: R+ d
0 O; v1 v$ C% e8 f! \7 R5 H try {$ g) l0 Y/ \) E0 [2 q) n. G5 A
Heatbug proto = (Heatbug) heatbugList.get (0);4 D7 u6 ]; K; ]& S7 R
Selector sel =
. L8 I. v# A$ J0 k( o- f+ ^! n new Selector (proto.getClass (), "heatbugStep", false);0 d5 T" L; T! K* S- t. a
actionForEach =% q, {. Q% h6 F2 U; f5 j( ~
modelActions.createFActionForEachHomogeneous$call- }1 H6 @9 v* S" l
(heatbugList,1 b0 l+ z% G3 { l# b8 T- V Z6 L
new FCallImpl (this, proto, sel,9 g" a& P4 B7 {8 ^! \& C$ w4 C
new FArgumentsImpl (this, sel)));' ^5 L$ m' a, |& s, M7 Z$ ?
} catch (Exception e) {
K1 B8 @3 Q* a# O8 Z- I% @* H e.printStackTrace (System.err);
) Q" L. r* ], G& g" t7 R* { }
: }5 J& t( n7 e/ ?3 F0 B % _" B6 H- k* q; Q
syncUpdateOrder ();, R- t6 C6 _! n v8 E
* ]- t+ g0 [/ V# h( c try {
6 @8 X1 \' e2 b( i5 q modelActions.createActionTo$message & X$ ]. L7 c7 Q1 c' s2 l
(heat, new Selector (heat.getClass (), "updateLattice", false));
) t1 X: A2 V- `' e I } catch (Exception e) { o. o" A4 I4 y" U( W
System.err.println("Exception updateLattice: " + e.getMessage ());
: ^7 @" Y9 s# f/ l( \2 k }! |- O E. ]! ?, A s/ W
; }: U3 [- M4 L, G
// Then we create a schedule that executes the
; I3 l* W% M/ B$ R* ? // modelActions. modelActions is an ActionGroup, by itself it
% O) X1 D. V2 Z& e( U // has no notion of time. In order to have it executed in. |7 H0 u1 X; a5 t) {
// time, we create a Schedule that says to use the
" q6 @2 c0 M1 a0 U8 k // modelActions ActionGroup at particular times. This
4 Y7 D$ U# F% ~ // schedule has a repeat interval of 1, it will loop every
% s! h6 i/ g/ ^2 y4 }. A6 W6 w& S // time step. The action is executed at time 0 relative to" [, M+ N0 H3 w, _4 @
// the beginning of the loop.0 o0 ^8 T5 V4 D
: I* @0 O- i4 m // This is a simple schedule, with only one action that is4 A4 ]9 v9 P: V; ?6 d
// just repeated every time. See jmousetrap for more
7 u9 R# X8 D3 G5 a1 V9 ^4 O // complicated schedules.
6 _6 K. @* h) z4 f. `, d5 G 7 r5 b! t* p# X
modelSchedule = new ScheduleImpl (getZone (), 1);
, G3 N+ j) m8 I: \( d9 V8 _1 V modelSchedule.at$createAction (0, modelActions);, B9 s5 h: a, f" m' Q4 p
$ X% [* K) _3 l+ H1 B# R
return this;
6 g% J& G- \+ D- d g } |