HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- z* q& _. y( w/ F6 w
+ ^; T6 B& G; i6 e' ~" k public Object buildActions () {
2 S# `4 e- u% v& p X1 ` super.buildActions();1 a2 c7 F2 |( R5 d+ S
" b- J* V3 E" I // Create the list of simulation actions. We put these in
6 c$ m! @8 ~; A- A8 I // an action group, because we want these actions to be
! `. n+ [0 D- i/ m // executed in a specific order, but these steps should
0 G- z7 R- G4 e7 o* U( p% S // take no (simulated) time. The M(foo) means "The message
+ V* k& J) ~" B [ // called <foo>". You can send a message To a particular" u/ e4 h ^3 k5 Y
// object, or ForEach object in a collection.4 `" M+ g6 K6 E5 Y; c
" m; B1 l+ o6 i' |/ B& M
// Note we update the heatspace in two phases: first run$ j) ?& k' K0 k) z, x
// diffusion, then run "updateWorld" to actually enact the
8 Q6 ]3 Z/ v& | // changes the heatbugs have made. The ordering here is
' o" _8 Q# G) m/ ~ [ // significant!
, [3 F& Q) I7 n! t% E0 K
6 {+ N1 o z/ H0 z3 `/ _ // Note also, that with the additional! {$ Y [/ _/ ^! T, s& E7 B
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ U2 d8 x4 u# H$ L8 R4 \% f
// randomize the order in which the bugs actually run* K7 X; Q% I4 i7 C- m! ^# t
// their step rule. This has the effect of removing any4 P1 K8 e( _: F8 @1 {8 h# F+ ~' v+ i$ X
// systematic bias in the iteration throught the heatbug6 T& k9 F# \7 A% ? x+ {# J i
// list from timestep to timestep
2 ^! d9 z7 g7 [6 {& r, f- D
( t! |1 y/ U' v; M // By default, all `createActionForEach' modelActions have
0 _) X! o- p! i" d$ ^5 x) K, O, } // a default order of `Sequential', which means that the
. @; {) Y4 F& Q4 ]4 p8 x) o // order of iteration through the `heatbugList' will be
9 g0 z$ T9 W- n/ m' ~6 F // identical (assuming the list order is not changed
6 f, \" @! H3 ^5 Q7 I7 c // indirectly by some other process).
V$ N. p" T7 l) w. {3 i# Y
% E& ^9 c$ K8 D% L* G- { modelActions = new ActionGroupImpl (getZone ());" e7 T! N9 s6 c2 u
! N' i; E5 G- {# D4 _* U8 U try {
) P5 S5 _* q! Q7 R8 y' |1 r modelActions.createActionTo$message/ C {( \- F- t8 x, r
(heat, new Selector (heat.getClass (), "stepRule", false));
0 d- K3 ], U7 Y$ l. Z1 H) ~ } catch (Exception e) {
" x+ e! \8 M& k t+ X8 O; ?) C) X System.err.println ("Exception stepRule: " + e.getMessage ());8 U$ S! a4 K2 e" Y' h- q( }5 W( |
}
! c; \3 C. f. I% o: l
" D/ Y6 G2 \' F' g: R try {" p, U2 \: Z7 S' C8 n
Heatbug proto = (Heatbug) heatbugList.get (0);
2 Z8 Q$ |' y" n5 L% B Selector sel =
; U7 V9 Z3 r7 X I8 L new Selector (proto.getClass (), "heatbugStep", false);
! {9 [* ?" B% h2 j actionForEach =! u6 v* L( @; I& G) U2 X$ q
modelActions.createFActionForEachHomogeneous$call
* |1 [ }7 H2 u7 k (heatbugList,& r9 v+ O9 w8 v& m' x
new FCallImpl (this, proto, sel,( S; h. y! P; Y) o- @
new FArgumentsImpl (this, sel)));# f0 w6 \/ v2 `
} catch (Exception e) {# h. G S: [9 ~
e.printStackTrace (System.err);; _: T- W! I8 [! ]: x# \
}" J! I! ^/ a& M2 a7 v
- @: D' o' w8 ^7 ?. s syncUpdateOrder ();
1 |, ^) Y& Q, @ u& |5 Y
9 v7 I0 D- @; o try {
4 q9 i; F7 B B7 B5 m3 ?. i modelActions.createActionTo$message
* ~6 J U1 p, r3 e5 O, ` (heat, new Selector (heat.getClass (), "updateLattice", false));; I) Q$ N) H9 J: a
} catch (Exception e) {
* g. n! i8 k( F+ |; z _# t System.err.println("Exception updateLattice: " + e.getMessage ());
) ~6 E# {$ q, p# b5 a0 z) \ }
o$ S, D8 Q6 S4 |; M% @+ ~
& e. E( ?# A' _' l // Then we create a schedule that executes the
- _# Q9 s; G" {4 S$ Q // modelActions. modelActions is an ActionGroup, by itself it
; Q; @0 m7 ~- C; t+ B) J6 S // has no notion of time. In order to have it executed in% D9 b2 ^" ?3 f G$ V- O* s
// time, we create a Schedule that says to use the6 [ I% H* ^ C$ e8 \( E
// modelActions ActionGroup at particular times. This( _5 W& P2 _* I( h# o8 f* r8 {4 F
// schedule has a repeat interval of 1, it will loop every, c s) a( ~6 ?, ]/ f& _
// time step. The action is executed at time 0 relative to) r" E, {2 F* R! X7 G3 J8 @
// the beginning of the loop.2 }3 o# W: U" r5 q2 K! A
, w# N( f+ y' Q7 [- R3 O // This is a simple schedule, with only one action that is4 h8 { }- k; D# U9 a7 @% o- t+ F
// just repeated every time. See jmousetrap for more5 c& D0 ^" `4 j2 w% y
// complicated schedules.
$ X2 ]. a( ^# l9 `4 e* p3 }
, L$ A) v9 c# B1 e3 ~ modelSchedule = new ScheduleImpl (getZone (), 1);
H; { {5 {& T3 \( } modelSchedule.at$createAction (0, modelActions);
) h9 W( {$ V& j4 w 7 H9 M% i5 G: [! ~# H; o
return this;3 R% F" [$ O& U7 I; G6 _+ P- J
} |