HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; [4 J% O7 k$ x" H, K
: U; S: `! t* Q$ y O
public Object buildActions () {
3 X" V, R" V4 s* I* k super.buildActions();+ [" O. K( i7 D( N* D
+ ?* p; B5 G, u5 M
// Create the list of simulation actions. We put these in' B8 T* @9 a9 R) M. K
// an action group, because we want these actions to be
: F! m% T' E4 l3 y0 b // executed in a specific order, but these steps should
8 w9 B, i1 l' n, R, |! ? // take no (simulated) time. The M(foo) means "The message
0 q: B5 y% e6 o* Q% j( ]' ? // called <foo>". You can send a message To a particular- `5 u% Y6 \* {$ W" N
// object, or ForEach object in a collection.7 B& e% s9 J. J$ r& ]
0 V/ Q: [7 ~! C4 H( C // Note we update the heatspace in two phases: first run
e( A6 O/ S1 H5 i* R. ~2 H // diffusion, then run "updateWorld" to actually enact the5 [* b; K$ B. F; E$ h% G' \
// changes the heatbugs have made. The ordering here is
- S% o. ?' o: H, M) j8 f // significant!
# D ^/ t4 F. B0 l7 k: ]$ M " g V; X. }. a* v' ^2 t
// Note also, that with the additional
! y2 r: }. M. W3 W8 y // `randomizeHeatbugUpdateOrder' Boolean flag we can* g `# w7 L7 x% n5 e* N; N
// randomize the order in which the bugs actually run, d9 G* i4 p; A; X1 F- K3 w6 A
// their step rule. This has the effect of removing any) s5 Q* S! M3 d) D4 X
// systematic bias in the iteration throught the heatbug
E0 A% |7 h) e3 J7 T# F; n // list from timestep to timestep }7 \! B7 G- b" o+ z7 ^+ C: @
2 D; ^; i9 f! G! {% M // By default, all `createActionForEach' modelActions have- k* H8 B$ e* ~) L
// a default order of `Sequential', which means that the# K5 w1 `/ ?- R, b; n6 t
// order of iteration through the `heatbugList' will be1 a8 F; C8 E: q; C; `% I- Z
// identical (assuming the list order is not changed& y+ |- s' R, S# M
// indirectly by some other process).) b& V* T- I" a) ?/ Q" f
4 l# C, j- @; c' U4 F+ l4 {
modelActions = new ActionGroupImpl (getZone ());2 w* p, g6 A# E. `! _
4 g- P+ K) i# |0 i" ?! G) S
try {( s) ?) \& Y7 {, m- {4 M% Q/ P |9 N5 M
modelActions.createActionTo$message% k% L3 X+ Q0 z9 [. b# F
(heat, new Selector (heat.getClass (), "stepRule", false));
% ]$ M# n& L$ q, `" p5 F } catch (Exception e) {
% f0 `# k2 G: a% }) X( d& T: \ System.err.println ("Exception stepRule: " + e.getMessage ());0 O2 z" t! @( w) Q- x/ M* ~
}
& B! I+ m" v" f9 `$ j3 k- k3 T- T& i/ A4 {7 c% c
try {9 L# ?0 p" p6 k. u. J1 N
Heatbug proto = (Heatbug) heatbugList.get (0);
4 M. l( e+ Y6 M2 y& R* a Selector sel =
: D- A& e) b. d' P% U. t new Selector (proto.getClass (), "heatbugStep", false);
4 X7 N; T- S; d; h3 n7 j; I% R actionForEach =; Q1 q, F/ Z" R
modelActions.createFActionForEachHomogeneous$call9 i3 G' T: N; q* z0 c
(heatbugList,
, q# t- S+ o1 v4 E. { new FCallImpl (this, proto, sel,% k6 g4 z& r% a* Y& {6 u* I
new FArgumentsImpl (this, sel)));. X+ z8 X3 T) f
} catch (Exception e) {0 H9 N/ q7 k- I9 q4 ?
e.printStackTrace (System.err);
% c1 f o, X( c }# O4 ~$ b; e: l4 Z, F9 C' {
# e/ b) o- w+ `7 z* v) {3 Y syncUpdateOrder ();( i% A9 W2 n' R9 p0 G
0 V2 r% m% g5 q" X try {
/ |/ j' t, T2 Z modelActions.createActionTo$message ! s" U' @& c5 ~! a! l7 B! L0 F
(heat, new Selector (heat.getClass (), "updateLattice", false));, @' X% \; [& B, V4 G# R
} catch (Exception e) {4 h: J- E( _. I+ k9 {/ n6 H0 d
System.err.println("Exception updateLattice: " + e.getMessage ());- Y2 u, E8 C! U! V& q% v
}
% ^/ ~2 H. q5 s% T2 f - p: m# J# |$ v' r# c/ B& p
// Then we create a schedule that executes the1 p, E, O9 ^/ T- P+ \) I4 }
// modelActions. modelActions is an ActionGroup, by itself it
$ k+ Y& {' N# R. v0 M! p% U5 F // has no notion of time. In order to have it executed in
. f; G+ H2 ]6 D) w! Q // time, we create a Schedule that says to use the
$ ~) I) B5 r/ c // modelActions ActionGroup at particular times. This/ c- r) P, ]3 p" Y, I
// schedule has a repeat interval of 1, it will loop every0 b; X- \/ Y. T9 O9 L: w
// time step. The action is executed at time 0 relative to
/ Q( [7 a+ x# j0 I // the beginning of the loop.$ U# o9 h: Q9 a- _: R* [% B+ w
, w3 t& {7 Y1 `
// This is a simple schedule, with only one action that is3 V4 \: Z& L+ _9 L9 [* g+ ?' H: \, Q
// just repeated every time. See jmousetrap for more
. t& ~! t5 M8 e. N3 E // complicated schedules." m6 {0 G* }1 \' U& u( g- E
5 }5 E* w8 @2 N1 Y" U0 R% q modelSchedule = new ScheduleImpl (getZone (), 1);
( O. _& W! r6 |% ] modelSchedule.at$createAction (0, modelActions);' d- f B- H0 D Y( k
# N5 H/ j: d* e/ r7 V; d return this;
4 A, |$ d2 {9 e9 b' t } |