HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- ^+ _. V$ o5 W/ ]6 n) j7 o+ S, L2 \" f* N3 I2 W1 X( k' b0 ~: ~
public Object buildActions () {
& C8 y2 T2 M+ f/ S3 w1 _# \4 O$ z super.buildActions();" t) X3 u" ? q% J
+ r# J0 E5 k% X- p8 h // Create the list of simulation actions. We put these in8 U3 Y' O1 U5 x9 r3 r6 w! |
// an action group, because we want these actions to be! g2 `3 H3 J3 S; o/ H
// executed in a specific order, but these steps should
$ k! Z& w. ^2 u3 M // take no (simulated) time. The M(foo) means "The message
r6 J& G( I8 j3 d. E a // called <foo>". You can send a message To a particular, C4 j- S2 r' ?' p! ?& f6 ~! e
// object, or ForEach object in a collection.
2 B. f5 A5 L. J( S C( l {/ b& z
H* l0 B* I2 N5 s, k // Note we update the heatspace in two phases: first run1 \3 A/ b9 ~& i9 B9 E0 \( a! p
// diffusion, then run "updateWorld" to actually enact the$ \; g6 @% w% x7 \* q4 n) _3 q/ w$ B
// changes the heatbugs have made. The ordering here is
; t3 o( [* r/ l // significant!
/ }* [1 P6 Z2 t4 U# L, |0 l
8 m0 I1 M5 P& D$ l3 M5 n$ k: v1 ` // Note also, that with the additional9 x6 d+ W3 m( B# Y9 `# u0 ~" M
// `randomizeHeatbugUpdateOrder' Boolean flag we can" w0 _) X" [/ }( [( q
// randomize the order in which the bugs actually run& |0 b7 c" C7 P7 G
// their step rule. This has the effect of removing any
$ G/ |1 t/ f$ R' r, N // systematic bias in the iteration throught the heatbug
/ E( {+ U9 T7 U: E1 Q# z( U // list from timestep to timestep
) {1 z; N: {( j; ?0 e/ I* T
8 f4 N( }1 W" S8 B" |. P // By default, all `createActionForEach' modelActions have
. Z X% g$ _ i2 _1 x6 P // a default order of `Sequential', which means that the3 l& K* m* P5 S. {4 ~7 u! r5 I
// order of iteration through the `heatbugList' will be" C3 K8 w; ]( Q
// identical (assuming the list order is not changed
7 I- Y$ {8 [$ M$ w1 I3 ?4 ^5 C // indirectly by some other process)., u, i" B+ C, w) I' ~" u
! d/ _. B- ?( W
modelActions = new ActionGroupImpl (getZone ());0 j- V5 T7 A, a% i
2 s% w+ X; G8 y try {
, Q4 x& _5 M- Z* @/ b modelActions.createActionTo$message
* Z& `/ C6 e l2 U1 ]8 W (heat, new Selector (heat.getClass (), "stepRule", false));7 ?5 g( S0 P6 N! Z3 r" W5 q1 W: L+ ]
} catch (Exception e) {
; }( {& J, U; x. u4 Q System.err.println ("Exception stepRule: " + e.getMessage ());
% S+ r q' V' m1 O/ v( e }
0 i! N0 @3 M B; D) s0 [
8 h& g- a! {& P. ~ try {
9 I' p- k/ u3 I1 j2 e3 [ Heatbug proto = (Heatbug) heatbugList.get (0);7 m" A2 B- ?( W6 O+ J/ n
Selector sel =
6 }: r# ?+ n" c5 b' O1 M; [ new Selector (proto.getClass (), "heatbugStep", false);
9 Q) S+ D. q: B/ p- y+ K% g5 O actionForEach =8 N3 z, A) u# ]9 O0 s$ L
modelActions.createFActionForEachHomogeneous$call- [# W d& O" W. Z: ` D# [/ `2 T
(heatbugList,
' J3 c7 B( u) e& L l; M new FCallImpl (this, proto, sel,
4 P/ F9 f2 f1 U# _# A, J$ ^ new FArgumentsImpl (this, sel)));
+ K& _+ L0 z. z; w" A } catch (Exception e) {2 ~! c' L9 B/ T5 g* k5 C1 B
e.printStackTrace (System.err);/ f9 ^ i# L! P. u
}
) \! u2 A# e9 k & k9 `6 D+ N5 q/ v7 {/ K5 G
syncUpdateOrder (); G7 i3 Z) r" ?1 w/ i) r* h
: u2 o/ E4 @' g6 O% O
try {) r# k# z8 ?) V A, B9 y
modelActions.createActionTo$message
6 M% q. }! a" D. U# A6 B! Q% B0 l (heat, new Selector (heat.getClass (), "updateLattice", false));
! B' R& s% J \( I+ K } catch (Exception e) {
0 t3 H8 N/ l J# R3 Y+ \& M System.err.println("Exception updateLattice: " + e.getMessage ());
9 R# L: U8 ~, Y0 ~' k4 ]: ?6 H }5 I: k8 H" [2 V
# e/ L1 x8 y. d4 x/ B! M1 a! o
// Then we create a schedule that executes the
6 z5 Y+ t# v! p& r$ q // modelActions. modelActions is an ActionGroup, by itself it
; T% X0 T0 V) a; J // has no notion of time. In order to have it executed in
# V' ?7 ~9 U- z E/ @ // time, we create a Schedule that says to use the
2 o+ d. w) j4 Y! s // modelActions ActionGroup at particular times. This
2 V0 g# p; [% S& o% q+ D$ ^. @ // schedule has a repeat interval of 1, it will loop every
& @0 d ^' `. }/ w6 T8 Q1 u // time step. The action is executed at time 0 relative to" i) r6 r2 I6 J9 j$ ^5 R
// the beginning of the loop.8 E' }9 U; `3 b
" N8 ^$ E3 \- R0 N8 o( E
// This is a simple schedule, with only one action that is; o6 B* ] G0 j' z" k- E. n( z
// just repeated every time. See jmousetrap for more k( Q$ Q. s& B& j
// complicated schedules.
! c, Y( C* ?1 _! F4 ` . \! T5 V3 Q& x' l# K. r
modelSchedule = new ScheduleImpl (getZone (), 1);
6 f" k6 V; q+ G+ `, Z modelSchedule.at$createAction (0, modelActions);
, L. a& ]5 v# O0 O* [. D0 E
{8 u. g* Q1 l return this;
: F( M$ y s1 z" w5 y2 M8 t6 I6 W6 S } |