HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- Q& a4 f1 ]+ s, Q% H$ m- e; ?( {
^! A8 y* i' ~* Y+ u
public Object buildActions () {
4 V% o1 t' m9 i/ ~8 L$ n- e super.buildActions();
, v6 {2 {# m1 f% S
4 o& l8 [* ~5 i5 q0 { // Create the list of simulation actions. We put these in
% _9 ^2 e: K6 e) m; s! X2 L // an action group, because we want these actions to be8 e- `/ l% e: V% t
// executed in a specific order, but these steps should7 d5 a# y" f* k6 v7 S
// take no (simulated) time. The M(foo) means "The message
! ], g3 I, O& p) [5 e3 a2 j // called <foo>". You can send a message To a particular4 o- ^8 z7 ^ ~$ U
// object, or ForEach object in a collection.6 `0 e: N& s' Q v z2 n; ^
. P" l5 f4 u! c) P& v0 P1 H/ g
// Note we update the heatspace in two phases: first run
9 M9 F9 c6 S( W* o // diffusion, then run "updateWorld" to actually enact the9 \1 `7 D$ [5 {( V
// changes the heatbugs have made. The ordering here is
0 A: M+ j b5 j: N. ~ // significant!
6 c# ^+ ~1 v! F7 b G ) k9 k( T& w8 j( R
// Note also, that with the additional
4 q; F$ I. I9 C7 G/ { // `randomizeHeatbugUpdateOrder' Boolean flag we can( y+ T8 G4 P$ T1 X. d7 R. t
// randomize the order in which the bugs actually run
( D5 C+ M0 [3 i1 X4 C. V7 |4 _" o( ?6 _ // their step rule. This has the effect of removing any7 D/ ]8 b! c; M! t8 }, _6 a% n3 z1 t& Q
// systematic bias in the iteration throught the heatbug# W7 }! U. `$ I$ S% P0 G
// list from timestep to timestep$ H& n) [; C! a5 p$ V8 J
+ g" K' m6 B- f: l$ Z% A q- K
// By default, all `createActionForEach' modelActions have5 v! w* ~; O5 ?2 `: K8 m; E1 {
// a default order of `Sequential', which means that the
* _/ `4 L- }3 H e0 z7 L // order of iteration through the `heatbugList' will be
) t% t4 M9 M/ X' m" e( X0 c1 |7 L // identical (assuming the list order is not changed3 R0 F4 a6 M2 T0 E/ A$ o
// indirectly by some other process)., w5 W" G! F2 V, j& S& N; K
& Q- M* S8 J s K, y3 Z modelActions = new ActionGroupImpl (getZone ());
5 w- l) O5 ?* c# ^6 _+ J! E" J7 X$ j3 |
try {4 L7 V8 Z& ~; T6 x
modelActions.createActionTo$message4 Y& L, ~7 \$ f/ R6 y" g
(heat, new Selector (heat.getClass (), "stepRule", false));
# B! ]/ f6 N! W# r N0 { } catch (Exception e) {4 V" `8 f+ j3 f: `/ Z8 Z4 c
System.err.println ("Exception stepRule: " + e.getMessage ());; p/ E$ k" u$ o
}6 X* e/ {; T7 p p5 S
2 I9 J9 S0 D- ]/ f
try {
5 C, a) a+ e( X0 y O Heatbug proto = (Heatbug) heatbugList.get (0);
4 E6 X6 H z9 C+ Q, ?: s* C6 M Selector sel = ! z4 W+ o# Q' J1 w& z1 v( c
new Selector (proto.getClass (), "heatbugStep", false);
5 A. `) z( P7 ~( L actionForEach =
8 v. I& f& `% U, Y/ i' | modelActions.createFActionForEachHomogeneous$call
( S( \5 T, c% u0 @5 q (heatbugList,, l) Z/ U) G ]* R/ M/ W
new FCallImpl (this, proto, sel,' P* ?, ?2 Z( E+ N) |' W
new FArgumentsImpl (this, sel)));! x; g8 Z7 X7 z4 T: w
} catch (Exception e) {
# S6 k1 Q* k: Q& l0 m2 B e.printStackTrace (System.err);
' N7 L; U8 N' q3 E% a3 m }
% k! o$ g7 k2 H6 r" z6 G+ u( D9 G' [
2 d; ^& f S: s* [" a! Z syncUpdateOrder ();
3 ]0 `8 \9 I2 S7 {7 f) @
5 m9 p/ v1 ]; Z& N' h try {
! ~- }7 p1 q- n7 w+ G* W7 B modelActions.createActionTo$message
% a/ ` R( c. o7 W2 i/ f& r (heat, new Selector (heat.getClass (), "updateLattice", false));
; W( q9 Y, C' G6 v& Y } catch (Exception e) {
. u" F+ z: J) v; Z2 } System.err.println("Exception updateLattice: " + e.getMessage ());- ^. q% m P. q6 l, v/ D t4 F& `+ e
}
7 t# H/ W. g" R' x2 p9 ]( t5 r! L) A- z
2 r" o, _! |+ a5 F8 S( P/ ` // Then we create a schedule that executes the' R2 I |. X; S% @0 k
// modelActions. modelActions is an ActionGroup, by itself it
6 y! ]$ [' N" R( y0 r6 d // has no notion of time. In order to have it executed in$ M8 ~7 a9 q# |1 w# l9 ]' T; g. h6 }
// time, we create a Schedule that says to use the
6 e6 H# p% g0 b& n // modelActions ActionGroup at particular times. This
. F; D/ ^* M, O; M& F' N // schedule has a repeat interval of 1, it will loop every. z) \: E5 V6 `& V
// time step. The action is executed at time 0 relative to
k& a) U/ }' Y1 s0 z // the beginning of the loop.
b" e K0 n0 q( f$ d
. `; j& T+ D& N9 [# M8 ?2 ?0 S // This is a simple schedule, with only one action that is" @2 y% f, j8 d1 @
// just repeated every time. See jmousetrap for more
( P' M1 F- M1 Y // complicated schedules.: q/ h: B/ H0 A0 P
U; P: Q$ ^$ K- H& e/ d; K; k modelSchedule = new ScheduleImpl (getZone (), 1);; f% |) Q: y9 d( C( ^$ B; `
modelSchedule.at$createAction (0, modelActions);
* |; U/ J3 b- {; b' R
% |& y+ n" `" r) O& Q' w3 w6 y: M return this;. Q; X7 f4 ]! J; N3 _& I' A
} |