HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ F+ n! K3 J/ x, T- D* B2 \! @$ l$ u5 b' Y5 A5 Z4 R9 e0 g2 Z
public Object buildActions () {' f1 v4 y4 e. |
super.buildActions();8 @5 I7 m4 H2 {( z5 y+ K
' M- _- u% j6 }! N
// Create the list of simulation actions. We put these in$ x9 _3 f- [ }9 E. D
// an action group, because we want these actions to be. d5 _, ]4 G d9 q4 S. I) ^! L! \
// executed in a specific order, but these steps should
0 ^% R; X7 v# H; l y // take no (simulated) time. The M(foo) means "The message$ M! j7 C, S$ w+ C3 \9 |# k
// called <foo>". You can send a message To a particular6 m: @+ {( o" P Z) `* Z9 q
// object, or ForEach object in a collection.1 l( G- a. |) ]6 c9 l
/ S2 y3 o3 u4 Y ^' X# m
// Note we update the heatspace in two phases: first run
% |, d: l; `7 t // diffusion, then run "updateWorld" to actually enact the
# J$ B0 N/ E% v/ { // changes the heatbugs have made. The ordering here is8 b9 _+ d, v3 k6 t+ ~- R5 S4 t
// significant!" v( [1 ~! s! O3 Q c' L9 i7 }
& Y1 W- @- _ c9 ]$ K5 r' w; s) c // Note also, that with the additional' M3 A0 S( v$ t" m; y# |3 D4 Q' k
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& g8 X& d! P& A. s* f4 s // randomize the order in which the bugs actually run
# V7 ^( M$ ~3 f/ g9 p // their step rule. This has the effect of removing any, |; r( j9 |$ {* W( J( c' f
// systematic bias in the iteration throught the heatbug7 x0 Y6 H2 K% {* b" R) _
// list from timestep to timestep
* F$ e/ G3 I% A8 L4 ]5 V
/ _0 S8 D% I+ t" x! C- Y // By default, all `createActionForEach' modelActions have7 ^' l+ @0 Z# r+ T) H+ u: d
// a default order of `Sequential', which means that the8 t: k2 S8 c6 K! l. N
// order of iteration through the `heatbugList' will be! W9 H% s$ u: x4 a# c0 y; Z; c
// identical (assuming the list order is not changed
/ s( h: T- u! @/ o* }( d // indirectly by some other process).
2 R- l$ a y$ J; h0 ?/ e- X' N / Z2 c: h' N, |9 [! b( X+ A- `
modelActions = new ActionGroupImpl (getZone ());6 N9 r e% w. q# _6 Z
8 J/ O: }3 o* i. V2 H+ S! S$ V try {
$ ^' _2 v5 M; X' [ l+ G modelActions.createActionTo$message" {4 @/ `; g2 P3 F
(heat, new Selector (heat.getClass (), "stepRule", false));
# D. q0 @5 ?: ^* G* A } catch (Exception e) {5 w8 Q1 c3 _% e) N
System.err.println ("Exception stepRule: " + e.getMessage ());' |) @1 C6 G W4 f
}9 x% Q; B6 |4 z, ^
9 C$ C3 h, ^5 S' [- r+ E X1 G3 k try {: P3 l+ S: q: t1 j# m+ i) W
Heatbug proto = (Heatbug) heatbugList.get (0); G) V3 u. Z) j7 l R
Selector sel = # \: `% T _! ^3 x
new Selector (proto.getClass (), "heatbugStep", false);7 k+ r- U+ b. F9 f- a, H* R
actionForEach =
3 K* W4 e0 W+ [! V9 l: ]; E/ D) h modelActions.createFActionForEachHomogeneous$call2 b' s" F! c- |* g5 ]5 E# d9 r
(heatbugList,) d& u* x! i3 ]. Y' L
new FCallImpl (this, proto, sel,
& X8 W( E9 C/ u8 d new FArgumentsImpl (this, sel)));
$ z0 j; x6 w( M } catch (Exception e) {
: X. A! G' N. ^7 `4 l f e.printStackTrace (System.err);
0 }. |7 v! u) }* t& f }/ V5 I6 e1 Y$ t9 A8 J# B9 V' ~# N
! ^, u8 \$ S* ?) P1 o: y9 _. L
syncUpdateOrder ();
1 g8 ]' v- X. c
9 r ?7 t* D! C( r- V- d try {5 N' C0 D7 v& ~$ `( b# ?& Z8 m" E
modelActions.createActionTo$message
6 I. _' X7 ]4 E (heat, new Selector (heat.getClass (), "updateLattice", false));* m+ v2 N' D- n p |% Q3 s" d n
} catch (Exception e) {
- U7 X) D% Q v; I2 { System.err.println("Exception updateLattice: " + e.getMessage ());8 v/ c; d% v! @8 w$ d& j# l
}
7 S2 j" `4 X& F2 a: G
2 h1 C; d# M/ s$ ?" i // Then we create a schedule that executes the
7 G( p3 i9 @9 ]' e- ?2 Q3 ?' G // modelActions. modelActions is an ActionGroup, by itself it" p4 ?" B) ^7 K1 j, j
// has no notion of time. In order to have it executed in3 Q6 L9 C1 d" }' {. s% Q
// time, we create a Schedule that says to use the" I6 i5 R1 H1 A/ T" `9 q
// modelActions ActionGroup at particular times. This
. D& e* U7 a: U3 x \: R // schedule has a repeat interval of 1, it will loop every
/ ?1 v7 g# N" \( v, w( q* @3 q // time step. The action is executed at time 0 relative to& @" f# }6 e+ [1 ]3 ]: o s
// the beginning of the loop.
4 i! `) t! C2 |" d( H1 i
5 w( T3 j3 M. S1 Q2 ? // This is a simple schedule, with only one action that is
+ [; U5 {+ A, o" Z% n% U // just repeated every time. See jmousetrap for more
+ ]- x5 Z ?: Y' C+ h // complicated schedules.# G! E& {& O) i8 t9 }
& O" @ u; n8 r8 }- [& W* Z modelSchedule = new ScheduleImpl (getZone (), 1);
/ U- ~- `3 B6 c! y2 c' M: ~- S modelSchedule.at$createAction (0, modelActions);! j7 a B' L7 i0 Q3 U8 r( y( [0 L1 h
; d2 |3 q9 Q$ d1 F ~4 I0 z3 [8 K
return this;
4 E6 c1 @4 W# `+ A; Y } |