HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ [+ ^7 {* n* H0 W! {& D( {. A
- ]' p0 w8 i7 _' O( G5 e public Object buildActions () {
& k+ B$ x) C$ K) Q9 r' b, I& Z super.buildActions();
5 y! G6 | v7 x: N) m% s4 j
8 \% U) L$ z/ h3 ?3 ] // Create the list of simulation actions. We put these in
! ]% t n7 y' t$ v // an action group, because we want these actions to be9 M& E: W, j1 p8 N0 I
// executed in a specific order, but these steps should
0 u3 `; Q: g% M! Z7 _& S# L // take no (simulated) time. The M(foo) means "The message7 N$ Q- I0 \+ `4 C
// called <foo>". You can send a message To a particular
4 P$ p8 L; y* e6 e2 a& X& Q% Y // object, or ForEach object in a collection.
) C1 J# w9 D. J! { ( q4 y2 b7 M6 D+ d9 f
// Note we update the heatspace in two phases: first run
* ^, m" C. i' Y- x/ ]/ G // diffusion, then run "updateWorld" to actually enact the
% {! H( {7 m. r // changes the heatbugs have made. The ordering here is/ w0 O1 E/ Z) h, C
// significant!" C1 d- j& G& Z! k
2 Y: S" C9 D, F0 s // Note also, that with the additional |) X- F9 X5 w; E; m
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. i/ r: [0 l- E. c // randomize the order in which the bugs actually run3 {( q5 w: P* t ], Y) @7 k: _
// their step rule. This has the effect of removing any
% I- Z3 K5 T+ C4 Z, b! t // systematic bias in the iteration throught the heatbug X6 k/ i0 M, U9 @, v: C
// list from timestep to timestep
- x! e1 C6 w6 T! B3 u5 ?" b 3 k3 c B" i8 {" T
// By default, all `createActionForEach' modelActions have
! K3 E/ j* V, k! i8 m // a default order of `Sequential', which means that the
: E6 m2 `0 [% j! ^6 W // order of iteration through the `heatbugList' will be0 \( q0 Z; J" m" V0 |8 w8 O# t: H
// identical (assuming the list order is not changed" l" |3 A. m4 G% r# D# D0 s
// indirectly by some other process).$ @6 k! X' I. P2 A
* T- j; ?2 {4 I5 w
modelActions = new ActionGroupImpl (getZone ()); A. q7 U; D N6 z3 N
9 C* |8 A+ V h try {8 {6 X& s) G8 J$ Q
modelActions.createActionTo$message
! [7 D( M" D( b2 K* O; [( K* e (heat, new Selector (heat.getClass (), "stepRule", false));
7 r; q5 B9 c) w1 y& I, B } catch (Exception e) {
1 `9 I; @4 @( _, `# `* ?. d System.err.println ("Exception stepRule: " + e.getMessage ());0 I" }; ~- H' V
}& t7 @/ ^2 n% I# H" [6 C1 x
x- Y- X: Z- Q" g8 N& t* `, ?+ V/ H try {$ B8 V; c1 ?: T
Heatbug proto = (Heatbug) heatbugList.get (0);
/ m. T: {" h, U2 P2 ?! ^& m5 }0 T Selector sel = 0 u5 G! i, S) Z/ m6 k! H" g
new Selector (proto.getClass (), "heatbugStep", false); n2 ]' @) z0 ]" g
actionForEach =- M: N5 a4 g* E: \/ _$ [
modelActions.createFActionForEachHomogeneous$call" x7 T7 f0 P7 g8 L
(heatbugList,! l) F- N3 e: \- s3 o1 K E# [8 U
new FCallImpl (this, proto, sel,
9 ^+ X: Z A* U new FArgumentsImpl (this, sel)));
& ?1 A, L0 T' k( h' I9 u$ R } catch (Exception e) {
( c: Q( `' D% N$ ^ e.printStackTrace (System.err); @# a; Z# _1 {5 y. Z% K
}# d2 }3 O; _1 o1 A# P" x
i+ _9 B9 r7 p syncUpdateOrder ();7 u" Z5 i; W6 r0 p* ~5 f3 |! M
; D2 H" f7 _5 z) M; ~ D8 c
try {$ F! u; C# m! T m5 i4 i$ M& E% B6 }
modelActions.createActionTo$message
t, F5 l F2 {3 t( Q6 O( s (heat, new Selector (heat.getClass (), "updateLattice", false));. P% S1 @+ T* _7 A* `1 u ?& U3 x3 |
} catch (Exception e) {3 m4 \) c$ X! r! B, N0 T$ v" U1 R
System.err.println("Exception updateLattice: " + e.getMessage ());
- O" W" x# W9 O. b' L }: R( J3 \2 R6 e
^# S- u! T8 i2 g, B- D // Then we create a schedule that executes the
. Z; O) d/ s8 ^/ B // modelActions. modelActions is an ActionGroup, by itself it: c/ k. i1 u. v8 l& ?) c, r# A' F
// has no notion of time. In order to have it executed in
5 g4 V. B9 y5 \6 c; ~& C // time, we create a Schedule that says to use the
, Z$ c, ]8 Z. o2 ] // modelActions ActionGroup at particular times. This
$ L1 x- T$ F! ~% ]& s5 h" V9 C5 r // schedule has a repeat interval of 1, it will loop every
5 z* m7 M# T5 o" z0 U3 p6 q // time step. The action is executed at time 0 relative to4 V+ z3 a5 @/ b5 Q k
// the beginning of the loop.
0 t% x/ w/ _( C- l' {
5 _: S4 L, t! a7 O // This is a simple schedule, with only one action that is
7 V" y4 W& g' m+ v6 S# c' [/ r // just repeated every time. See jmousetrap for more
8 o A0 N! F5 u: }# X9 I // complicated schedules./ z" ^3 e- G9 j' u r. F
9 B, W& {0 l4 |6 y. x$ [* T modelSchedule = new ScheduleImpl (getZone (), 1);9 Z+ c# g. X) T
modelSchedule.at$createAction (0, modelActions);
4 |, n& t+ \' H/ b3 {
# w- B" s" n" I5 w2 N6 C/ [3 N3 s return this;
$ b: ?( `+ G, ] _ } |