HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ r. S5 ?6 f, J, q# e" @" w
+ o7 m7 e6 Z8 c3 u$ L public Object buildActions () {6 u E$ k4 U3 L8 |6 V5 l( ]* c4 _# b
super.buildActions();
' K* n3 H% B1 Z
; J. [- m* C8 N& h# T& Q% w$ s7 Q // Create the list of simulation actions. We put these in
8 y0 [4 {7 ]& g( `6 { // an action group, because we want these actions to be% |) K( l( G& g8 @
// executed in a specific order, but these steps should
+ j H, P+ E6 |) K1 L // take no (simulated) time. The M(foo) means "The message/ d; _8 _& X2 g# p
// called <foo>". You can send a message To a particular6 `$ t7 X( Q. M9 k M) W. v6 {
// object, or ForEach object in a collection.
5 w" ^; g8 L: [+ P* v
+ }. Y$ O* _' J- O! o# V // Note we update the heatspace in two phases: first run; B( H! M* s; Z7 t
// diffusion, then run "updateWorld" to actually enact the
/ W5 V# r+ q2 _/ \1 A( O // changes the heatbugs have made. The ordering here is
/ v. P a$ l3 E" r // significant!
4 ]( J& M6 S$ s& i! o5 }- y
# F! s0 c$ \4 c' W! Z! \ // Note also, that with the additional* E3 z5 O ?! |# \" L: f
// `randomizeHeatbugUpdateOrder' Boolean flag we can. b& v5 b9 x/ A. c: e2 Z# N
// randomize the order in which the bugs actually run
4 P7 o- W* K$ R/ Y0 E( U; s // their step rule. This has the effect of removing any$ n# V9 A! N' I
// systematic bias in the iteration throught the heatbug
5 p8 h# T7 |: s0 ~2 m, p // list from timestep to timestep
( j k& z: h6 a1 @/ v5 c) O
3 t8 r' H5 V1 o5 | // By default, all `createActionForEach' modelActions have( ~3 t, b5 t4 q' p5 V
// a default order of `Sequential', which means that the5 m# A; j4 f7 b+ H0 ?
// order of iteration through the `heatbugList' will be
* e! q9 e: d+ m4 Q: C1 t7 C // identical (assuming the list order is not changed
/ L! n' _' q. }7 |7 z4 G // indirectly by some other process).
8 l/ x% I7 @4 G; d5 z7 J& d' i
( v& D& `3 O' Z modelActions = new ActionGroupImpl (getZone ());: h3 P6 G9 l4 `$ b+ G
( G& }: J* C4 R, O try {
d! `" z# k U$ m* b modelActions.createActionTo$message- o1 O) B4 ?6 C4 x! q
(heat, new Selector (heat.getClass (), "stepRule", false));! _" K2 Q1 z' x: w8 b! h
} catch (Exception e) {8 R0 F0 P3 F* _ z0 m
System.err.println ("Exception stepRule: " + e.getMessage ());0 @9 I' _+ @# F6 N/ s. g% {% P, @% J
}, Q+ T9 j3 K* p- b0 e; j# ~* v
% o( f5 } I* b0 Y! z' b
try {9 }- G/ L% w! B0 K5 D% K
Heatbug proto = (Heatbug) heatbugList.get (0);) o5 I: O6 V( t- f5 x3 [ q& z
Selector sel =
3 ~: r' _# k$ U t; O0 p new Selector (proto.getClass (), "heatbugStep", false);8 S& B% l, i6 [* i& u. ?
actionForEach =
0 X: l; N, I: z! X1 }0 J8 v" ^/ F* \ modelActions.createFActionForEachHomogeneous$call% h/ d( R* C! R+ D$ f6 V
(heatbugList,1 J5 P3 ?3 p' d: y3 B8 _
new FCallImpl (this, proto, sel,0 F# B6 X) @4 }6 h- j4 R
new FArgumentsImpl (this, sel)));
! G W, t) z( a% K/ S) i- } } catch (Exception e) {- N5 [7 g3 m5 |' g' x" B
e.printStackTrace (System.err);
( z- t% _6 v2 Z% C& [ m }
2 D8 ]; A+ J. J6 B4 h4 r5 V
o* u7 r5 }# F; I. K syncUpdateOrder ();
6 l, ^, j: w) Y( V2 T- m5 x, A* Q k7 T; Y) C
try {
& h6 u8 f1 V& ^4 m modelActions.createActionTo$message $ I7 a- t; \1 w p- R* F4 C: p! ] |8 O
(heat, new Selector (heat.getClass (), "updateLattice", false));
% Y# s0 J& b9 h } catch (Exception e) {, `: x) Q' I9 t, u5 \
System.err.println("Exception updateLattice: " + e.getMessage ());
+ r# o) h) V$ |' J( Z/ m: R }
& p( Y) ?& j1 Q8 B
9 C9 N* V: m) d+ J% u1 j1 t // Then we create a schedule that executes the+ S5 k9 ?9 j# h+ e/ o7 E6 K& j F
// modelActions. modelActions is an ActionGroup, by itself it
: m" k5 C, Q O1 N; _ ] // has no notion of time. In order to have it executed in
6 a1 s% _3 N$ C9 y% k$ d // time, we create a Schedule that says to use the% {( s" i7 A6 }& d: F2 T5 r
// modelActions ActionGroup at particular times. This
3 B1 g5 l5 e5 ]* i7 }! s1 T // schedule has a repeat interval of 1, it will loop every# G, X' h; o$ X L& N4 O
// time step. The action is executed at time 0 relative to. d v6 c8 [& l# P# a$ v' X( b
// the beginning of the loop.6 i( h2 X! l5 L# @0 e7 G$ R! {
" }* x9 o, H( k, A; t$ s
// This is a simple schedule, with only one action that is
: ^+ e: E; H: ~ // just repeated every time. See jmousetrap for more6 a& v' G6 t3 a, `4 h! L* F
// complicated schedules.
$ Q: n9 L3 m$ {
2 Q4 W: R7 b. j% Z! V- }- i5 j modelSchedule = new ScheduleImpl (getZone (), 1);1 t; i" r" x) [/ g' x
modelSchedule.at$createAction (0, modelActions);
% u* F9 ]( S/ L7 S, Y9 ~
+ F! `) \7 I# C* g) @2 ] return this; y( x, M) b5 q* B% y
} |