HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! ?1 [9 e: Q3 W) h6 u) O
: G3 W% ~! S2 h% e public Object buildActions () {# w: j% ^- ?9 }6 T( ]: K3 x4 y9 J
super.buildActions();
8 m3 U) l3 a2 q( P6 f # Y) b6 \* S( q! b s1 J. A i4 O
// Create the list of simulation actions. We put these in
+ d% e F" ] ^( ^ z e6 J, ?* t; p" s // an action group, because we want these actions to be5 t: G( K& C9 |5 O- t7 e
// executed in a specific order, but these steps should3 u* P* Z9 b7 u) H6 x
// take no (simulated) time. The M(foo) means "The message- V0 |2 [ d1 g. T2 H6 s; @! b
// called <foo>". You can send a message To a particular
, I' o; y$ ?: D# h6 q$ M // object, or ForEach object in a collection.2 b" S, z* r/ C( ^# Y% w# V
6 b; G2 k) c f' E // Note we update the heatspace in two phases: first run9 C b4 t, d' w7 J6 R- e0 @/ a
// diffusion, then run "updateWorld" to actually enact the
' l2 K7 X- p6 J6 T) e9 d // changes the heatbugs have made. The ordering here is
% ^ u# v4 @ e4 G5 B( R- ?. V // significant!3 \- E% ~' [2 }& u' M. l
4 E8 {7 r1 d3 o1 v5 I# c O2 A // Note also, that with the additional2 [ Q: f8 W. H1 m ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can& V) O2 Q5 F9 q
// randomize the order in which the bugs actually run
: Q& z) G. j9 P6 V // their step rule. This has the effect of removing any
3 J2 i; ?- g O4 i: D) c // systematic bias in the iteration throught the heatbug2 e4 L8 v( P) l* J" z v6 L
// list from timestep to timestep
7 e# w3 N+ o: v" n6 Z) Q0 K+ u* S " Q$ A7 B& z t, m; J9 M: T3 D3 t
// By default, all `createActionForEach' modelActions have' C* a U" P, k2 J H
// a default order of `Sequential', which means that the0 O& e9 H8 o @+ e* i! U; d
// order of iteration through the `heatbugList' will be
% G. J* h! L2 a/ U# H" e, ]: N // identical (assuming the list order is not changed
6 k P0 R, b2 [' a' f7 m' V$ i1 }/ [ // indirectly by some other process).- a% e! U; s! A8 A. Z
" ?0 w5 v0 M" y# F. `& y" ~
modelActions = new ActionGroupImpl (getZone ());
( }% ~: e' U% F# T _5 F6 o8 |; E2 A
try {. M# r! U0 J; z' M$ }
modelActions.createActionTo$message
- s# Y z$ M1 E) `' N/ g) x; \; K (heat, new Selector (heat.getClass (), "stepRule", false)); G9 k4 E2 S2 k$ s2 p2 H
} catch (Exception e) {; f. I2 G; g: a4 K* Z6 E
System.err.println ("Exception stepRule: " + e.getMessage ());. C* l# b/ j0 J: v9 J5 a0 h" e" E. ~* Z
}
$ w V7 V. a$ K- b# g
1 ^: Y* ~( z: D6 _0 _" i( g2 b4 a! P try {& T5 Z$ D/ W6 `& W$ ~
Heatbug proto = (Heatbug) heatbugList.get (0);7 U4 F. C3 Z D" P' W! c
Selector sel =
4 ^' T, _* v3 R) O% k7 }( s7 C3 o new Selector (proto.getClass (), "heatbugStep", false);- Z# v6 Y9 N, }8 m
actionForEach =
* ]" _' T1 u1 @0 U8 Q modelActions.createFActionForEachHomogeneous$call- _$ M1 t$ I( s* x- O- o
(heatbugList,5 O5 M3 q# w0 R0 n" p) _* `2 u
new FCallImpl (this, proto, sel,8 ]6 S- k) h: R6 A8 \
new FArgumentsImpl (this, sel)));& F/ ~' z! z2 A1 i
} catch (Exception e) {
; B; b/ l6 _, S% R5 @ e.printStackTrace (System.err);! M# y: P. i1 j4 W2 B% C+ O6 g
}) R$ v; A' x( g/ i, q
: r3 x! p1 R- D3 S7 h$ r) w
syncUpdateOrder ();# Q7 b- M! D: o4 ?4 B
* V: |# Z7 f9 q2 t$ B \+ i8 B' [
try {
& E/ o5 ?. `: G" \ modelActions.createActionTo$message 7 A) N0 Y5 i' a! X4 M1 I4 @
(heat, new Selector (heat.getClass (), "updateLattice", false));
: M; b+ y* R* X1 R. v0 u; q$ t% {4 @ } catch (Exception e) {+ Y/ C: H+ B8 _- k/ f3 l; H X" O
System.err.println("Exception updateLattice: " + e.getMessage ());
5 O6 c" q" x! m. S }
/ I. m5 d1 R: W# z
p+ z3 x( v0 g1 v // Then we create a schedule that executes the
) h/ ^4 _6 n5 F3 k2 }2 i // modelActions. modelActions is an ActionGroup, by itself it3 k- U; J. I! A$ S q
// has no notion of time. In order to have it executed in( E. n: ]( n; k# C% ~: o) N
// time, we create a Schedule that says to use the% ^7 f: I7 U' l# ^
// modelActions ActionGroup at particular times. This- e/ \' D. ]; D1 `- r3 e
// schedule has a repeat interval of 1, it will loop every
6 i9 g5 v) ~1 B) Z V/ o: N% A/ Q( B" {* [ // time step. The action is executed at time 0 relative to- ^, @/ E$ X5 r( e( H( ~% B2 z
// the beginning of the loop.% F( T0 s4 I* e1 P* `
+ T' `9 M6 D. E3 i! i // This is a simple schedule, with only one action that is
1 p; X7 Z4 G- E1 F // just repeated every time. See jmousetrap for more4 k8 P; ]4 H, W E' U0 S
// complicated schedules.
5 M0 s4 f1 }+ ^* y9 w% ^
0 ^4 f0 P3 ~9 M9 t1 r3 E modelSchedule = new ScheduleImpl (getZone (), 1);+ [7 I2 {% ?5 A: c
modelSchedule.at$createAction (0, modelActions);( C! K' Q. v! `# F# l! T0 f. `
' g% I: @/ D ~( l4 B# V. ^
return this;8 ^) }, b7 A5 h7 ? V% |; Q% X
} |