HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 P5 s. h. x2 ~$ m/ P: X& r- r6 I1 |7 Y, B+ t- R- }
public Object buildActions () {; D) k e$ m* c$ J1 ~- m
super.buildActions();- ^& U3 y+ [8 Q* [5 g) o. ]7 y) G
. k* u" G7 L' V6 x" p: ^1 o. M
// Create the list of simulation actions. We put these in
) p+ `1 J+ t) r, \ // an action group, because we want these actions to be" e) q1 |) k# z% V8 j& r9 H
// executed in a specific order, but these steps should
# o2 L. B( \1 ~1 H' b2 | // take no (simulated) time. The M(foo) means "The message
! E( v2 z; ]$ C; } // called <foo>". You can send a message To a particular
+ x7 T; L- s+ p6 ?8 S7 w+ J // object, or ForEach object in a collection.; b3 C. Q0 K2 l1 j( ?. p
: @4 z9 z. c' n* e. r m7 M0 E
// Note we update the heatspace in two phases: first run* k5 k3 b0 R# [9 ~) g, h4 n0 U) {
// diffusion, then run "updateWorld" to actually enact the
. C: |! s: s) H: G) z // changes the heatbugs have made. The ordering here is% N' `5 z M# V+ _. C$ T
// significant!' R3 D! z- ?: @( n- g
- M' Z/ N: ~4 m: { // Note also, that with the additional' H$ M9 L; F6 Z: B( A* z- G
// `randomizeHeatbugUpdateOrder' Boolean flag we can3 V( M/ _& x4 }
// randomize the order in which the bugs actually run. k# M* j8 f0 {# L3 v/ k6 | V% q
// their step rule. This has the effect of removing any
l& `2 W3 I; N& @6 l L // systematic bias in the iteration throught the heatbug
8 H3 e: R' t# a( d) |: w1 [ // list from timestep to timestep( p, i5 _0 g) E3 c% o
$ V2 q/ V4 |/ z // By default, all `createActionForEach' modelActions have& T; A) l2 w! _6 H5 F
// a default order of `Sequential', which means that the
! u6 d$ h( U3 K6 _ // order of iteration through the `heatbugList' will be
j3 j! i, }- J6 j // identical (assuming the list order is not changed) t8 e0 |6 A9 D1 a5 p& k
// indirectly by some other process).
S% l7 E+ ~% t7 Q# g ( o, g4 K# z4 y9 p# Y. m
modelActions = new ActionGroupImpl (getZone ());" F3 R( [9 s. g0 `
; O' D1 J8 w" p! m try {
9 R$ u) d: ^, P' c- }' L. y modelActions.createActionTo$message
6 V! p: [, w; h1 v9 s9 B" ]& D' R (heat, new Selector (heat.getClass (), "stepRule", false));
; ?% y, M+ V* E' x3 g1 n: m. @1 m% Y } catch (Exception e) {
* s5 q; _" x; o, N System.err.println ("Exception stepRule: " + e.getMessage ());
7 P, c* f4 Q' @( x }9 d: o* Y& [% [7 I% Y8 E
, F$ O7 M6 w; ~/ ]5 P, v% \3 K& w
try {2 f) D; f. ?& P# i
Heatbug proto = (Heatbug) heatbugList.get (0);
$ y# P; {& O+ x Selector sel =
% y3 ^* e* D& z+ B8 n9 _% j new Selector (proto.getClass (), "heatbugStep", false);" \) |% e/ o. H2 J1 g
actionForEach =
: x! G8 |( d- D7 x1 n6 \7 N4 d$ x modelActions.createFActionForEachHomogeneous$call4 b. `6 h0 v: \' y
(heatbugList,3 Y, U% [9 ]( `8 l* Y
new FCallImpl (this, proto, sel,2 \/ w9 p, r( L: b( G# X$ a1 D
new FArgumentsImpl (this, sel)));
! r/ F- E4 U$ i' E0 t } catch (Exception e) {$ M$ }- N# o" }; A# {
e.printStackTrace (System.err);
6 C/ q2 J0 _7 }$ V }
, X+ ]+ o8 p& O+ o2 l6 N, B) ~
8 ?) {9 Q* f* w4 L9 v syncUpdateOrder ();
' x- V9 d. _4 \0 _3 k$ c' O" R6 F# }, v$ q/ C2 ? ~
try {5 a% }, Z8 `+ R0 P2 S8 u( R
modelActions.createActionTo$message + m" [4 _+ _+ F& ?2 [8 m
(heat, new Selector (heat.getClass (), "updateLattice", false));1 u% @: I' o- D% \2 s
} catch (Exception e) {0 @. V) p1 k$ k
System.err.println("Exception updateLattice: " + e.getMessage ());9 M5 a' e/ C1 g8 y" M
}; y# ?$ i( l% w7 s: L' l c/ g
% K- _" q" Z! C8 u8 k8 d // Then we create a schedule that executes the
% R8 X( x3 m4 M1 b# W // modelActions. modelActions is an ActionGroup, by itself it
; q E7 e9 k# u // has no notion of time. In order to have it executed in- e8 P* I/ d% |# p( {2 r5 a
// time, we create a Schedule that says to use the
3 g( A {' W# h3 b( s6 k0 u // modelActions ActionGroup at particular times. This6 N b$ h% s h* X3 T9 k0 J
// schedule has a repeat interval of 1, it will loop every9 H5 P; F/ v6 k3 s& d
// time step. The action is executed at time 0 relative to0 d6 @/ t6 H2 _* @( f9 P; z A! ~( t3 D- O+ Q
// the beginning of the loop.1 k' Q. ?. `* M ]& P. G
# q' I1 h4 a% e; G# `' L* l2 Q // This is a simple schedule, with only one action that is
7 |4 Q& l1 z; h) \8 j0 O/ B. L1 }* @2 U: q // just repeated every time. See jmousetrap for more
; I3 [- h3 y0 z l0 p, l // complicated schedules.0 S1 M f7 {) m" c7 U, S; m( D
8 t y { r1 v/ G modelSchedule = new ScheduleImpl (getZone (), 1);
/ l1 P8 ~! n# T# g modelSchedule.at$createAction (0, modelActions);8 Y. d, p' N& F: i. p T
& E8 E7 w6 z/ |# t# m2 h return this;0 f6 V: V, f% \+ Z7 D- j6 j- I
} |