HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ ^* l; ?( {; o% i/ G5 _5 s; V6 N' Z# [& i
public Object buildActions () {
& |0 H8 @8 E' ?& m6 { super.buildActions();
$ H3 x+ m" Z" j
- \( p" @0 n4 I5 a // Create the list of simulation actions. We put these in& H( L8 N! E0 Q4 T
// an action group, because we want these actions to be+ o X* `' B! o' u8 h/ N% h; d
// executed in a specific order, but these steps should# a% k" a, D. Z" m
// take no (simulated) time. The M(foo) means "The message Q# B) `) R* _4 q2 ^0 B9 Z2 \
// called <foo>". You can send a message To a particular, b7 d, r L) X: d7 ^
// object, or ForEach object in a collection.
/ \! J1 y N! n" |5 [ & x* r- g8 S, Q- ~
// Note we update the heatspace in two phases: first run
# S; z4 ^$ T3 B) W+ Y // diffusion, then run "updateWorld" to actually enact the
/ U Z, m/ N3 i* K) H4 J // changes the heatbugs have made. The ordering here is3 G( U$ C+ ~% B9 {
// significant!
7 B' d" U* w g! x, C
9 U; Y9 D$ J+ h1 m8 r // Note also, that with the additional5 N* |/ D4 u. c( d2 x& {# P
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& ~8 K( p' V6 r& l6 G( r/ \# Z // randomize the order in which the bugs actually run" ]+ S( [$ m, R& C( s
// their step rule. This has the effect of removing any
3 S7 N0 }3 J& u; h+ { // systematic bias in the iteration throught the heatbug
6 C# }; E9 D+ w( d! h2 B/ ? // list from timestep to timestep6 p* A5 }) ]6 ?3 S; p1 r9 n, ^2 B2 t
2 C2 D$ \) w2 _4 J+ I n7 H // By default, all `createActionForEach' modelActions have" S0 j' s# d$ z: [) h/ \
// a default order of `Sequential', which means that the
# }: f/ Q, m1 `/ a5 G2 x' J // order of iteration through the `heatbugList' will be
p9 W5 q6 e. f5 I // identical (assuming the list order is not changed
( n& r2 _# [# ^3 d: o // indirectly by some other process).
. G' ?/ ^* L+ O. U) L 5 ?/ S/ j& I+ z. R
modelActions = new ActionGroupImpl (getZone ());
3 t) V( U5 W4 l
, ]1 A- H* h9 ^& D b% T' p try {
/ H; @% X5 F0 g6 J3 r$ }) d7 O modelActions.createActionTo$message" G' R* a9 ^, \, g+ y, ^; c
(heat, new Selector (heat.getClass (), "stepRule", false));: _7 P2 u: i! t+ Z2 l* N9 z$ x0 A
} catch (Exception e) {) L7 Y: h B- Q! I9 a
System.err.println ("Exception stepRule: " + e.getMessage ());+ j7 i4 ?$ B0 g# f6 W" n
}$ A' |$ O" G8 T# s
- i8 y f, _9 g1 J$ K" k try {; a& r A) M5 [- X
Heatbug proto = (Heatbug) heatbugList.get (0);
! w# s. G" p; f$ A0 C/ Y. e1 } e, D Selector sel =
7 ]" `6 |) k0 R9 L; w" c' ? new Selector (proto.getClass (), "heatbugStep", false);- B. i0 V5 b0 H7 F4 S
actionForEach =/ e; Z. T, A" G, o7 H/ `+ A
modelActions.createFActionForEachHomogeneous$call
( y# J" Q) I0 M (heatbugList,2 W+ b: y) f$ U) t
new FCallImpl (this, proto, sel,& U; R. P/ u4 X
new FArgumentsImpl (this, sel)));3 t0 Y% P2 `# C+ b0 [9 k
} catch (Exception e) {- B5 Y7 Z( H S2 S* H* W9 F
e.printStackTrace (System.err);2 Y/ l( p* ? U) u& r; t! E& ?
}
. N1 f2 E7 C- t2 C* m% [& Z % U9 S6 n' ?# ]1 t+ ^' Q
syncUpdateOrder ();
! B5 E' U7 i, c3 U; y) ^) w! Z+ d3 b( u+ s6 S3 L/ U' I# _& `
try { J" G0 Z1 y7 A6 J# q
modelActions.createActionTo$message 2 b5 P' Z: e; I* _
(heat, new Selector (heat.getClass (), "updateLattice", false));
" O+ \1 u: o5 L4 ` } catch (Exception e) {
2 k s2 M9 p& E6 }- @2 \* y/ ~" g2 o System.err.println("Exception updateLattice: " + e.getMessage ());
8 o, b' R8 u) I* q( `. q }3 C: j! c2 f4 O( u* |5 M! H
) u; v c! j& o7 b7 C6 ]2 s // Then we create a schedule that executes the0 \. o6 t* L" `4 [2 C" B7 f
// modelActions. modelActions is an ActionGroup, by itself it
9 t B! t7 h5 ~4 {& D // has no notion of time. In order to have it executed in
1 R- Z5 N$ k2 l2 \3 @- ]4 G. t // time, we create a Schedule that says to use the
$ V6 n3 q% _# u. P // modelActions ActionGroup at particular times. This+ e l7 @( s, T; Z( ^+ W7 |
// schedule has a repeat interval of 1, it will loop every' `9 i K' ?- F
// time step. The action is executed at time 0 relative to
3 C# ?4 m2 n& l* b* {1 x9 Q5 _; p // the beginning of the loop.: o$ l0 b' m) Y: k. ]8 x g- q
% c6 p7 G+ _2 y+ m4 Z
// This is a simple schedule, with only one action that is4 Q' z# q+ F; O7 ?2 ~
// just repeated every time. See jmousetrap for more
! G+ p4 B+ G" m' M // complicated schedules.
- t+ V: u8 e% l 8 ^# F8 P# P. z* \0 V2 Q
modelSchedule = new ScheduleImpl (getZone (), 1);
# C. ^5 B& [* O- X" y! {' b modelSchedule.at$createAction (0, modelActions);8 F2 M9 U* {% f: W; a- y
$ H% I$ I' e+ \( l8 G. F, N+ J return this;
" X( M2 Z. G! Z } |