HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, V2 I* K) e( S5 ^2 {1 w* E( G* y8 I1 W! j4 G* H
public Object buildActions () {$ L' d% k9 b1 R8 ]2 O% T1 X5 C
super.buildActions();$ M1 Y& A# J- x8 q3 E F2 f+ ?
1 a. }8 r" z# L* x. o: W$ }
// Create the list of simulation actions. We put these in$ F9 h- W* R" T/ x8 u
// an action group, because we want these actions to be
5 U/ r1 {$ D- x" F1 k/ `7 U // executed in a specific order, but these steps should, I+ r5 }: r9 F5 N3 m0 j4 k3 g
// take no (simulated) time. The M(foo) means "The message$ {: R' \1 ?* p( n, A
// called <foo>". You can send a message To a particular
9 ]! }' {5 r) ?/ m( v // object, or ForEach object in a collection.# U6 k. L' u. h+ y: ]' y e
- X8 r7 E8 v' G$ f' Z8 B& G# Z0 s // Note we update the heatspace in two phases: first run# |: T7 z# y* @6 g1 A: N
// diffusion, then run "updateWorld" to actually enact the
3 a# S- p0 ?+ l: [; t, D9 | // changes the heatbugs have made. The ordering here is
9 i. x6 }7 Z0 u# s- a+ L // significant!9 T4 l1 X3 J) @! v: @; `* p' ^3 W
& f; t5 {) S2 V, O6 s, E' g // Note also, that with the additional
! M) C5 f7 q/ S+ h0 _$ ? m7 ]& v // `randomizeHeatbugUpdateOrder' Boolean flag we can
# n: w; i" X. c; N, h5 h& v // randomize the order in which the bugs actually run
2 S s- G: G d1 u // their step rule. This has the effect of removing any
. @$ b; f0 m# r' B; E // systematic bias in the iteration throught the heatbug6 a/ _: _: O" t" d# C% r' s' v
// list from timestep to timestep0 I, U2 \8 z" U- H. s- M9 k
- x8 Y9 y) o8 |0 y. Q) G1 w // By default, all `createActionForEach' modelActions have: b s) o2 V6 @' @( y( \
// a default order of `Sequential', which means that the+ \$ a( R8 G$ N4 X0 s
// order of iteration through the `heatbugList' will be
j5 O* V! N% N( o // identical (assuming the list order is not changed% p V5 J' z* S/ U/ |6 e
// indirectly by some other process).
- q$ {( X/ s, l$ i- Y * ?) G( B) [5 a
modelActions = new ActionGroupImpl (getZone ());7 B& D2 \( U; o
9 N0 ~8 y9 n' S$ G, w3 W5 H; a% G% B7 K try {
" k# D& c+ Y5 o1 _ modelActions.createActionTo$message
/ z/ c& V( _3 P' o# F (heat, new Selector (heat.getClass (), "stepRule", false));7 ]" I( y# {0 W# f
} catch (Exception e) {
) Q& v- A. k. Q0 b4 r System.err.println ("Exception stepRule: " + e.getMessage ());6 o' S% w. x# l) c) t- k
}
# T/ r( D4 Z1 y I6 m7 |! H. q0 `( A8 q" }% ~
try {
, H* s9 Z7 \+ w1 ^1 k8 `8 z Heatbug proto = (Heatbug) heatbugList.get (0);0 W1 e9 d$ m# {3 K, |8 R7 N. e
Selector sel =
" X1 y: w e4 L- O7 y. [' N new Selector (proto.getClass (), "heatbugStep", false);/ c; u4 l! q. g( N" f9 S1 u/ a
actionForEach =
! `/ O, }, p, L6 i5 G0 ~ modelActions.createFActionForEachHomogeneous$call+ d# Z* g' `" B: t& ]
(heatbugList,8 W7 P* o3 F6 y, R
new FCallImpl (this, proto, sel,& V* t3 T1 X9 ^& A8 ]- ~! b
new FArgumentsImpl (this, sel)));
- C" V$ f' u1 B; v4 X2 W( R' O } catch (Exception e) {8 h: x) Q7 d* B$ H
e.printStackTrace (System.err);0 x b+ s- V3 C
}
6 X2 d3 ]$ k4 y- t4 a ' v2 J$ O( w8 a# N5 m0 o+ Q. V
syncUpdateOrder ();1 `3 \# c" G5 d6 h! H0 ~, a- X5 C
7 Y# l% a' L, b2 ]$ j try {# {' B; X+ J6 ^0 O+ l
modelActions.createActionTo$message 5 U6 G; q6 ^6 _* ?3 ?( D
(heat, new Selector (heat.getClass (), "updateLattice", false));
. x# ^, H4 Q# z } catch (Exception e) {& l( {+ x2 |' Q2 q) E, x! G% A
System.err.println("Exception updateLattice: " + e.getMessage ());0 a4 I6 @- Y8 d6 H
}
- W$ _" q$ Y v, q- M6 c
5 B" U7 u2 i) F8 f/ O% \1 a9 W- u9 \ // Then we create a schedule that executes the
. U) s/ f/ ?) x4 q // modelActions. modelActions is an ActionGroup, by itself it+ |2 O3 W5 ^9 J7 A" ~
// has no notion of time. In order to have it executed in
) \; {; S' z0 J% V' W // time, we create a Schedule that says to use the, X3 c* B# B% E" A h; r6 q
// modelActions ActionGroup at particular times. This( Z' ]; n, r0 ]
// schedule has a repeat interval of 1, it will loop every
( w% h# `* a, a4 d% [" M. W // time step. The action is executed at time 0 relative to8 \7 Q: S) @7 p. l1 l, r
// the beginning of the loop.
) @7 F1 K0 _8 I
v' I; B7 R" v2 X. P J* ^ // This is a simple schedule, with only one action that is
" C; Y4 o, e9 d, D# m: f) G; U // just repeated every time. See jmousetrap for more
# h+ [6 |7 E7 j: P // complicated schedules.
# f/ {0 w* f$ @# W1 d5 x4 M( P7 k / Q1 r' q# h# k" p5 B( T& H# h
modelSchedule = new ScheduleImpl (getZone (), 1);* l V' K- `4 `) i% L
modelSchedule.at$createAction (0, modelActions);
3 }3 f7 B! q5 ~" s+ o ( y, J& _6 W. Y* j5 q3 a0 ~" M8 q
return this;' n- _3 t1 }& K. `
} |