HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 b# m W0 a" |8 \8 a+ ]2 g
_2 z# A7 I! O/ v7 V& L public Object buildActions () {
8 ]% u6 d2 m& W& y* T! c super.buildActions();( @6 [. }7 g3 j/ H% d. W9 [
4 f. x. ~9 |, I' x" j
// Create the list of simulation actions. We put these in
3 f8 |; ^2 Y- a" E' b // an action group, because we want these actions to be
" L+ s% ?* `5 v& J+ X // executed in a specific order, but these steps should
6 h* ^$ x9 H) | // take no (simulated) time. The M(foo) means "The message3 I0 _! {: y2 h" J8 u
// called <foo>". You can send a message To a particular! A7 V! S+ B3 o0 y! ?+ G
// object, or ForEach object in a collection.' g' x% Z+ R: R& ~
^1 M/ ~0 B! B6 u0 _) M0 G
// Note we update the heatspace in two phases: first run: y7 t5 W) h$ \
// diffusion, then run "updateWorld" to actually enact the
/ k7 V9 B& B; w( B // changes the heatbugs have made. The ordering here is. e0 V) e+ ^% P" H
// significant!
4 R6 r2 }/ Z! e' K+ {: W 1 T0 _, @) ~% d6 r+ t
// Note also, that with the additional
: ]* K+ Q( f. c/ ~( i) B8 j9 \$ Y: E // `randomizeHeatbugUpdateOrder' Boolean flag we can3 f; ~. d5 I( Q {) b% M- n% o4 c* L
// randomize the order in which the bugs actually run
) P5 N ?6 B# U) G- f! R# \ // their step rule. This has the effect of removing any
7 r4 Y& E; v( N, e) h5 j // systematic bias in the iteration throught the heatbug9 |) q9 ?7 G: \# M. t( K. u3 Y
// list from timestep to timestep# E- x0 } r+ s* q5 ?
' z1 g% }/ C- I. A9 s: Z // By default, all `createActionForEach' modelActions have9 E( i, p/ \% o* v. j9 v
// a default order of `Sequential', which means that the
5 ]3 W" a. U3 g+ Z$ f( U: @. G3 i // order of iteration through the `heatbugList' will be4 f3 `9 `$ F+ f) J; {
// identical (assuming the list order is not changed
+ |* N2 E1 R3 h3 ^ // indirectly by some other process).% Y6 b# F) m/ D6 p( N
; t7 y/ A& B# o- I( K5 d
modelActions = new ActionGroupImpl (getZone ());- j9 K! |* C! \* I7 y4 I' d: e
) d* M/ d5 J2 g( O5 {4 c! F
try {
0 | x+ a7 N) j7 }& i5 X" s3 c modelActions.createActionTo$message1 i" e- i7 M% H3 N/ @
(heat, new Selector (heat.getClass (), "stepRule", false));
/ H) q, ?9 L2 K# W. S6 S } catch (Exception e) {
! Z) d9 B! ?0 {& A+ L/ I9 t- X4 t$ K System.err.println ("Exception stepRule: " + e.getMessage ());( {& G0 d, l H
}
$ ~0 N' X3 {. Q7 N4 r- E* C K" u* u' H1 C) T
try {
+ { c# q$ i1 E/ j7 e) ]8 H Heatbug proto = (Heatbug) heatbugList.get (0);% Z' K8 _7 {) u1 ?- O
Selector sel = " w+ ^( _3 f g' Y3 r- y. d( U
new Selector (proto.getClass (), "heatbugStep", false);
1 v# C+ k) e6 ?* }& L actionForEach =- i2 _' X" f; m. l2 Q
modelActions.createFActionForEachHomogeneous$call
4 L4 p! z" | A5 ?/ t5 n (heatbugList,
3 h- \* R$ ~- x. n! m new FCallImpl (this, proto, sel,1 A3 ]: t# T+ Z8 w5 W
new FArgumentsImpl (this, sel)));! m h4 m+ D( | d2 n4 b( {3 B% k' ]
} catch (Exception e) {" M& C9 L; k m7 y; g7 u9 |
e.printStackTrace (System.err);8 p- y) i- B8 k
}
1 ~/ ^, o t. c/ R5 f# ?+ g; S* w 2 q- X8 m( B+ o$ z- B/ x2 O- j
syncUpdateOrder ();& m. p. q; e' U! B: C% w
" m& l4 Y# X7 D8 H, E/ _ try {
* G5 L0 l7 W- u3 b& r/ u modelActions.createActionTo$message
* S! {. B8 H) ^0 y (heat, new Selector (heat.getClass (), "updateLattice", false));, ?6 ~# G E1 t& b# E+ e
} catch (Exception e) {* Y" G$ K( f, G* o4 y: K. I
System.err.println("Exception updateLattice: " + e.getMessage ());
0 L8 y$ C- p @ }5 W0 O1 @4 f% q) l2 k
4 D Q5 B- ?) V. N% j2 C // Then we create a schedule that executes the
' Z7 T; a" Y1 @0 A' p! { // modelActions. modelActions is an ActionGroup, by itself it
& ?3 I' w+ N, _& T b/ X4 Y // has no notion of time. In order to have it executed in) X7 z" s) Q; S/ R1 s
// time, we create a Schedule that says to use the
9 W, E1 x( T2 F4 C$ p; f/ V // modelActions ActionGroup at particular times. This% b* y' Q# `0 A, n
// schedule has a repeat interval of 1, it will loop every" x5 x5 |2 v8 b0 F2 _+ T
// time step. The action is executed at time 0 relative to# ` O! z+ Q4 w4 ^* p
// the beginning of the loop.1 {6 f* V" ? U M1 L+ ?) o
- G; s8 o% L0 g$ n // This is a simple schedule, with only one action that is) y3 I! d G% c& Z, Q
// just repeated every time. See jmousetrap for more# [7 E3 G9 i5 G& R4 L7 c7 S1 j
// complicated schedules.
1 i' t6 L% |" U0 j5 x8 e! v 1 I6 r1 Q+ D$ {" Z$ j& k9 J: N
modelSchedule = new ScheduleImpl (getZone (), 1);" z4 j) S) @3 {2 T4 |0 b
modelSchedule.at$createAction (0, modelActions);9 P5 S. c. A$ U) C# f) r6 \' X
8 d u/ J% Y& V: J. a
return this;5 p7 h" ?% y' F& z5 H$ Z% V/ x
} |