HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 U! a3 e/ F+ z- L- q p, t) e1 e( |9 y, V% o
public Object buildActions () {0 N* T6 N0 i- m) @; j# y
super.buildActions();0 @- F6 b9 G, Q a" s
/ C, h2 x L, p3 Q8 ?0 k% X4 u // Create the list of simulation actions. We put these in
- _1 Z$ M: q: G1 N4 a) C // an action group, because we want these actions to be
/ ]4 e, E y$ u& v; M6 ` // executed in a specific order, but these steps should
. e9 [8 D# w: b8 `# n" ~7 x, A // take no (simulated) time. The M(foo) means "The message
8 d7 ]* L# J4 C# H4 \5 L0 ^! X // called <foo>". You can send a message To a particular0 {. J5 u2 h/ m0 i
// object, or ForEach object in a collection., {7 C) P6 P0 z: l' J5 z6 |: H
/ I' b" Z% K1 f
// Note we update the heatspace in two phases: first run
9 |0 f/ U; ^. S. T6 G7 ^ // diffusion, then run "updateWorld" to actually enact the& f1 u* t4 G% {0 ^1 K( A* b3 V3 f
// changes the heatbugs have made. The ordering here is" M1 i4 H2 g6 j8 }: `; j7 n" v( e/ x+ B
// significant!, N: P) O7 d) d: l
' d: R* r! U! c# z. n! E) d$ f, u
// Note also, that with the additional
! I8 N( b5 n+ H3 E0 f; Z4 L // `randomizeHeatbugUpdateOrder' Boolean flag we can
8 @* O9 F, s: g // randomize the order in which the bugs actually run
1 L0 Q% D5 C) X7 k- U // their step rule. This has the effect of removing any1 r4 [+ f( @/ `/ J2 C3 J3 O' @4 v
// systematic bias in the iteration throught the heatbug
2 k& z- A7 f. w3 X$ f) m // list from timestep to timestep
( u- {3 Z7 b# s5 t' h0 j; u& S ( a8 J% B, T. {' o" H( }
// By default, all `createActionForEach' modelActions have; k, S, @! T( r! D
// a default order of `Sequential', which means that the% J; G% e0 a* l# a1 D# M: D6 m/ W
// order of iteration through the `heatbugList' will be# G' F. }) w' D' [
// identical (assuming the list order is not changed( n' s- [) C6 c6 T8 {& P+ t! V* |
// indirectly by some other process).4 ]: h5 d: E8 K9 l/ u+ w
# }( _# K: C5 p2 R& ?3 K
modelActions = new ActionGroupImpl (getZone ());# e; q, D, b2 {" ^% e
! j: f5 \/ R$ d
try {
$ F8 |8 ^, L+ \1 Y) J# c modelActions.createActionTo$message! f) o2 ?4 \/ B0 R& @
(heat, new Selector (heat.getClass (), "stepRule", false));
9 O: g9 b4 B* V0 [5 s1 S } catch (Exception e) {6 \/ \3 |' M4 b* p/ S. ]& e1 J
System.err.println ("Exception stepRule: " + e.getMessage ());
4 l* e3 x. d) O. ~% O. Q }" ]( J5 v* E9 n# U, t6 D
* y/ q2 i6 e! L
try {3 T0 M+ a! O% k4 h$ M
Heatbug proto = (Heatbug) heatbugList.get (0);; b( g2 [; F1 q
Selector sel = : a& v$ J7 K2 _& U+ r) h8 ?
new Selector (proto.getClass (), "heatbugStep", false);: u; C. l, l- \3 t2 N
actionForEach =! a+ n% y0 N/ m+ b
modelActions.createFActionForEachHomogeneous$call
+ b4 Y, b( G- G+ p/ C* ? (heatbugList,5 ]: w+ P) }& k7 w X, ^
new FCallImpl (this, proto, sel,4 x6 k, c3 Y2 J
new FArgumentsImpl (this, sel)));
! G8 Y3 \, g0 a } catch (Exception e) {/ T* ]( }% C1 `2 R
e.printStackTrace (System.err);9 i8 R$ d, l: i* S. H
}4 k R& b$ y) k$ m
" w, i" U' a3 Q
syncUpdateOrder ();
: V8 K; Z! U3 h# m* y/ ]2 F$ i: X( {3 A" \* t% g
try {/ G. v8 {$ V7 Z# z0 A
modelActions.createActionTo$message
8 W: F; O3 {/ ^! Y/ O3 A7 F4 w (heat, new Selector (heat.getClass (), "updateLattice", false));; H) b1 k# h& B- v3 x5 M9 S e% i
} catch (Exception e) {
X E/ D) T" R9 R( K- F: ?# z4 n$ n C System.err.println("Exception updateLattice: " + e.getMessage ());- D6 }$ ]5 d |( G' {
}1 v r3 l8 n3 Q/ S' v3 ~
, i. w. n! I% N: L# T, C. O+ O // Then we create a schedule that executes the
+ t5 g6 i2 e; j$ Q k8 m // modelActions. modelActions is an ActionGroup, by itself it
! ?' `4 I: e. H // has no notion of time. In order to have it executed in
3 I3 @- n. X! i. X7 j9 V. R7 f4 d6 Q // time, we create a Schedule that says to use the# V9 ] J: Q* L; \- e& K
// modelActions ActionGroup at particular times. This
: J) R: y3 Y( E5 \ // schedule has a repeat interval of 1, it will loop every& i1 C; L! r7 e
// time step. The action is executed at time 0 relative to6 @' v3 p. q; h9 i I
// the beginning of the loop., D* E+ p* |9 i7 v" W
7 X9 U! Q1 {0 v$ O // This is a simple schedule, with only one action that is
! o4 ^1 R+ O& Y) |: d4 M$ x // just repeated every time. See jmousetrap for more3 a- X2 c) ~4 V6 `( p
// complicated schedules.
1 o J1 o0 U$ i2 N" P
# w8 k8 x& s5 D modelSchedule = new ScheduleImpl (getZone (), 1);! a# \) H% o q x/ o- l
modelSchedule.at$createAction (0, modelActions);' v' M5 ^3 J8 s- g% [" d
: k- ~/ B8 }6 h
return this;/ V2 m6 J( l2 d/ A2 O* t
} |