HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 z; t4 E2 h6 n& s. j7 o1 C% j1 ^8 t3 `/ z
public Object buildActions () {# E) ]8 e# n" e d
super.buildActions();
* O0 f. x+ ^' a% F. C2 s7 t
7 a" {4 L. _( J. d \% R // Create the list of simulation actions. We put these in% u/ ~+ D4 i$ L5 h6 x
// an action group, because we want these actions to be
2 Q; N8 [6 j0 g5 r5 x% K // executed in a specific order, but these steps should# p. n4 K) X+ Z. ]; S' f' ^
// take no (simulated) time. The M(foo) means "The message
C+ P* m( J) I) l // called <foo>". You can send a message To a particular
% t$ y2 l4 ?5 @6 @, t# C3 h // object, or ForEach object in a collection.
/ m$ l) v* \+ V. e. U7 G1 m% O
# {: l& L# v/ ? // Note we update the heatspace in two phases: first run
" f4 t- z$ h0 {$ m5 T! e: Q // diffusion, then run "updateWorld" to actually enact the
7 G. K7 U5 x) v // changes the heatbugs have made. The ordering here is
0 V$ X: c g2 N" _ // significant!
# e3 |9 t# E! T+ @ / n; R' H4 P6 e+ `: o0 v
// Note also, that with the additional) ?: H1 S% x8 m: L6 M0 ?- S: p
// `randomizeHeatbugUpdateOrder' Boolean flag we can
" j* W3 g, K$ n! `7 Y0 ]5 p: M // randomize the order in which the bugs actually run) U. y7 ~; U' F+ @- g( V& S/ }1 E; y
// their step rule. This has the effect of removing any
) P8 d0 E' h0 i9 j // systematic bias in the iteration throught the heatbug
, y, n8 }4 ~! P' O // list from timestep to timestep
# d7 h Z1 l: m2 Z: G 2 v( M) N7 h6 h$ L8 c
// By default, all `createActionForEach' modelActions have
% _1 B9 d# u ^1 o$ E" O // a default order of `Sequential', which means that the8 }% n1 N" h8 f* Y7 b6 ^
// order of iteration through the `heatbugList' will be5 L% Y9 \5 F$ B
// identical (assuming the list order is not changed
0 n6 i4 j0 J. I // indirectly by some other process).9 e' j; U* w8 d, P5 K
9 {3 y8 l: p1 F8 V5 ?% x) g* U modelActions = new ActionGroupImpl (getZone ());7 M4 Z$ L4 r/ l
k; e+ e1 U5 p" c: K4 ?' L% f
try {
3 Z1 I1 j8 }/ m) h modelActions.createActionTo$message
1 I5 W7 V/ }/ G, ^9 N, V4 a: P (heat, new Selector (heat.getClass (), "stepRule", false));# X+ z! m6 p, a" o9 K" _
} catch (Exception e) {
8 B' l0 u; t% j5 v System.err.println ("Exception stepRule: " + e.getMessage ());$ a2 G: k0 i0 h' s
}
* v6 s1 e: U V& r+ ^6 d7 p5 [/ a/ }, w( \3 ~+ _& C
try {
& F0 W H8 B& I% P. z Heatbug proto = (Heatbug) heatbugList.get (0);
4 D, z4 [9 E! r; ~; z Selector sel = ; ~. A- B, {9 f P1 E0 x* k7 ]; j
new Selector (proto.getClass (), "heatbugStep", false);( I3 x; }& L5 O+ ]( u
actionForEach =
* K+ l+ L6 o; d! I* a- r) m modelActions.createFActionForEachHomogeneous$call' h% p+ L$ X7 `* x+ M1 w c) y
(heatbugList,
. \9 \1 @- _2 n% B2 o/ J" y new FCallImpl (this, proto, sel,
* Q( g' R @/ ?- J. I2 ^ new FArgumentsImpl (this, sel)));1 F0 v/ u+ r! i8 |( m) H. B
} catch (Exception e) {; z" T% E* s5 V, w _4 H
e.printStackTrace (System.err);( O! |$ H% V9 U, l$ J
}* W, q1 k! z: y+ j3 y
: ^/ U( e* p" V syncUpdateOrder ();$ ~$ |5 K& v1 a- @
1 |4 p- \2 M0 [$ N try {
" l) ~) ?, F. o6 F7 a& b+ X modelActions.createActionTo$message ; w( g# A- p2 b0 {- J! a
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 k5 A) x3 [) a& z } catch (Exception e) {
' s6 x: h$ w3 |7 X5 q System.err.println("Exception updateLattice: " + e.getMessage ());
! ]# c3 Z& H5 x" i+ n$ @ }& }- c, L# E8 j/ K
/ K0 N9 z' b4 ]
// Then we create a schedule that executes the8 Z' r" \ o* m( E0 j
// modelActions. modelActions is an ActionGroup, by itself it
7 m- R: E( F4 y9 U* X1 a // has no notion of time. In order to have it executed in
8 E! J3 ^/ q& d# \* q- r& ]5 l // time, we create a Schedule that says to use the; A! v& F6 w8 ]! J6 T8 Y$ U
// modelActions ActionGroup at particular times. This
5 Z: N$ |* j5 o: R: G9 J, j8 C // schedule has a repeat interval of 1, it will loop every
% l/ z! D! p8 m0 d9 u& p // time step. The action is executed at time 0 relative to
6 ~9 x8 k( h0 j. E8 }; x0 e // the beginning of the loop.
$ T0 f" K! ^8 y3 M( W
( z4 Z e# N8 @5 } // This is a simple schedule, with only one action that is+ t8 B: g! l1 c3 X0 S3 X3 _1 @5 w: ~
// just repeated every time. See jmousetrap for more
' z+ X: W2 T5 [$ M7 J; ?+ I // complicated schedules.
3 {* L- ?& `% _, L# S' K
- V" O: x" e; O+ n modelSchedule = new ScheduleImpl (getZone (), 1);
% t7 A+ a0 p5 F' ]# m* w modelSchedule.at$createAction (0, modelActions);
2 \9 h# b' i$ [& O3 S( _
1 t$ [6 b1 X8 T1 u: ? W return this;
8 ?7 L! L; ~, N2 k( C; P } |