HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 e) y* u6 s7 z1 s- m
- C5 b9 w0 S0 u3 S public Object buildActions () {
+ R% ?9 W& F3 K$ d) l super.buildActions();% ]8 l! x1 N' W; K% _
1 u2 L w: Q3 g& O, f$ ~ // Create the list of simulation actions. We put these in; e+ Y+ f$ v0 ^( F/ c9 e- B
// an action group, because we want these actions to be; j6 }) c4 F/ S" Y; F
// executed in a specific order, but these steps should3 d, j' g8 z4 ]1 f
// take no (simulated) time. The M(foo) means "The message
5 z% y! g- K6 }) L" M5 {. [ // called <foo>". You can send a message To a particular, Z9 e. \ n0 U* O0 z* a6 @) D3 J
// object, or ForEach object in a collection.7 J+ P1 ^' o, X% C5 |' g8 d3 ?
' C: Y) l- C0 K/ m+ q ^
// Note we update the heatspace in two phases: first run# ^& W6 M+ Z+ Z& K2 e( S: [
// diffusion, then run "updateWorld" to actually enact the5 N$ ^& ^. d7 ^6 `5 {8 u
// changes the heatbugs have made. The ordering here is$ f, u& E6 y3 g7 ] t
// significant!4 u5 f7 h" `3 B: i. u
( g; B5 }( I$ d/ w5 \( i2 N
// Note also, that with the additional! @$ N' |" A" O
// `randomizeHeatbugUpdateOrder' Boolean flag we can7 Z4 {( }' d! P: j
// randomize the order in which the bugs actually run
W. n3 l- d- H6 ?0 Z7 @ // their step rule. This has the effect of removing any
+ Y6 n6 |" w% ^' q // systematic bias in the iteration throught the heatbug
2 Y" ]" ~! K! h" G) ^) G$ ` // list from timestep to timestep7 O- P4 O D+ n9 }5 n/ ^
8 K7 q, P8 U5 k0 E( X: |. h // By default, all `createActionForEach' modelActions have; G) Y) {! P' Q* c* d
// a default order of `Sequential', which means that the
% z( U( }9 X0 V# ?7 N; {0 w1 Z // order of iteration through the `heatbugList' will be
" E* _# j+ v) ^: S4 T // identical (assuming the list order is not changed
. E9 N+ i. G: x/ \ g% b: ] // indirectly by some other process).
4 O# R( R9 ~: F" Z# _4 p
o; g3 B$ w+ x modelActions = new ActionGroupImpl (getZone ());
% J# O' h: z' O* }. k' b6 B6 f& \9 e4 @. L e
try {
; M7 g9 \/ i o% f modelActions.createActionTo$message2 s( P% J I( D: J
(heat, new Selector (heat.getClass (), "stepRule", false));0 h: m) K5 ?1 ~ D! m
} catch (Exception e) {
$ F# |- d/ O. \: C System.err.println ("Exception stepRule: " + e.getMessage ());: m6 C/ }# ?; k7 a
}$ `/ J% ^( q ^
* m5 R2 @5 n7 G+ l try {; a" e( C% _; d, ]0 c) Z# A
Heatbug proto = (Heatbug) heatbugList.get (0);4 A# V* E0 Z1 S6 d7 E
Selector sel =
* r/ }: O- k. M: Y) n/ J new Selector (proto.getClass (), "heatbugStep", false);
. T0 K8 P$ Z+ h* N% M9 F0 r; h actionForEach =
* [ {" m0 } J3 R% Y( l# p7 P modelActions.createFActionForEachHomogeneous$call
& S3 I# v3 l; y (heatbugList,
5 V6 X/ e; {! l1 f8 k8 F; r new FCallImpl (this, proto, sel,
; R5 e5 p; x. d new FArgumentsImpl (this, sel)));
6 P" j1 r" s+ h0 @; T4 ? } catch (Exception e) {; ?2 O5 \, z! n7 c. e& L/ f
e.printStackTrace (System.err);; e/ G* u1 Y4 }9 }4 t$ y& p2 x
} x- w* `. k- J0 e O7 v
: f# m- t/ R+ ?8 y7 W
syncUpdateOrder ();& E9 r( [- C+ A2 Q B" a6 I
; w% d- [( o+ J4 E0 l# e
try {& B3 J3 m$ B$ v9 C7 z
modelActions.createActionTo$message ( p# L* d% Y) h6 H8 I: V
(heat, new Selector (heat.getClass (), "updateLattice", false));
( m) Y4 a& E5 k4 a } catch (Exception e) {
3 W- n$ L8 w) y P+ y2 k System.err.println("Exception updateLattice: " + e.getMessage ());
$ \* ?5 \) r5 r/ M }
# O3 W/ R, e1 e! c6 A0 m 5 i! A" `$ F3 u+ a/ e9 f$ S% j
// Then we create a schedule that executes the6 f$ C% Z% g' h3 e A& D6 ~* U0 L
// modelActions. modelActions is an ActionGroup, by itself it
6 T; V4 G7 \. \8 K3 j; K- ] // has no notion of time. In order to have it executed in9 Q# T1 @9 n: U0 p8 ~
// time, we create a Schedule that says to use the; {! _. D$ E7 {6 G( C6 Z0 L) V2 P! Y
// modelActions ActionGroup at particular times. This
; E' S# a) k" E9 Q$ Z( W // schedule has a repeat interval of 1, it will loop every
! B+ a( W! X5 z // time step. The action is executed at time 0 relative to
0 I3 E+ q0 U' X9 ~: L // the beginning of the loop.! \) m! h( l1 Q5 i8 ^9 Q
- w$ }8 w* S( O- n: F% D // This is a simple schedule, with only one action that is: x: ?/ f N0 E6 t- z
// just repeated every time. See jmousetrap for more
, Q9 v! w( k3 }$ n/ W% ] // complicated schedules.
2 w. K) \0 C% Y* i
4 N( g9 {1 M) O modelSchedule = new ScheduleImpl (getZone (), 1);+ {! G: Q7 L5 x8 `# O- a5 l3 P. y
modelSchedule.at$createAction (0, modelActions);$ j! H5 T- p; Z1 x8 g V, @
: M- s0 ^0 n( \9 }2 \" P return this;4 E4 x2 j/ ]* d) n4 M
} |