HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, {) a$ e( O0 ~# T
* _# G! u* J+ n- R) o, X public Object buildActions () {6 j1 m' G+ ], \
super.buildActions();
9 w8 Z, x( N# q5 Q- [; e" f! J
3 v Q4 o6 |1 V$ P! a // Create the list of simulation actions. We put these in+ Y) Q, g" V4 {+ L* K
// an action group, because we want these actions to be M* l5 ^" x, G: x& B8 g- ?* C
// executed in a specific order, but these steps should" o, ?, B9 ^' C9 q- G
// take no (simulated) time. The M(foo) means "The message
7 o- o( Q C' [ // called <foo>". You can send a message To a particular
) t, R7 g( L$ p. s // object, or ForEach object in a collection.1 k: |- x% L, s. U
2 s# Q) W; O* O, T
// Note we update the heatspace in two phases: first run
$ t6 i( C+ @4 W3 k // diffusion, then run "updateWorld" to actually enact the
' j! w( Y/ x$ m; _2 | // changes the heatbugs have made. The ordering here is
9 d" t+ C! K- V // significant! W9 _* O2 ~3 i
% S& Y& O0 _' {' |, Q4 K
// Note also, that with the additional
; N) d* U: ^! k" o4 E: n4 k. R // `randomizeHeatbugUpdateOrder' Boolean flag we can
- z4 E* ]1 l/ K1 Z. | // randomize the order in which the bugs actually run
' f, E) J% Z, _1 \0 Z! u0 L // their step rule. This has the effect of removing any
) A* C. A2 a& u9 f- R g& `4 J // systematic bias in the iteration throught the heatbug
# [8 J' a7 e S. C% e' \7 Y // list from timestep to timestep
g# N2 u% {+ `5 P2 r
( f" y1 l8 c' F. j" j/ @' \ // By default, all `createActionForEach' modelActions have
8 N6 M7 \& V8 R! R7 ` // a default order of `Sequential', which means that the. ~7 P5 S& `; M7 X5 d7 p$ s2 n
// order of iteration through the `heatbugList' will be
1 b5 V, B L/ o // identical (assuming the list order is not changed6 N& k+ ^. ]" P0 U9 s. r. L
// indirectly by some other process).
$ X7 s }$ I. R( R9 k 5 y6 j @: P0 {. `; p9 k! k
modelActions = new ActionGroupImpl (getZone ());. \% ~( D" M( n2 `" w, c- K8 }
/ b7 j5 A9 w" g: S7 v( u- \* [
try {
" h( _' a* W {- g$ c& i/ U modelActions.createActionTo$message5 T' E% G' r0 m0 v6 X2 W
(heat, new Selector (heat.getClass (), "stepRule", false));: ` D* e8 z$ O
} catch (Exception e) {' l+ j; t7 G2 Z: U9 C: i: j
System.err.println ("Exception stepRule: " + e.getMessage ());/ A) B3 |/ \8 M3 G0 ^0 ~5 i, K8 H
}( {: C! V1 w; T* [" a
! Y7 Q2 d- j" Q$ T/ n
try {. Y) i/ }( a0 b8 _# r
Heatbug proto = (Heatbug) heatbugList.get (0);
/ w/ @: N! b- r6 H; {& Z( J8 G Selector sel =
$ i$ c+ O4 i2 L7 y1 M3 G new Selector (proto.getClass (), "heatbugStep", false);
3 l" ` y, Z B3 ]3 C, Z actionForEach =. R0 E7 C5 f/ H2 `) ^; H- G0 x
modelActions.createFActionForEachHomogeneous$call
9 O5 j$ n, {8 P) [( m; \# t6 Q5 | (heatbugList,/ a: U+ h: P! q. S
new FCallImpl (this, proto, sel,. n! T; U% N$ f7 d+ E& l
new FArgumentsImpl (this, sel)));8 W) X5 {6 y: g) h; S3 {
} catch (Exception e) {
2 r- @, o6 D& a e.printStackTrace (System.err);
4 w- x: R1 m" S1 l+ S9 }& k }
0 j& M' b9 \; N" ?* n% i5 m * O1 W& Q3 h' W1 k1 u0 ^- z
syncUpdateOrder ();. x/ J& w' o$ Y1 E7 \* ?
& C% W# O+ u/ _) V! }2 m try {6 `9 L5 r0 P$ Q4 D
modelActions.createActionTo$message
6 x( `' S4 z) n; u- { (heat, new Selector (heat.getClass (), "updateLattice", false));/ G0 d& u; E1 V+ ~5 O7 |6 Q; X7 o
} catch (Exception e) {
/ W. G7 |- T, V4 ~4 A System.err.println("Exception updateLattice: " + e.getMessage ());
% \# e- G- C6 ~8 F }
) V: X! h1 a3 Z# H) g5 z5 |! U & C) T! g' |. b" S' V* [
// Then we create a schedule that executes the
" s1 b. b/ X( L' H // modelActions. modelActions is an ActionGroup, by itself it
5 M ?5 J X: f, i% O/ F. `! Q // has no notion of time. In order to have it executed in# a! F, L' q8 e9 n1 h; d
// time, we create a Schedule that says to use the
' Y% `4 [7 b1 D3 L // modelActions ActionGroup at particular times. This
: q2 A" Q8 |* U T9 i6 b // schedule has a repeat interval of 1, it will loop every
5 D; d/ |* e# k3 J) @) K // time step. The action is executed at time 0 relative to
" X/ G: E" Q3 |+ f // the beginning of the loop.
6 y; }( t% p D4 a+ @
) j$ u' i" z/ {, t // This is a simple schedule, with only one action that is
+ l; p+ U5 }8 c |5 }2 m // just repeated every time. See jmousetrap for more9 t$ Y9 I; r, p
// complicated schedules.
; |7 ?6 ?+ E, `1 n O3 ^ # h8 x% y/ h S; g5 ~* [
modelSchedule = new ScheduleImpl (getZone (), 1);) [+ `; k! K! R7 n$ i& x
modelSchedule.at$createAction (0, modelActions);
" Y; B7 W' w! n. k. E6 w
" Y7 N S: e" c; @0 c return this;
d3 Q, d# m& f R$ S } |