HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ n4 \5 [& B/ i, D3 y
6 v: V2 w0 X! N4 B, z. ?
public Object buildActions () {) Q( _1 Z' k! V8 r3 |7 x8 i
super.buildActions();
- A2 s- ], v9 M, C
5 _# V: D: B! K o // Create the list of simulation actions. We put these in: {4 m( r7 C8 \* N4 e( n
// an action group, because we want these actions to be" Y% i) v" c7 o% A
// executed in a specific order, but these steps should
$ N- A3 |. M" r D // take no (simulated) time. The M(foo) means "The message
) n6 G; [" j; B, V& d // called <foo>". You can send a message To a particular. I# F0 J6 k" Q
// object, or ForEach object in a collection.
9 u/ ]- U: i' |& E5 i, z
+ y- g* ]& `) o) e4 D7 ?( P$ S // Note we update the heatspace in two phases: first run
+ N( n( G. U/ V9 e+ m1 f0 ? // diffusion, then run "updateWorld" to actually enact the+ I+ y) p& F3 d2 n
// changes the heatbugs have made. The ordering here is! I D! r! O* o( N0 ]! j& h$ O
// significant!
. }! X1 d* B k, a, Z2 [ 3 z! E, J; l) b x! a$ n; s$ T% \8 {
// Note also, that with the additional# Q% P* A( i( q0 ^1 T- b! L
// `randomizeHeatbugUpdateOrder' Boolean flag we can% [" d- [+ i! Y! D# l6 Z7 {- Y
// randomize the order in which the bugs actually run) T& I7 }2 E5 }, m9 o! U, P5 g( X6 M
// their step rule. This has the effect of removing any6 b: P2 ^, n; {. |
// systematic bias in the iteration throught the heatbug- j9 q( F+ n* j, S& R' Y8 D
// list from timestep to timestep
# J0 A! C: |- ]1 n h
8 [9 a* G0 r( f( x8 v // By default, all `createActionForEach' modelActions have
* a$ B0 v! @; H6 n$ @ // a default order of `Sequential', which means that the
6 G b% j) X$ E // order of iteration through the `heatbugList' will be
( f8 S3 |* h# x' B+ _4 e$ ~ // identical (assuming the list order is not changed/ J3 r; B0 K+ I' o# z/ A
// indirectly by some other process).8 ~1 w6 y$ B) S) D7 j
' e( j6 c L# Z modelActions = new ActionGroupImpl (getZone ());
3 e! p+ R! {- s2 Z! e
5 h r& n8 L4 W/ d( {( _ try {% u( w+ D1 X, w7 v6 p6 ?2 R/ _
modelActions.createActionTo$message$ A2 x: U+ |' h% g
(heat, new Selector (heat.getClass (), "stepRule", false));
' y4 C3 T5 K' m } catch (Exception e) {
! |! D" C/ Y! G! v# j+ B/ B System.err.println ("Exception stepRule: " + e.getMessage ());* D# ?+ q& A" B, O
}
8 ~4 S X1 U7 ^ ?
: s, ^3 U& D' }2 I9 J" \ try {5 m/ ~2 M: Z: c; M
Heatbug proto = (Heatbug) heatbugList.get (0);
$ J# m: u( c$ a; T4 `$ c Selector sel = : S9 ^$ t# F, i
new Selector (proto.getClass (), "heatbugStep", false);
! z6 k2 {- a1 D+ G2 N/ W actionForEach =$ `" {/ g% y4 ^( n( W0 U: e8 D
modelActions.createFActionForEachHomogeneous$call- r7 k8 X. Q: f7 ^
(heatbugList,
% _' I0 X3 Z. m- o new FCallImpl (this, proto, sel,
: ^- @4 ^; W% j, J8 o q: _ new FArgumentsImpl (this, sel)));
9 a7 H" e8 J, f z% X' G; X } catch (Exception e) {
* m( Z5 k! V& V/ `( F! _2 @/ D e.printStackTrace (System.err);
1 p8 u9 ?, B* U3 p7 ]% K8 t2 T$ q }
( B$ d" g( V3 M8 n& x! B % H* X+ s7 f7 Z; R; m
syncUpdateOrder ();6 c4 z( U, }* `4 H- g6 i0 z5 j
0 {& F) d5 k( ^# N+ s3 }5 @2 F7 W
try {. { w3 g( Z. R8 F* Q U0 o' Q y6 d6 C
modelActions.createActionTo$message 9 \/ l, X$ x0 @
(heat, new Selector (heat.getClass (), "updateLattice", false));
* f" H! F `6 M% R0 I \ } catch (Exception e) {
% m! [/ m# F1 v- ~# p/ s. { System.err.println("Exception updateLattice: " + e.getMessage ());
. S4 J, n+ K: ~8 R }
* P. W3 C6 f/ I5 {! Y8 P$ R 7 R. X5 o6 b2 n
// Then we create a schedule that executes the
9 [5 B/ s, ]7 b" I+ J, { // modelActions. modelActions is an ActionGroup, by itself it* {, h* ^% P& L8 \* u
// has no notion of time. In order to have it executed in
/ t& u' [/ a; P* g/ | // time, we create a Schedule that says to use the
- p5 `% e/ ?9 J1 J // modelActions ActionGroup at particular times. This$ y) d8 ]: Z L8 ]0 Q) f/ C E- Y7 f
// schedule has a repeat interval of 1, it will loop every+ S0 s, Z5 v1 | q" H6 T( L+ D) g8 |
// time step. The action is executed at time 0 relative to
! z9 B. S4 K$ {5 [# y: e O // the beginning of the loop.) `: L1 p) F! ]; w9 n1 s
+ l' C5 G% N/ @ ]0 R
// This is a simple schedule, with only one action that is7 R7 f- u V9 G% Z' A8 x8 n3 B
// just repeated every time. See jmousetrap for more R5 E+ v/ i+ c/ ^( q) J+ @5 n' R
// complicated schedules.
* H* I* G4 f2 w" T; a 1 R$ ^% g; k8 ]2 {+ {2 V
modelSchedule = new ScheduleImpl (getZone (), 1);
1 S. p9 Z& L7 } modelSchedule.at$createAction (0, modelActions);* f$ q, P* k+ s: q. P
) _5 S9 p* g! Y" @1 [ return this;
: |$ r8 e" i" x; d } |