HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 m( d- P" ~. G* k" j3 |+ [6 Z& w0 w+ e
0 w; _% x* w X; t7 K
public Object buildActions () {
" K2 \# K5 O. C v' m$ \6 k super.buildActions(); M1 S9 v+ T3 G2 k4 s. ^) x/ ^
3 b8 D" Z4 i( I# o8 M. ` // Create the list of simulation actions. We put these in
+ ` x9 r" F% A0 C$ y9 L // an action group, because we want these actions to be' o! |8 r* z' b1 k% |: F% U
// executed in a specific order, but these steps should9 t" I; R l6 ]; [: y/ x( b3 r3 y
// take no (simulated) time. The M(foo) means "The message9 |6 z9 N, `9 b% Q& \
// called <foo>". You can send a message To a particular
# J- p0 y& v4 c // object, or ForEach object in a collection./ q8 D& X: i0 q: N, _
$ i+ Q2 g I; Z" g& b // Note we update the heatspace in two phases: first run
& D- a! R5 [. @! e+ ~- _ // diffusion, then run "updateWorld" to actually enact the
" T1 d( L: q2 u7 H! i7 u. j! b5 q // changes the heatbugs have made. The ordering here is6 z# w0 h) b# g- [, J3 a
// significant!3 x4 j8 E% Z8 B H& ^/ M
! f* e+ k4 A, `9 c3 { // Note also, that with the additional* y7 S8 N1 v$ I- ]* h3 r
// `randomizeHeatbugUpdateOrder' Boolean flag we can
5 C+ X- }- U7 U9 @2 e" X/ p // randomize the order in which the bugs actually run0 h- L4 J% M% b3 Y
// their step rule. This has the effect of removing any; b$ T) u6 E; L" S* d& R# c+ l2 X8 F
// systematic bias in the iteration throught the heatbug
$ }6 T% w+ x3 j% Z! ^ // list from timestep to timestep
" s1 i; w6 `( w, U4 j0 H 2 j# B/ T4 s2 z Z" z" S* M
// By default, all `createActionForEach' modelActions have3 r- x" i* z: {. ]- [
// a default order of `Sequential', which means that the1 n9 ^- F) ?( t( O4 Z5 R# y2 A
// order of iteration through the `heatbugList' will be
3 u0 `. J* B! q! C4 ?) s& Q // identical (assuming the list order is not changed9 h) j% |. Y# j2 q
// indirectly by some other process).2 B: w% z8 Z! @: y. H( k3 r
' z' i. h* k( t. J8 x- s
modelActions = new ActionGroupImpl (getZone ());
: B) b8 C. y* L! b6 h d; B/ P" e
. N/ d1 U7 R* E# x) w. e0 u try {( f( K- i' e" f
modelActions.createActionTo$message
o, _6 H% Z& o+ b" r (heat, new Selector (heat.getClass (), "stepRule", false));; J9 m# Y: r+ D7 J( k4 ~
} catch (Exception e) {: A; z. x' S8 e P8 _2 r. j9 _
System.err.println ("Exception stepRule: " + e.getMessage ());
6 ]$ N& k% q2 Q, y: U* @; y7 N }/ a/ l a" P9 w4 y, T
7 i, e _+ T- t' j' t3 p$ y
try {
) n" m9 J' {2 m4 y7 I: u" D Heatbug proto = (Heatbug) heatbugList.get (0);
$ {: \4 ?' Q% C. y/ I Selector sel =
7 X" T% _5 J5 K6 @ new Selector (proto.getClass (), "heatbugStep", false);' c5 I$ {# a3 C. K" f
actionForEach =
. \" h2 F' m a2 G, G) Y2 T) T modelActions.createFActionForEachHomogeneous$call2 j1 I# E3 J& W" n
(heatbugList,
2 `/ h, j" I3 j5 p% i) @ new FCallImpl (this, proto, sel,
& n; @- H ]: l$ J new FArgumentsImpl (this, sel)));
- U+ |$ n, k. c. J, ~. i } catch (Exception e) {
: H+ w+ w+ t+ g. x2 f- B+ w e.printStackTrace (System.err);
" V- I m6 q6 r* r5 \( } }* c z" r; \2 f+ o) p/ i
Y A @+ O( }) Z& y, S$ d
syncUpdateOrder ();- j2 Q0 J7 m( |1 d- z4 R% h
( b( A8 N+ ?+ F0 B6 O; a9 L
try {
3 F% H- c# ~0 J+ H modelActions.createActionTo$message
0 z+ M) r0 R7 a' K4 p (heat, new Selector (heat.getClass (), "updateLattice", false));
$ Z" {: E# [. G1 K) t. W/ e6 R } catch (Exception e) {9 ?; v+ i/ B+ q. l+ N
System.err.println("Exception updateLattice: " + e.getMessage ());! _( E2 P0 D" J7 k0 K, U
}5 p/ \: K7 E% l' x$ F
* q/ Q: N" f) Y( b) C // Then we create a schedule that executes the
' k* o) f5 V. c5 D // modelActions. modelActions is an ActionGroup, by itself it
6 {+ \: @3 }! Y, n // has no notion of time. In order to have it executed in
. a/ D/ k' Q( \ // time, we create a Schedule that says to use the
- E( B3 ]7 H* q W* p // modelActions ActionGroup at particular times. This
+ x) d, Z5 {, o) A+ p. I; @ // schedule has a repeat interval of 1, it will loop every
/ p, l) L* y. r2 e$ S // time step. The action is executed at time 0 relative to& p! e7 [. `( S% ?0 d! Y5 \
// the beginning of the loop.
3 p! K+ W: z! w0 G4 d# B6 \
2 K9 i" j8 J/ _ // This is a simple schedule, with only one action that is2 ]5 R. }: z5 c) M2 {
// just repeated every time. See jmousetrap for more
# K2 ~% r) I2 N // complicated schedules.5 l" y5 X* b% W" {' O2 s
; V/ i1 P$ Q1 _& w& \
modelSchedule = new ScheduleImpl (getZone (), 1);2 _; V3 [: W7 y0 L9 r
modelSchedule.at$createAction (0, modelActions);9 a! R" Y Z Y/ v" c+ \3 `
" e5 x! d( [% ~. |# V) o# O- V
return this;
! A' S% S4 G4 m( K } |