HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' f' Z/ v. g7 C, ~* R
% E0 V3 H6 Z0 N2 ~* @; O0 j* x( s public Object buildActions () {3 I% j" L! g; G/ y8 G* I
super.buildActions();6 t6 r9 L g' x- d y0 Y$ Y
9 a2 s) L" C, J" ?: g
// Create the list of simulation actions. We put these in
6 C4 L) i3 T+ d' R' {: u // an action group, because we want these actions to be
" z( j% R" H1 M0 E // executed in a specific order, but these steps should j( _- a! _; j$ n
// take no (simulated) time. The M(foo) means "The message; {/ A: k1 T, }
// called <foo>". You can send a message To a particular
/ h) X: _4 U \" P* a; | L // object, or ForEach object in a collection.
( _1 z# M- u; g' ~' e : P2 `9 e! c6 c/ Q2 `# K( I* o: ]! _
// Note we update the heatspace in two phases: first run
6 c1 v. G9 p7 i i& m // diffusion, then run "updateWorld" to actually enact the( k& v+ Y, L7 M) k+ E
// changes the heatbugs have made. The ordering here is
. C! i6 o9 U) p4 J, A) g // significant!
5 U8 ~2 D+ J6 f" n
. ?0 B/ t) V* ]" \( \0 c // Note also, that with the additional
1 \5 Q" Z& l. r4 i // `randomizeHeatbugUpdateOrder' Boolean flag we can2 J$ r0 l, M4 m% }: P
// randomize the order in which the bugs actually run& s- M/ Y+ X2 t0 q
// their step rule. This has the effect of removing any% h+ p' v* g% E+ [
// systematic bias in the iteration throught the heatbug
3 F: W( Q& M) b1 i4 g // list from timestep to timestep
& H8 f8 {" q- k8 ?# b ' J; p" V3 |; o
// By default, all `createActionForEach' modelActions have! \( x/ A" N/ f% m0 ^- n
// a default order of `Sequential', which means that the& I+ }7 u3 T3 B& ~
// order of iteration through the `heatbugList' will be
( D+ m, n, z% W% y- w+ } // identical (assuming the list order is not changed- q1 H) l( S/ \; ?2 L
// indirectly by some other process).
5 f6 E' m3 z) s, q 5 u. K0 m$ N5 L# v
modelActions = new ActionGroupImpl (getZone ());
* k3 S/ L' w; e" M' x7 m- s7 q$ b( @# q4 s/ O# r
try {
# a7 m) V% S$ `, l2 K* d. z" _( R modelActions.createActionTo$message/ E- J+ q+ v( g) ?; O5 A
(heat, new Selector (heat.getClass (), "stepRule", false));/ l/ t7 o9 @8 \) f5 J9 d* P8 ^
} catch (Exception e) {- b6 b5 w: t" {: t9 o$ A
System.err.println ("Exception stepRule: " + e.getMessage ());1 q4 w- J% i5 n
}! i0 ~2 ?: [0 c% m, A) L5 Z3 _
8 S& P9 |: O- d u) z6 `' w
try {
( n; l8 O+ }4 N3 T3 _! l+ ^ Heatbug proto = (Heatbug) heatbugList.get (0);
, Y; |$ s; @6 S Selector sel = # q* R9 T, j, j
new Selector (proto.getClass (), "heatbugStep", false);: F, T8 Y: E" V' N$ c
actionForEach =: V8 G8 P0 z. I# d3 R& g9 P, g
modelActions.createFActionForEachHomogeneous$call
, l& u g0 r3 m (heatbugList,
8 U9 v0 F2 [% R1 E8 K new FCallImpl (this, proto, sel,
. a6 G* o3 U8 ?* q5 O, g; L; { new FArgumentsImpl (this, sel)));& S5 O2 L3 e7 F' z ]# I9 h+ U
} catch (Exception e) {& X* |8 b$ j j' E. ^$ v8 I
e.printStackTrace (System.err);
1 o" o8 \8 M# [& m }2 P+ x7 o- K3 z6 F" T3 N g
5 J) |! {/ Q3 U6 K9 R& W- @: I
syncUpdateOrder ();
1 e8 b* z' x' p% _" b$ O- e* {, @% m x! V* |4 A/ M7 ]' ^9 @ T
try {
- u w( y# s1 ~8 U4 h modelActions.createActionTo$message
; E2 g/ e$ _: B1 T: `7 S6 p (heat, new Selector (heat.getClass (), "updateLattice", false));' Q9 O" w0 p1 X0 T0 a- H
} catch (Exception e) {+ s4 F- e/ e& Y$ {5 K/ X( y. o" p
System.err.println("Exception updateLattice: " + e.getMessage ());) M1 t3 G* w8 ^) M, o3 s# I0 l
}0 F0 T7 Z# d0 r/ }9 B
5 ]8 ]2 b( O/ I3 D# o2 |
// Then we create a schedule that executes the
: |+ u- N1 F( `( T // modelActions. modelActions is an ActionGroup, by itself it
1 o0 `( X: r0 ]' v7 | z" x7 ^' g0 f // has no notion of time. In order to have it executed in) n8 D8 ~. O) u- o
// time, we create a Schedule that says to use the, r, m! W4 [! p+ L/ D) |
// modelActions ActionGroup at particular times. This* _9 |# {1 x! |6 L" N& W
// schedule has a repeat interval of 1, it will loop every
D, L% q: @- u& o% w2 I* M8 t // time step. The action is executed at time 0 relative to* L8 a& b9 u/ x
// the beginning of the loop.
0 Z2 }& z8 I2 R8 Q- o: B$ [% C! R4 ?0 p& D) R3 l; F6 I, V" }
// This is a simple schedule, with only one action that is
- { J/ T6 W9 E // just repeated every time. See jmousetrap for more ?& z# _+ e6 U" f
// complicated schedules.& O& a5 l8 O- x# j- K5 r, L- Z
( ]' {8 x A* J0 \% |+ o _
modelSchedule = new ScheduleImpl (getZone (), 1);
. e4 U; w! y( d! v* @' Z( K modelSchedule.at$createAction (0, modelActions);. n; Z3 Z! I9 d; F+ J+ E; ?
9 `+ b2 J) F6 l- S return this;. u, W L* ~+ T' d: T
} |