HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) e/ \: e8 s5 U2 K, ]! g, j
7 e* W) u* i h, g public Object buildActions () {
& X& |9 }" x! P5 O super.buildActions();
# e0 f. F7 X3 p( \) n+ w 3 @4 O0 j3 `7 M8 `+ o" J, e- i- Z
// Create the list of simulation actions. We put these in$ ^- ?4 ~6 ]; a5 W0 l, Y5 h
// an action group, because we want these actions to be# Y' f8 A7 W- I7 ^0 X; h0 }8 m4 G% R
// executed in a specific order, but these steps should! e9 M9 [* I. k, E
// take no (simulated) time. The M(foo) means "The message
, T! G: g* l3 v // called <foo>". You can send a message To a particular
2 E; @# `7 i' L // object, or ForEach object in a collection.
, C, n: q* Y l) l7 y4 i! J( w- R 6 c. p2 u) }/ D7 {1 b, ~
// Note we update the heatspace in two phases: first run5 ]4 o8 v/ c% }2 y4 ~3 j
// diffusion, then run "updateWorld" to actually enact the7 @, C e, O. K7 y3 u7 V+ T& w
// changes the heatbugs have made. The ordering here is
- g& r4 N4 Z' z* ^5 O, ?9 y# Q4 S( A( ` // significant!* I, h+ O s. b# e
3 _8 L0 [5 o; v: d6 c8 L // Note also, that with the additional
8 i6 J( K9 B, B+ h& s* E* f* T( U // `randomizeHeatbugUpdateOrder' Boolean flag we can4 ]" g6 A! @5 _! U
// randomize the order in which the bugs actually run
/ i* L. i Z& x" q, J% X) } // their step rule. This has the effect of removing any! l; S* }# D+ f" @ `
// systematic bias in the iteration throught the heatbug
9 F" m( m" t, e4 L K+ z // list from timestep to timestep
/ N2 S, ?) z' ? }! t( c1 H
; J7 S% X9 ^- t/ Q# h // By default, all `createActionForEach' modelActions have3 u8 u6 d' C5 s% k2 M
// a default order of `Sequential', which means that the
0 ^! {! _$ f( h9 ~# C! Y // order of iteration through the `heatbugList' will be
1 ~, l1 y8 V& A9 p& d* R // identical (assuming the list order is not changed, s" Z% g+ G2 D. d0 N5 c; C+ ]7 K
// indirectly by some other process).
" [* t5 K, J7 i- w, y
t, @4 n+ N9 f: V modelActions = new ActionGroupImpl (getZone ());' u ^, e) O1 p8 E0 k
( L# q8 [( H% l' N
try {8 p% m( z+ X" G/ M$ E
modelActions.createActionTo$message
$ M- L$ v. I4 l/ E1 K; T: ^ (heat, new Selector (heat.getClass (), "stepRule", false));8 m0 r6 S ]5 x# O0 g; q! B
} catch (Exception e) {
5 z& ^" _2 @' A0 [$ [# \9 Z% s5 B3 D" I System.err.println ("Exception stepRule: " + e.getMessage ());/ ^. @/ O' [1 @' p8 s+ l- E' u
}
F# Y* A" t; ^7 v
, K) Q" d0 m$ L! G) `* a, @5 | try {. z- v' m5 u. K2 H* Z8 y9 w2 S
Heatbug proto = (Heatbug) heatbugList.get (0);& V G; @5 \) v( h# P; F! v! j
Selector sel =
" x! Q' Q& S9 E9 j% B9 l& K1 J new Selector (proto.getClass (), "heatbugStep", false);
2 @" z7 h0 B z! W actionForEach =
. V0 I \6 g" K) M modelActions.createFActionForEachHomogeneous$call
* \, v; d1 m! [ (heatbugList,
/ n7 O1 @# V9 T: o7 M% Y9 r/ c: h new FCallImpl (this, proto, sel,
- _3 ]9 ^8 o6 ?, i; W8 |1 j new FArgumentsImpl (this, sel)));: c8 a0 f" ^) ^+ K& n5 p
} catch (Exception e) {- E( @! Q! V- {$ ]4 x& q
e.printStackTrace (System.err);. K2 L }! _( g3 U& K) m
}
1 ]0 `9 y5 p- T* K+ J) E' t( ` . O# G# e: [* o4 j! g. n
syncUpdateOrder ();
- m; E4 M0 B: L3 ?/ L8 ?' _( B( E: E* g7 E" |8 U
try {
- s% p9 ^$ m% n" x. g/ j) I modelActions.createActionTo$message
+ w5 m# O. c4 n; w7 S2 `/ Q! s9 ~! D (heat, new Selector (heat.getClass (), "updateLattice", false));
, I7 b3 i0 C+ q$ a1 ^ } catch (Exception e) {9 x3 M( J$ m$ O2 M9 T
System.err.println("Exception updateLattice: " + e.getMessage ());
% u R$ \! H$ t6 i) P2 x }
/ e4 V$ W; m* q+ t/ {; }# ` N 9 P$ G7 c/ v; N+ H2 j. w- ^
// Then we create a schedule that executes the" S+ r; \: K" s1 v( e2 g
// modelActions. modelActions is an ActionGroup, by itself it
0 k( S) ~( o* f8 N // has no notion of time. In order to have it executed in
/ d* @2 E9 Z( w0 Y; g: g7 L- t2 [1 j // time, we create a Schedule that says to use the7 a) |/ M* ?6 j% ?# Y# W
// modelActions ActionGroup at particular times. This
% J! k* x4 `: u, k. R* `* B0 y7 c# t // schedule has a repeat interval of 1, it will loop every
! I6 S( u' [1 H0 o1 M+ r+ r // time step. The action is executed at time 0 relative to6 f' q- g5 ~7 f0 [& g0 m
// the beginning of the loop.; ]% z& T8 {* j8 U2 S
$ A4 ~6 g: F8 t* {& Q // This is a simple schedule, with only one action that is( G1 t2 ]' \) X
// just repeated every time. See jmousetrap for more
3 Y9 c6 O* \. Q // complicated schedules.7 w y2 X: p- ?; b
% v$ [! G( v' S& F6 Q* M7 f modelSchedule = new ScheduleImpl (getZone (), 1);
8 m" Q" X! h) O L) w" P0 m3 Y modelSchedule.at$createAction (0, modelActions);0 U v8 o3 E7 n8 u' B$ X
! ~4 g5 @# C; L5 ^( B* y
return this;
; R$ j% u3 O9 G2 G$ f. g# i } |