HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ O2 p, H* A6 M4 A8 h' r3 h
/ }. j r5 N* | public Object buildActions () {8 \% h( T6 M6 r; g( u
super.buildActions();: D9 f* L, f1 S. {
0 g: B2 e; n! b* M, @" U
// Create the list of simulation actions. We put these in7 ]* l6 U1 K; m! {1 e* r) `
// an action group, because we want these actions to be6 {7 f) `! q, }+ E
// executed in a specific order, but these steps should, U: B8 u2 U: y8 u t# X8 [" Y S; L
// take no (simulated) time. The M(foo) means "The message3 ]- M k* r: L* U# | e- c
// called <foo>". You can send a message To a particular+ d* I0 B# v+ x6 B; r& T
// object, or ForEach object in a collection.0 z# a6 }! ?( m, d Z& o1 q& i& h
- Y, K: O# r$ ^& X" Q" R) F' k
// Note we update the heatspace in two phases: first run5 y* m; @6 `( h9 R, t* r9 B4 e
// diffusion, then run "updateWorld" to actually enact the
" C, W0 I( j3 N0 P8 {/ ] // changes the heatbugs have made. The ordering here is
! |4 w3 x, X% q. T0 j // significant!
2 O* Q# s2 G) n+ s, `, } # H; Q8 C( B+ O
// Note also, that with the additional
; P7 \/ ?& F! q# w/ \/ g* G // `randomizeHeatbugUpdateOrder' Boolean flag we can/ u( r% L. s# H' U
// randomize the order in which the bugs actually run8 k J# Q: n4 P* Y$ @: V
// their step rule. This has the effect of removing any' M1 s: Y! j' Z5 F- q3 a
// systematic bias in the iteration throught the heatbug
9 K1 }: C5 E, C7 x // list from timestep to timestep
& z" J I0 u+ r) ^/ @- h. w, L+ ^. J8 }' r 7 |2 s+ ^1 W9 H! y) V
// By default, all `createActionForEach' modelActions have
7 E4 H N* o* W ]4 }: H+ _* N // a default order of `Sequential', which means that the% A% ~: R) p* L g0 ]2 w4 e/ w' t
// order of iteration through the `heatbugList' will be
9 V) W( s9 I1 k& h- Z# @8 ^ // identical (assuming the list order is not changed
3 [9 l) A/ M; H# a2 h // indirectly by some other process).2 y" E+ \/ i- B. q+ g! Y( T! W# @
/ x: M: Q$ b: P6 o
modelActions = new ActionGroupImpl (getZone ());% W i& p% O3 o& }# ~
3 K& Q) l7 C- c" J7 C try {0 \# j6 _1 K' e w: ^. \# b( C
modelActions.createActionTo$message) w; w9 l( D8 t) ^# b& _+ m" p
(heat, new Selector (heat.getClass (), "stepRule", false));) g( V% u2 H( H2 @
} catch (Exception e) {% ]& Z1 R, C+ M$ Y* S+ t
System.err.println ("Exception stepRule: " + e.getMessage ());" W8 E" x4 x4 z
}" g$ t/ }3 j7 c3 R
3 g" g1 z0 \( x) \7 ~
try {
4 h _" F9 @. O7 C Heatbug proto = (Heatbug) heatbugList.get (0);" ^8 |2 X" e! e6 y' P/ F
Selector sel = 1 \. S4 ?$ V+ ^
new Selector (proto.getClass (), "heatbugStep", false);
* ~+ V; c4 ^, c( b actionForEach =- @! O1 X5 ]$ j x
modelActions.createFActionForEachHomogeneous$call( }9 S8 I& q) R; u
(heatbugList,, M3 n; ^" o' B; j
new FCallImpl (this, proto, sel,2 W% I% {, B5 C
new FArgumentsImpl (this, sel)));# g2 k# k. n2 O6 l7 A6 _2 |
} catch (Exception e) {- }% a! Q$ Z$ K4 J S) q
e.printStackTrace (System.err);1 C% ]6 _4 T6 V% M3 Y
}6 {& [; S+ _9 c- X
7 l/ {5 T7 B1 }: x! ` syncUpdateOrder ();- n0 I4 o! _$ w, v
5 H3 N8 v4 O) }/ f/ p3 v9 Z# s9 o try {% Q% [& o- \ X: L R/ a; J
modelActions.createActionTo$message
, r% i1 _3 Z( G# i+ ~ (heat, new Selector (heat.getClass (), "updateLattice", false));6 p9 \* z i$ ]/ a# ]; w- O
} catch (Exception e) { V6 h1 X2 p) `, \( @8 u
System.err.println("Exception updateLattice: " + e.getMessage ());3 a4 k$ ?8 s) e" L% D8 X( z
}7 r' g3 L; t$ q, l4 w5 J
; q$ f- T* _1 J" T3 ^
// Then we create a schedule that executes the" h; P7 r; ]+ R, H) [/ l e7 p
// modelActions. modelActions is an ActionGroup, by itself it4 v+ k2 ^! @" m' A
// has no notion of time. In order to have it executed in# j* c% g \. d! K; |* b, U
// time, we create a Schedule that says to use the
z7 ?4 V6 q. L3 t& ~ // modelActions ActionGroup at particular times. This
/ x3 |2 X% D' X; K' K0 Z // schedule has a repeat interval of 1, it will loop every: B. |) _2 p3 S
// time step. The action is executed at time 0 relative to
5 Z0 Y4 x M+ t' c3 K% ] // the beginning of the loop.+ u0 E$ [6 f+ M. M" O* y; y
- @) H. T$ F# x0 }7 t/ ]9 |! I; e
// This is a simple schedule, with only one action that is# [# }' o) y1 l
// just repeated every time. See jmousetrap for more- m% O- X7 J/ [5 u T
// complicated schedules.
6 q. p3 @- |$ B5 y- T1 I
, G# l: {1 h \4 r1 m/ _ modelSchedule = new ScheduleImpl (getZone (), 1);, s1 U2 [5 L% O z( f0 b
modelSchedule.at$createAction (0, modelActions);
# m8 m& m1 E0 T% y3 C6 \5 n3 m 0 w/ [, l4 m$ x& J
return this;- Z8 }3 @$ a$ o0 V i; r6 z
} |