HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( ~7 X0 V, x6 A- O+ C s
& s' y# c. O. K9 n0 v2 B/ v public Object buildActions () {
4 t+ u$ r/ @2 {3 s: J3 W super.buildActions();6 j* U" X8 i5 e) `3 ]
% b3 C Y3 t+ }2 j
// Create the list of simulation actions. We put these in
& Q7 U2 r! S; e5 B2 a- k // an action group, because we want these actions to be
. H5 v+ k0 \6 M7 c. F- T // executed in a specific order, but these steps should3 ^' s( Y( z* F
// take no (simulated) time. The M(foo) means "The message: `7 Q9 `* f+ Y+ ~
// called <foo>". You can send a message To a particular" T+ X' J* ~" K G( v! `
// object, or ForEach object in a collection.
' M9 E9 f! m. u2 i! B6 K+ h
2 G3 R1 t, N6 B // Note we update the heatspace in two phases: first run
( O3 p6 x$ s7 Y$ {9 ]3 T // diffusion, then run "updateWorld" to actually enact the
, v& G6 o" L& z9 _6 O4 g // changes the heatbugs have made. The ordering here is
( {8 [: q) [4 [8 u // significant!
l3 k0 A8 ~. }
2 ?6 q9 P9 C" P( e: e7 Z // Note also, that with the additional, t; h; X$ x2 L. M7 P7 R
// `randomizeHeatbugUpdateOrder' Boolean flag we can- f: {2 C0 s$ |0 t: |
// randomize the order in which the bugs actually run
1 ?' f3 L: q: c. ^ // their step rule. This has the effect of removing any
0 h+ w. r/ d: m // systematic bias in the iteration throught the heatbug7 A) W2 x4 L1 i" g; C
// list from timestep to timestep8 c. _5 ?* V. |0 W# L
$ H* m9 U( f( y6 I Y" ~ // By default, all `createActionForEach' modelActions have5 `/ C0 k3 y6 k$ @1 k/ p6 m j
// a default order of `Sequential', which means that the
3 c4 L) W( }5 E+ N9 ^9 f4 l) o [1 F // order of iteration through the `heatbugList' will be9 t! h1 P) Z' _) b
// identical (assuming the list order is not changed" j9 w$ x- O; ]& b
// indirectly by some other process)., V+ F& z+ g( n+ Z& E3 M+ y
( _8 V* h$ w7 ]0 J$ v9 V modelActions = new ActionGroupImpl (getZone ());1 z' j9 h, a, |+ l Y& Q1 Q
" w) @7 N% D/ P* S* g4 P
try {
) l/ W0 `8 Z: a& z modelActions.createActionTo$message
' M& F5 r. W) ]! w- o; ?3 \' X (heat, new Selector (heat.getClass (), "stepRule", false));
0 u; c: r: V: |$ u: f! A- j8 b } catch (Exception e) {
& O6 o/ p; y: t2 N$ ]5 w System.err.println ("Exception stepRule: " + e.getMessage ());
+ L+ `) h; o$ {' p: J* z" l }
1 i8 D6 W, r9 c/ _5 L
" g# H7 ]' {8 l4 F; t p try {
( v( R( C; T* i Heatbug proto = (Heatbug) heatbugList.get (0);, h' c+ L& i9 c# g7 ^
Selector sel = 4 [! `4 k, E% u% y3 X; ]% S% _
new Selector (proto.getClass (), "heatbugStep", false);
- h; [4 D- H7 ]' g7 h% n actionForEach =
: _, k- E* B- a modelActions.createFActionForEachHomogeneous$call
3 K4 S0 ]4 K; r4 G) U( z1 a (heatbugList,( d6 O( w1 @& Q; G
new FCallImpl (this, proto, sel,
2 K& ^4 b- J ^$ k- y; W new FArgumentsImpl (this, sel)));4 c6 @4 Q$ ~ P& T
} catch (Exception e) {8 A' C2 A+ _, l# r
e.printStackTrace (System.err);
7 ]% v$ n* y% N% ^+ b. l }, h' P6 D" q7 T: X
" J; Y5 j/ O, u9 Z: {. }4 y* D
syncUpdateOrder ();
0 p+ [9 N1 q% A( D; k. }, N: g P( P/ p1 R+ ^" r2 w3 Q
try {
- {2 {( U/ ?8 M1 ?( H3 o modelActions.createActionTo$message - Z. n8 g2 g4 Q+ G- d6 W$ T1 N! [
(heat, new Selector (heat.getClass (), "updateLattice", false));) V$ m2 J; w- ~4 v
} catch (Exception e) {$ s0 ^1 r0 Y4 M
System.err.println("Exception updateLattice: " + e.getMessage ());
' R2 H' ?9 y3 U) \ }" L: B5 r/ c" M/ E8 ?
5 ~9 B$ \# a+ a* {+ _& } // Then we create a schedule that executes the7 m; Y, s+ N5 \# ^" x* e* t
// modelActions. modelActions is an ActionGroup, by itself it
0 x; s$ l6 T$ K* G0 L2 p' Y // has no notion of time. In order to have it executed in
( D% z, {1 S+ _8 T$ @3 }" D // time, we create a Schedule that says to use the
- Q# w) l2 [! m: K( Z) P" ?( U7 l" H // modelActions ActionGroup at particular times. This
5 C1 o7 W: A# V% \1 r // schedule has a repeat interval of 1, it will loop every
8 O% L- d* c' a( ^: b // time step. The action is executed at time 0 relative to
- k' V5 o+ D& `2 B // the beginning of the loop.
$ b/ C q- s. m! G6 U4 ?8 R* F/ E3 X" h
// This is a simple schedule, with only one action that is
( m2 c0 f( u; s+ |( x // just repeated every time. See jmousetrap for more0 ~2 Y8 A0 r# W" G
// complicated schedules.
3 c7 n' a! K' h2 _ 9 |; S0 v+ T6 Y7 f& s
modelSchedule = new ScheduleImpl (getZone (), 1);/ X2 t+ P* V, l" q6 K6 w
modelSchedule.at$createAction (0, modelActions);2 v/ S K# v' \2 L' S4 p- O. G
4 y5 R. Q* l# G! Y
return this;+ R6 V" }; i7 y l
} |