HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ t1 T4 w7 d% x: |' D( D. q) U
; C, b: Q+ x' }, u
public Object buildActions () { w% a! [1 _9 e( A
super.buildActions();" F$ {9 k- M7 p* m2 l+ a! `: F
1 `7 E* ]! Q* y // Create the list of simulation actions. We put these in
1 p8 ?5 D1 ?2 ^0 @0 m* S. w // an action group, because we want these actions to be
$ W+ \' g- f* q& H, y // executed in a specific order, but these steps should' H# H% K* E8 O+ i1 ?" }/ d6 w( s
// take no (simulated) time. The M(foo) means "The message0 U* _( m+ W- i2 ^6 c2 V w
// called <foo>". You can send a message To a particular
$ `' r) E5 v6 p4 j+ h% R# I: j0 a- } // object, or ForEach object in a collection.9 d, l, Q7 u2 S. u; w/ ` U
6 v- o% O1 y" T3 m3 F6 O
// Note we update the heatspace in two phases: first run$ G, @' y$ q6 h0 c R# Z' e/ R
// diffusion, then run "updateWorld" to actually enact the
/ |' s0 r& f, b- T$ D // changes the heatbugs have made. The ordering here is2 @9 s* [7 r9 M/ y5 S
// significant!- t; ]- N( q* V
, g1 V% q$ {! Z7 M2 Q
// Note also, that with the additional& B( s& s8 f; I# c: d7 e z% f) t
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 v/ g3 y. B7 K5 J& k' C; O1 b // randomize the order in which the bugs actually run
3 J$ h' V/ u0 S5 ~ K1 O // their step rule. This has the effect of removing any) g. \$ R4 B) }2 H. R
// systematic bias in the iteration throught the heatbug
: [0 y5 H" W2 b; N7 y* J // list from timestep to timestep$ `4 U3 O0 ^: ~( T5 h" y
8 x/ C8 l5 |, U1 ^
// By default, all `createActionForEach' modelActions have( e" {1 Q8 q0 ]# U* d" |( g
// a default order of `Sequential', which means that the
0 Q' Y0 m- `5 J0 Z% ` // order of iteration through the `heatbugList' will be
9 l" F$ O, b0 P7 P% Y& Y( t6 M // identical (assuming the list order is not changed
, H0 T9 y" c+ z! r2 Y+ y. a // indirectly by some other process).& p; P; j" P( w0 U# n- r6 @% y
3 J; o& L( s$ f3 Q2 T2 B
modelActions = new ActionGroupImpl (getZone ());
' F2 u: ~" O) Y" ^6 R, G
5 g: z# m& N! M) @1 g try {
' u/ a0 V- R8 W+ } modelActions.createActionTo$message
3 F3 ]2 F% J* {* Y- X (heat, new Selector (heat.getClass (), "stepRule", false));
: a# B3 X8 Y4 q } catch (Exception e) {
7 y7 S7 H. t" f: Y2 h System.err.println ("Exception stepRule: " + e.getMessage ());, E4 z: I/ C4 H* @
}
* ^- {; b3 }$ m; X; p$ t4 Z' Q- P/ r& i6 k- H
try {9 Z! B& y- `% _% h9 B
Heatbug proto = (Heatbug) heatbugList.get (0);
3 H3 P; {5 [; d# ^* w9 R Selector sel =
" M: K9 j; N9 D2 ]9 y% G, N- W new Selector (proto.getClass (), "heatbugStep", false);, U- k9 ~9 {9 I7 Y1 h! S
actionForEach =
) d+ f0 h$ d, k- d" ] modelActions.createFActionForEachHomogeneous$call
! }4 l$ O* _5 C5 b (heatbugList,
$ D! F4 m1 c& |) S: ~. D0 ] new FCallImpl (this, proto, sel,7 `: F/ ~8 V+ U: i6 V. i0 W
new FArgumentsImpl (this, sel)));
2 v2 J4 ]" K6 t } catch (Exception e) {
2 ^! K& L) Y& h2 O8 h9 ]. a e.printStackTrace (System.err);' L8 ?! V9 h+ c1 L% z( M" f
}1 L- P0 l: p1 `& U* G8 f3 B6 q( y
- a" L8 Y( c- c% b
syncUpdateOrder ();
: i3 W8 V1 x a6 U1 [
: t6 |6 |# r! D ^5 S" {4 x8 Q3 G try {- ]0 t/ L9 A& O' z
modelActions.createActionTo$message & f; R4 J. e# H8 p4 B
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 m/ E% u5 j: v* }: F6 t" o( e } catch (Exception e) {& R- b9 z9 {* N0 t" s: p
System.err.println("Exception updateLattice: " + e.getMessage ());
' p" D4 e+ J9 i4 \ }6 z3 ~9 F2 Q4 g' S' p
/ Q4 {# U5 q8 z6 V' @
// Then we create a schedule that executes the' S# t" B+ ?2 l; i$ u
// modelActions. modelActions is an ActionGroup, by itself it
- L% {/ y' g+ H0 t // has no notion of time. In order to have it executed in3 P8 D' f! S7 M- J( I
// time, we create a Schedule that says to use the& k4 \3 S' ?7 u/ n3 P/ d% _. Y
// modelActions ActionGroup at particular times. This$ q! s0 _( R( t- w0 G# g
// schedule has a repeat interval of 1, it will loop every3 c" V6 g! d& @7 I
// time step. The action is executed at time 0 relative to. @/ p; D, W" ]8 l/ b
// the beginning of the loop.
* V' o' `* W, \9 g* s+ F z: O8 A: K. m0 w. U
// This is a simple schedule, with only one action that is% L( p+ H. O! g* n2 H
// just repeated every time. See jmousetrap for more
3 y& z# i( m6 `4 ^* v // complicated schedules.
- r5 e1 c; M/ l- y/ h( q# k 6 l+ w g* H* p
modelSchedule = new ScheduleImpl (getZone (), 1);
1 x: u: F1 p! m: O6 [ modelSchedule.at$createAction (0, modelActions);4 S' Y. q# c( Z5 y9 t
: L' v8 ~- f; x return this;" w! h) m4 b! y0 z% V
} |