HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 I) Q3 j/ }- D' _
! S- t6 B! p0 q5 j7 g0 f public Object buildActions () {. T' ?* o/ W" q
super.buildActions();7 Y' A2 I* }+ L2 ~! ]
, l4 q+ c1 e W+ r0 S6 F7 o# k // Create the list of simulation actions. We put these in
! I2 {) L5 F# Z4 W( Q. v! ] // an action group, because we want these actions to be: _: Z8 _; M5 x* f7 c j
// executed in a specific order, but these steps should7 [$ v0 l" u( F$ L' n$ K* ~
// take no (simulated) time. The M(foo) means "The message8 w- G, q0 q. D
// called <foo>". You can send a message To a particular, h7 a9 _+ t" @, L+ M
// object, or ForEach object in a collection.
4 J0 p/ H5 q( m
* D4 h$ h! z0 U: N. _7 U // Note we update the heatspace in two phases: first run& j0 X+ S s- h( U- Q( u/ J- u
// diffusion, then run "updateWorld" to actually enact the
1 j' q. J! }* | // changes the heatbugs have made. The ordering here is
5 p' L3 l# T f9 }" L* q' { [ // significant!
& K5 {0 {% l2 X ( T C1 ?4 g. R' N# w3 }# B
// Note also, that with the additional( Z1 O4 M0 K# ]$ l( l
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; ?7 z* J; d2 q+ X // randomize the order in which the bugs actually run
6 x# c' d6 i) \7 N9 \$ I // their step rule. This has the effect of removing any! Z z- v! s0 n- p& w% ^
// systematic bias in the iteration throught the heatbug
# [! t+ J" ]* U: [0 w // list from timestep to timestep
% V6 x0 A1 N& N6 s! S4 `7 [4 j- O
, F B5 C$ V# O: l ]6 i // By default, all `createActionForEach' modelActions have/ }8 _. h8 H5 u7 f
// a default order of `Sequential', which means that the8 k* x* V: i3 p
// order of iteration through the `heatbugList' will be
& H+ w9 e1 L( I0 t4 e5 Y6 y0 B // identical (assuming the list order is not changed
1 V- g; m6 b7 E4 y // indirectly by some other process).
! Z/ ^) B# X: Y& I' D! I! ^ ^( F" g 0 `# E) d& q l: Y1 b
modelActions = new ActionGroupImpl (getZone ());; \4 Z0 Z1 D. Y% G. Y
. y3 v. E6 p7 M try {
" F. r: h( j* t. {; O% n modelActions.createActionTo$message c X4 t# Z& e3 ?
(heat, new Selector (heat.getClass (), "stepRule", false));7 K) F. @& S/ |4 [
} catch (Exception e) {
/ A4 }: R8 |) O0 X. F: { System.err.println ("Exception stepRule: " + e.getMessage ());
0 }' \; g! O( x* l* k }
, S; w7 k9 g e% E* A9 ~
x% b3 f& X3 Q try {% C, y1 h! `7 \# ]4 S4 g
Heatbug proto = (Heatbug) heatbugList.get (0);1 L, T2 X J* C2 o, U: ^
Selector sel =
2 M! N% c# k+ Y% ]* G: c9 ] new Selector (proto.getClass (), "heatbugStep", false);# N. P) ]4 R9 p4 k. G6 A
actionForEach =
& P. |* Q) z+ R4 Y! C/ M modelActions.createFActionForEachHomogeneous$call
" Z. C9 T) ]* p8 e9 T5 z* }* z (heatbugList,& q. L7 \& f" ]
new FCallImpl (this, proto, sel,8 I# S1 S8 t w( A) }
new FArgumentsImpl (this, sel)));* m$ V9 B6 I& T6 ]7 L# K, k
} catch (Exception e) {2 L& P% t, @$ a! w" @$ }: N
e.printStackTrace (System.err);9 U' i3 v& P1 o$ d) f
}+ w& i, n/ W8 X9 B7 L
8 Y7 C% ]$ _8 v: j syncUpdateOrder ();* d" @- q5 B& [7 n- I( q
y& t3 L4 |7 Q9 _$ H4 E
try {
Q/ S3 S9 `0 m# ~! n5 { modelActions.createActionTo$message
w! b; s8 \% W( T/ @ (heat, new Selector (heat.getClass (), "updateLattice", false));! {# H5 i( x3 R" C. g! ^
} catch (Exception e) {0 [. t+ U. M+ N( _3 ^8 o. b2 k
System.err.println("Exception updateLattice: " + e.getMessage ());
# |4 H# I( w9 n6 w }
- ]% H/ N: X. P# T4 x , |7 N( h6 c3 Q3 c6 S/ H2 Q* _! `
// Then we create a schedule that executes the5 C5 X5 N& H- c( f; p# o
// modelActions. modelActions is an ActionGroup, by itself it
' C+ O0 p4 d7 u& X3 Z // has no notion of time. In order to have it executed in
# A. f+ E; I7 V* B' u0 A( h# Z) d! _ // time, we create a Schedule that says to use the
8 e' {( @& h1 }, {9 n( U // modelActions ActionGroup at particular times. This
7 P1 ?1 M5 b- F6 I // schedule has a repeat interval of 1, it will loop every, l. \0 L9 G8 B8 d+ |7 c N. Y
// time step. The action is executed at time 0 relative to N/ n2 I, A# e( O; |" C4 N, B: ]
// the beginning of the loop.) v9 l2 I& s/ F+ f2 Z6 y3 x V Q
0 K- V1 \, A, ^( [4 C // This is a simple schedule, with only one action that is
1 S& ?: L- E: o* U, g5 f3 D: _! X F // just repeated every time. See jmousetrap for more
' X, o( G a$ o5 ~4 E: s6 x2 W // complicated schedules.1 q5 n1 R* W# f: ^
, v; p: K+ d+ s
modelSchedule = new ScheduleImpl (getZone (), 1);6 T) [+ A2 ^6 Z6 p& i1 P& e# H
modelSchedule.at$createAction (0, modelActions);; ]5 O( [6 ^- [) ?
! g* [$ O2 W- t% G+ |7 t4 | return this;; n8 b& f# q4 ~! ~3 I
} |