HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, l) \. r3 r# G( T. K
; N u) J% V6 m5 F public Object buildActions () {
1 i& n% p2 Z7 t% q$ e' ] super.buildActions();- H$ Z% e% q$ s6 Q; {6 Y3 u# g
& p- f8 H% P. D: A
// Create the list of simulation actions. We put these in
$ r5 U+ d2 m$ x7 J% M; X; w // an action group, because we want these actions to be( B, K' A# ?1 \ y+ ^4 K" a1 m) |3 w( t
// executed in a specific order, but these steps should
9 R* l; W& C, l( p2 D // take no (simulated) time. The M(foo) means "The message1 L5 [5 c1 d$ c2 u& v* B' B0 |
// called <foo>". You can send a message To a particular L$ y/ `* v; M1 {
// object, or ForEach object in a collection.- U+ k/ B) U; P2 e( F
2 l; P" `. j2 n6 x) X% X // Note we update the heatspace in two phases: first run) E. f+ |- S: C
// diffusion, then run "updateWorld" to actually enact the7 ^, O6 G$ v9 `" H$ [# `
// changes the heatbugs have made. The ordering here is; u( b9 k0 t! u! K. S, E
// significant!
1 L( M! F. T- U# p * U) s) q0 k9 c$ O& @# }5 Q2 V
// Note also, that with the additional, W8 v- ?$ w& j) ]. b; l6 i* |( F
// `randomizeHeatbugUpdateOrder' Boolean flag we can X! w: y+ L7 x
// randomize the order in which the bugs actually run0 b* \" |6 B7 n8 Z0 R+ C
// their step rule. This has the effect of removing any6 b) i8 X9 G2 M0 P9 A& E/ N% q) y# m: \
// systematic bias in the iteration throught the heatbug
0 ~( `; Z1 s" l# D5 H" H! c; q9 \ // list from timestep to timestep2 I# m9 g) A+ i% ?& Q
( o4 I/ j, M: M1 H- d9 S" x- b5 O, J
// By default, all `createActionForEach' modelActions have
+ B- \/ n/ u8 T1 O) F# I# r& { // a default order of `Sequential', which means that the
1 b& X, i! g) f. s& u+ u // order of iteration through the `heatbugList' will be+ T" p) z: O. T
// identical (assuming the list order is not changed
/ n6 h5 C1 Q, o5 S! h) `0 R8 k, u // indirectly by some other process).
M* @4 C1 s! z9 [$ U- I. ~
0 G2 |% {4 p9 M( e$ ? modelActions = new ActionGroupImpl (getZone ());/ s0 ?+ T% n0 d. F, g
: W& j; M2 w" x, z% @9 \, C) V
try {. } U5 ?; ?7 S8 W& F6 a
modelActions.createActionTo$message+ j& W) M% Z3 s* R" C# i
(heat, new Selector (heat.getClass (), "stepRule", false));
" U# [3 s7 R" f: T- g } catch (Exception e) {1 p7 W+ D( q; N: Y: P# A
System.err.println ("Exception stepRule: " + e.getMessage ());
% P, d" Y+ H' r5 W f* T }
+ O8 h0 @: |( b; O. }) w5 W9 z# j$ X' h3 ]- \: h
try {
/ B' r `& f) { Heatbug proto = (Heatbug) heatbugList.get (0);& L+ G0 A; N8 I: i2 M* s& D# F1 \
Selector sel =
& U: G1 G1 U0 q, L$ h$ L new Selector (proto.getClass (), "heatbugStep", false);
/ c/ W9 b% u1 E" a! A; I actionForEach =/ v1 V I2 O6 z* q4 E
modelActions.createFActionForEachHomogeneous$call
$ Q# ]* _6 t( v8 { (heatbugList,% `: I8 n) `7 ~& u
new FCallImpl (this, proto, sel,$ o. L. X- y& f+ Q# M' |3 }6 h
new FArgumentsImpl (this, sel)));
$ F: S' x1 ^. ~+ E7 q, D } catch (Exception e) {6 T. {1 r; u* }
e.printStackTrace (System.err); m3 K: M0 Y2 ^& I
}( e+ u2 k% {1 Q7 J s# F
6 d% z0 d* k. n
syncUpdateOrder ();
" `1 F3 I I. G# @3 b
4 ^6 \7 w# Q! C, F: W5 d- Y) m try {, x- J O6 J+ l4 i" e% u
modelActions.createActionTo$message 2 [2 C4 ^8 ~$ W; O' H1 h
(heat, new Selector (heat.getClass (), "updateLattice", false));" n0 C: P5 d M
} catch (Exception e) {+ x2 Q( a, `. R1 Z9 S- L& w
System.err.println("Exception updateLattice: " + e.getMessage ());, k* y" O6 U N9 O: A
}3 ]& B f& N- W- x( x% p$ D
; \0 y8 T) i" H: J3 _: M+ o
// Then we create a schedule that executes the
( e6 Z' {9 M- m: B5 Z2 \2 h9 j: @ T // modelActions. modelActions is an ActionGroup, by itself it
# ?6 L! e# x) u1 D z; P% \ // has no notion of time. In order to have it executed in% L( K' B& d$ W3 g3 R' g9 L
// time, we create a Schedule that says to use the
1 q& [0 ^. d4 c // modelActions ActionGroup at particular times. This: ^9 X) N6 c% Z/ G, {
// schedule has a repeat interval of 1, it will loop every
7 H. m H' X7 |6 I, t5 H1 o/ I // time step. The action is executed at time 0 relative to( ]) F. H a7 C5 \0 M
// the beginning of the loop.
& f; V( L# W' w. V
- Z! S6 @# h/ c4 r3 p7 j' c // This is a simple schedule, with only one action that is
0 \7 W7 e% c# N/ l9 k: ?4 i2 r // just repeated every time. See jmousetrap for more. D+ m3 k% U7 u: n
// complicated schedules.
# S) g6 F, ^8 z/ W$ y8 Z- o0 i ( m5 A4 u; E: z' a) _" V) B# y
modelSchedule = new ScheduleImpl (getZone (), 1);2 P! O! w; K7 z: u# T
modelSchedule.at$createAction (0, modelActions);
3 o, P# W4 G* U# i5 o4 m
+ B! u1 Y, D0 A B return this;* T- A; l% y ?2 B: m: O% S
} |