HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 d8 e8 M3 Z! ?) r( J+ J+ x' ~* T6 u; p( d2 D
public Object buildActions () {
- O) I2 k5 {( F super.buildActions(); Y. [0 j W. v- f; \5 X8 m7 ?
" {3 X6 t/ B! i* t; g6 c/ v
// Create the list of simulation actions. We put these in
8 |5 ]4 C+ `( T/ f" J) b // an action group, because we want these actions to be
( H# H! O. |' O7 m! o, z/ L+ ] // executed in a specific order, but these steps should: E3 I4 T$ S. j2 n% ?+ F8 W8 K* ~
// take no (simulated) time. The M(foo) means "The message! e; u2 ^3 X4 q7 @
// called <foo>". You can send a message To a particular
3 k2 S0 R: m) ]3 X // object, or ForEach object in a collection., m# @) V6 r3 t" v; p! L6 x, X
& P" S: ?4 J) \. e. }& _
// Note we update the heatspace in two phases: first run
2 L P' C4 j5 i5 ` u // diffusion, then run "updateWorld" to actually enact the
% b% K. s, Y$ T& k7 `. l // changes the heatbugs have made. The ordering here is
+ o0 U9 t# r2 |* X% a# k7 @0 u% A // significant!
# ^ g5 k; v# U( P1 ~" b3 y: w 5 }. n( v5 u" f7 _
// Note also, that with the additional
- N( b' W2 g& Q) F. @ O0 _# ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can
3 K5 l( o: e5 p& Z3 j4 ~* V // randomize the order in which the bugs actually run
9 u, W% z( e4 r/ K% k. ^- \$ H6 m // their step rule. This has the effect of removing any3 z* u) c7 {1 L, |
// systematic bias in the iteration throught the heatbug
& o' }+ g2 `: v6 W7 x // list from timestep to timestep* l4 g* a0 c l O
) V7 u0 g; l# k: | // By default, all `createActionForEach' modelActions have4 s! C" l1 T: |/ U6 a0 C
// a default order of `Sequential', which means that the; b5 n5 P2 \# x" |+ ]
// order of iteration through the `heatbugList' will be% I; C" x% v9 C6 h0 H$ S
// identical (assuming the list order is not changed0 O: a2 \) ^2 P9 @7 O8 H/ E/ }" t
// indirectly by some other process).
5 Y! s) e3 I6 M
- L7 J4 G) A4 m- X Z modelActions = new ActionGroupImpl (getZone ());
; D( d+ ]4 }7 t; u- Q7 z# u o+ u0 q) x: q- V7 T' f( F
try {; z; I* y) r1 ^
modelActions.createActionTo$message( ^( ?" v6 c: P2 Y$ J0 o8 ]
(heat, new Selector (heat.getClass (), "stepRule", false));
5 c1 g& _. _8 t" N, n } catch (Exception e) {. O3 [- K5 w- G4 z' h u7 g8 s
System.err.println ("Exception stepRule: " + e.getMessage ());
% U, H2 q0 [/ V' ~& x( b4 t( P }; \0 v3 k& n8 y; ~ I
( A( [5 j6 W/ Z# Z6 `% Z
try {
/ A: j( }' ], L Heatbug proto = (Heatbug) heatbugList.get (0);4 G5 [- I8 C; n/ I. B
Selector sel =
! p) R% g/ }; e3 F new Selector (proto.getClass (), "heatbugStep", false);. `5 [- Z/ z' n4 U$ s
actionForEach =
1 z. J/ s/ m9 ] modelActions.createFActionForEachHomogeneous$call& ^! g' O8 {7 I3 m8 ~
(heatbugList,
5 ~, u7 ?: R: l, e% M4 l n2 T new FCallImpl (this, proto, sel,8 g) J) m: K! U0 B
new FArgumentsImpl (this, sel)));" w9 i6 A; x. y& r0 e. \
} catch (Exception e) { z5 u& _& w: r1 [0 I# A
e.printStackTrace (System.err);1 n+ j1 [8 P2 T9 e
}
5 m: e' ?# F3 G: S5 r+ h' i, C 0 b6 K0 m) q4 q4 U+ e) M- [
syncUpdateOrder ();6 X( t8 H) @/ \0 ~ _( ]: h
5 O$ Z& @1 M# e+ ]
try {( d, w( I$ H+ f* }+ F$ d
modelActions.createActionTo$message 4 Q0 X6 n7 ?1 M+ l$ M0 E
(heat, new Selector (heat.getClass (), "updateLattice", false));
' H+ D2 ?( m* J. O! T! t4 e& w } catch (Exception e) {( |9 h7 ]9 q, k7 Z
System.err.println("Exception updateLattice: " + e.getMessage ());: \/ o+ D4 L6 g* S3 n( `0 ^
}
+ A: u! {6 f7 C) Q4 ~4 `: W" i
9 f+ _7 ~' r9 y4 G# Z$ O T // Then we create a schedule that executes the1 L J% N& Q; U" T; j0 a
// modelActions. modelActions is an ActionGroup, by itself it4 R+ h' m4 l% G% d
// has no notion of time. In order to have it executed in
$ A: M) x7 e: g. E: E- S: A! |6 E2 S // time, we create a Schedule that says to use the3 b6 H& y" G+ }9 ^
// modelActions ActionGroup at particular times. This3 X* a7 B$ j; P; T b: \
// schedule has a repeat interval of 1, it will loop every
2 J( u6 W: t9 f; ` // time step. The action is executed at time 0 relative to6 ~0 H) m9 A& T6 {8 A
// the beginning of the loop.
* R8 c1 U: r+ _- s/ m' r4 B& g6 m; ?7 H
// This is a simple schedule, with only one action that is
" u+ s" R# v3 [7 l7 } // just repeated every time. See jmousetrap for more
9 @0 N' k0 o. G5 V2 b6 w // complicated schedules.4 g# t2 n$ {$ D. Y' |
* e& r) s O) u/ I* ^. p
modelSchedule = new ScheduleImpl (getZone (), 1);/ K5 ^4 R5 p0 }/ V4 I
modelSchedule.at$createAction (0, modelActions);
3 B5 `9 V C$ y1 ?5 k2 D( E( H - u h1 d* T2 g' z1 F% D3 ~' {
return this;
; k) ^# b8 N5 D) _% _ } |