HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! G6 K/ i$ ~1 D a! ^, |0 I+ C" i6 E
public Object buildActions () {; A8 d" I* P* l; K. F5 d5 S( K
super.buildActions();4 l& }" L) s5 o! Q; x* H \
8 a# X* ?5 K) q
// Create the list of simulation actions. We put these in0 G+ _2 r% x# `5 b' ]; ]$ z
// an action group, because we want these actions to be
7 G* S) x0 `3 S; o$ w0 c- F // executed in a specific order, but these steps should
2 ]& J5 k9 T0 p& ^ // take no (simulated) time. The M(foo) means "The message$ k. ?* x- l% G/ r2 E+ n4 j' g
// called <foo>". You can send a message To a particular w% `! V# ?1 L9 r$ X
// object, or ForEach object in a collection.
# t2 H N, f8 i! v
9 a: ~6 w7 m% y3 ? // Note we update the heatspace in two phases: first run
1 }/ {1 W5 n& T, P8 M U // diffusion, then run "updateWorld" to actually enact the
; ~# U3 z! W, V4 F3 v2 [/ e // changes the heatbugs have made. The ordering here is
/ Z! B, q4 S" r7 w$ v# @) x // significant!: U% o7 _, r& `0 l
! u n6 u+ s% s9 \7 s0 P/ s3 V // Note also, that with the additional
|2 _1 ^7 v* f // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 k; P' E; e- V5 H+ S# T // randomize the order in which the bugs actually run& j6 G$ b. \. [& m+ x5 A
// their step rule. This has the effect of removing any
0 x# L: r6 ]6 ^. t3 t // systematic bias in the iteration throught the heatbug
/ a+ u; H& b; ^# Q/ h- u // list from timestep to timestep' j# z* h4 [5 t1 g
& e6 d: Y& _" Q! E( J( A2 G2 b
// By default, all `createActionForEach' modelActions have# u; v. a6 v0 w5 ~- F& f! s4 c
// a default order of `Sequential', which means that the
% k# N/ `( a+ n$ o4 {' L5 w // order of iteration through the `heatbugList' will be& C+ i' k8 ]' e9 @
// identical (assuming the list order is not changed
2 b/ r5 W* Z3 f& k1 W* t // indirectly by some other process).
K. `* B. V; j1 Q: m" }; b, b & }$ X0 ~$ G% y) j6 y f/ P$ Q
modelActions = new ActionGroupImpl (getZone ());: Y0 _9 Z! V2 y r- x
7 v2 I1 |! D) [* M# R4 I& F, d
try {
+ ?: Q7 o2 s8 h6 Z modelActions.createActionTo$message
& t# @' Z4 l6 u$ ^. T' [1 [1 S I9 h (heat, new Selector (heat.getClass (), "stepRule", false));' c5 }$ f' a5 G* t
} catch (Exception e) { ^* k9 E$ s2 z. L# n5 c5 D' W
System.err.println ("Exception stepRule: " + e.getMessage ());
4 p; Y9 i$ V( {8 A }( s9 K5 D/ R9 k
: q) K4 D) M& A
try {, Z3 H' U8 J% C* o E
Heatbug proto = (Heatbug) heatbugList.get (0);; Z+ R) O7 o; |0 z
Selector sel =
! s/ ~: ^, ~' h5 [8 ^ new Selector (proto.getClass (), "heatbugStep", false);1 T) c9 i- ]* r6 n4 f- r" A! D
actionForEach =
( G* P! o3 S" V' ^0 Q J; l modelActions.createFActionForEachHomogeneous$call" F5 H* p% X- o: E+ \9 Q3 W* X- w
(heatbugList,
1 ]. m' b. t* n6 h5 ]! @ new FCallImpl (this, proto, sel,7 B8 f9 W* i* c) R& Q1 u4 o
new FArgumentsImpl (this, sel)));: z g% { a7 D( C
} catch (Exception e) {
: t0 J5 G+ R: h# K3 Z, w' B& a( P e.printStackTrace (System.err);# n. P# N4 G% J) t" Y/ `2 p* D
}
, h; x: |4 N$ |% E* n * G" K- B) F& D/ A- i* d' a1 A
syncUpdateOrder ();% l6 x4 X. K7 e+ V: i @! w* z9 i
; U- Z) p' p: R# h9 N# Z try {
; Q: P8 j% E6 f4 x' [ modelActions.createActionTo$message + p! X5 Z! B' @0 f
(heat, new Selector (heat.getClass (), "updateLattice", false));$ J& N% G" T: F& _
} catch (Exception e) {
/ m3 v( G4 M& M# B v6 N4 D System.err.println("Exception updateLattice: " + e.getMessage ());1 O0 H# q+ k5 \7 \) |/ f
}
0 g/ u: M0 B7 h- p+ u
9 D$ P. Z4 K$ Z) w3 A0 R6 Y // Then we create a schedule that executes the- l- k; T$ c9 T
// modelActions. modelActions is an ActionGroup, by itself it
: ]. j3 F8 O) C // has no notion of time. In order to have it executed in
* k, l; a9 G$ v, U$ _ // time, we create a Schedule that says to use the
% ?6 z& a. s' \8 k1 h( m6 O# j // modelActions ActionGroup at particular times. This& b3 e/ h! `( C' R1 X2 v9 n" o2 x
// schedule has a repeat interval of 1, it will loop every
1 @ s! Y1 R* t9 Q& {+ C+ V // time step. The action is executed at time 0 relative to0 K% B7 J' x, A) ~- Z9 k/ x2 w0 A/ d, k
// the beginning of the loop.6 N% g8 @4 I Z* ?; i, @+ d
Z/ {* M! j7 n& W$ V1 B8 z( V* ]8 o // This is a simple schedule, with only one action that is
( _ ^. i( S; N4 ^ // just repeated every time. See jmousetrap for more
" p* }# u3 r5 @: { // complicated schedules.
w! C7 E1 f% D/ H, ^5 n l7 |& Z$ d) S; c
modelSchedule = new ScheduleImpl (getZone (), 1);
. n U8 I& t# K) x5 [2 S* L; E modelSchedule.at$createAction (0, modelActions);
: D0 {5 g2 i3 Q3 V% G* {+ G : ^' R6 O* y7 E. L# f" t! G
return this;3 u% h# F1 S, g# R4 I" Z
} |