HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ K v i$ s8 T5 ~5 H
& f" M0 b& f: e public Object buildActions () {
" V. l6 u6 e) s2 t' E$ L super.buildActions();
( A6 N, }9 s# P: Q6 }+ X: `, f
4 p7 |+ `" x: Q2 Y8 P; A' T C* f2 { // Create the list of simulation actions. We put these in
/ ^; L! S! T8 ? // an action group, because we want these actions to be
l4 ]5 l% W1 l1 X, O9 }& R8 K // executed in a specific order, but these steps should
. [$ o; l) k" M+ r' l // take no (simulated) time. The M(foo) means "The message1 v; b3 _% f7 p# l
// called <foo>". You can send a message To a particular3 `/ L1 W3 f8 x- b3 U
// object, or ForEach object in a collection./ b" D: Y6 s# l# M$ T
+ t; m& z" K. N& ^
// Note we update the heatspace in two phases: first run
/ G. }* G8 ~6 r- J3 C7 S // diffusion, then run "updateWorld" to actually enact the' Q4 z0 f$ @+ @8 R$ @/ s
// changes the heatbugs have made. The ordering here is$ t& h7 Y; J' c8 q E, o& f/ ~
// significant!
; l( q! B" D" o7 b' e7 o$ e 1 r1 _) ~! d* O# |+ a# c5 D- L
// Note also, that with the additional
& j8 G5 g( \; f7 Y3 y5 \ // `randomizeHeatbugUpdateOrder' Boolean flag we can& r! {2 b- m' L5 H
// randomize the order in which the bugs actually run
2 }0 _! R6 w& ]7 @# x // their step rule. This has the effect of removing any
; d, t9 w( t; }' Y) k( } // systematic bias in the iteration throught the heatbug
: X& V( J' C* R // list from timestep to timestep
6 I! v+ N! Q3 N8 x 9 u2 A) {6 M6 U! c* |
// By default, all `createActionForEach' modelActions have
' B+ Q0 R* C5 ?+ J2 d, Z // a default order of `Sequential', which means that the2 k. V( G* e+ h7 v1 m! T
// order of iteration through the `heatbugList' will be! [1 p6 b" o& |0 W! _ z
// identical (assuming the list order is not changed
! x/ v5 K/ j* }4 ?. @9 g- O: d8 V // indirectly by some other process).
P* P0 Z) {% T. B5 ? E: I 3 q+ v( i1 J; C" X' p1 k
modelActions = new ActionGroupImpl (getZone ());/ Z1 s- o* M. U8 ?1 B
1 G3 L9 m+ g/ n3 i
try {' P( K2 L W" O" g t( B4 M/ f: i
modelActions.createActionTo$message
& ?! C& B8 ?$ w% f- B2 S# I (heat, new Selector (heat.getClass (), "stepRule", false));1 m% a7 I1 Y4 N/ o- {, c. Y
} catch (Exception e) {( d* i" j' W$ R$ @3 g: @1 E
System.err.println ("Exception stepRule: " + e.getMessage ());
6 }- L: Q+ D- W! y }) u( f9 |# O9 y. r: Z3 S% H% a
% Y- \1 @% k/ j- n$ C/ J; @: _
try {
6 D# B" \* C: n$ q7 }# j* P5 ]2 y Heatbug proto = (Heatbug) heatbugList.get (0);$ j5 p9 B/ g8 @5 h5 v
Selector sel = ; ] n P" O% B k
new Selector (proto.getClass (), "heatbugStep", false);( {5 n& o: Y- y6 [ C o1 ^
actionForEach =3 a7 g+ @8 D+ n) o3 k
modelActions.createFActionForEachHomogeneous$call8 N$ z- u" i1 z' q
(heatbugList,/ A0 g$ K, g5 d2 t
new FCallImpl (this, proto, sel,+ {5 v9 {9 t* j/ y% w" a
new FArgumentsImpl (this, sel)));
( g" V- D* J( @4 O } catch (Exception e) {
$ [6 v, ]/ A) Y e.printStackTrace (System.err);3 F6 L& v8 H4 j! ~
}& a) u l6 H; Y8 v, k6 L) ]# B
7 j1 g9 C( }7 G, U0 S% ?6 T syncUpdateOrder ();
f) v" \* U) g1 X
' ~8 B! K4 P# _ try {1 i- \+ @: v( `
modelActions.createActionTo$message , r2 A% m" C3 L, q4 f1 d
(heat, new Selector (heat.getClass (), "updateLattice", false));
3 g& n- b- p8 @. f } catch (Exception e) {
. l$ [. q* P" I+ R( Y3 a3 p System.err.println("Exception updateLattice: " + e.getMessage ());' l6 h% M) t2 V/ q' ~) C0 S
}
/ d: X5 \3 J% V7 s5 u+ n
5 A( ]4 l% B( d5 w // Then we create a schedule that executes the* l. W O4 Q H* P
// modelActions. modelActions is an ActionGroup, by itself it6 M2 Z% o2 ?$ e+ E
// has no notion of time. In order to have it executed in* U6 k$ \3 ` G
// time, we create a Schedule that says to use the: @ R$ O, ?# U0 ?+ R
// modelActions ActionGroup at particular times. This
3 s7 A- z# o9 m. t0 x* P // schedule has a repeat interval of 1, it will loop every+ v+ v0 x x! n, \
// time step. The action is executed at time 0 relative to
+ `2 j8 v! m1 d ~6 a // the beginning of the loop./ s) w# N" h8 F2 R8 f
" L- @3 B8 S- a1 w, U
// This is a simple schedule, with only one action that is$ p& J. m( y# ?2 i- r
// just repeated every time. See jmousetrap for more
+ F- x( o: G" F // complicated schedules.
; W$ {$ N W% q* Q3 k8 t1 l* r ) w% W5 `0 w* ~( ~7 ^6 K! W0 a: L
modelSchedule = new ScheduleImpl (getZone (), 1);! s$ }, F" k3 P0 q: x o( m
modelSchedule.at$createAction (0, modelActions);
; ?/ d# N8 u1 n0 r1 X , P# d, i( f1 p3 F& \5 I8 n
return this;0 g# l! |6 b. K4 f
} |