HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 \) O- P- U. q2 V* j) e3 c: B* }/ Y& b' d* z1 ?7 J" m
public Object buildActions () {
& u; u [, ]4 q; B6 C: I7 E$ ?1 k) j super.buildActions();
% f1 B# Q( N+ \) k8 h ( V/ P( W8 q& W0 O7 w; y) V& m- }
// Create the list of simulation actions. We put these in' z* b5 Q) M# z/ U* A9 F) @7 o
// an action group, because we want these actions to be
& k7 w5 _) C+ i! n% A$ r1 W5 c // executed in a specific order, but these steps should
& I+ {$ A0 f* b+ a; c: t // take no (simulated) time. The M(foo) means "The message. o' E5 r& B c# I; s
// called <foo>". You can send a message To a particular+ T5 K8 i1 R; [$ O) [
// object, or ForEach object in a collection.9 M0 v4 v) J+ n$ x
0 W& W$ u3 s0 H. c+ G" @
// Note we update the heatspace in two phases: first run
s1 ]6 f- ^' c# Q# r // diffusion, then run "updateWorld" to actually enact the
" a. l* ]8 {$ L1 q // changes the heatbugs have made. The ordering here is
3 S6 S5 o$ G* |, q/ c3 V4 e // significant!
6 D$ O9 b+ C2 y0 c8 N5 O, v 8 }% \; t" p9 M9 l6 W% _3 j* ^/ |
// Note also, that with the additional
& p3 |9 i: @) C, Q# w# ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can
& _0 e8 i9 K2 `7 J0 G! n, b4 ]8 O // randomize the order in which the bugs actually run
, _) Y/ W) B7 a; J; ]. P( B* ` // their step rule. This has the effect of removing any
8 w0 [# j/ H% o // systematic bias in the iteration throught the heatbug
& I( |" |( | U+ Q5 E // list from timestep to timestep
$ {/ J/ o3 T0 W6 W4 H( c4 o ( K1 d9 u' b0 W
// By default, all `createActionForEach' modelActions have
1 l& n" a1 n7 y3 S1 i) \ // a default order of `Sequential', which means that the7 \4 i" S( v* ~
// order of iteration through the `heatbugList' will be
, A4 X! A$ W, D/ v1 {3 _ // identical (assuming the list order is not changed
* w/ I0 m) O% B; n% P" \% ^0 U // indirectly by some other process).
6 P* m) T8 {! _" v ! q9 Y* _7 I1 S Z. r& ^$ m# k
modelActions = new ActionGroupImpl (getZone ());
$ T* c) H4 z n( [& X1 L5 v4 O. I# Y. |, c) g+ Y
try {
& X% u8 k( w! z2 J1 Z# A0 } modelActions.createActionTo$message
; Q8 z: R2 T. F4 A (heat, new Selector (heat.getClass (), "stepRule", false));' K7 O" J3 o2 k# a4 `
} catch (Exception e) {7 [2 L# H" r+ n7 a8 J
System.err.println ("Exception stepRule: " + e.getMessage ());
( c) s9 m' D8 [8 P. U/ s }
W) B2 D Q3 t- g- h( F/ j4 p6 [0 }
! K2 [( ]0 A$ A. w8 B% V try {
3 Y# w3 ~6 X- J Heatbug proto = (Heatbug) heatbugList.get (0);* b9 v8 V$ ?* o
Selector sel = - T* x+ X+ \, [1 b2 P1 |- J
new Selector (proto.getClass (), "heatbugStep", false);
+ \+ L' v8 }9 d, t/ c actionForEach =+ o& N2 U- g0 O- N( M+ r9 r
modelActions.createFActionForEachHomogeneous$call
& Q8 ]4 R$ W% ^# ]/ n% S2 g9 c (heatbugList,' J# Q+ b, u, W" s l" F
new FCallImpl (this, proto, sel,
1 [/ _- L; e6 r new FArgumentsImpl (this, sel)));
9 t) q! a- f7 \% Y; M* Q4 c } catch (Exception e) {0 R0 u7 O, Q$ E: }7 g3 j
e.printStackTrace (System.err);
% j5 S! j5 A- U& q' J) T; ?4 h }$ Z2 I) X6 l- J
* }" t- L3 m- @1 e
syncUpdateOrder ();- P4 _* T1 H: M+ F
# b2 i& @% o4 J9 K! I
try {
) o9 @6 v1 \3 b7 b% v/ G* U modelActions.createActionTo$message 4 ^8 @0 G/ Q) W1 N. t$ G
(heat, new Selector (heat.getClass (), "updateLattice", false));
3 \3 w' c0 \1 F m* k2 `" [ } catch (Exception e) {
; D" A$ ^; b2 _8 f% s5 Y( G( o System.err.println("Exception updateLattice: " + e.getMessage ()); S- H5 b* a1 F+ c. h% `3 Y& Z! A
}5 e; A4 k/ S2 V4 u) t
6 j A! r* ^0 E+ y4 c
// Then we create a schedule that executes the
3 d0 W$ S' i/ |. z // modelActions. modelActions is an ActionGroup, by itself it' p0 D5 ?, [: ~! d
// has no notion of time. In order to have it executed in9 d9 ~3 A! b" _- ?! m; P0 E) E
// time, we create a Schedule that says to use the% o; s9 x& s( ]; y% J( R! u0 u& n- T
// modelActions ActionGroup at particular times. This
' Y6 E Y( r, l- a- \; @- R$ h( e // schedule has a repeat interval of 1, it will loop every( H" V1 y0 s7 t% a: ?
// time step. The action is executed at time 0 relative to
# C- A! G* p. o' ]7 t- j // the beginning of the loop.
) y5 y( p) s Q' h6 I1 S# {4 C# Z# U5 T% K9 z
// This is a simple schedule, with only one action that is
% `& P, ~, ?0 n. }+ _2 v* Q // just repeated every time. See jmousetrap for more6 k( W: j! B) S* Y& Z. Y
// complicated schedules.9 R3 a5 q: _) v' [
+ D6 {$ `, j7 L. `+ t4 J modelSchedule = new ScheduleImpl (getZone (), 1);
/ X8 @2 X9 n2 _$ P b) A modelSchedule.at$createAction (0, modelActions);! {5 g& d7 K, u6 V, E0 ~
6 M2 g5 V! O$ L! p$ I5 i1 ]9 Y6 r9 } return this;
1 j# j$ q( z/ ]% G* d } |