HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 J7 A! x% J6 S, m* J- q4 s$ ]& N& m
public Object buildActions () {
6 Q: P. y9 H) c. F5 d) P# s super.buildActions();7 h$ `5 l+ y# Z6 G- `4 ^, B
2 S1 V# i7 O9 \% R& W' y
// Create the list of simulation actions. We put these in w8 c) \& p, i+ l K
// an action group, because we want these actions to be4 }- _# N6 \( g6 u
// executed in a specific order, but these steps should r- ?0 S' R8 ^, A/ G7 k- M
// take no (simulated) time. The M(foo) means "The message' M6 Z7 j( x2 r* [$ n$ c! z t
// called <foo>". You can send a message To a particular
# ~& S- w+ ]) L$ s // object, or ForEach object in a collection.2 ~& T. ^2 }1 G) @. J" U+ I9 o
}( `( e7 c, c6 p2 g2 i# s4 n7 o1 e% [ // Note we update the heatspace in two phases: first run
: Y) Y# {! A5 a/ v // diffusion, then run "updateWorld" to actually enact the4 X9 E9 ]! g; v0 |+ S6 U' Y# [
// changes the heatbugs have made. The ordering here is
3 {& t% z' p' Z! q* d // significant!
, q/ t* [: O4 t: }# Q, ?3 p ; ?8 R7 c r8 |' ?4 R
// Note also, that with the additional0 N; ]! Y1 A1 }' f& a
// `randomizeHeatbugUpdateOrder' Boolean flag we can
9 P5 B+ a# ~7 {& p$ k // randomize the order in which the bugs actually run
) ~$ b; l) j6 J // their step rule. This has the effect of removing any
, l# y5 K5 W2 S. t3 u // systematic bias in the iteration throught the heatbug
+ K! F7 V% m6 A+ \7 y' J7 U3 w // list from timestep to timestep8 ]% @4 b5 M3 G% x7 J
5 h7 Y8 Y% J4 }: m, }6 f // By default, all `createActionForEach' modelActions have! B- | h0 Q8 r
// a default order of `Sequential', which means that the
+ A B! C; _, L g# i" m* \" T6 o) ] // order of iteration through the `heatbugList' will be' O. x- y! q" ?" k+ j: K
// identical (assuming the list order is not changed* H1 a, B7 e( i
// indirectly by some other process).
: M; E3 J1 F1 z# b% ]2 {+ a8 Q# \8 t ( a; Z# G0 ^" I( g% M* d$ Z* [
modelActions = new ActionGroupImpl (getZone ());7 v% W+ R" j# ?% m K6 Y& X4 S! \) b
1 V0 z% Q( c; f8 s! W6 A- d try {9 j1 w+ |$ v2 d( G1 _9 _
modelActions.createActionTo$message1 o4 W5 z' X' n, ]% o8 J/ u
(heat, new Selector (heat.getClass (), "stepRule", false));
- V) d- s: C& o/ U6 K } catch (Exception e) {$ P( P, T) { @. g7 \
System.err.println ("Exception stepRule: " + e.getMessage ());' |7 e6 E2 n. p" c
}2 w: W$ c" l4 @; G/ t* g
/ G7 v3 n/ V# M7 d; `& | try {
; S2 t! b5 @1 [. B8 w% X Heatbug proto = (Heatbug) heatbugList.get (0);
8 j. j1 d& B* K Selector sel =
$ r' Q( y+ V& g1 A$ v new Selector (proto.getClass (), "heatbugStep", false);
0 T! u2 T0 G" u7 N% M1 T actionForEach =# s8 X) L& k' r6 w
modelActions.createFActionForEachHomogeneous$call
: E+ P" x. S. v& a0 N" p! x. R (heatbugList,
& y2 Q1 o. q6 [' h$ n) v+ p3 K new FCallImpl (this, proto, sel,& _+ Z6 G. Z! O* Q9 w' F# X
new FArgumentsImpl (this, sel)));
" J$ Z% D4 c8 u } catch (Exception e) {0 V2 |& T% b. f; i# f
e.printStackTrace (System.err);
9 X2 y3 t' S8 M5 u! w }
% `' w+ n5 o- l6 R/ O
% w4 b9 L! P- C2 [+ K G syncUpdateOrder ();
1 Z/ m; k. B9 a' D; v) Y/ ^ N( P; B- N. @! E% C! U' F
try {
7 N. N6 @% B9 E" b9 W! g: l modelActions.createActionTo$message 9 M7 x2 D2 y q" b3 x) F
(heat, new Selector (heat.getClass (), "updateLattice", false));: ]/ l9 s* G1 F1 v( K" }4 n; r% z
} catch (Exception e) {$ g0 p0 O, K+ g' b/ t' o7 D
System.err.println("Exception updateLattice: " + e.getMessage ());& _& B9 i' H& D! }
}
9 B3 S# n. O7 k8 q
# Q# V$ K5 U7 n6 u$ T // Then we create a schedule that executes the
( h% \5 e) M; `9 \' n // modelActions. modelActions is an ActionGroup, by itself it1 \! X8 g+ D4 Q# u! s
// has no notion of time. In order to have it executed in9 V: G4 A5 m& Q
// time, we create a Schedule that says to use the
! ^# V% `9 o, k: p" _9 _( q4 | // modelActions ActionGroup at particular times. This' J& O# q& w% I8 I& q# N
// schedule has a repeat interval of 1, it will loop every
# c. s# V, j; V4 y: g* M% E" [ // time step. The action is executed at time 0 relative to
6 ?& r5 [3 k) M5 _ // the beginning of the loop.
. k( b' l/ E1 k& q
5 j" w) C( R( B# s* m& J // This is a simple schedule, with only one action that is7 ^3 K% `, F' O( n* a: d' p
// just repeated every time. See jmousetrap for more0 c3 M& Z2 ]; o" L* {
// complicated schedules.; `, o: }" u0 R
" T9 l2 u% @5 G modelSchedule = new ScheduleImpl (getZone (), 1);
) m0 k* I0 G8 I9 h' {' L modelSchedule.at$createAction (0, modelActions);
8 @% ]( \( k8 t8 H( K + R7 i' S4 Y2 ?$ v8 r* h
return this;
. t5 ^( s5 p3 R# v6 d } |