HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 a1 V* T; f& S
" X- u% T& [; S# \* y public Object buildActions () {
: T0 A6 I! O/ W4 ?, X5 } super.buildActions();* V2 a5 O5 ?$ ?1 l; ?6 `: @) N
* Q) ]( u" e. v. n // Create the list of simulation actions. We put these in
8 M& j! F; H7 r Q* | // an action group, because we want these actions to be
- I; F# O x5 \1 ] // executed in a specific order, but these steps should
! [) D1 m, n5 z5 Q. V // take no (simulated) time. The M(foo) means "The message
F; v& g* w9 V. _/ U: V3 t // called <foo>". You can send a message To a particular% f' t4 ?+ v8 k" a% r0 t* z5 m0 l1 m
// object, or ForEach object in a collection., ]+ s& [& s) t+ \$ _
. w8 E* n K3 B
// Note we update the heatspace in two phases: first run
) j8 g- I/ D6 U0 A2 y // diffusion, then run "updateWorld" to actually enact the
8 G* X- j; q6 p5 I // changes the heatbugs have made. The ordering here is: d2 u, U [/ ~: b; m9 c
// significant!: E5 l% I$ W0 ?' t8 h, I
, m# |% a! P- f" z // Note also, that with the additional
* S; g+ i2 J% @. } // `randomizeHeatbugUpdateOrder' Boolean flag we can2 p3 h3 u) j/ w
// randomize the order in which the bugs actually run& B- V* e: ^! C7 G& Z- w% G
// their step rule. This has the effect of removing any3 M& l6 l: {& o- d, i4 R
// systematic bias in the iteration throught the heatbug
- A9 O: e/ N G6 u6 ? // list from timestep to timestep- J$ T$ e& _' G6 W3 ?
! F: z6 X0 V1 u3 \ // By default, all `createActionForEach' modelActions have$ y9 y2 j* ]0 f& _
// a default order of `Sequential', which means that the4 @% H+ @0 b4 z) |
// order of iteration through the `heatbugList' will be& A. K- K' s' a
// identical (assuming the list order is not changed
4 t' z: I* L& j& t# @ // indirectly by some other process).: C: O% Y& R6 a" ]! U O
! T; k, X, x/ R1 b# H1 f
modelActions = new ActionGroupImpl (getZone ());
5 F7 P( o$ W2 U- S, Z A9 C. F4 `# L% g; {5 s
try {
' Y- X, |6 ?7 R% M+ ] modelActions.createActionTo$message7 Q& _* q0 G2 ^* R0 b1 b
(heat, new Selector (heat.getClass (), "stepRule", false));
. {% g Y3 w' O+ v8 J( M& } } catch (Exception e) {
% Y2 F5 V" i4 M! j$ i' H System.err.println ("Exception stepRule: " + e.getMessage ());
! P! r+ c: A5 b* t }
6 l1 q8 B1 _$ }* J6 G8 Z
5 @& j1 y- e- F; ?& ? try {0 i/ s) _8 y0 E+ u3 x& e: f
Heatbug proto = (Heatbug) heatbugList.get (0);
# j8 p: g4 _# T3 k& u7 q Selector sel = " G, G1 C R1 ?0 }, c
new Selector (proto.getClass (), "heatbugStep", false);* v7 q) }' {) _% s( i4 w
actionForEach =
5 |9 j+ |# @7 ^5 x# i modelActions.createFActionForEachHomogeneous$call4 {4 v" K& P: D7 h8 Q
(heatbugList,
% T x2 {8 n* V$ @: z new FCallImpl (this, proto, sel,
( ~! e8 L8 x# m4 H+ [# z% G new FArgumentsImpl (this, sel)));4 M5 r0 n0 ] L n) e3 {; _
} catch (Exception e) {
+ g$ L3 J `) K* ?# M& Q8 X/ @, J e.printStackTrace (System.err); L3 C# a1 m" O) i
}) G) e; N1 E7 l7 ?4 M8 b
# E$ \" b4 M+ r" n4 O1 S0 ~4 o syncUpdateOrder ();
0 R. O/ N' `/ m; L* h M" B* z3 o+ ^$ t! Y A
try {* T. V* T& A4 h+ d1 f
modelActions.createActionTo$message - L! f7 o( h8 X! J) S/ W- j
(heat, new Selector (heat.getClass (), "updateLattice", false)); W8 R. y6 t% D4 K
} catch (Exception e) {
: u. E, f. J' }; i System.err.println("Exception updateLattice: " + e.getMessage ());! H, ?. r! B7 G" ?
}+ ^% _$ u/ p( _8 b4 A
5 \2 J9 g9 j: n9 P/ }5 c2 O // Then we create a schedule that executes the* r0 B. y! Q1 f8 m" s7 ~
// modelActions. modelActions is an ActionGroup, by itself it+ ~3 g5 z) H: v
// has no notion of time. In order to have it executed in( q- W# b7 y" y0 K( Q% X* ?- Y/ p
// time, we create a Schedule that says to use the8 ^6 @2 Q( u0 \# Z6 k
// modelActions ActionGroup at particular times. This3 e( A0 [, A4 V) S- B& h
// schedule has a repeat interval of 1, it will loop every2 p$ f: G* d2 B- J) x& ^
// time step. The action is executed at time 0 relative to1 U i! U F4 [6 w( B9 d
// the beginning of the loop.2 k' ~! a$ ]( m' o
( e- i( z: L j // This is a simple schedule, with only one action that is$ {7 Q \7 ?2 |4 }$ g2 C
// just repeated every time. See jmousetrap for more
q* h2 ^! ~4 x" C( U // complicated schedules.1 P5 M' _8 x( u( |- e" R
; c' S4 z" I. j, }/ A modelSchedule = new ScheduleImpl (getZone (), 1);
" h( d& t0 L1 H: d8 G& i4 q; ?2 R modelSchedule.at$createAction (0, modelActions);) E L9 C( t5 p- ]
# x- o; N1 W4 ^3 H0 Q" g
return this;5 C, W% C) R) s: E
} |