HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* A* e6 A* l m1 [2 G0 a. M; _
2 O# ~/ t6 a5 b! [1 Y" B9 i public Object buildActions () {: S6 c% ~; q; W1 `8 s9 [: x: J
super.buildActions();/ A$ R7 u; v4 Q# h) T% g I
3 B0 z" a. ^) k. ]/ o2 m9 ?# B
// Create the list of simulation actions. We put these in- m' g# u5 S5 Q0 m5 [: z2 G
// an action group, because we want these actions to be
4 c ~* r0 X* }# F5 A" H // executed in a specific order, but these steps should# n. d! T: h7 ^) F2 k! a2 Q
// take no (simulated) time. The M(foo) means "The message
3 D7 U( K& y0 j# v' D2 V // called <foo>". You can send a message To a particular+ b+ h' Z- A/ F2 H7 K" c
// object, or ForEach object in a collection.% N1 N- L0 F7 e! e6 q9 t) t
; m+ G7 e. U& D' B* V
// Note we update the heatspace in two phases: first run
) Y0 K" }8 q0 f! X+ h2 A // diffusion, then run "updateWorld" to actually enact the& X. z o) o& h3 R- L' M6 \3 M+ M: S- E9 v
// changes the heatbugs have made. The ordering here is( l3 u( W2 \2 B: o0 g
// significant!
4 u1 @0 C! w+ U x# f1 m+ L
! P7 g) m! l) [# v // Note also, that with the additional( p. {; u8 A+ Q5 d! u4 o) O
// `randomizeHeatbugUpdateOrder' Boolean flag we can# H( u5 Y" q) T, Q1 q
// randomize the order in which the bugs actually run. H4 {: ?2 e" ^8 v: e3 j7 U% Y* m2 U
// their step rule. This has the effect of removing any
4 g E$ V: Q4 ?5 o/ J. _ // systematic bias in the iteration throught the heatbug
% E9 w, y6 s% \, F1 \+ L, V4 a // list from timestep to timestep
$ e( G$ f l: {. _& G0 e
9 x1 q8 j/ r: b, J- N O // By default, all `createActionForEach' modelActions have
, P, e8 O+ H. [/ W // a default order of `Sequential', which means that the
& J% j- G. ]. [5 {4 {" f- _0 _ // order of iteration through the `heatbugList' will be9 O' K; j* j- X9 n
// identical (assuming the list order is not changed
j8 b3 D4 L/ h0 G8 Y. V" K" ? // indirectly by some other process).1 u' A+ c" V$ ~( p7 w$ U$ ^
# f$ R6 f& c: z" D4 E5 R
modelActions = new ActionGroupImpl (getZone ());; p( s% J8 e5 ]3 B$ q+ y
. u* T G+ Y/ R- C( Q' B
try {' i- ?7 K6 q% }* C+ p7 Z
modelActions.createActionTo$message- I( |6 A `7 v8 o: e- @
(heat, new Selector (heat.getClass (), "stepRule", false));
7 t9 \( A% W( i+ B' r } catch (Exception e) {
B2 z0 V" @6 G7 i System.err.println ("Exception stepRule: " + e.getMessage ());
. X* Q8 v. @2 k& S }% e, `" d4 W8 C9 F: {; z, z) y
6 n7 d; w0 M( i7 U try {
. x/ I& t& X' B. c# e/ `, s3 x Heatbug proto = (Heatbug) heatbugList.get (0);6 v' w# p" _% c( S
Selector sel = , k* b. e% X- n4 F. N) W, v9 ~6 _
new Selector (proto.getClass (), "heatbugStep", false);. p- o$ M% {' D
actionForEach =. P! X3 V5 C- u/ G$ T& s
modelActions.createFActionForEachHomogeneous$call: y) M1 m; ~2 g# g; p% W
(heatbugList,7 S, X3 `7 ?# d/ y
new FCallImpl (this, proto, sel,
9 N# E0 m3 V" M& }! L new FArgumentsImpl (this, sel)));' V9 S \2 p4 [2 K
} catch (Exception e) {/ c- x; Z5 S% J# v
e.printStackTrace (System.err);9 n5 ]/ {2 b& G! U& Y7 x
}: Q$ p( x5 W# a5 d+ ^
~1 m. o, X* w; k) P
syncUpdateOrder ();
5 {9 p8 q* k: z& T) W; n" z. @* C9 ?' w4 {6 k; n7 N. M0 s# A
try {: Z' S, w1 w/ O3 i: }
modelActions.createActionTo$message
& ^9 D! P+ V! z (heat, new Selector (heat.getClass (), "updateLattice", false));# }& Y4 _# c& ^6 _$ }
} catch (Exception e) {$ e% d7 r7 j5 L" \; p5 q1 N
System.err.println("Exception updateLattice: " + e.getMessage ());
6 ~% S2 o% c( J' V }* K3 g0 W. x7 _$ r! s1 }
0 j1 c3 m$ q0 n, }% R8 t
// Then we create a schedule that executes the
* d# j4 t" M& L- R // modelActions. modelActions is an ActionGroup, by itself it9 O$ h0 v0 v: w! C
// has no notion of time. In order to have it executed in, e5 E V8 w9 h' v8 c% w1 B) L* P
// time, we create a Schedule that says to use the! R4 H/ @3 G. u$ [; A4 q3 [! {
// modelActions ActionGroup at particular times. This
$ c5 Z& N$ S& [9 |7 ` // schedule has a repeat interval of 1, it will loop every
/ W0 t- \ }, B8 L // time step. The action is executed at time 0 relative to- @% m% k5 @6 C d( K
// the beginning of the loop.% z$ J7 X( p" K# U, c) s
& _/ H9 Q, w. e& B
// This is a simple schedule, with only one action that is' r. R( C: f/ B: |/ L' v
// just repeated every time. See jmousetrap for more( Q& k, W! A/ _& c4 o. S3 K
// complicated schedules.9 A. [! F! u. f0 \* r1 m6 n
3 L( G/ {+ S; J3 R) J modelSchedule = new ScheduleImpl (getZone (), 1);
' r% b; F0 R1 A modelSchedule.at$createAction (0, modelActions);/ ~$ f9 A% M3 _
! C+ P- s7 x+ m0 g
return this;
% d6 O* O" `/ o3 D; u } |