HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! `! W9 ]% x* l3 Z& C0 P
$ d- R g+ c" `- x6 {
public Object buildActions () {
* B% c2 v+ U8 V, j7 s( R! o' s1 A$ c super.buildActions();" A( q& K9 M- Z
8 o7 g- ]" u1 O4 g, v3 x7 M
// Create the list of simulation actions. We put these in" y9 M- a4 f1 @, o0 x6 Q/ |
// an action group, because we want these actions to be6 G/ W5 U) s- r
// executed in a specific order, but these steps should
. ^& G' J, N; M, S, q1 G // take no (simulated) time. The M(foo) means "The message
; s& U' Q4 B2 F& ?7 ?, u0 L7 T2 n // called <foo>". You can send a message To a particular1 s" `8 l. p, s+ N+ Q
// object, or ForEach object in a collection.
' d# Z, g- w. Z1 }' R& u
0 c+ M8 y$ I, u2 a // Note we update the heatspace in two phases: first run) w7 P+ K2 V" o6 h1 Q# l
// diffusion, then run "updateWorld" to actually enact the
; J4 Q m- Q8 H/ i // changes the heatbugs have made. The ordering here is
6 ^. G% B1 `" [" ] S# r: t: q // significant!3 q% R! u$ k6 S. |
4 U( _. A m0 N
// Note also, that with the additional
( n% X( M/ m. U) r // `randomizeHeatbugUpdateOrder' Boolean flag we can8 f" \. P! Q6 F6 H
// randomize the order in which the bugs actually run
" V) b1 O# }. I2 u1 W) c // their step rule. This has the effect of removing any
) p0 d4 _3 [5 U // systematic bias in the iteration throught the heatbug/ a4 e b6 O/ y
// list from timestep to timestep8 |$ H7 y! t/ N
, ]2 l4 W4 v. x) Q, }
// By default, all `createActionForEach' modelActions have
# e$ P* q; N7 P( `* ~( T // a default order of `Sequential', which means that the
3 V* L4 W* ?2 t% Q. W // order of iteration through the `heatbugList' will be
- q( z. e% B" V1 A7 v // identical (assuming the list order is not changed7 f9 K) a$ w& R+ s% g ]9 V
// indirectly by some other process).$ n1 Y" T5 q2 R2 A8 b4 B
! w$ k& g5 {& k- M3 l4 M3 b
modelActions = new ActionGroupImpl (getZone ());
. l% e! R+ T" m& G( \7 X- T% U1 t
try {
6 h! g: ]+ W/ D8 S( I" R/ @ modelActions.createActionTo$message
9 z4 S/ ~# U3 ~( h (heat, new Selector (heat.getClass (), "stepRule", false));
% ~2 U0 k1 P6 e+ m3 k } catch (Exception e) {* y6 L% l5 {/ q* {% Z1 I. b# [+ b
System.err.println ("Exception stepRule: " + e.getMessage ());, P* g) B ?" a
}
. j) ?0 `3 Z) f: ?, g1 J1 O2 h. d! o4 W0 _
try {
3 U% l1 K' }: N Heatbug proto = (Heatbug) heatbugList.get (0);; z" `7 M0 G7 d1 Z
Selector sel = % |- O; d' {( |; ]5 [
new Selector (proto.getClass (), "heatbugStep", false);. q, q! k- |+ c2 p% X, o& |; K' t
actionForEach =# U7 E5 v' a7 S$ M! l; T
modelActions.createFActionForEachHomogeneous$call
# G; P3 `0 i, N1 i# h (heatbugList,
8 n+ \+ C! N0 i/ [ new FCallImpl (this, proto, sel,
4 ^; E( B# \+ a' x, | new FArgumentsImpl (this, sel)));( C N; {1 A: g# Q" q: m
} catch (Exception e) {' P& G5 I) o7 I# H$ Q
e.printStackTrace (System.err);
. w3 U, i0 [/ s: W }' v' I3 M- \. U
8 H/ w# k+ f* W0 r syncUpdateOrder ();2 ]/ l, ?4 E* | i: s# L
- G8 x9 ^- y6 Y" q+ T3 F' ~
try {
8 E% q% p( m7 D& E# a3 R% q# K modelActions.createActionTo$message 3 f5 y( s" q& D p: O% i
(heat, new Selector (heat.getClass (), "updateLattice", false));# ~0 L. R' O* U- k, Z% t7 i
} catch (Exception e) {, R2 T3 T# k0 n! W' ~1 V0 u4 A# E
System.err.println("Exception updateLattice: " + e.getMessage ());; s9 y- [. l9 R* W2 o
}
5 ~7 ?- o4 E$ z, T5 D j# F8 j0 p0 H0 `6 |
// Then we create a schedule that executes the( D2 b% z- A2 M1 N
// modelActions. modelActions is an ActionGroup, by itself it+ ]+ w4 d5 w d
// has no notion of time. In order to have it executed in% n9 e7 Q* p7 t; w) T' {+ X
// time, we create a Schedule that says to use the
( ]9 J3 ~% s k! g# N& R5 D) v. V // modelActions ActionGroup at particular times. This0 p+ h' b7 N; \. h0 d
// schedule has a repeat interval of 1, it will loop every
7 Y/ r8 }9 {# B& T, @* K* a // time step. The action is executed at time 0 relative to- n% E7 p: B' q' S" N
// the beginning of the loop.
! ^( B8 M8 j/ r+ I! r! ^ `& C- `5 A: u& M9 u9 T9 H7 x
// This is a simple schedule, with only one action that is
' \" G' F. z- o0 Z' T, L. M; r // just repeated every time. See jmousetrap for more- C( U3 @& b& k$ K2 X5 A
// complicated schedules.
8 c- M+ _5 ~/ ^6 [* K, ]
, ]- X) X7 C4 d& I modelSchedule = new ScheduleImpl (getZone (), 1);
8 b* ^& d1 ^8 B( M( B& S modelSchedule.at$createAction (0, modelActions);
. x5 S5 R, q9 F" ?+ C , I5 P( z4 o% o% a
return this;- M5 \% Q2 o# n A2 _" H# ~! p
} |