HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 @4 A/ @' z1 o' l( m6 z
$ _& F2 F* F8 m4 d public Object buildActions () {
% D8 R/ w3 ], K; Y* y super.buildActions();; `* B; P/ n! I0 p. U; K7 P
4 |$ V8 C6 \6 X" ~6 X1 r // Create the list of simulation actions. We put these in1 `7 O' T' [6 |; h8 w/ p3 U
// an action group, because we want these actions to be
# G4 |- M1 Q+ q2 ~2 w5 h // executed in a specific order, but these steps should8 R0 u* p. i* J! z( m
// take no (simulated) time. The M(foo) means "The message: a5 Q8 b5 H6 h
// called <foo>". You can send a message To a particular! ^( D* H+ m; c- [6 N5 J* G1 {" ?
// object, or ForEach object in a collection.. s1 Y! J; a ]4 I8 e, l ^
& ~; @( Y" Y4 P: o8 {$ } // Note we update the heatspace in two phases: first run3 r# A9 k+ s! Q* p& N
// diffusion, then run "updateWorld" to actually enact the
0 t& t2 b' M7 K; k: K // changes the heatbugs have made. The ordering here is+ d: K, n* X u$ b( v s
// significant!
9 A+ X9 d% O. E! G
# _. y& v. j$ V( |/ w3 l // Note also, that with the additional( m+ F2 ]: J3 u2 Q( W; S
// `randomizeHeatbugUpdateOrder' Boolean flag we can4 f& T) S7 ^( Z5 C
// randomize the order in which the bugs actually run* Q4 L: z3 M# F, o7 W& P
// their step rule. This has the effect of removing any* w7 e9 `2 m/ ~
// systematic bias in the iteration throught the heatbug
5 G* Z8 ]: g7 l) R // list from timestep to timestep' l% ~9 h$ v' f1 K- K7 t: X2 d: a8 N5 Q7 ?
2 d! k4 G; Q* D$ [
// By default, all `createActionForEach' modelActions have
( ~% b9 q6 T- p1 y. R // a default order of `Sequential', which means that the
) a' u+ o) ~. V4 F9 M // order of iteration through the `heatbugList' will be
' ?' n2 a7 B1 R0 E6 N8 U& u // identical (assuming the list order is not changed0 f" G1 ~! A1 Q8 x* d J1 `
// indirectly by some other process).
9 a5 i. c" v8 @* x, _ 0 E3 X5 [, l& C; Q% J
modelActions = new ActionGroupImpl (getZone ());
/ \& o' r C% R$ d8 z# J* Y& v- x
8 Q& F, B7 P' S4 q R try {
Y# A. _; M8 s( C, U5 c6 V1 Q. S modelActions.createActionTo$message' U, c: ?7 R3 E9 E0 z: P4 A- H
(heat, new Selector (heat.getClass (), "stepRule", false));: S, W/ k- u% D1 c
} catch (Exception e) {
; G) f1 Z5 F; j/ q3 K# k; B- q8 c System.err.println ("Exception stepRule: " + e.getMessage ());' y3 j/ T% S% k& Q A* |
}9 H j' Q, Y( `* s4 p2 c
$ `- x' W2 O# r0 s try {
. H& N5 t. q4 X) j# X5 O( R: l Heatbug proto = (Heatbug) heatbugList.get (0);, s! e( H1 M; O
Selector sel = 9 L0 _) V7 D5 c. J1 J c" u; q
new Selector (proto.getClass (), "heatbugStep", false);
' x. ^. b9 L r, K2 |; Q actionForEach =
* F# f0 V5 m! Z& u modelActions.createFActionForEachHomogeneous$call
# o/ w8 M9 D2 ]- n( o. d4 P2 e4 ` (heatbugList,
' ~+ @4 e+ l3 ?5 r new FCallImpl (this, proto, sel,
; ~$ W# V* V$ L- s+ A% Y new FArgumentsImpl (this, sel)));1 L8 O L0 N0 C+ A/ B! M/ z
} catch (Exception e) {
/ f) y9 W3 ~; x- q5 Y e.printStackTrace (System.err);
2 x9 Z4 h. {3 y5 W }% J6 P( F7 a1 N! Z
5 {" m0 Z n# F6 V syncUpdateOrder ();
0 c1 e2 @) u+ W9 x1 ]- D. c, \5 O0 |+ u2 E
try {
( @) E) X9 |& o3 i- D0 ~5 [ x) C modelActions.createActionTo$message * B; w+ k& U3 K9 K
(heat, new Selector (heat.getClass (), "updateLattice", false));7 r9 B# e8 Q- r! n
} catch (Exception e) {
, ~2 V6 Q& ?5 Q5 V% F: R& V; C* j System.err.println("Exception updateLattice: " + e.getMessage ());! j) F3 ` i$ @! Z. C8 \) `
}8 D/ S) K7 D" ^! h' e9 e# k8 r
6 _. g9 Q2 j5 Y3 g1 ?
// Then we create a schedule that executes the2 F5 d* \* Y1 n: [' F2 `, r3 h. p& w
// modelActions. modelActions is an ActionGroup, by itself it ?3 U6 @0 ~" o3 B' Y
// has no notion of time. In order to have it executed in
1 r/ N% t6 m5 V# v+ a" w // time, we create a Schedule that says to use the
, I9 q- Z1 I. z( G // modelActions ActionGroup at particular times. This
& b; y& J1 w/ m1 H // schedule has a repeat interval of 1, it will loop every, A# ? L* \* w* X U
// time step. The action is executed at time 0 relative to
3 l# m$ U) p8 \' @' ] // the beginning of the loop., C' @; S7 O# R" e* n4 `# z$ v5 v
\. h& L% M, v
// This is a simple schedule, with only one action that is+ P" _$ g! D( ], C4 ?2 |. w
// just repeated every time. See jmousetrap for more
7 }! O6 D, U' x( Q: Y5 i* { // complicated schedules.5 e$ v) o, w+ C8 X5 _6 ~, Q. Q* ]
* y) W' E1 O. ?$ _ q modelSchedule = new ScheduleImpl (getZone (), 1);
3 y1 l. T0 a( e5 M) q2 z" w; R+ d0 _ modelSchedule.at$createAction (0, modelActions);$ V- ~: g& J1 q1 l2 Q" V7 D! g
6 s- T( e3 a3 R7 k8 @
return this;
0 O& U: i% C8 X* p1 P } |