HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 S2 [( q/ u m& s0 ]( v5 a
9 t, ]! n8 G7 w2 p7 F public Object buildActions () {
+ {& U% E) O1 O4 C0 z x1 p2 q super.buildActions();
; q0 ?6 Z) E% }* J ) e2 i3 x* o s
// Create the list of simulation actions. We put these in+ m B- m- ^ u4 V) v C! D y2 A, Q
// an action group, because we want these actions to be k9 g* K' l$ P2 C! B5 s
// executed in a specific order, but these steps should
( I- m& C' G8 Z' S# a // take no (simulated) time. The M(foo) means "The message% M: _: ~4 ]+ Q0 {
// called <foo>". You can send a message To a particular
2 m- y& y/ B9 x // object, or ForEach object in a collection.
! `5 b" S$ K! X: Y+ x" }
/ C$ h) y9 q* O4 f // Note we update the heatspace in two phases: first run
7 v+ }$ q; x4 k1 e G7 c // diffusion, then run "updateWorld" to actually enact the
. k9 K n) h$ H: F // changes the heatbugs have made. The ordering here is# L7 P7 R0 i, g. u. q' ?5 c
// significant!
" y" S( [% M8 u
5 w R# P$ R1 B // Note also, that with the additional
3 ~$ D. p( Q ` // `randomizeHeatbugUpdateOrder' Boolean flag we can7 \ N5 B( y! v( D0 `( A# d
// randomize the order in which the bugs actually run
! B8 X: A6 ]9 `9 l) Q( H: k) V // their step rule. This has the effect of removing any
' |( F- B6 Z f+ b) B& w // systematic bias in the iteration throught the heatbug4 v4 V. a0 z6 P0 \0 d
// list from timestep to timestep, Q4 |2 M) u! D8 Y3 o
/ {- U5 ?) M; v- i1 o. N, T // By default, all `createActionForEach' modelActions have. o, d- S$ c/ `0 l( Y2 {
// a default order of `Sequential', which means that the
2 C1 y& U, u6 T9 ~3 T# W& r) o // order of iteration through the `heatbugList' will be6 F- [2 L4 r( P6 u1 Q
// identical (assuming the list order is not changed
) F# c) p6 Y: |3 p+ I: G4 r // indirectly by some other process).6 w' o- ~: \9 H6 K' n
# B" ~$ @1 Q& w6 T' V0 N modelActions = new ActionGroupImpl (getZone ());
) q" p- T0 f6 w% z' a# ?8 P& t d, m% q2 _6 t- H6 n: H z- w- n
try {
( g7 j1 C9 V2 z- J9 z modelActions.createActionTo$message$ e, C! K4 X# R" }1 U2 j, {7 p
(heat, new Selector (heat.getClass (), "stepRule", false));+ c z( l& j! G9 N. |* `& z. Y4 a
} catch (Exception e) {
$ w6 s: l$ r4 Q+ f/ ^$ O0 p System.err.println ("Exception stepRule: " + e.getMessage ());
2 c' C2 L* A" M" M( p }/ C. N B6 x* F Y6 u! x9 K' o
6 ?8 ^) ^6 C, D% o! U" r0 K1 j' s
try {
% J2 E4 Y* \7 p9 I Heatbug proto = (Heatbug) heatbugList.get (0);
( h ~1 D& G; E5 R& U Selector sel =
2 g, ^( M# b& [# C0 s. I: ~ new Selector (proto.getClass (), "heatbugStep", false);
* Q# Y7 A( s2 g9 k) M actionForEach =
( H& t- B' e) i# G( i+ b modelActions.createFActionForEachHomogeneous$call
9 q. B6 M" X( y9 ~0 _' x# e (heatbugList,4 q; P* s3 D' H
new FCallImpl (this, proto, sel,0 b; y% X9 o& p; o. f; A" N
new FArgumentsImpl (this, sel)));
8 w$ e) B* J: _& p* E } catch (Exception e) {# H" B; p( G" t) t( }8 d, I/ U
e.printStackTrace (System.err);" M# s8 J+ G- x+ n
}
3 f3 Q( ~$ z) C" x0 u% a+ }
7 [- s4 b$ @. F5 x syncUpdateOrder ();) J5 H/ d v( J7 H% f! x( j; R
3 {; S @, v7 }2 e) A' X- o' P3 i try {0 q9 @8 [" P M7 |% w" `
modelActions.createActionTo$message 7 o# C, W6 ]# y( j- n
(heat, new Selector (heat.getClass (), "updateLattice", false));! B1 l3 o& f3 H4 Z) }9 w1 y4 r/ \
} catch (Exception e) {8 j0 W, D9 W# N% ?$ L) v! j: ?7 c
System.err.println("Exception updateLattice: " + e.getMessage ());8 R2 q* y1 I# Q: {8 c
}
& m* F4 k& G/ n, k P n - H4 s' ^) u5 u4 p% ^/ \# S* p2 R- Y
// Then we create a schedule that executes the. O; c+ h$ t) G: k$ E: P
// modelActions. modelActions is an ActionGroup, by itself it$ Y9 n& q% q& \; w# t( Y& u, i
// has no notion of time. In order to have it executed in
8 ]( `, l& ~% n8 e // time, we create a Schedule that says to use the. Y" w0 f( _4 i& _' U9 I
// modelActions ActionGroup at particular times. This0 g' l9 F6 Z( t' ~4 T% c( ^
// schedule has a repeat interval of 1, it will loop every
, \3 U" x- ?! I' Q // time step. The action is executed at time 0 relative to' G7 p! }. _( `. V( G* f
// the beginning of the loop.3 ~/ T5 N5 Y2 E& L0 C6 ~
! ?$ n. X( J, ^* ?0 z7 [- O
// This is a simple schedule, with only one action that is, n# w0 W C9 g
// just repeated every time. See jmousetrap for more
2 ]9 ]4 J: |% i# j // complicated schedules.+ l a9 G2 c- I2 y
, F7 A$ L" y4 U( N7 Q. } modelSchedule = new ScheduleImpl (getZone (), 1);# t& E! d5 P y0 I! ?0 M
modelSchedule.at$createAction (0, modelActions);3 N# v9 T9 L: i9 n* x
0 o: N8 `# w5 j& Q( r- z return this;6 K; m" t! G9 _0 G' P1 e
} |