HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ l0 D1 L- G( A# z
# G) Q, B y4 ]3 F E' s1 _' A. W" p public Object buildActions () {* [: r3 K% r: Q, u: C
super.buildActions();1 p8 }9 e; l: v( e, J
7 U5 D" V3 G# R1 j( E4 y // Create the list of simulation actions. We put these in
' E7 @. |# @& Q // an action group, because we want these actions to be8 ^+ y9 M, |7 _! i! m$ J
// executed in a specific order, but these steps should0 u% U/ x( ]! J$ w9 N) ^/ s% y) O; q8 c1 f
// take no (simulated) time. The M(foo) means "The message
! O! c* K, @$ L // called <foo>". You can send a message To a particular6 L) D1 j7 I( J+ p
// object, or ForEach object in a collection.* z; t5 A3 G' J G0 o- p( Y) ^7 r/ Q
% n$ k. y9 R- d3 ]' p5 m7 {
// Note we update the heatspace in two phases: first run6 s0 x+ F# i3 f, X
// diffusion, then run "updateWorld" to actually enact the8 t) w$ B4 @+ ^, f
// changes the heatbugs have made. The ordering here is! ^! C% l* f z6 x% \" I1 j$ y1 W
// significant!
$ G2 `% `7 j7 H. o8 X- a4 d# [3 [
0 j/ i3 C. q! x" ^2 s // Note also, that with the additional" ]5 t8 b4 E: X0 h
// `randomizeHeatbugUpdateOrder' Boolean flag we can3 w; c4 b' q% Q6 ~7 _$ L G
// randomize the order in which the bugs actually run, x2 f2 N# r7 p' l9 @
// their step rule. This has the effect of removing any
+ {5 q0 a* _6 ]* v2 p // systematic bias in the iteration throught the heatbug
( v: I& D" ~9 [% M/ C6 O // list from timestep to timestep1 U. W# ]0 d# z9 D2 l6 p' F" }) M
. L5 W8 g! g! W& W. N' A
// By default, all `createActionForEach' modelActions have
( F# W7 B3 _" c% M$ F4 f7 h9 Q9 e. f // a default order of `Sequential', which means that the- U0 l8 n! l* M" g! W$ m
// order of iteration through the `heatbugList' will be
3 v; S# e7 }& w // identical (assuming the list order is not changed" h6 s* X9 S1 ^9 t* p
// indirectly by some other process).
# `, A; D+ y& W+ |0 i
# n8 F: M: h9 n1 F* J modelActions = new ActionGroupImpl (getZone ());
+ e; k( y: e) p" S
; L) R- h4 q3 H1 ?4 |. a" h try {( o; \# X8 \' t$ \. c
modelActions.createActionTo$message: i5 v! r, d Y. Z
(heat, new Selector (heat.getClass (), "stepRule", false));
6 R/ Q. b5 X& m9 f# Y- v } catch (Exception e) {
; @1 a+ S* _0 ~) ^) ~" p8 W' f0 ^& [ System.err.println ("Exception stepRule: " + e.getMessage ());, m2 v5 z' W# ?
}
+ |, X6 p6 N. u: V# |1 k5 Q: t) M: T Q4 p. Z
try {3 B% m+ Q9 L' t$ h2 T1 }
Heatbug proto = (Heatbug) heatbugList.get (0);$ N, w% p* y& A$ _. d) |8 q
Selector sel = 9 c& }" v' s; m6 P8 D
new Selector (proto.getClass (), "heatbugStep", false); s8 X. n0 _9 I& R) _
actionForEach =
/ e/ n9 Z i4 z( u modelActions.createFActionForEachHomogeneous$call! _3 z3 b7 W9 D7 Z) m
(heatbugList,1 m5 i) w/ t+ f1 J) y
new FCallImpl (this, proto, sel,
3 O1 l7 H' K1 L9 m! ` new FArgumentsImpl (this, sel)));) U( d. O8 W+ c
} catch (Exception e) {
1 ]6 S: w7 ]9 [$ V- g e.printStackTrace (System.err);
; K4 s5 z8 I" I9 J2 \ }+ T* C1 o5 W+ R6 G9 `$ i
/ n; v% g, z' t# w4 u syncUpdateOrder ();* @3 A4 t6 n% x/ V8 z2 `" s9 ^0 S
4 ~- J n, ~* C! H try {
2 b# I4 P( |# e9 { modelActions.createActionTo$message
4 m" K! p" A2 i! \: z. P6 J (heat, new Selector (heat.getClass (), "updateLattice", false));
' m) Q- B* I7 u7 B+ ^ } catch (Exception e) {7 P- ^6 ]$ y* |9 P- k1 A
System.err.println("Exception updateLattice: " + e.getMessage ());( T% H1 Z- a- Z/ v6 I" q
}" `. X, j2 g( W5 s# ]: K' ^) j7 w6 C" R
- L! b' Q( x- }% v& L // Then we create a schedule that executes the/ h+ Z- f$ U% t* B& B- Q* z
// modelActions. modelActions is an ActionGroup, by itself it
' D; P4 E( W8 {& p7 |" v* T9 W // has no notion of time. In order to have it executed in. |* _8 A ?* i6 v) ~
// time, we create a Schedule that says to use the( `: P, b7 b" R1 b1 G: U+ A$ B s
// modelActions ActionGroup at particular times. This' j8 ~2 Z3 r) {& s; F3 ~1 R
// schedule has a repeat interval of 1, it will loop every: V9 {: o' s2 [# w, q; F5 ?) d
// time step. The action is executed at time 0 relative to+ D; X& D9 B9 V* u! B* `
// the beginning of the loop.
7 v; | W2 ^2 E6 K$ l0 T
; G- O: U0 Z3 m- p+ h4 n+ o- E7 J1 ] // This is a simple schedule, with only one action that is
, x _7 x c4 b( [7 v$ O! L5 H // just repeated every time. See jmousetrap for more, \5 H$ E k) p+ C. p
// complicated schedules.
3 u! X) j& c0 l b $ v; B% z2 n1 H" e7 [( J
modelSchedule = new ScheduleImpl (getZone (), 1);# H4 P/ V% u3 T1 ?
modelSchedule.at$createAction (0, modelActions);5 E) j5 I; s' Z- L
: M7 ^- h( y; ~# v" N- q
return this;; m8 X1 N" @; c
} |