HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: t# c( [5 B& f) E& _# b
- ~- J3 Q- X' y1 \: y: m
public Object buildActions () {
7 }, v; b: [3 N' t; e super.buildActions();" g# T4 U7 u F# W
8 u6 G! ^/ G" V, |# v // Create the list of simulation actions. We put these in& S, q, J; _/ H$ z- b) E9 [) x' l
// an action group, because we want these actions to be$ _* Y( Q4 Q9 N0 Z% c$ |( l
// executed in a specific order, but these steps should6 J5 B( ]. h; O) y7 n. s
// take no (simulated) time. The M(foo) means "The message
. t7 F4 a/ Q8 a+ m ^; u. b // called <foo>". You can send a message To a particular" b5 \: n5 Z) j( T% r2 s B/ H
// object, or ForEach object in a collection.
" q0 h$ ]& E7 l4 k2 Y* M , Q' {* F9 Z( G; |% Z! ?8 s
// Note we update the heatspace in two phases: first run
* l, l* U5 c& m& S: H1 G8 S // diffusion, then run "updateWorld" to actually enact the
. |) y' [) e. J // changes the heatbugs have made. The ordering here is. c v+ ?- r7 A; I1 N& Z
// significant!# q; p, J" R( E& z; P( Y
( e' @$ N# F/ c( X" L // Note also, that with the additional$ T8 `, |% O; P7 p" H# J' c
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* g+ Q& l( p2 P. B. H // randomize the order in which the bugs actually run
( N# A7 Y0 M( }) p+ ^ // their step rule. This has the effect of removing any
3 ]/ }6 v/ x$ Q // systematic bias in the iteration throught the heatbug
/ T+ ]* ]( ~' o" s( w // list from timestep to timestep
' g" @% ]+ l" m% `5 t" p$ A # H" [. s% W3 N3 e) r& S
// By default, all `createActionForEach' modelActions have4 x G* R2 D4 c
// a default order of `Sequential', which means that the
- e2 B0 K/ Q1 q. U // order of iteration through the `heatbugList' will be6 ?* W+ H) s0 {
// identical (assuming the list order is not changed
2 H) ]4 e0 t( d6 W // indirectly by some other process).
& d& R/ }; T2 m d5 K5 z - G8 I5 Q: y8 e; L0 ]& ~) H' T
modelActions = new ActionGroupImpl (getZone ());: u3 n: G- t- h% z" o7 c" v
1 T, F" x' b+ R& ^6 A
try {" g( u( n6 X" s3 D1 D6 L
modelActions.createActionTo$message1 k; P1 h. i8 ?" V' d
(heat, new Selector (heat.getClass (), "stepRule", false));" q6 B. v. b3 T+ s
} catch (Exception e) {; {: G% e' Z, d0 d& Y+ ^9 M5 D
System.err.println ("Exception stepRule: " + e.getMessage ());
+ B& j4 l& j5 Z) s6 n }
1 }/ \6 U X4 t M$ p7 m* J' Q+ R4 \0 I( g1 s
try {
) V9 U) _9 a7 d% r1 e( { Heatbug proto = (Heatbug) heatbugList.get (0);0 s% ]4 u9 \4 M& Q
Selector sel = 8 u! b# c: h# Q4 {* c
new Selector (proto.getClass (), "heatbugStep", false);$ C. c6 |* ? {
actionForEach =7 Y( ^, V, \: @5 O% u- d
modelActions.createFActionForEachHomogeneous$call8 o7 ~, k$ T: {, }+ \
(heatbugList,
' v* Y; F; `0 L8 X4 q* C# U9 N v% t new FCallImpl (this, proto, sel,
6 F3 i2 q' R+ P/ _ new FArgumentsImpl (this, sel)));
F0 X j1 U2 i } catch (Exception e) {
1 O" g* N* H2 l9 o e.printStackTrace (System.err);0 C# N. b% ~/ K% C- }; E; B# A
}4 I* ~" X7 F N7 | r
, m2 \" n4 ^; O: A0 }8 S
syncUpdateOrder ();
; c0 W! m, o- s/ l8 |; _2 o
! g( }( ?2 c2 c- V" R6 A try {
, q6 C* t# G# o: p0 A modelActions.createActionTo$message ' N! N3 @' ?/ b* u% ^$ {6 A
(heat, new Selector (heat.getClass (), "updateLattice", false));
9 r) B1 ^3 J, I' Y+ ?" x* F) J } catch (Exception e) {
" f: q/ C$ H9 f' `+ L( k System.err.println("Exception updateLattice: " + e.getMessage ());" @8 U& B6 l! u. d# O$ x& b
}
) \( ?1 ~ M" K P# e% f/ ^6 D: s 1 W+ R! j9 x2 z) Y% j0 o/ y0 k
// Then we create a schedule that executes the
i z8 J |' t$ Z* i- x // modelActions. modelActions is an ActionGroup, by itself it2 X8 m2 i. [( l3 g- p' F' ?
// has no notion of time. In order to have it executed in6 Y; ]& R: U* q; p
// time, we create a Schedule that says to use the( N) `0 s( e- Q" a; F% l! ^
// modelActions ActionGroup at particular times. This- U8 y+ F- O) G% D. s; k
// schedule has a repeat interval of 1, it will loop every
+ w7 Z# d7 _1 c // time step. The action is executed at time 0 relative to3 B8 Q, U% R3 |: K5 j' q
// the beginning of the loop.9 K7 l2 E5 g4 I
, z( ] y' e2 l, L/ |4 \
// This is a simple schedule, with only one action that is
6 _. i/ ^7 K4 N // just repeated every time. See jmousetrap for more/ h/ \' \+ W9 q$ |- U: |
// complicated schedules.
& g* S6 I! r) g, t 7 a9 r6 m3 s: N7 Q* b2 P& J I- g
modelSchedule = new ScheduleImpl (getZone (), 1);
0 V" [1 ^" D4 ] L+ T" j, ~ modelSchedule.at$createAction (0, modelActions);5 e: e/ A- X0 `# L5 J1 }
+ A3 n% p6 U9 X return this;
6 e. r) C' B) u9 y; v+ m) r } |