HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! w- n( }. H2 Q: L) U
0 w9 m2 E2 D( G6 A/ N1 p) S public Object buildActions () {$ e! R( V3 v! A. e
super.buildActions();
& y( r" N }0 |* }6 L ( x+ B+ S' }4 B* D+ y
// Create the list of simulation actions. We put these in0 n8 w" ]( m; d3 J2 m9 N
// an action group, because we want these actions to be
- m% y5 ?3 ~2 w$ H+ j+ a9 E // executed in a specific order, but these steps should
% k& u3 B3 V8 p$ g" B+ X // take no (simulated) time. The M(foo) means "The message
- t l' X4 V: i# B* u // called <foo>". You can send a message To a particular
6 v( Y; o2 |% {9 O1 B& l; S8 e // object, or ForEach object in a collection.
- k. _; Z$ q1 `6 e6 ^4 [ $ H& g" s/ w5 i; j# E+ |$ e
// Note we update the heatspace in two phases: first run
$ R0 {$ L- i4 i9 Q // diffusion, then run "updateWorld" to actually enact the$ O# z, A! _/ a" V0 b, M' N! B
// changes the heatbugs have made. The ordering here is
% X k$ R$ v. a/ ]) g // significant!: r4 s% \4 H% [, k+ a
) k, w$ b2 K! e& a
// Note also, that with the additional
A, F5 | f/ z3 L0 |& U2 s& T // `randomizeHeatbugUpdateOrder' Boolean flag we can% U' n6 P7 M: V
// randomize the order in which the bugs actually run+ v# u7 O9 D5 A. b9 Z
// their step rule. This has the effect of removing any- N3 P' y# c/ u0 j9 _, l
// systematic bias in the iteration throught the heatbug
% Z1 [! i5 E" H9 w, B! Y // list from timestep to timestep
: K R1 E; H# i6 r4 a. }6 {, C
* Y$ {# [: W& z ~6 R8 i& p, }. E // By default, all `createActionForEach' modelActions have
; k# [. z% I" g" g // a default order of `Sequential', which means that the+ i9 t: l+ M6 f% z
// order of iteration through the `heatbugList' will be4 ^/ g5 Q8 s# |) f) e
// identical (assuming the list order is not changed
: d0 B% W# t7 o e! u4 \( I // indirectly by some other process).- A; q+ Y" P3 x7 Z) e
- j l/ T3 }9 P modelActions = new ActionGroupImpl (getZone ());
# G' U K: u9 I8 m: M$ w8 f* A2 Q" X5 A, r5 b
try {
U3 X: s4 ~* z' l modelActions.createActionTo$message" Z% H/ } x' e( F; `' q
(heat, new Selector (heat.getClass (), "stepRule", false));4 _6 |- m1 \4 I9 @% [& Z
} catch (Exception e) {# w0 V$ u3 K6 b% P4 M" o5 @
System.err.println ("Exception stepRule: " + e.getMessage ());; i) O4 e' X$ x. i$ k, W$ V- A% K7 q
}
' p" [4 \( |/ r. }# k9 f {# Y" W3 c- v# k# z) [! y) G, l. I
try {
& A; I! y: E1 A" t! b Heatbug proto = (Heatbug) heatbugList.get (0);
1 _7 J5 G7 n8 ] d8 R7 V' W Selector sel = # t6 h' _8 h1 A0 i! _
new Selector (proto.getClass (), "heatbugStep", false);3 ]% ~; G {5 L" ]; C% k) N
actionForEach =
% i3 |: b' J( f ~: F modelActions.createFActionForEachHomogeneous$call
* F/ M+ o6 m% p1 w# W. r, O" t (heatbugList,
7 \, z4 g# q1 d new FCallImpl (this, proto, sel,
' E3 T( h5 c2 a) y0 y5 ^* U! ]4 ] new FArgumentsImpl (this, sel)));
H% R. |; \4 y( l0 v+ H' \& V } catch (Exception e) {
5 p; O9 x' D; } e.printStackTrace (System.err);/ |0 N. Y6 C6 G. E4 R# Y3 G% H
}
Y6 _& \0 l n8 C( N % n# O" M" H" x/ G. b# n7 m) u
syncUpdateOrder ();
- \0 Q/ N9 q! L' ]8 t$ @, x7 L# s) T3 u! }; b) ~' x
try {2 i) \- |; o# k, J
modelActions.createActionTo$message
6 O8 a; M" W3 X (heat, new Selector (heat.getClass (), "updateLattice", false));2 u$ e# A: u. g6 j- F% o' F. Z2 L( E
} catch (Exception e) {
- B- p6 i2 ? a4 K; X System.err.println("Exception updateLattice: " + e.getMessage ());
6 D- b3 i: V2 L/ ^ }
$ r) G, C# \4 W2 |' L3 p" C" ? y I. d$ m4 {$ H" l
// Then we create a schedule that executes the
* N; [- \0 _5 w8 T) B$ d // modelActions. modelActions is an ActionGroup, by itself it5 |' W6 y7 ?) H2 w: X. L
// has no notion of time. In order to have it executed in
# [1 W) g: Y X7 m7 i4 G, ?7 q // time, we create a Schedule that says to use the
! N9 x0 I @0 R // modelActions ActionGroup at particular times. This' d) Q" }+ x3 F S3 N+ X
// schedule has a repeat interval of 1, it will loop every/ }! @3 s1 I$ E- Q! X
// time step. The action is executed at time 0 relative to
4 ~' ~4 n/ |0 R6 @6 v- t6 f // the beginning of the loop.
& ]! k6 Y) k& l; ^# a0 a) R
2 [3 ^4 z& ^% I1 ^ // This is a simple schedule, with only one action that is
1 a% u- X: N9 T* d" c$ p // just repeated every time. See jmousetrap for more
( m- J3 x/ A* G% h, K- {$ Z // complicated schedules.
$ @" F; V5 K/ b) Q
; | }, a1 G/ g1 s modelSchedule = new ScheduleImpl (getZone (), 1);) q8 w4 O% n: W( q) F
modelSchedule.at$createAction (0, modelActions);. U2 C! v0 Q# C
! v8 U, X1 P0 @8 ]) I
return this;4 r z; ^) h# w( T+ ?! K' j6 L; c
} |