HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 T5 Y( \% ?' L$ s( T3 V
+ U; E3 o5 y' H) E" p8 M2 \ public Object buildActions () {
; j- L& Q' ?1 D$ S m: v& a super.buildActions();
2 _, _; j& i2 u# k8 a
$ a0 M, G& a. Z2 X // Create the list of simulation actions. We put these in
( b3 e; B$ }) W: l6 O9 f // an action group, because we want these actions to be I7 C+ v) t \2 O1 @$ G
// executed in a specific order, but these steps should
) h5 E7 m- L3 U* F# B1 g/ B8 x: ^! t // take no (simulated) time. The M(foo) means "The message
V+ J% s- v7 Y2 W2 m // called <foo>". You can send a message To a particular9 h/ h1 ^, X; }4 a! G8 }1 H
// object, or ForEach object in a collection.! x, {7 R0 B% s! E
d& X; B& Q/ e7 Y. c0 n `! f% P
// Note we update the heatspace in two phases: first run
4 s* g- ` L9 }0 U z- k5 W // diffusion, then run "updateWorld" to actually enact the
" V6 p. T# Z/ @6 x2 n5 R // changes the heatbugs have made. The ordering here is) w- S/ R9 {: H N: B6 [( Y |" A
// significant!, i6 Q& f& ?6 e }
& Y% M& Z5 N* b+ e( X
// Note also, that with the additional
; Y3 }, w+ T' A4 _1 a u* h" U // `randomizeHeatbugUpdateOrder' Boolean flag we can2 F1 ^$ Z$ m: F' s9 x: g0 u
// randomize the order in which the bugs actually run1 B' y" g9 j( L5 P) ^
// their step rule. This has the effect of removing any$ S7 Q' z- y; D& g
// systematic bias in the iteration throught the heatbug
2 O V( a, B' J5 ^; P' ^' M // list from timestep to timestep4 @5 \1 u1 S4 T
- i8 {, B! f/ J2 p
// By default, all `createActionForEach' modelActions have
4 E9 I& h8 G) C // a default order of `Sequential', which means that the
F; p& O7 g( [6 n' z // order of iteration through the `heatbugList' will be# f) c }+ h, P0 w" z& A$ {9 D
// identical (assuming the list order is not changed
/ H7 w& M6 F+ _" P$ Y: O // indirectly by some other process).6 l2 E6 X O# b
! I% `" |- I! Y! A" f
modelActions = new ActionGroupImpl (getZone ());' a; o d2 k1 w
. m. d0 x, F% O- I. [/ m try {; ]# {9 @ T7 W
modelActions.createActionTo$message
! [0 o: H" m A" b (heat, new Selector (heat.getClass (), "stepRule", false));
/ y. o1 b$ W& z) m3 e } catch (Exception e) {1 ~ i ?; D- U4 f9 e& D3 t' h
System.err.println ("Exception stepRule: " + e.getMessage ());# ]/ E7 N9 a# N0 O
}! ~9 Y" Y8 X; z$ C- `9 v% M" z
4 R! z& {- T# Y- x* _! F, ~ try {9 E/ W3 J0 n! p2 \7 `- I
Heatbug proto = (Heatbug) heatbugList.get (0);+ @/ z% d. K: y, H* A, h4 L
Selector sel =
( s* o- l/ n+ E1 e6 F* \ new Selector (proto.getClass (), "heatbugStep", false);! J( K6 i" Q0 _' R5 A- e/ e
actionForEach =& x! T! M" f7 d2 q8 T& z& x* ~8 j( P
modelActions.createFActionForEachHomogeneous$call
2 l3 d7 s1 R* o6 w (heatbugList,
8 c) O& v$ D# h9 {; P new FCallImpl (this, proto, sel,4 C: @5 n1 ?6 i+ S0 u4 f
new FArgumentsImpl (this, sel)));
" {! D. g# [# P, A: l+ @3 ^( s" ?( L7 a } catch (Exception e) {
9 c" C5 Z8 ~$ r7 Z& E$ ~* y e.printStackTrace (System.err);4 o/ \$ g, N+ K {( P
}' f* Y8 i& D; |, J
& r% m" G# N: K syncUpdateOrder ();: ?- I" w/ y% k! I# P: a3 z
6 B O1 d' M: h# m- ]( Y
try {" W1 A% K5 \$ ?7 u* S3 X
modelActions.createActionTo$message 0 B) q- R' O+ a
(heat, new Selector (heat.getClass (), "updateLattice", false));
) }3 k; ]. d9 [3 [& o0 q! h L1 k+ } } catch (Exception e) {
. |$ B) a- J3 ^) ?7 a System.err.println("Exception updateLattice: " + e.getMessage ());+ }8 ^, Q# _/ w: v& E
}8 {/ `7 N0 i* f0 w
- }' M% |% d& s8 k& x- A+ l1 E // Then we create a schedule that executes the( W$ N1 b: X4 \! ~$ r% @/ K: s" s
// modelActions. modelActions is an ActionGroup, by itself it
9 g8 K! r; h7 v* b. |, ^ // has no notion of time. In order to have it executed in
% y' r9 b A0 Y // time, we create a Schedule that says to use the7 `0 t& R( Y1 t0 u8 T$ L6 ]
// modelActions ActionGroup at particular times. This s% I, | h" |% c
// schedule has a repeat interval of 1, it will loop every5 i$ d# T& \: a: z5 X) v; u
// time step. The action is executed at time 0 relative to0 v) T9 ?) m1 L0 M& x% A+ x
// the beginning of the loop.
: x5 _, `# D% B$ R/ v7 n/ N& _/ t4 ~
// This is a simple schedule, with only one action that is4 s. r, z( U! \* L, l3 N' F& P
// just repeated every time. See jmousetrap for more6 k' M" Q6 o! h! L1 ?# s% @+ V+ _
// complicated schedules.! b5 ]7 p' F& A
/ V; P+ L, l: _' Z5 A* P/ R3 _ modelSchedule = new ScheduleImpl (getZone (), 1);( W, w% @0 y/ Y8 d
modelSchedule.at$createAction (0, modelActions);2 i2 ~1 ~4 G( w: U8 x/ V0 _
' S5 C. u9 [: J5 l* H: y3 E, D return this;; B8 ]" o- g$ X; y% H j
} |