HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. b8 e6 R2 g X, K! b2 N
2 U9 B! O9 s1 m2 i A q" S7 K
public Object buildActions () {5 P% z0 R( S! _3 ?/ Y
super.buildActions();; e* F0 {0 l8 H' \% J5 {% t0 l7 j& P
9 F& G" N% y+ H$ G/ y/ p* Q2 `8 M+ P0 T
// Create the list of simulation actions. We put these in9 {2 c" K2 l# K8 G
// an action group, because we want these actions to be
0 p1 ]- O; i+ v6 z$ ~" R. @; H( Z // executed in a specific order, but these steps should6 q6 @, b. W/ J% D; ~
// take no (simulated) time. The M(foo) means "The message# v& t7 L( G& C+ d$ N
// called <foo>". You can send a message To a particular
" ?2 B! S! [! |: d- N: Q4 S // object, or ForEach object in a collection.0 ^" Q) z! _4 x8 _# V, F }
* p( U) j3 s) u3 s n! R8 u
// Note we update the heatspace in two phases: first run' y" n/ x' _* E( |+ z: l
// diffusion, then run "updateWorld" to actually enact the5 q! [; [/ l8 i3 a* ~3 D5 s
// changes the heatbugs have made. The ordering here is
; t: i6 t! ?) v$ A // significant!
& t& A+ ~6 T2 ^* ~& V6 L
# X3 L3 A; y1 F+ w // Note also, that with the additional
; j5 [! B- @2 P. k5 C8 [ // `randomizeHeatbugUpdateOrder' Boolean flag we can
! D0 G7 T: M7 u6 W! L // randomize the order in which the bugs actually run
8 F) u: V k0 l/ X8 C, E // their step rule. This has the effect of removing any- b9 |) v8 m$ S$ h
// systematic bias in the iteration throught the heatbug, g7 H5 W1 G! g
// list from timestep to timestep7 }$ q* e9 _ V) t
. s) G2 _2 Q6 P# ?/ Z! E
// By default, all `createActionForEach' modelActions have
M0 ?2 {" u" ]& w // a default order of `Sequential', which means that the- J o; J/ |# ~2 R& l
// order of iteration through the `heatbugList' will be
6 s2 `* R( f A // identical (assuming the list order is not changed
7 P& j0 b( M2 Z$ K/ \! @ // indirectly by some other process).- z' [) ?4 j6 t- J4 Q0 a/ J; c/ F
) m" m# v I# a- {. q* ? modelActions = new ActionGroupImpl (getZone ());
. ?' ~" T6 t8 U% N) Q& q( ]: t. Z3 w# |' N( J9 X8 V# k
try {
$ X) b& n" [) T modelActions.createActionTo$message" M Y8 S, |' d8 s6 x6 Y+ e2 e
(heat, new Selector (heat.getClass (), "stepRule", false));: C4 V* f# _2 R
} catch (Exception e) {1 b- v# @" O8 `# A! X
System.err.println ("Exception stepRule: " + e.getMessage ());5 H6 [- j( J. w! n; b0 j
}
" Y3 |( w: F: Z8 }% _7 Y- g) {/ y2 r! j6 o# H
try {
* @/ J: @. C5 ?+ l- o4 I Heatbug proto = (Heatbug) heatbugList.get (0);
/ g9 S2 m5 c$ Q. x/ }8 W Selector sel =
# f) l, `* ^% } n2 p. n new Selector (proto.getClass (), "heatbugStep", false);0 g4 m6 i( i( ^! e
actionForEach =2 V9 v f1 L) D
modelActions.createFActionForEachHomogeneous$call# Y9 z( s/ K. i0 |& a8 }' O
(heatbugList,
" y6 ]4 _) k s new FCallImpl (this, proto, sel,/ E- `8 ?$ ~" a% L
new FArgumentsImpl (this, sel)));
; r/ b, Y! I+ t# J } catch (Exception e) {
" b0 i; E7 O% x e.printStackTrace (System.err);
( u- j! A0 m% T/ E e( j: C5 ? }2 U! s. H! M% q1 t
n& H( p! ^' I8 O2 ]4 Z: z# y syncUpdateOrder ();. O V3 `4 }8 N( \/ _
8 U* t+ a, z' t8 @" Q
try {# h& c# C6 z# g8 m) _. y5 n9 l" d) R
modelActions.createActionTo$message
3 V$ c% H* x# x1 Q (heat, new Selector (heat.getClass (), "updateLattice", false));
& B+ J! B% j6 o0 U! z } catch (Exception e) {( n. y1 S! N! P! ~/ K! T
System.err.println("Exception updateLattice: " + e.getMessage ());
. {0 I% D5 m- | }9 m, U g! U# ?; e. W
5 g( n: ?+ N! u% F9 t) K* C1 g // Then we create a schedule that executes the4 l6 C# ?7 x5 Q- F
// modelActions. modelActions is an ActionGroup, by itself it) ^4 Y/ \0 X3 l/ s' h. N9 O7 m
// has no notion of time. In order to have it executed in) D' }4 {, j9 a$ V/ A! b7 J
// time, we create a Schedule that says to use the
; k- b( ?$ [% H2 j& V8 z$ a // modelActions ActionGroup at particular times. This0 e6 T0 P9 C3 v) R: D
// schedule has a repeat interval of 1, it will loop every) N: |+ B6 B/ u) v' ?
// time step. The action is executed at time 0 relative to& ]' L9 a$ a3 @7 P/ u V- {9 F. N, _
// the beginning of the loop.0 D5 _# c; K' l q% {
7 E" Y. b) O; R! f) ^ // This is a simple schedule, with only one action that is3 e; B: q+ S% w$ e+ t. g
// just repeated every time. See jmousetrap for more: l7 b8 r y! y" A/ O, ^* a
// complicated schedules.) T2 U* \4 k" _! }
6 g8 z! }0 \/ l9 Z: K modelSchedule = new ScheduleImpl (getZone (), 1);) B: ]+ w, k+ j
modelSchedule.at$createAction (0, modelActions);
+ L, G [0 e0 K) n6 K : D5 E# e* o; f9 Z) s9 J
return this;
( Q# Q6 L7 \0 U8 e9 K6 ]: u } |