HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ j; w! N& j! p: ^6 O4 k( _
8 {+ k' A3 y7 t! n public Object buildActions () {. q6 z# H0 w( [* n
super.buildActions();1 W# G& A, \; o& q( s$ X
; a8 n4 W3 p3 W3 v- n5 ^5 n
// Create the list of simulation actions. We put these in$ @7 g% w( v" H6 q! w" j2 p% [
// an action group, because we want these actions to be
% Y! r% |! O0 d // executed in a specific order, but these steps should
, R: c, o, W8 L8 ] // take no (simulated) time. The M(foo) means "The message
' ^# U* ] o! V" X0 {0 B; J! M( D // called <foo>". You can send a message To a particular" \9 {, e) u/ n. ~- V) E+ V
// object, or ForEach object in a collection." \, K& p# w% x+ B' Z- o" r$ X
) d- l0 l V& X
// Note we update the heatspace in two phases: first run
4 J" T& t3 z5 l5 v p/ ~ // diffusion, then run "updateWorld" to actually enact the' l- y( y& Y7 u4 a2 j
// changes the heatbugs have made. The ordering here is( M! C8 P$ p7 ]( g$ Q: N
// significant!2 d4 a F( N/ a3 J, ]! L
; l: O5 k/ L1 X1 U4 @ // Note also, that with the additional/ x1 t3 X$ o% W) R* k* q
// `randomizeHeatbugUpdateOrder' Boolean flag we can; G: ^4 } [6 e: X# s% o2 L
// randomize the order in which the bugs actually run
% k! U$ l& U! a2 `7 H // their step rule. This has the effect of removing any; r2 s/ W- p9 N! v* K* ?
// systematic bias in the iteration throught the heatbug
4 \) R6 i* t$ k; I* A" l( z6 O- C& l // list from timestep to timestep
! s- {' s1 A! `( ?' V0 h5 u8 m0 S
1 K8 a; _4 t7 v/ f) m5 K // By default, all `createActionForEach' modelActions have0 \5 |- c5 T1 i1 y2 ^
// a default order of `Sequential', which means that the
7 J% C& l: a/ s8 F: o- M // order of iteration through the `heatbugList' will be
5 k) x& n# W* X // identical (assuming the list order is not changed7 [8 Z3 m- \' a, [+ F8 N# m1 }+ _- f
// indirectly by some other process).
6 E0 x/ j) S# _8 s6 A
1 w- m8 g/ m1 U2 \" ^$ n& O0 O; N+ } modelActions = new ActionGroupImpl (getZone ());8 P. w% o3 t- J5 l$ c5 ^/ c
- ?9 b3 S' ~8 B: U try {
. V( {: m- x* ?2 Z modelActions.createActionTo$message
& X6 D8 ?" T2 C l6 z (heat, new Selector (heat.getClass (), "stepRule", false));3 @6 F6 b7 v1 U; Y/ J* V$ d- d# k" ]
} catch (Exception e) {7 W/ M9 {' y* }
System.err.println ("Exception stepRule: " + e.getMessage ());% ? U2 p9 x! o( I) ^) X( |8 y
}
. T# s. q- ?$ C c& J; Z `2 b, y* P+ C5 y$ X
try {' r+ }; e+ \. H- c; a0 S% \* ?6 J
Heatbug proto = (Heatbug) heatbugList.get (0);
0 v0 V' ~3 n& d Selector sel =
" S4 z; }# [# \: q; } new Selector (proto.getClass (), "heatbugStep", false);
1 s- q* B, m( @9 s F' Y actionForEach =" Q7 O. G5 z- l" Q
modelActions.createFActionForEachHomogeneous$call' ~: B8 ]* s$ f5 ]8 _
(heatbugList,# R% {" Q: {' ?2 H) l- k
new FCallImpl (this, proto, sel,8 ?& x7 J5 g7 i0 c- _( ?0 @6 K
new FArgumentsImpl (this, sel)));
f# p1 r' U1 f+ P5 \ } catch (Exception e) {
) t5 Y5 P8 Q( w& g) k$ f( z/ _ e.printStackTrace (System.err);
, x3 x$ R6 \/ k5 n2 N8 @* X. y }5 B- i8 r# p) o( ~
5 I9 M2 `/ a6 O, X- @4 y syncUpdateOrder ();
3 s7 a. M+ k+ v1 M/ C. t$ F$ n1 m$ @4 @
try {; |0 {' ^5 O7 U7 V
modelActions.createActionTo$message , K5 Z3 x% Q! |2 r* h& f0 b
(heat, new Selector (heat.getClass (), "updateLattice", false));! R. |. F( H; j: ]; A8 k
} catch (Exception e) {
* {& i- _7 V0 l# a System.err.println("Exception updateLattice: " + e.getMessage ());# v* t2 r p7 y
}
8 ^: u4 b8 j5 I7 [) F' c" |
: D5 L3 F! ]; {7 v9 u) o+ n [2 r% u // Then we create a schedule that executes the
" r$ `; C, }. q" Y' T9 R8 a // modelActions. modelActions is an ActionGroup, by itself it! ]# @ {+ e8 a# [3 |
// has no notion of time. In order to have it executed in
: l) \2 G9 f3 D; T // time, we create a Schedule that says to use the% m( J; S. ^, P+ ?' l& a/ A
// modelActions ActionGroup at particular times. This3 |# {4 n* a$ s* U
// schedule has a repeat interval of 1, it will loop every
+ Z9 H2 S* K$ P: V) |; r/ c: L // time step. The action is executed at time 0 relative to
2 g/ g% U/ n- A, D // the beginning of the loop.1 v# I5 o9 Z& \* y! Q( T7 j3 l- B; x, D
7 {: p* W* X7 J) j5 F1 H" J
// This is a simple schedule, with only one action that is
! w* X$ D: f, a; s/ x // just repeated every time. See jmousetrap for more4 n" G9 U) x1 U1 G' T; q
// complicated schedules.8 ?! _3 Y! _1 y' \/ G
9 k- Q% ^. F0 ~0 }4 c modelSchedule = new ScheduleImpl (getZone (), 1);. l( X9 {/ W4 `
modelSchedule.at$createAction (0, modelActions);) U8 W. ~1 d6 x
0 {% q2 t! |( I3 C, i
return this;" G2 _7 ~/ _. K6 B6 D
} |