HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' ?% Y& W- O+ `% m! u$ ^9 w) s" ~
+ V6 X ], B- e8 w$ D public Object buildActions () {
: o! V( u$ {, h0 q super.buildActions();# n) y5 Q0 s& p9 D
1 @, m+ j; J5 h F, F$ L3 }' Q // Create the list of simulation actions. We put these in
1 P& v' V3 \/ }% t1 ]& C // an action group, because we want these actions to be% B5 r" a; f5 m u# i ?& a
// executed in a specific order, but these steps should9 t- a" J. U. A2 F) l/ i5 d
// take no (simulated) time. The M(foo) means "The message
% R4 L4 G; s' {. c/ L, u9 o* U // called <foo>". You can send a message To a particular1 V7 s3 v! V& ?) c' P1 x
// object, or ForEach object in a collection.( K+ g9 ^* s! u* [+ P2 Y
" }7 d, n/ F2 Y* L3 r/ Z1 m& N! r( T
// Note we update the heatspace in two phases: first run2 i7 N: K( D0 M. N
// diffusion, then run "updateWorld" to actually enact the: _$ D8 S) y5 v2 t$ y6 r
// changes the heatbugs have made. The ordering here is
" X2 ^/ M9 V" J1 R // significant!5 r' x7 c5 W3 M+ g0 @+ ~
% z; P+ {0 {1 ?. X // Note also, that with the additional6 \2 c) t: _7 O- g; t* c
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( a7 w5 A8 @8 [: A$ z // randomize the order in which the bugs actually run# j; K/ M7 o0 V1 \3 v- n
// their step rule. This has the effect of removing any: d0 C/ ]( Z8 g" Y# ?. ?8 ?/ E8 K1 @ [
// systematic bias in the iteration throught the heatbug2 e) a2 E' m$ @( {* b
// list from timestep to timestep+ t* M0 g' H) W R8 [
; Q# ]/ q7 L9 p* w4 E) w5 n
// By default, all `createActionForEach' modelActions have, @) d( n* S4 a2 u0 h2 | n
// a default order of `Sequential', which means that the
1 z: G/ P( ?/ S2 | ]: ] // order of iteration through the `heatbugList' will be/ l, |# C% A, C5 @2 z/ l
// identical (assuming the list order is not changed) P' z: g" B# E* Q
// indirectly by some other process).. _+ R9 O1 {0 W5 P5 V1 V2 s) f
9 A" S5 a( z; { modelActions = new ActionGroupImpl (getZone ());
& x+ J5 p! s; H" c$ u5 m
; n1 m2 a7 i- Y5 g7 u try { h! n) h/ _& K/ z2 J" o6 d
modelActions.createActionTo$message
. R' j0 ?8 c, v q: t( Z (heat, new Selector (heat.getClass (), "stepRule", false));
/ o; T, R# `6 X t; C n j, D } catch (Exception e) {
* @9 s! ?2 `. O# {, c$ V2 { O System.err.println ("Exception stepRule: " + e.getMessage ());
( P4 S& O/ |% Y* t6 P: T } s2 d2 J( ]3 C: z
2 c0 T6 y' m5 V
try {
3 f- ]. g. n, S, j' X Heatbug proto = (Heatbug) heatbugList.get (0);
, m7 D' ?! o2 T# x- p5 a9 ~ Selector sel = 9 o) n% A, f* w9 c9 B) Z
new Selector (proto.getClass (), "heatbugStep", false);" Z4 T7 S3 D. V) }3 w5 J& m
actionForEach =8 \* {( p0 u0 \' D0 R) K0 C; h" w
modelActions.createFActionForEachHomogeneous$call
* {& v( f5 e, Q7 K/ w' p& ] (heatbugList,; L( X0 s/ |( D7 P4 R) v4 {+ ?
new FCallImpl (this, proto, sel,
% ^0 h) h. O% p, a" f A; q new FArgumentsImpl (this, sel)));
# q. `6 j, B: l+ V) Z } catch (Exception e) {. s: f$ p. P4 E! _; K3 O0 I9 u( [# D
e.printStackTrace (System.err); u6 n# ?) ~! l( _4 M; M% A# V
}
9 m# P1 k5 a" Y- _ M' M) C" \
- M5 E* C; b& D6 O( R syncUpdateOrder ();/ N& K! N) {/ k: `
x6 I/ x7 q6 |7 ?0 ~ try {8 X; g; ]0 m* s6 B; B5 M
modelActions.createActionTo$message
0 c9 l0 h/ [ s/ |3 ^5 T (heat, new Selector (heat.getClass (), "updateLattice", false));# q2 l9 I/ [9 {! `# o
} catch (Exception e) {
- @, A. Z4 z O* w! d' d, [ System.err.println("Exception updateLattice: " + e.getMessage ());& b( U2 Z3 m5 [; ~) i
}
1 v$ [; \5 G1 u% D: {6 g
) y( [$ k- [" f! O // Then we create a schedule that executes the( j8 [# U2 [: M! R2 {' D7 K( S; N
// modelActions. modelActions is an ActionGroup, by itself it! t( f t& O ?# Q t# ~& _
// has no notion of time. In order to have it executed in
6 [/ }; v4 ^0 J // time, we create a Schedule that says to use the4 b- \' _) Z4 l' d; s( G* z! ~
// modelActions ActionGroup at particular times. This
; W5 C$ M! O: Q) T4 Y# r/ u5 w // schedule has a repeat interval of 1, it will loop every. M2 t# ?2 h6 g- |% Q
// time step. The action is executed at time 0 relative to+ K8 n: E/ B" X$ ?, f
// the beginning of the loop.
/ x8 m- Q, |. R1 s* O* T) T" j! d
// This is a simple schedule, with only one action that is6 \- M7 b$ ]" F& }; ^
// just repeated every time. See jmousetrap for more1 h* h& S1 f/ H: Y
// complicated schedules.
) c) [6 z8 p* b" }' C # k5 {! ~& }, \! p7 q7 M k' O/ r6 {
modelSchedule = new ScheduleImpl (getZone (), 1);
9 ~3 F4 M a* V; U5 u) P$ u6 M modelSchedule.at$createAction (0, modelActions);
! W0 x: W( n+ i5 k& a/ |
8 O, V; f( z" a5 s9 q9 ^% g return this;/ E+ V6 l+ k2 Q |# g$ K4 O7 |
} |