HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 ?& A* [9 i6 S3 v8 A
$ r U- F- ?* @
public Object buildActions () {
( }& u' s- V! [, l super.buildActions();
, l G7 u! V; M
6 q8 _( j& O9 q, F S( X/ M // Create the list of simulation actions. We put these in
+ S; ?) X" ^3 m- _9 b! r // an action group, because we want these actions to be7 s5 r D" T# B/ \2 x
// executed in a specific order, but these steps should
1 ?7 _% u( h/ } // take no (simulated) time. The M(foo) means "The message" a; w) L6 L+ s
// called <foo>". You can send a message To a particular
B1 R; t* B; b* [% e, Y4 l* g1 |% h // object, or ForEach object in a collection.9 q) \* h4 l/ H1 Q
, @. ^6 ?( y# N$ G // Note we update the heatspace in two phases: first run
, U4 @% p8 d Y4 Z9 z // diffusion, then run "updateWorld" to actually enact the- A2 w. ?0 A3 y" k9 F
// changes the heatbugs have made. The ordering here is, I& F: s8 G+ N* s+ s) M/ w# v- p
// significant!) m% k/ l; _" n
: l- ?6 ^! y" c+ W8 w. |' j1 A; y
// Note also, that with the additional/ w) }* S- }1 r4 Q
// `randomizeHeatbugUpdateOrder' Boolean flag we can" T1 B3 [+ O" y- v6 t; o
// randomize the order in which the bugs actually run
5 g, l" s: P% E( g // their step rule. This has the effect of removing any+ |& T) p2 {1 N% t
// systematic bias in the iteration throught the heatbug# q9 p. j% }) n! M
// list from timestep to timestep$ p: l! p( x# Y1 a4 j
, q- ~3 z/ j( u, I- |0 a7 g; t
// By default, all `createActionForEach' modelActions have- Z$ N! ^- l9 n6 x, j
// a default order of `Sequential', which means that the
3 B) v, E4 S v2 O B3 S3 u3 h" _ // order of iteration through the `heatbugList' will be8 {" ?" i2 D3 M! }3 ]* Y- r
// identical (assuming the list order is not changed
& V6 w' y& E6 i( O: ^$ L // indirectly by some other process).
# m9 I0 |9 V; q. e/ V( \ 6 j5 i! W( b+ K( ^
modelActions = new ActionGroupImpl (getZone ());' `5 e; G" d |# ]
$ i$ m/ H: H/ ?# Y8 s try {
5 s8 @3 A* |+ M% B: x9 Z modelActions.createActionTo$message
) ^) h" J/ v; t9 _ (heat, new Selector (heat.getClass (), "stepRule", false));* n/ F$ `7 p; {
} catch (Exception e) {" q: O) r/ X5 q" U
System.err.println ("Exception stepRule: " + e.getMessage ());$ q, m# u( C& ?9 e" V( p' a9 I
}
/ `- \$ M6 o' j$ L9 x: h* a4 i2 J# u( T1 P5 v' w, a
try {5 E3 i) y" G# K3 [2 W( O. Y
Heatbug proto = (Heatbug) heatbugList.get (0);
5 u9 M2 `0 O8 U Selector sel =
2 t7 X# g. _# r4 L U new Selector (proto.getClass (), "heatbugStep", false);
8 ]1 ~0 W, M( y actionForEach =7 e9 e' k6 r; Y
modelActions.createFActionForEachHomogeneous$call
# U }+ c' E2 s" ^ (heatbugList,8 w% }# c: y# [2 _4 e+ t P' U: {
new FCallImpl (this, proto, sel,
! z9 A) l9 a n new FArgumentsImpl (this, sel)));. j1 E( G& _- |) l7 ?9 _. _/ {8 O
} catch (Exception e) {/ i- [/ l' g8 j3 [7 W' |# I9 {
e.printStackTrace (System.err);
+ K8 S! N. z/ B4 J }
( D9 `; B" p Q& q- \( W0 T# M1 p {% ~- v! h, r0 _
syncUpdateOrder ();
[7 D) j. n5 q# E
2 P: J& D: b* [7 P2 g$ R try {
( m8 S+ ?! `3 _% G9 E/ a. U& a modelActions.createActionTo$message
/ V: | S5 [. u, V (heat, new Selector (heat.getClass (), "updateLattice", false));1 c! r% \) q5 `
} catch (Exception e) {0 \# f% r% p1 \4 \# A: ^
System.err.println("Exception updateLattice: " + e.getMessage ());
5 ^7 q2 \% m, n) ~- H }
+ j6 @0 l, C& a , w! k% X0 v' j4 J
// Then we create a schedule that executes the
* R; E1 w) K( D& M // modelActions. modelActions is an ActionGroup, by itself it
5 t$ p8 A3 q1 ~4 A$ u a6 j // has no notion of time. In order to have it executed in
4 S( j! i! I- K$ n0 | q/ u // time, we create a Schedule that says to use the
0 l& x- k! K0 u# U // modelActions ActionGroup at particular times. This
( Z* [& x4 y4 M4 N' |% w8 C3 Y // schedule has a repeat interval of 1, it will loop every
1 j& E8 h0 v, |' A+ ?3 F // time step. The action is executed at time 0 relative to
5 `) M4 K" W1 B/ U // the beginning of the loop.2 X7 J0 @ I$ C7 M' C. [$ S6 z7 @/ V
% F8 z0 y5 J7 Q, k* V# t, n1 e // This is a simple schedule, with only one action that is& h. S9 _2 E w+ N% b) y, D5 X
// just repeated every time. See jmousetrap for more
6 v& Y8 S* O" K7 Y0 e7 x // complicated schedules.2 W7 n' N" X" h; V
; U* ` N- c( n# Z modelSchedule = new ScheduleImpl (getZone (), 1);* c: M2 l' E8 i5 g' i$ e" j9 _ |
modelSchedule.at$createAction (0, modelActions);
7 a' C W# C* C / T; n% j7 @/ V" e8 g: o. ^# i
return this;$ Q1 Y4 X& O( ?1 z) X5 ^# c
} |