HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 v7 s; ]# r0 x5 O
9 E8 e# b' N! b0 i public Object buildActions () {0 I; m% }6 E C' o
super.buildActions();5 P" I X1 Z* s
' ?- H3 i, a. F1 `8 I
// Create the list of simulation actions. We put these in
* [( \! `6 W- a // an action group, because we want these actions to be6 t! F' q% x/ f+ @# r
// executed in a specific order, but these steps should
2 Z4 Q1 c9 {0 @! N" v+ x8 } // take no (simulated) time. The M(foo) means "The message) k5 t6 w0 c$ @
// called <foo>". You can send a message To a particular! G% U3 ?% G/ }0 q4 C9 P
// object, or ForEach object in a collection.; Z/ W& O8 {$ i7 ^* _
/ ^1 I+ V! I) A0 e( r // Note we update the heatspace in two phases: first run& Y# Y! [6 ?, f# M3 a) ~/ x
// diffusion, then run "updateWorld" to actually enact the
1 B/ W1 K3 C7 f% @2 R+ d( f' S // changes the heatbugs have made. The ordering here is( t$ }' K& g5 k6 L b/ ]8 E7 F; H
// significant!/ F/ s. [) S+ j. l- v
- L5 t& c4 D! }4 M; f. H
// Note also, that with the additional) P0 w9 ]6 j s1 g! i$ C
// `randomizeHeatbugUpdateOrder' Boolean flag we can
7 i W& O& J$ p- S; R, z // randomize the order in which the bugs actually run: Y' U3 q* q; P% s
// their step rule. This has the effect of removing any# y3 m! n) Q U, \, T) ]' F5 L3 N
// systematic bias in the iteration throught the heatbug' F8 d" D# J3 R) x' X
// list from timestep to timestep
6 c% t) h( R( \ a2 z- _2 ?
( W8 ~" D' V7 x6 s4 u! l, T, {$ _ // By default, all `createActionForEach' modelActions have+ Q: a9 \: f" \/ U
// a default order of `Sequential', which means that the1 ~8 j; n) B% W& h5 N1 b4 C
// order of iteration through the `heatbugList' will be: }6 o" A8 f7 C7 A1 w
// identical (assuming the list order is not changed
* }: T: S; w1 Q$ k6 H // indirectly by some other process).8 I5 Z/ ^; e4 h. d Q- M
9 m/ x6 W6 ?, E( [# _7 m* `& o modelActions = new ActionGroupImpl (getZone ());8 M+ K7 f! A5 h" p8 W$ V1 Y1 s
3 r1 y' j! O5 b! t/ O try {0 i, {! `! L5 @
modelActions.createActionTo$message3 \- G' \' a+ t; Y M n; |
(heat, new Selector (heat.getClass (), "stepRule", false));% U$ R$ U) A' K9 }3 \" E5 d
} catch (Exception e) {
% y# K: W! S- T' }1 q6 n* V% m7 X System.err.println ("Exception stepRule: " + e.getMessage ());
* D; ?, ?$ }; u" ~ }* f5 e5 ]) K* h9 \. N( [
{% S8 Q* X# d
try {
" q( `: V7 Q* L" @3 [4 _ Heatbug proto = (Heatbug) heatbugList.get (0);
! q& @' ^. ]6 ~ Selector sel = . a9 H: K5 S& K4 X# t5 x+ I0 m9 U4 P6 p
new Selector (proto.getClass (), "heatbugStep", false);( {; s/ A/ m, A+ D7 m. ~( L8 h
actionForEach =# j- n" o' g X+ s
modelActions.createFActionForEachHomogeneous$call( a F* g1 S% t B$ b& @7 Q
(heatbugList,$ ^: l, k ^) h' h7 ^. w9 k! v1 s
new FCallImpl (this, proto, sel,4 c( ^- k- u$ i( I5 r
new FArgumentsImpl (this, sel)));2 m6 q. w* G" I7 \( v
} catch (Exception e) {
; O0 a, t$ C% K2 t3 O, d e.printStackTrace (System.err);" ]* I! o5 `) s( @/ Z3 y- u
}
5 _6 o2 D/ d# f
& j/ r" x" K$ T+ b syncUpdateOrder ();
8 l% i: C: g, Y _5 S8 r, E) o2 B3 I" m+ a l4 A1 v5 n& v
try {
7 G# V& f4 R2 ?* p, Q modelActions.createActionTo$message
4 f" H: w* I5 K( h3 \ (heat, new Selector (heat.getClass (), "updateLattice", false));
2 x6 P2 X/ a4 Z+ z$ m0 k; `. w } catch (Exception e) {* b+ D. Y2 Q0 b: p2 m# ~
System.err.println("Exception updateLattice: " + e.getMessage ());/ h8 L, d7 Y7 t" [9 P1 D/ d
}
% \, e7 X# H) J# Y v( @, J0 } & B/ i' P# I& ]: @. l
// Then we create a schedule that executes the
5 s% j3 w3 o* [8 a7 U // modelActions. modelActions is an ActionGroup, by itself it0 N' d* n' A/ A6 T( r( G& M9 k: F
// has no notion of time. In order to have it executed in
% e7 x0 K( d, t0 t7 S3 e# t( V, g // time, we create a Schedule that says to use the
" F4 O" [" g# y8 s$ { // modelActions ActionGroup at particular times. This
% K4 ?1 M% {) _" h% K$ {: t3 s // schedule has a repeat interval of 1, it will loop every
! ^7 l6 s" _ W! V7 T3 R // time step. The action is executed at time 0 relative to- G% T3 L4 m& w. q: A7 s4 T
// the beginning of the loop.) a$ f% ?4 P) }9 J0 r. N$ B
; ^4 i8 d% I4 {$ _0 R
// This is a simple schedule, with only one action that is
: u# W) Z: |4 [" v' c y+ h4 y; N // just repeated every time. See jmousetrap for more
q, c- y7 |, F( a* q* H" W // complicated schedules.9 g+ }9 l. [, r( H) G1 e
# c( ~/ @6 G6 ]+ x
modelSchedule = new ScheduleImpl (getZone (), 1);
2 r3 N9 f3 P3 x7 P: I* e modelSchedule.at$createAction (0, modelActions);8 j! h7 X: z5 b
7 S, m( d" R! C; H9 E/ c& D return this;$ T2 J- J) {/ H1 V4 Q
} |