HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! _) N( T6 U8 g8 Z3 \ @5 W1 e# p
" _$ H" f' w/ c/ L& G
public Object buildActions () {: I- L5 q( _& h1 Q3 Q
super.buildActions();
) D: L% V4 o6 \9 s) K$ B
. ~' }, f8 d! O) N) e/ Q2 y // Create the list of simulation actions. We put these in+ i* T; R- ?- h$ _- L3 d5 b
// an action group, because we want these actions to be. @0 c$ r# p( ]' X
// executed in a specific order, but these steps should8 k ]* D) T, Y2 _
// take no (simulated) time. The M(foo) means "The message& g1 l4 n$ I& p* l& L9 v
// called <foo>". You can send a message To a particular, X' }- b2 Z! W) n
// object, or ForEach object in a collection.2 E7 T( l# L' \6 V
& B2 e& a3 ~4 @8 z // Note we update the heatspace in two phases: first run
8 d8 c; p+ u; G1 v2 F" h // diffusion, then run "updateWorld" to actually enact the
0 }* [( F" l, D // changes the heatbugs have made. The ordering here is
7 E; _4 c7 X* u: i' L1 a, { // significant!8 |* ?6 F! V+ O- `4 J z" t
9 F1 l; m+ A1 N( U T8 W( {" a5 H
// Note also, that with the additional1 F' `4 w. I* e
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 C9 P4 V' u: ?
// randomize the order in which the bugs actually run0 v2 Z. \2 w# W! K
// their step rule. This has the effect of removing any, ~5 T/ c2 q& a$ I8 k# T
// systematic bias in the iteration throught the heatbug
4 {5 ?; b( b! C" v8 ^+ l# E0 R/ D* m0 i // list from timestep to timestep" }. S* E4 A' F/ G- `* `- Z
2 O* \/ |0 _8 W. r9 O- Y // By default, all `createActionForEach' modelActions have* }6 }! \& D! M$ U' D4 B
// a default order of `Sequential', which means that the& ?" W7 Q- q1 w0 |$ N( n3 h" J
// order of iteration through the `heatbugList' will be i7 m( e/ q4 u) e. T
// identical (assuming the list order is not changed4 @* j" k" P1 m' C: k
// indirectly by some other process)./ B. s0 c; n6 D3 n) O
: I2 c& f0 z6 h modelActions = new ActionGroupImpl (getZone ());3 O2 d: h2 ~$ j" F; _
8 x5 Y) ^% j6 n7 Y try {. z* C+ b; p$ K1 M2 i8 `
modelActions.createActionTo$message
+ S- E7 P* m1 A4 V/ j, H8 ^ (heat, new Selector (heat.getClass (), "stepRule", false));- x! y4 n! { c9 i
} catch (Exception e) {
; z/ ^5 k/ l' A# L$ i System.err.println ("Exception stepRule: " + e.getMessage ());0 S' c+ }9 t9 W S0 v
}
5 W" o- C% `, r7 ]" c1 _+ }( E
! X* E+ e" m3 z Y6 g0 e( m$ l try {
7 Z m# D7 t' D' ?% e. r Heatbug proto = (Heatbug) heatbugList.get (0);
) W |% _! Y) }2 k% N0 ]+ J Selector sel = * _; H0 u: k9 y+ k$ {% G6 }
new Selector (proto.getClass (), "heatbugStep", false);
7 j. I# c' Y5 J1 p/ S' F actionForEach =
7 ~2 n# Q( d2 B modelActions.createFActionForEachHomogeneous$call
) D' s7 O ?0 q1 O2 H$ g" m# ` (heatbugList,
1 T6 N. b0 C( r! D. F new FCallImpl (this, proto, sel,( T# H/ z! i) \4 x% r
new FArgumentsImpl (this, sel)));% h2 l& H4 C t7 W! C# e
} catch (Exception e) {6 i( F& A7 X/ o
e.printStackTrace (System.err);
+ M9 Z' k A, b2 s% N }1 Q# l! Y9 e* d% A* L( ^2 M
- X! e6 p& V0 E: C! k
syncUpdateOrder ();
* f/ I9 S$ G- z1 c& w( G3 f
8 M+ v+ |7 O7 L: r try {& B1 D+ p9 K& N" a* S
modelActions.createActionTo$message
7 w9 _' t, n# W% N3 }4 [" ~ (heat, new Selector (heat.getClass (), "updateLattice", false));
1 R9 J1 T5 P9 ^; ?5 k8 V% W } catch (Exception e) {
7 ` W7 {$ F, b$ L System.err.println("Exception updateLattice: " + e.getMessage ());
9 l( T1 q" j! D" |7 a% d% T$ M }* G2 ~# g" k& H! ]( m# W; s
4 W8 c( T! D6 C // Then we create a schedule that executes the
* a) Q `. M3 |/ N // modelActions. modelActions is an ActionGroup, by itself it- o/ r8 f/ J& Z' f% M
// has no notion of time. In order to have it executed in
, J( x4 s' s4 R8 P* k* P // time, we create a Schedule that says to use the2 ^7 e2 Z2 v. f# ^. Q
// modelActions ActionGroup at particular times. This# {7 Z/ ^8 k6 v
// schedule has a repeat interval of 1, it will loop every
X9 O9 p d6 }- O, v2 w // time step. The action is executed at time 0 relative to* \. v5 z. k1 H, Y
// the beginning of the loop.
6 Q9 D6 k- y- {( Y; h% B6 P2 C. [& p5 m. j) x
// This is a simple schedule, with only one action that is- X ]% j! c+ T1 Y: P8 u; m+ c& u: x
// just repeated every time. See jmousetrap for more$ {+ V7 V0 T' n) `* B
// complicated schedules.
9 O3 D" K; F- Y7 h% _ 2 [8 m% A& U5 S6 p
modelSchedule = new ScheduleImpl (getZone (), 1);
2 |2 u1 l/ c2 p% w; t8 w3 b modelSchedule.at$createAction (0, modelActions);: k9 U/ U/ Z, a7 q" N, Q3 G
+ M6 d. @0 i) R0 d9 y# v0 T- E return this;5 H* Q/ g. m& C. G
} |