HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 U& N& I4 _8 B4 g8 v8 _
1 b$ @% H: P: I$ l( r$ V2 ^- p
public Object buildActions () {6 T0 P6 [) P& H" B& z
super.buildActions();
" q0 N) b1 J$ Y! H; m8 l 9 V5 v" o3 [. ~0 U# k
// Create the list of simulation actions. We put these in
8 H2 g% {" c0 K // an action group, because we want these actions to be; L6 V$ K7 o. l# }8 d3 T4 R
// executed in a specific order, but these steps should
$ E' W$ [' ? u9 h+ a- }* W( g; J a( Y // take no (simulated) time. The M(foo) means "The message
3 B7 W$ t, `; p8 I# l // called <foo>". You can send a message To a particular
) P0 i( }0 v# s; D' D2 o4 E // object, or ForEach object in a collection.4 L1 t. l i8 ~! |
( _- O, M, ?& I3 g6 ~ // Note we update the heatspace in two phases: first run3 G+ U. `9 o0 f! ^0 w( I2 Q. e
// diffusion, then run "updateWorld" to actually enact the' B' k4 d6 D: Y- W Z$ O
// changes the heatbugs have made. The ordering here is
+ W1 H0 s5 ~$ C' B9 R) r; B) u // significant!
; u ]3 e9 `4 }" Q' O( O) e, h
3 N& E) o" ?+ A2 p& b // Note also, that with the additional
, I# Q3 ]- s' T3 I T // `randomizeHeatbugUpdateOrder' Boolean flag we can
; K6 s9 L8 G. c$ e4 d7 [0 ? // randomize the order in which the bugs actually run8 a e r/ ~! w0 M, k
// their step rule. This has the effect of removing any
0 @- t5 n/ d' w# ?4 a4 R$ H0 N1 g1 m // systematic bias in the iteration throught the heatbug
5 F P; R' ?, G* V: { // list from timestep to timestep3 k- u1 \5 z5 x
, r8 G3 N8 Y w; t |
// By default, all `createActionForEach' modelActions have9 D! c1 A5 u! }' Z+ z1 j1 x3 q' b
// a default order of `Sequential', which means that the9 }" x, g$ E }9 {/ s- ~* U
// order of iteration through the `heatbugList' will be
) D- A! j2 k7 i- B! S$ Z // identical (assuming the list order is not changed
0 v& f- y- W& e0 I. U // indirectly by some other process).
; J0 a- K+ g i& p
& t) q, J/ R5 M, } modelActions = new ActionGroupImpl (getZone ());
: b& L* v2 O- b, j' P/ `" r+ y! | V# L0 t
try {1 q. e4 ? H: w0 j7 ?( t) \/ Z, U J
modelActions.createActionTo$message5 f, {9 r" d9 m/ s% M1 d
(heat, new Selector (heat.getClass (), "stepRule", false));
$ }$ R6 `6 g1 q) G" f# }. h) L } catch (Exception e) {
! l; n* O/ M- t0 i9 A; w; G System.err.println ("Exception stepRule: " + e.getMessage ());* h1 |7 O6 h5 K/ {4 O. G- p
}
" r* P8 F0 }4 N9 R1 r2 G( E& D* ~1 J9 j) T F
try {
9 a: P1 l& R- B' \ Heatbug proto = (Heatbug) heatbugList.get (0);1 U" i+ e4 N* ^) ^
Selector sel = ) Q# B9 F; d1 ^2 c- F2 z
new Selector (proto.getClass (), "heatbugStep", false);: }% V) ~! ?* a1 {; y5 o8 l" ^& O
actionForEach =
- T- a9 ^& L1 i, g5 \" |0 W modelActions.createFActionForEachHomogeneous$call
" Q& D+ R1 j* W3 j6 x% y (heatbugList,
+ C5 w* Q; j6 `8 ?6 K new FCallImpl (this, proto, sel,
' p& L3 I0 O3 v* W1 V7 q new FArgumentsImpl (this, sel)));* d# n$ @, Q, G3 |% C" a9 C) q
} catch (Exception e) {2 N9 Z, ] L* z0 `- M6 c+ G
e.printStackTrace (System.err);6 c, F& e/ g P* ]; T0 P
}
Q: L" G1 O. F6 E
+ o. u" J+ Z& d; B w syncUpdateOrder ();
, X6 D2 s: V; M, }
) i/ _- }9 M$ r- @ try {
/ x0 G9 g1 P5 |1 a+ p# ~6 `- k modelActions.createActionTo$message & P! G0 b/ C' A+ K. M; _. i
(heat, new Selector (heat.getClass (), "updateLattice", false));* n' w; \' r/ {1 C
} catch (Exception e) {! {! z6 `* C, \$ I
System.err.println("Exception updateLattice: " + e.getMessage ());
) C8 m }& L$ i+ j) Z0 ^ }
# c" y% g# x( o# }, i, G 8 M& Z; _) {, k
// Then we create a schedule that executes the
# x( \! O3 }) Q0 ^0 o // modelActions. modelActions is an ActionGroup, by itself it* a" X" h+ z1 y
// has no notion of time. In order to have it executed in
. B9 g! w% Q6 r4 k. F // time, we create a Schedule that says to use the/ u% Q9 `' d& K$ _* ]. `; q. D
// modelActions ActionGroup at particular times. This
+ Q* E4 _: f$ a3 k9 Q // schedule has a repeat interval of 1, it will loop every+ K+ H5 s' ^, ]. u
// time step. The action is executed at time 0 relative to( I' A& n) s* O2 J
// the beginning of the loop.0 K/ z( s) O( M9 t
/ K9 l6 Q% Y/ { // This is a simple schedule, with only one action that is
. O' Q! c) p8 e; r0 k // just repeated every time. See jmousetrap for more! h$ U0 S3 ^ I
// complicated schedules.
: P6 ?9 K% A2 T4 t7 L, N' |" i * q5 u1 i) D0 g6 N% _
modelSchedule = new ScheduleImpl (getZone (), 1);
4 K4 V% b& J( V7 [4 v% N( @ modelSchedule.at$createAction (0, modelActions);
# ?1 X9 F% H8 Z3 h! S# R1 L ! p% ?4 N$ d& k% b, Q4 {# Q
return this;
1 O9 U9 h/ w- c9 w3 s } |