HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 [8 ~1 w( c- V% k j. H& `. R) E# r2 Q, H
public Object buildActions () {7 [1 w" J8 C1 T; o f0 z
super.buildActions();
4 ], G2 d2 z: C
0 b( n) f! M# b; `8 w+ A7 u // Create the list of simulation actions. We put these in
0 Z' ~! O7 s4 Z s" z // an action group, because we want these actions to be
+ G; Y& w7 B' }' y9 v+ T // executed in a specific order, but these steps should
& }+ |9 S* Z& v" W // take no (simulated) time. The M(foo) means "The message( E7 x+ ?9 g1 K) n d
// called <foo>". You can send a message To a particular& L6 ?, G- w1 a# M5 E( s4 u
// object, or ForEach object in a collection.. S% r$ d/ L( J7 B0 l6 p
1 {* x2 [/ C. f2 Y: y6 _5 W8 F // Note we update the heatspace in two phases: first run" u9 m1 X2 S2 r) X9 f6 {# z
// diffusion, then run "updateWorld" to actually enact the
8 L) F& O! b! b/ Q! D // changes the heatbugs have made. The ordering here is. `1 ~% U j3 S. w- a* u! f
// significant!4 ~; D8 K. u- \
+ _) e. d& C, B% q/ X% h' N* r) H // Note also, that with the additional
0 S4 q0 u: T0 ]0 T; U, L& y# V0 Z' I // `randomizeHeatbugUpdateOrder' Boolean flag we can1 R5 P$ ]. J' K3 |( J
// randomize the order in which the bugs actually run* g0 f- J+ h8 `4 w% `5 z1 j
// their step rule. This has the effect of removing any- c/ K3 n3 E4 v0 F1 h
// systematic bias in the iteration throught the heatbug; \9 g {, F |. I. z* m4 s
// list from timestep to timestep( |" e" c% r6 A1 X) u/ ~( i3 X
# g( J1 |+ ^! O O: o7 I: E
// By default, all `createActionForEach' modelActions have' H9 I, C' T8 w. M
// a default order of `Sequential', which means that the
5 d! L5 z6 i" F+ s4 ` F // order of iteration through the `heatbugList' will be
( D6 L+ v$ d' b) C // identical (assuming the list order is not changed
$ Y( V1 }# P( { // indirectly by some other process).$ h* p0 W; @9 f2 @
7 o/ r2 x+ J' z6 c3 z modelActions = new ActionGroupImpl (getZone ());
* M4 j2 v5 E; l2 l# h/ E/ X& J1 n7 e. J
try {( m5 Z' _4 v5 {+ A: E+ v7 p' q
modelActions.createActionTo$message6 {: U) x) [2 L4 y; M3 k
(heat, new Selector (heat.getClass (), "stepRule", false));
7 p1 x" g. L' G: L } catch (Exception e) {
2 @& i+ r8 J n System.err.println ("Exception stepRule: " + e.getMessage ());
9 i' e5 Z% Q# x0 g4 } }
$ t3 M0 s8 O; c8 x
, v; ?, G- A; e0 b try {
" C% L. M. j- U" g/ ^6 ?$ N Heatbug proto = (Heatbug) heatbugList.get (0);
9 s/ ~& c' ^8 _, i Selector sel =
" N7 P, J# d, D% ` new Selector (proto.getClass (), "heatbugStep", false);+ T: \" u, H5 z& h5 [
actionForEach =: K2 [; S9 g' {4 r5 w
modelActions.createFActionForEachHomogeneous$call! }) A3 o2 y. ?. b
(heatbugList,% T. E, Y* g8 H$ ?& T- ?% d8 Q- U
new FCallImpl (this, proto, sel,& [3 s2 F6 X1 u4 G5 ^% ^
new FArgumentsImpl (this, sel)));& x# p N- ^& j( J' p+ b" ^/ Q
} catch (Exception e) {
: `+ H# j8 v5 R4 x; G$ Z e.printStackTrace (System.err);
2 a; c/ S+ l4 A( G }
v+ v5 z' q2 K* L
9 i [( {9 N0 s; E syncUpdateOrder ();
" L# y( x2 t4 n) T5 O
w+ _* Z6 c" I* s3 M, c$ ]7 p try {1 e! ?! K) i2 J, T" U
modelActions.createActionTo$message 0 U2 f1 d* g/ a. [6 S
(heat, new Selector (heat.getClass (), "updateLattice", false));
7 T% V1 I# U2 Z G( f2 o } catch (Exception e) {" H& D- `9 W/ [% F' ?# `
System.err.println("Exception updateLattice: " + e.getMessage ());
8 E3 i0 x5 }& ~ _! x% f$ e9 a }9 R: H; \, p3 C% Z6 j4 `
+ Q; R4 K1 M* `7 L! _. c
// Then we create a schedule that executes the
) H$ R. E0 o5 K# Y1 g" L // modelActions. modelActions is an ActionGroup, by itself it
& m5 j# x& U3 t* o: M // has no notion of time. In order to have it executed in
& U- t( s3 M9 d `9 ?" Y // time, we create a Schedule that says to use the
- Q9 A X. L1 ] // modelActions ActionGroup at particular times. This& C8 J' V) g* R& a: A
// schedule has a repeat interval of 1, it will loop every( |& S' B' T L0 Q2 V
// time step. The action is executed at time 0 relative to
@$ C8 Q6 Y* n0 j Y$ e0 A. i5 C4 c // the beginning of the loop.
. i1 J/ o' o# [. y7 m( e- s4 r. q
( n0 S# H# Q& H& O+ |; n' P // This is a simple schedule, with only one action that is
; P: q3 h7 v) i. H ^ // just repeated every time. See jmousetrap for more
/ k3 y( F+ b6 `0 n# Z3 B // complicated schedules.
+ v- G6 {2 l* [1 Q 1 f* V+ N% l1 J4 |, Q9 l' C* T- g. g
modelSchedule = new ScheduleImpl (getZone (), 1);7 K4 u. L6 x- b \
modelSchedule.at$createAction (0, modelActions);
" X; l8 ?1 }' w9 y
& e4 X) q7 k& f8 J5 ~( } return this;; M- U7 ?1 S v7 [+ W' q
} |