HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- `! `% w$ G9 ]3 k4 `
2 ^) N8 J% s9 v( r/ k
public Object buildActions () {9 q1 a$ d+ x9 c7 P: c3 ?
super.buildActions();
4 K) J' _; T1 s% z" ]0 |6 b . g0 |; Y1 C* ~5 h
// Create the list of simulation actions. We put these in; u5 s& |& e( K% h9 [% ^# u }& `
// an action group, because we want these actions to be
! ~% s3 {( d" E% F& t // executed in a specific order, but these steps should
0 _& F: ~. e5 Z! X7 D' b: j // take no (simulated) time. The M(foo) means "The message, I) \* P( R- Z5 p7 t2 R0 `1 a) S [
// called <foo>". You can send a message To a particular
8 }% f$ K `7 \. @7 M // object, or ForEach object in a collection.; v9 l3 `; L0 p, L3 |
$ ]4 J( |6 g% t7 L; u' O // Note we update the heatspace in two phases: first run
, _9 ^. C$ k- a, N6 J // diffusion, then run "updateWorld" to actually enact the
6 O5 P. `: b$ i' d9 T& @% g( W // changes the heatbugs have made. The ordering here is g; o, A4 n/ @; Y6 U
// significant!/ \0 d. ~6 p4 X/ c. u* N3 ?! d
7 _8 i& l2 u$ A/ x( G6 Q: |9 ]
// Note also, that with the additional
; j: ~9 L0 s# K' T& X // `randomizeHeatbugUpdateOrder' Boolean flag we can3 ~5 `& Q% H" r) z" j# r* f
// randomize the order in which the bugs actually run; _3 U- q8 \+ j1 E$ h' r
// their step rule. This has the effect of removing any
# z1 ^" o( O$ t% ~6 f // systematic bias in the iteration throught the heatbug
: e( r- K/ ^" W6 S, K* w5 w' ` // list from timestep to timestep/ }# s' Y! `# X8 e) q( ^+ U
! x' `5 \9 Q; f! }
// By default, all `createActionForEach' modelActions have
& H" Y) ^' D' a7 a; n5 t0 P // a default order of `Sequential', which means that the8 `) q3 \$ o/ s$ x8 N( V
// order of iteration through the `heatbugList' will be
6 x" n$ C4 C2 }7 z. Z // identical (assuming the list order is not changed. Z/ z# u' V. U
// indirectly by some other process).- a8 ]* O( k4 o& u, i4 I
* T, K) ~7 X- M3 U; ?& Q- t modelActions = new ActionGroupImpl (getZone ());* H* Y' o! V. r# B4 U
. M9 K8 G% d6 s( |/ [ try {
9 [: {0 p1 h1 I( P1 `7 b+ @' K2 @ modelActions.createActionTo$message& {# j$ B+ F) S- z, `: z# Z: N
(heat, new Selector (heat.getClass (), "stepRule", false));! h! _' a6 F3 D9 @- N* v; G
} catch (Exception e) {
5 r: `7 d. _0 p; H; P+ D System.err.println ("Exception stepRule: " + e.getMessage ());2 a1 @% D- Z9 g% P/ C
}
! ~1 K% S7 r+ Q8 P, F0 p: j
# m* w0 ]- s5 L! p* c# K! G6 ~0 R try {
8 J5 c4 x1 A) Q1 [0 g Heatbug proto = (Heatbug) heatbugList.get (0);
" v8 l% |- b4 p: V. h1 o Selector sel = 7 g: h, e' V' P) A6 ~
new Selector (proto.getClass (), "heatbugStep", false);
( x7 A& T/ m% _6 E+ Y actionForEach =7 e% P5 Z! ~, j
modelActions.createFActionForEachHomogeneous$call" [$ U( s: p% G/ g9 f+ A. A: j
(heatbugList,9 Z2 @! n: L* V o" U: C/ L/ Y
new FCallImpl (this, proto, sel,( ^/ ?: ?3 i9 ~7 o# R4 D
new FArgumentsImpl (this, sel)));
$ k, H2 `4 G6 H7 W- j) j9 }0 Q } catch (Exception e) {5 k* s' D: G% L1 T: B3 S
e.printStackTrace (System.err);6 |7 d6 k4 [5 Q) E0 w7 O2 r8 K
}; O u; A: G! z' Q9 k, T. e9 T
7 P: B D9 ]' _1 A' r0 j6 i, M1 [ ]& _ syncUpdateOrder ();
[- k5 T9 |" ~! l2 Q( `+ f; ?
2 l& ^: l3 z3 m. y4 H4 V& p try {
4 w- b: W" z5 r( T modelActions.createActionTo$message
7 t! C, W! O" ?( F" l8 w: G! Q (heat, new Selector (heat.getClass (), "updateLattice", false));; m: ]5 `) P$ T' M9 K& |7 ]' z
} catch (Exception e) {
# q5 X1 l9 v, ] System.err.println("Exception updateLattice: " + e.getMessage ());
! t& h; w$ [- f P( U9 z }) H$ S3 P' s9 c
$ Q8 K9 z) ?, _) }0 O
// Then we create a schedule that executes the* w$ r; `; L/ S, t
// modelActions. modelActions is an ActionGroup, by itself it
% r9 }. {+ ~$ _0 B. J // has no notion of time. In order to have it executed in! I1 ]6 P+ z8 k0 }2 t) ` X
// time, we create a Schedule that says to use the
- Z5 P$ y' f6 b" h" o // modelActions ActionGroup at particular times. This9 ~. v, q9 Z$ j o* Q8 m* @/ R0 c
// schedule has a repeat interval of 1, it will loop every6 R7 l$ q8 ~8 @* @$ t
// time step. The action is executed at time 0 relative to
5 m r& [% d& }! I$ H // the beginning of the loop.
8 f. `/ M5 S' ?" K2 r: Q# s9 }( s
! h2 K/ O; M' T, M // This is a simple schedule, with only one action that is
, N, J$ j! i) ~, s& f" O // just repeated every time. See jmousetrap for more# x2 g e- V4 `
// complicated schedules.
% Y# t# H- o. j* g3 b! z: b$ i
3 K9 ^$ |; S/ j$ y" G modelSchedule = new ScheduleImpl (getZone (), 1);
) z5 S0 }6 x; \: c# A/ v modelSchedule.at$createAction (0, modelActions);
; x% ]7 C+ y8 Z6 o* z
; [+ w* d( \: w return this;1 Q" `' h; E! c) k4 o+ s R s
} |