HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! x. O3 F$ F+ W2 {
H' F1 I9 K: C% [& L public Object buildActions () {
/ x" d$ i" j- m& r' B super.buildActions();
/ \ y) E* h7 ]3 @# m
* x6 ~, E+ N7 P+ J // Create the list of simulation actions. We put these in7 W: O: X" m S& `
// an action group, because we want these actions to be
, p$ T; r2 u6 t, Q8 ]7 M // executed in a specific order, but these steps should
1 Z6 } ~1 c4 K- S% c // take no (simulated) time. The M(foo) means "The message
/ G2 |9 [, V, L0 I // called <foo>". You can send a message To a particular2 s1 [) S/ W6 ], [
// object, or ForEach object in a collection.
: l O3 f7 G7 }5 F 1 j( d7 d. b" m) L8 u
// Note we update the heatspace in two phases: first run6 S% S3 }% `. d( e
// diffusion, then run "updateWorld" to actually enact the
/ h8 ^- F5 F; o; n+ x0 C( _% F // changes the heatbugs have made. The ordering here is+ M9 K O0 b' y) p1 [1 C- O
// significant!+ t2 S9 F6 Y; V/ U: I
' b! ^/ f* V2 t9 ]8 f2 x- v3 \$ O3 o // Note also, that with the additional& t4 r( O8 ^9 o5 f8 R
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ p3 h. f: ^% I$ |, A' |& K- |
// randomize the order in which the bugs actually run
3 M2 c$ u; \0 a& {, @7 S% G // their step rule. This has the effect of removing any
$ u1 H; P* ?, u1 }4 v# L9 L9 i& u // systematic bias in the iteration throught the heatbug1 c% y( G7 {3 r$ e6 J; N' C& V
// list from timestep to timestep
0 C/ j/ h% G5 f! R/ P/ c a
3 _1 b# U l! H* _3 b7 N: J // By default, all `createActionForEach' modelActions have! e0 z6 S5 c6 I% w$ p
// a default order of `Sequential', which means that the/ h6 x3 j1 N* ~; _4 r4 E
// order of iteration through the `heatbugList' will be
# ^3 ^3 b: X+ I! e: { // identical (assuming the list order is not changed- M) W% V8 g4 D# A' q
// indirectly by some other process).
, s! r' g) G! P2 i. o0 f2 n
( D5 I) {1 ?3 |! n+ s modelActions = new ActionGroupImpl (getZone ());$ ]% W$ G# @7 ]1 g, F2 l" _1 @
. a* ]8 Y) l* ^- ?# W) k+ M try {1 M1 a9 v9 m; D2 M/ Z! I6 I
modelActions.createActionTo$message |! O: q3 Y. Y5 P
(heat, new Selector (heat.getClass (), "stepRule", false));" ^, M( A* I$ J1 j
} catch (Exception e) {
; [1 E* a0 P( C9 z& L System.err.println ("Exception stepRule: " + e.getMessage ());; E+ ?: f ~8 l/ |
}! V% C6 M% N! |9 d, r
5 \* B2 j5 E" x/ E1 j- [- ~' U! s" B try {% n# f$ n- m0 v
Heatbug proto = (Heatbug) heatbugList.get (0);
5 U+ o, P2 z' l( J5 C* v Selector sel = ! u8 F0 f' }& E9 s$ L( A
new Selector (proto.getClass (), "heatbugStep", false);7 c2 V5 Q( ~0 r+ r0 G
actionForEach =3 K4 D8 T0 ^% U2 z: Z
modelActions.createFActionForEachHomogeneous$call* R# ?3 z- N) b. r
(heatbugList,3 I% ]0 X$ o0 x) n7 u2 g \( ?
new FCallImpl (this, proto, sel,
* j7 r, _7 a5 o( a+ O* [4 s/ I new FArgumentsImpl (this, sel)));. [, ]) P G* ?" t4 }* j Q( ]
} catch (Exception e) {
# k- |5 t$ u' t e.printStackTrace (System.err);* K% \$ p v. I2 D. ^& f3 Y
}6 }: m# T# S! G" `; x
& S5 M( z* F6 M f! M syncUpdateOrder ();6 M$ h; S9 m. p) Z
' Y$ o0 p7 G. P8 A4 W, m+ v
try {) o6 ]/ s" g* P5 @ Q
modelActions.createActionTo$message
9 z- j" Q' V3 k/ i1 t (heat, new Selector (heat.getClass (), "updateLattice", false));" _4 h0 k3 w8 F% w0 I. y
} catch (Exception e) {8 `! p) i3 `2 _& [+ r- t
System.err.println("Exception updateLattice: " + e.getMessage ());
- N0 n; J& R. ~- { c7 O8 F }
) F' ]) D/ ^# w) ]* `# N
; @0 C$ S& ?5 ^) _( }' L // Then we create a schedule that executes the. m; c5 _/ w w- I" Z, E* O+ C* u
// modelActions. modelActions is an ActionGroup, by itself it
. G' w8 s* [& g. T) U& J$ i* Q // has no notion of time. In order to have it executed in
0 G& V4 S7 @6 n4 I; S // time, we create a Schedule that says to use the) y/ M* F- l3 b, z1 ~$ d; Y* @
// modelActions ActionGroup at particular times. This
' F, y1 `+ N0 @9 k; G1 X5 \- [7 T // schedule has a repeat interval of 1, it will loop every, e: W, [3 H0 |7 q" D: N& E
// time step. The action is executed at time 0 relative to
4 C M& \0 Y" Z // the beginning of the loop.
* @8 x: ]4 W: k% n" P7 P* T6 v, S8 q7 e& P2 e3 B, R
// This is a simple schedule, with only one action that is+ b) z4 o' ?0 b) T
// just repeated every time. See jmousetrap for more
* `3 I: V5 U' Y# _3 \1 l // complicated schedules.
: O: y9 ^. T. Q6 x$ B2 G 1 e! u6 @$ O$ x4 z- Q
modelSchedule = new ScheduleImpl (getZone (), 1);+ ]" M( j Z* C- M" [; v
modelSchedule.at$createAction (0, modelActions);
& M7 C* D: T" u
* y: |' g/ w3 i( {9 N: J return this;
" v5 { J3 @' B' s } |