HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: M) Y6 w; F8 |- s c( K
- q3 q; d/ v! q
public Object buildActions () {
* e4 l1 O: s0 O$ `; c6 C- t super.buildActions();
, M! M, b& r' @ 1 V: X0 h; k7 `0 J8 Y' w) N
// Create the list of simulation actions. We put these in
: L; x5 A% R, q1 _2 H, D' R0 M) a. j // an action group, because we want these actions to be
3 l* A7 e l5 H1 K3 v) } // executed in a specific order, but these steps should& v! ~. Y2 @" q% l u% H6 B! F. V
// take no (simulated) time. The M(foo) means "The message
; m0 _5 y3 `& I }/ O // called <foo>". You can send a message To a particular! Y5 s" w. j/ u' Q, i- Y3 I
// object, or ForEach object in a collection.
, R& P9 i) @( ?0 C) y- J# f0 n+ X
; Y+ t6 p- x( R // Note we update the heatspace in two phases: first run
n* S5 o% a" l8 c2 y // diffusion, then run "updateWorld" to actually enact the
\' N7 F7 P( G, {2 _9 } // changes the heatbugs have made. The ordering here is& Z* [+ f( ]. g( y5 V1 f8 d* ?
// significant!, Z$ g! a2 x. H) u% V* }
2 {" M+ P: A, s4 I9 M& | // Note also, that with the additional. }8 n r2 G6 J" M3 u
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. K* h# A6 w/ Z: r6 O( f% F" ]' L- J% j // randomize the order in which the bugs actually run$ B7 a [" h6 V. k) r
// their step rule. This has the effect of removing any' Q! P/ u, Z4 y2 d- f
// systematic bias in the iteration throught the heatbug$ h( {8 k3 ]8 x8 F
// list from timestep to timestep$ ~' K$ k+ P1 K2 G" j7 {
; ~" b, L" b+ q0 a! ? // By default, all `createActionForEach' modelActions have
% q' Y- A' [, a9 c: | Y7 w+ D" R // a default order of `Sequential', which means that the- R+ l. n# j$ R9 X7 M
// order of iteration through the `heatbugList' will be
, ?9 \% o7 \/ @* c! z // identical (assuming the list order is not changed
+ [9 j4 m# P: ? // indirectly by some other process).
7 C1 O% i$ F6 W8 j+ E. V7 e3 [ ' F- }3 B0 V2 j* D
modelActions = new ActionGroupImpl (getZone ());
0 Z. V1 o1 r4 c8 J8 ~1 p3 w! I4 d& M0 Q8 d& U
try {# q8 Z; V, o: A4 d5 P# F
modelActions.createActionTo$message% i' e8 V1 K* R2 [6 W# S9 D
(heat, new Selector (heat.getClass (), "stepRule", false));
4 @- t! s% r# Y9 k1 V } catch (Exception e) {4 w) o9 {; A$ M8 z3 ~
System.err.println ("Exception stepRule: " + e.getMessage ());3 U( }' a' K: v+ ]
}
( l- o5 d, C% \3 i: H' M9 C$ G# S8 V1 ~- D
try {. Y! k' A$ ?+ ?, K* O
Heatbug proto = (Heatbug) heatbugList.get (0);' m- l. W3 X7 P% ]9 K: ~
Selector sel = : e' }, ~3 l/ G
new Selector (proto.getClass (), "heatbugStep", false);7 t( F9 h8 R" P- l# X* ]2 P7 i
actionForEach =6 O) N3 j1 w1 f) i, ^& Z( F
modelActions.createFActionForEachHomogeneous$call
, j6 s" K6 h- R! s" {# U (heatbugList,
1 v9 S1 q% K2 N, s new FCallImpl (this, proto, sel,
+ U9 ?5 W8 _' _3 ]( V new FArgumentsImpl (this, sel)));
# \# {! v8 X. O9 d( [6 h# n } catch (Exception e) {6 h* y5 H; S( s5 ^) k* m) ~' q
e.printStackTrace (System.err);. ~5 B1 V* L8 \3 M
}
- h+ e; f5 @+ F9 y; `1 ?
! S3 M9 Q x: Q8 f! n: N syncUpdateOrder (); H; V& h$ p8 S: S4 G0 ]
, r0 T2 |' O# |9 s1 } try {+ f( C3 T4 O! v$ c) ~# H1 P" m# ?
modelActions.createActionTo$message
$ {0 }6 B% {! j/ J" Y: u (heat, new Selector (heat.getClass (), "updateLattice", false));2 Q4 m0 L0 c) k; M: R
} catch (Exception e) {2 B' q' Z. Q0 q- H
System.err.println("Exception updateLattice: " + e.getMessage ());+ `0 D4 a v- t, B4 _! M+ E
}
. m' O, T4 m' L5 n" B / I- I- c4 |; D2 T) B. X
// Then we create a schedule that executes the
" e1 v# B0 q; A7 | // modelActions. modelActions is an ActionGroup, by itself it' i! W1 o2 S1 N3 i! d
// has no notion of time. In order to have it executed in% a* J. S! ?/ `. T9 _# ^# s, K
// time, we create a Schedule that says to use the
) n, h3 o( H6 _; \$ ^, S // modelActions ActionGroup at particular times. This$ ]" e) Q7 u) d- k( k
// schedule has a repeat interval of 1, it will loop every
% M3 e) ?' u0 Q+ H! i& d2 G // time step. The action is executed at time 0 relative to
/ r1 K; s/ F; D9 ]8 {& E5 b4 ^ // the beginning of the loop.- [" d1 M' S" X8 y
; X- ~4 X+ A/ R/ E. Y6 L
// This is a simple schedule, with only one action that is
/ u6 c/ s& n. B8 D // just repeated every time. See jmousetrap for more
! S& V6 H% k5 \9 Z# p // complicated schedules.
& ?2 M5 ?4 x# A* ?
" L7 c5 H F( F modelSchedule = new ScheduleImpl (getZone (), 1);1 F" a0 R0 u5 D% W: L0 R: w
modelSchedule.at$createAction (0, modelActions);8 ~# X! \" l; M! u M- A/ l4 b
6 | ]" J0 i3 S: z4 a* x" b7 R
return this;
6 y# I9 Z2 i0 x) |' p$ y } |