HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. _; H% B' \1 B. U9 G. T
. O' W0 J6 M, Y8 q+ V, `
public Object buildActions () {- @- D2 U: r) Z, L. z
super.buildActions();
4 ~# W+ g0 g' Z. P5 V5 M* \' w
& w- ]- A I, B. a // Create the list of simulation actions. We put these in
, l3 L! x0 `0 k' L+ O4 N- v3 N // an action group, because we want these actions to be* z% m8 F G; b- K
// executed in a specific order, but these steps should
$ N9 T6 j# y# c0 F; I( Q // take no (simulated) time. The M(foo) means "The message
, f- J+ r6 }0 M6 w7 B; N // called <foo>". You can send a message To a particular
: {) o' {2 V( z5 { // object, or ForEach object in a collection. b) i! n8 c/ S) }# l
, m! h1 T3 W* S // Note we update the heatspace in two phases: first run
! g7 }8 N/ P8 {( E* N // diffusion, then run "updateWorld" to actually enact the' v e" G& S$ z% ]
// changes the heatbugs have made. The ordering here is8 o) M( B b% z3 N6 P) X, V) Z }
// significant!
" T* v) O; M" a0 U& N. u! D0 F
$ W6 @* c' T: X5 }( n3 Z. \ // Note also, that with the additional& z2 E7 c1 |0 E! J+ J. j; E
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 o, e) [& p$ B // randomize the order in which the bugs actually run- |4 c( p2 G$ Q7 M4 `
// their step rule. This has the effect of removing any
% B8 B; d0 T: ~/ a5 M // systematic bias in the iteration throught the heatbug
8 k% P$ I9 h& F" S4 R5 m // list from timestep to timestep7 l* f) Q( S6 b1 [) @: A( G
7 m$ K+ d& x5 E# d* D- v) e3 H" g
// By default, all `createActionForEach' modelActions have
& z$ f3 c4 c+ T( q( I2 d) s$ w) P // a default order of `Sequential', which means that the9 r8 y( s# b- [* I; k7 g# ]
// order of iteration through the `heatbugList' will be F6 z" j: e, \" a) t8 z
// identical (assuming the list order is not changed9 P' p0 c6 i G- I
// indirectly by some other process).
: b% u4 R0 v& Q* e R 7 I2 M1 R5 S: z; S2 u* V6 Q; w
modelActions = new ActionGroupImpl (getZone ());
" o1 D/ ~0 N- }4 J+ }6 b: L
, y" y3 `; R3 p% Q try {
$ E) c9 z1 _( _5 P2 a8 n modelActions.createActionTo$message
; t* Q0 k* ~# k (heat, new Selector (heat.getClass (), "stepRule", false));& b6 e. v* U' Q, q, o1 c- H8 }( ~
} catch (Exception e) {1 J! X+ U1 `4 {; N
System.err.println ("Exception stepRule: " + e.getMessage ());
$ a2 _, f/ e4 I. k$ \, Y$ i }: A! {& c0 p3 N: g8 g/ H+ \
$ t; Z9 j$ F% z5 _! u6 I7 v
try {
1 i$ E: i0 n3 \ Heatbug proto = (Heatbug) heatbugList.get (0);
/ z+ G$ _9 D' y4 M, ]$ R v5 Z Selector sel = - f2 q4 J2 X$ E% C. O) V2 m5 K
new Selector (proto.getClass (), "heatbugStep", false);& {$ s! N) t0 M4 n! [% o
actionForEach =
! H8 p. `- g$ }- C modelActions.createFActionForEachHomogeneous$call$ a2 X8 z3 r1 I) V
(heatbugList,
" n R- d& r d% U new FCallImpl (this, proto, sel,
) Q7 T( c8 ~5 K8 c new FArgumentsImpl (this, sel)));+ Q6 F/ ]+ y; j' m# j, A% ~
} catch (Exception e) {
$ m: p- J) U) b, P e.printStackTrace (System.err);' |/ A$ H7 l' w$ [2 q
}, y' ?5 X+ ]+ z8 l2 @9 h/ z
6 o3 B7 n* J# \8 i) {! ^4 @
syncUpdateOrder ();4 O6 F3 [9 Q) E
, l0 A @. A8 c( s' t6 B
try {
% y% X& l- P. O modelActions.createActionTo$message ) B: W1 c& c7 `8 u* p
(heat, new Selector (heat.getClass (), "updateLattice", false));
" }3 S3 f9 k$ T; L } catch (Exception e) {7 L Y' U) M4 ~& f! g
System.err.println("Exception updateLattice: " + e.getMessage ());* \- w7 O7 k) q! |* Q. Y9 h0 e) s
}
+ ^# I; Z& R" S; @; p+ v, [, b
& V- X# n! x5 {; V // Then we create a schedule that executes the
0 {$ ~. |+ |- z7 r // modelActions. modelActions is an ActionGroup, by itself it
% \6 B. [, c% _$ J7 i7 V2 A/ g2 \ // has no notion of time. In order to have it executed in
8 A* J) i$ M0 A% p( ] // time, we create a Schedule that says to use the1 k: [2 M ]& R- l- q1 A& e
// modelActions ActionGroup at particular times. This
- B) q8 U1 `2 { // schedule has a repeat interval of 1, it will loop every' W: i7 k5 C: I# x2 X. }
// time step. The action is executed at time 0 relative to: p2 I+ E$ i+ H- O% w* R
// the beginning of the loop.
: J! d, L( D& j; o2 l
' U2 z& q0 T0 I6 a& e% x8 p2 T // This is a simple schedule, with only one action that is0 r- ^% j- [$ P% b( y8 c$ j U" V% r) H
// just repeated every time. See jmousetrap for more$ `$ t$ |+ R' w% ~# t- B
// complicated schedules.; q8 q8 o7 A, Q: x! v9 ^0 `
+ |8 U! o" o5 p( W" b) u# d4 W# R modelSchedule = new ScheduleImpl (getZone (), 1);$ [3 H- o7 }. D9 \/ B1 o/ @
modelSchedule.at$createAction (0, modelActions);$ ]9 k+ Z: c0 v' G2 z% z, G+ K! Q
0 @: N& {& Q2 b! w5 G; j return this;4 a3 {6 N/ \! e4 H
} |