HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 B8 }: m1 K1 L9 l' l
# D3 ?$ j7 ~9 a1 A3 p public Object buildActions () {
; A4 @# \1 U2 W- L! u2 y- z super.buildActions();' L! p0 B* s' @
2 {: X2 C" i( r' V n8 S1 n. ?+ O5 C+ V( S // Create the list of simulation actions. We put these in$ d7 o+ z* ^) D; S7 P
// an action group, because we want these actions to be
% J& L8 o4 n0 G0 N$ Q5 Y0 }$ p; w // executed in a specific order, but these steps should. K) R% W% t) k! G8 Q* a$ @ V& T$ U0 D
// take no (simulated) time. The M(foo) means "The message
' C5 p& z( S. O // called <foo>". You can send a message To a particular
7 k" {$ A% } X- `+ S7 w // object, or ForEach object in a collection.
2 w8 ]' @7 k( D5 ]- ^2 y
: b) a u* T, S5 b0 H9 b3 h! q // Note we update the heatspace in two phases: first run2 C" }8 Z2 l% C- X! i0 ^3 n; R" H
// diffusion, then run "updateWorld" to actually enact the
: x: c! l. Q8 B. K% Q // changes the heatbugs have made. The ordering here is) C( \) ~* _% M: q7 v. F/ R
// significant!2 a6 T0 K! \& t. h) C8 p
+ h% l" L7 H2 Y9 L
// Note also, that with the additional
$ ^1 s% c. O! }+ c // `randomizeHeatbugUpdateOrder' Boolean flag we can8 x- a; u% E) F' l7 L
// randomize the order in which the bugs actually run) `7 t/ d+ F! z) |( T! A) |2 P
// their step rule. This has the effect of removing any8 j' F3 j. q' p
// systematic bias in the iteration throught the heatbug& n* D) ~( F S
// list from timestep to timestep0 L- u+ t! i, |) h, s# c- c
6 @* b! T' l. m* |! g; O
// By default, all `createActionForEach' modelActions have
. c3 H* a- o1 `: t6 N8 K9 k // a default order of `Sequential', which means that the
. N X6 F- R2 C' w; l O* y' K // order of iteration through the `heatbugList' will be
- u& e6 K, S4 Q! u! G // identical (assuming the list order is not changed
, E7 U9 _( W; C* P. l+ h6 H // indirectly by some other process).% x3 X" I" j! ]6 d( x: g
4 b! b- _ Z+ L- r1 P I
modelActions = new ActionGroupImpl (getZone ());
# P2 F6 s# w7 B0 M, ~: Z6 ^# B. o. _, f8 {; E" K* }
try {5 w; c2 P4 M! `: s
modelActions.createActionTo$message5 v5 j7 R- t% V! C4 }" G. u
(heat, new Selector (heat.getClass (), "stepRule", false));
8 ^; b' C+ t& `( x A } catch (Exception e) {
* T* Y5 ?4 m5 ^( E& q g- M System.err.println ("Exception stepRule: " + e.getMessage ());% t- A. |7 t7 }5 s: T. L7 S
}8 C; k3 S7 U R; U+ N# u* l! E
! r" E; g9 P9 v try {& |5 I- n" C0 x
Heatbug proto = (Heatbug) heatbugList.get (0);
+ |( Q0 @. k4 f. E6 c Selector sel = : N3 I9 z0 }/ }8 t
new Selector (proto.getClass (), "heatbugStep", false);
0 F X/ I w3 ` actionForEach =, N9 I1 V; R/ p% u6 I5 h
modelActions.createFActionForEachHomogeneous$call/ r2 r+ u/ ^% H! l+ l9 u' f# O
(heatbugList,
; N/ {+ `5 x4 F6 Z+ R new FCallImpl (this, proto, sel,6 |- ^5 m/ D8 t/ `: f
new FArgumentsImpl (this, sel)));4 e( D8 x$ p. T' Q- m
} catch (Exception e) {9 l# \, P/ {0 i" o' V
e.printStackTrace (System.err);
0 K. c1 U) Q( Z& w }
6 ^1 c7 V- g% `% O; z, Q 0 d! Y- X" A% H H6 m) m
syncUpdateOrder ();" u3 G/ H8 a1 x5 f% U$ j
+ x( X! ~; b/ K) R; x* D9 K; H
try {
- N" k+ g# }3 E modelActions.createActionTo$message . x# L1 H! m/ o- d7 r/ `
(heat, new Selector (heat.getClass (), "updateLattice", false));6 F- w% Z$ i" v i" b
} catch (Exception e) {
, ?# e2 `6 h, }- b% f4 h4 q% S System.err.println("Exception updateLattice: " + e.getMessage ());# |0 |; z8 Y; M# s* N
}
% O0 _# T; D0 s+ ]# U% v# A7 h" K * j& a7 L) C7 y0 O' @, p
// Then we create a schedule that executes the# R. P1 G1 o+ l7 z% A2 V. ?) h1 a1 @
// modelActions. modelActions is an ActionGroup, by itself it( X& b- W/ W. T% W% x! V" Z' I& D
// has no notion of time. In order to have it executed in$ e/ j8 r: g9 _$ Q- W
// time, we create a Schedule that says to use the
: J* `5 U% } e2 _ ?1 _$ `$ D. A // modelActions ActionGroup at particular times. This1 x6 N! I3 y9 k9 x
// schedule has a repeat interval of 1, it will loop every
- A9 X3 g( f1 t9 A% V // time step. The action is executed at time 0 relative to( x& P" Y( a d, n
// the beginning of the loop.
! Q, N* n0 n. E) o+ x
! t3 t3 u6 _% P5 T! e$ D // This is a simple schedule, with only one action that is
0 ?* X" L3 \* V% L // just repeated every time. See jmousetrap for more, Y. U2 D) D$ n+ \- B8 a* R, l* [/ }
// complicated schedules.
* I7 ^- Z4 P: H$ `7 u / u. H/ k4 P0 l) G' Q A n# n
modelSchedule = new ScheduleImpl (getZone (), 1);
$ g# m P K, `0 P! o modelSchedule.at$createAction (0, modelActions);- f" T/ [% ?2 R s. j0 U6 T
6 t8 Z; I# X, K3 c9 t$ J/ x5 q, B
return this;
+ b* y* U4 Q6 P3 e7 P7 H } |