HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& w. s r+ c) A' u7 u6 Z
2 s3 ?1 I* T1 W2 C public Object buildActions () {
\" O/ ` B7 C super.buildActions();
; ^. e, m3 P( [9 n# S6 v& f2 i$ x
% k* ~; O$ [8 }9 P4 m* v% v // Create the list of simulation actions. We put these in
6 x; u# b% w9 X+ G // an action group, because we want these actions to be* z3 C& I2 R. o( J8 |: S& A4 |
// executed in a specific order, but these steps should
" N, g* \* W3 n. U // take no (simulated) time. The M(foo) means "The message
! Z! e: h2 {$ A4 K: E // called <foo>". You can send a message To a particular
# `# r7 S0 ]& D. X // object, or ForEach object in a collection.
7 s$ F/ @: Z1 ]! } 5 s; u& f2 ?4 ]' Z. T* G
// Note we update the heatspace in two phases: first run- o4 h) i, N/ ]4 w3 Q9 A
// diffusion, then run "updateWorld" to actually enact the' A k! G% v/ N, n! q7 _
// changes the heatbugs have made. The ordering here is4 p' K- K; o' }; b$ l; \
// significant! d, Y2 U7 w$ r4 U. Z% M
# x( T ~/ ]7 ~1 s$ H: H/ F: x // Note also, that with the additional# a$ y0 W; x) O0 k# P$ Y
// `randomizeHeatbugUpdateOrder' Boolean flag we can
7 H3 d. Q: ~; q t2 M; T // randomize the order in which the bugs actually run
/ m% x7 ^* j7 F- ` // their step rule. This has the effect of removing any0 d$ i3 Y3 C" e% g# v
// systematic bias in the iteration throught the heatbug; M+ M0 ?) r, O6 b% J: O3 v& z$ P# A5 ?
// list from timestep to timestep6 Q, p c8 y0 W" o
u. n! S4 S3 p5 w! P$ R% h // By default, all `createActionForEach' modelActions have% X+ E! A1 e) V4 }5 X, K
// a default order of `Sequential', which means that the8 f9 S1 [+ a# M0 ^, u' p7 a0 R
// order of iteration through the `heatbugList' will be6 V2 C9 |: R7 i0 b$ C& D
// identical (assuming the list order is not changed
( q5 k" m; X9 E" v // indirectly by some other process).. w! s, c- [7 {2 p* s
: B2 Q0 ~. {5 O# }( x modelActions = new ActionGroupImpl (getZone ());* h. F2 W) _5 K0 L# K5 u
+ B3 f. q5 S/ }$ P try {
G# R+ `3 T, N8 w! f/ `: d( c modelActions.createActionTo$message! ~. }. E# b5 I- F" H
(heat, new Selector (heat.getClass (), "stepRule", false));$ D+ Y) Z+ m3 o+ r; h, \
} catch (Exception e) {
+ X' ]( [$ E" x" W2 R% d# \ System.err.println ("Exception stepRule: " + e.getMessage ());( }9 z9 O8 p- `9 U
}% X; T2 U- [5 L+ P
h! E/ a9 g, K2 H/ q
try { ~2 j4 v% e) S$ v/ y9 E0 J
Heatbug proto = (Heatbug) heatbugList.get (0);
# @+ @' P; V; I. t6 s7 w Selector sel = ) Q4 g5 [5 L- h, O8 G5 T
new Selector (proto.getClass (), "heatbugStep", false);2 y: i' t3 v) \+ v5 d" Z2 v' `& v
actionForEach =! c# w, k0 D5 [ L! S6 W% \
modelActions.createFActionForEachHomogeneous$call% Y3 \( ?3 ` E* ]7 l
(heatbugList,
' O8 p0 `) n ?4 H new FCallImpl (this, proto, sel,
& {, l( J5 w( T: } new FArgumentsImpl (this, sel)));
/ h: m+ S1 B! u" D } catch (Exception e) {' D, d# Q1 k" M% f' c
e.printStackTrace (System.err);
0 Y" x6 J" T( ~ }
/ T3 @0 I: k! K' u4 j
' W7 j$ n$ d& A4 K syncUpdateOrder ();+ x( w w% ?% O$ ]
2 o1 m& y% ^" D9 D
try {
* a: i$ [( `" Y) Y6 z2 M( i" j modelActions.createActionTo$message + k. I# y0 _2 _+ Z
(heat, new Selector (heat.getClass (), "updateLattice", false));
* [1 P- [5 C7 b k- U. m7 _ } catch (Exception e) {: u9 a" ^% d2 V, V4 h, p7 i
System.err.println("Exception updateLattice: " + e.getMessage ());- i0 ~. z, ^9 m
}6 _. R: }9 e( {& B% b" x' Z, h# `
# k. D; _& {9 U M7 R // Then we create a schedule that executes the R+ m3 C. ?! @% i; ~' |2 A
// modelActions. modelActions is an ActionGroup, by itself it5 G T$ r& p3 p& p, {+ E: A7 H
// has no notion of time. In order to have it executed in
0 H- u5 E* S$ ?1 m. S- t // time, we create a Schedule that says to use the
' N' k1 S. i+ p. c- | f) E // modelActions ActionGroup at particular times. This' L1 z4 e7 y3 }$ R) V# C" O! N
// schedule has a repeat interval of 1, it will loop every: o% X6 a1 z1 E/ z# T& J& r4 i) T/ n
// time step. The action is executed at time 0 relative to
1 b( _* T/ _7 { // the beginning of the loop.
, Z, [2 z% m7 H' s3 H, K! d$ f! q$ d. t P: S
// This is a simple schedule, with only one action that is! ~9 y/ w/ p# A9 v( o% h
// just repeated every time. See jmousetrap for more
; }5 l) a" x) G/ ]: o% H // complicated schedules.8 [' W8 k' x3 L+ u* P3 h
2 o; S) [' Y" V4 T ?$ l2 {7 } modelSchedule = new ScheduleImpl (getZone (), 1);- t5 X. L8 j1 U" J- e; d
modelSchedule.at$createAction (0, modelActions);' v& M' [5 ^3 d
& `7 W! o5 ?' d. m" g9 P9 X
return this;# W% }& q2 O' T( {$ ^4 q% p/ X- q
} |