HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 Z* Y* I) }3 ~4 F* ?
: N; M$ N/ g0 N3 P: I/ C; \
public Object buildActions () {
& N" @( b6 t, {- Z) C0 H super.buildActions();8 i/ {9 }9 x$ o8 v% I; H& ?+ a8 i
+ y$ m! d5 m4 ] {) i# z // Create the list of simulation actions. We put these in# a" m4 [& M/ Q4 m. c! h* I& o2 q+ n
// an action group, because we want these actions to be1 {& }8 G# i( q# F! A& i) I: w
// executed in a specific order, but these steps should: [" P; k) e# s( y' O8 K6 b& R7 l
// take no (simulated) time. The M(foo) means "The message! H8 ~0 n; _( F) O8 y# H& M' X
// called <foo>". You can send a message To a particular4 P* s' T/ C6 N! m
// object, or ForEach object in a collection.
4 ~. C8 N9 _9 b: q7 C: `0 e4 |
8 W4 y1 g* K# F8 `2 r // Note we update the heatspace in two phases: first run, J% k/ \0 Z; R# Z% w( D$ h
// diffusion, then run "updateWorld" to actually enact the( _' l4 w+ w0 k& b& I
// changes the heatbugs have made. The ordering here is
$ r' b; {$ i# e$ T( b // significant!
0 m7 w1 A1 t p; @' k
6 `4 ?; e- r) L$ W: g Q6 K // Note also, that with the additional
* k1 ~. J) X% ^- n. r" g // `randomizeHeatbugUpdateOrder' Boolean flag we can9 Q4 {0 u6 h a0 O& Y. G, `
// randomize the order in which the bugs actually run4 @2 `6 P0 a4 x0 B; K j
// their step rule. This has the effect of removing any
4 |3 x2 `& G% m* R# Q // systematic bias in the iteration throught the heatbug
! F1 m3 G* N( E- L- V5 B7 H8 } // list from timestep to timestep
0 B/ Q- Q0 X6 H' U* r5 j, s
, I! m- B$ L/ c' ] // By default, all `createActionForEach' modelActions have
5 n! D2 H! \ c L: {6 ^8 q ] // a default order of `Sequential', which means that the$ A7 }/ a" v( ?/ T
// order of iteration through the `heatbugList' will be, a' H" E8 i Z
// identical (assuming the list order is not changed3 F$ t+ J1 S7 r2 k$ Z2 E5 |# T
// indirectly by some other process).
- A0 Q7 o# Z# A P1 b: z ! G, k. I0 a1 z3 F8 j
modelActions = new ActionGroupImpl (getZone ());
) K* o5 |6 {7 z# u, ^7 Y- s9 D6 a9 B# S- o- f1 S
try {
) q8 p" A/ h+ I0 A0 S modelActions.createActionTo$message
' c0 y0 O5 l8 N l, t( e/ i/ ^: @4 b (heat, new Selector (heat.getClass (), "stepRule", false));
- e. }4 i2 o9 L6 P. {8 z% l2 g5 y } catch (Exception e) {0 U" C; {0 t9 c: K4 M
System.err.println ("Exception stepRule: " + e.getMessage ());' m7 Q8 D* J! g4 d) c1 N
}
4 K% m, O! K# M. M; X
, U; p6 O6 w ] try {8 ]: j( @+ L# j# n+ f }% b3 @
Heatbug proto = (Heatbug) heatbugList.get (0);5 U2 z, q# @( a. {# h" W8 D7 L
Selector sel = ; ]& _! N' A v' I. D& k) h# R
new Selector (proto.getClass (), "heatbugStep", false);
" A# j3 w L5 l, l actionForEach =
h6 v% D! W8 K! J" a2 i modelActions.createFActionForEachHomogeneous$call/ _0 ^8 f0 k: y
(heatbugList,
1 Y% J7 H. {7 ]% y7 a6 w: l new FCallImpl (this, proto, sel,9 S- l: c. l' E; B
new FArgumentsImpl (this, sel)));6 ^5 s q, Q+ f6 G
} catch (Exception e) {5 v* y) E% A0 C) d( B2 r* ~+ [
e.printStackTrace (System.err);$ ~" y& N! o$ K k0 s$ y& `
}
( e0 s9 D, A+ ]+ @+ S% i
3 l0 m0 s# O* p2 o& E syncUpdateOrder ();
1 {" g5 Y2 z/ w! v+ i& _
$ x, t4 W# b2 a" d try {2 @1 ]) B8 ]8 u* P% a
modelActions.createActionTo$message
1 O y2 B2 {$ O) O$ W (heat, new Selector (heat.getClass (), "updateLattice", false));
# f9 W7 m' z) D7 k3 D/ C! O } catch (Exception e) {- t9 L4 ~! }2 v) _0 ]) ^: j0 i% C
System.err.println("Exception updateLattice: " + e.getMessage ());, ~/ m0 b3 ~* v# m' G
}
3 E$ Y l9 x$ |; j5 E0 ]9 b 5 P* p8 S+ c1 o7 V- n U7 X
// Then we create a schedule that executes the( d. X- A) |2 Z: }. E$ V
// modelActions. modelActions is an ActionGroup, by itself it L: B4 j. m- s: \+ R
// has no notion of time. In order to have it executed in- f6 ?, o& ?9 J" E( Q ?* [& ~7 {
// time, we create a Schedule that says to use the1 y& t4 n; {, N$ v7 W8 i7 q
// modelActions ActionGroup at particular times. This
R. l M% n% k% L: m3 @' W/ e! K // schedule has a repeat interval of 1, it will loop every/ A& F7 L$ D; U: d; I- I1 U
// time step. The action is executed at time 0 relative to
8 P1 \, E- p, f* B p // the beginning of the loop.
% q* ] A1 c- M! q- Y$ U5 @# ?2 k- c- e6 T; s6 t0 A
// This is a simple schedule, with only one action that is, t, W- d8 P/ M8 J7 a
// just repeated every time. See jmousetrap for more5 `" B% M, z" `) w* Q: E) n( c P* G
// complicated schedules.
- N q! _. `, F0 u' s
5 T! m/ v* k& |8 ]- ^2 J( ?0 X modelSchedule = new ScheduleImpl (getZone (), 1);. a6 |" G2 ]& e2 e9 f$ \
modelSchedule.at$createAction (0, modelActions);
* F" \) o/ [; i; u / F. G9 u: k! F5 ~6 s
return this;
5 n" N0 X, s$ Y1 h! ^ } |