HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* J$ I; A$ s6 b& R' U: q( z
9 z$ ]+ z8 Z1 e
public Object buildActions () {' ?. ~% s% I4 ? y: A1 p, c: @
super.buildActions();
& R- n0 Y9 |2 {' S! ]. e" t ' D' _' }4 f4 ~# r, I
// Create the list of simulation actions. We put these in U1 N) ~% L: r+ @" N$ X# ]& t
// an action group, because we want these actions to be
) c0 ?: A1 B( I" I# B f // executed in a specific order, but these steps should2 {. Z8 p u2 s5 M' L1 j: [4 _
// take no (simulated) time. The M(foo) means "The message
& |+ h* _7 S# n; P. C3 `+ U3 [ // called <foo>". You can send a message To a particular& I: Y5 | a; f7 t
// object, or ForEach object in a collection., V- v0 U: ^/ D( Z
3 ~. R7 x' B2 j' k$ u, A" F4 ~0 m6 R
// Note we update the heatspace in two phases: first run
7 x5 ]9 m& t' D // diffusion, then run "updateWorld" to actually enact the% X) x9 g1 g6 c; _& E6 {
// changes the heatbugs have made. The ordering here is
! A Z6 f& ` k9 L5 q // significant!) P: \& D% m- h2 q2 Q9 b& m
7 M! S, T( f5 X" Q! \, e/ S
// Note also, that with the additional
3 ~" D& E: z9 r2 m+ d; |- n+ s // `randomizeHeatbugUpdateOrder' Boolean flag we can
: L! K& D% G5 Z, U // randomize the order in which the bugs actually run
0 \5 m( J5 a0 Z) y/ M // their step rule. This has the effect of removing any) Z0 P) @% Q; a3 V
// systematic bias in the iteration throught the heatbug
; l3 A% M* K; l/ E* G! i; ~ // list from timestep to timestep
?9 K: ?! g' j
3 Y: G& C- _4 H+ G // By default, all `createActionForEach' modelActions have7 S) Z+ h1 i, a+ x# _3 q; e7 v& H
// a default order of `Sequential', which means that the
6 u' W# k4 Q! v& p // order of iteration through the `heatbugList' will be
' M v- V4 e# G: ? // identical (assuming the list order is not changed: U) }8 p$ H; C. c
// indirectly by some other process).- e! r b" Z2 V: F. E$ M
. U3 U6 X/ h) A$ _! j9 Z
modelActions = new ActionGroupImpl (getZone ());# j0 v, Q( P) Q" r5 Y: I% A. E
: B5 Y0 l, D: W" o% T try {. B- h* |* Y, e1 \
modelActions.createActionTo$message
0 W J: ^+ ?0 B$ @+ C+ |1 f }9 e$ k (heat, new Selector (heat.getClass (), "stepRule", false));4 r( P4 e7 W- z. P# z
} catch (Exception e) {2 ]" [1 n5 m9 T0 l y7 `
System.err.println ("Exception stepRule: " + e.getMessage ());$ o( N9 [+ o1 Y2 P% [
}" ?8 b# g- p* R7 O( M( E
, z2 K* u, l: ]' h try {
/ A+ q; j0 w1 u) S Heatbug proto = (Heatbug) heatbugList.get (0);
4 E# z1 ?0 p% S Selector sel =
( r G- p* `8 C1 B* Y7 L new Selector (proto.getClass (), "heatbugStep", false);$ j0 b+ c3 N6 p
actionForEach =- k& n$ q8 M. L. u% @+ N$ Z
modelActions.createFActionForEachHomogeneous$call" K) n3 J, ?' L! d& \2 ~
(heatbugList,5 k! W; f& r2 O
new FCallImpl (this, proto, sel,
; H6 O1 I1 K% u8 l# w new FArgumentsImpl (this, sel)));
6 j# W" Q% X. [, m; h3 r; J* K } catch (Exception e) {
( f; L0 Z* y: S( z e.printStackTrace (System.err);: N$ v6 A; ?9 S; @4 |
}
6 l& t, S9 i9 J% @0 j7 |+ x . c) d, N& y% k0 K& z+ _. G8 v4 O9 m
syncUpdateOrder ();
$ e4 z8 h& x' F1 Z+ `; I/ d, g `/ B4 X( F8 C
try {, w$ S8 o- l. O$ A( v3 D
modelActions.createActionTo$message ' Z# _# J4 |6 R5 y/ y( l9 u
(heat, new Selector (heat.getClass (), "updateLattice", false));
1 N$ I/ A* {) I% u( O8 K7 P P } catch (Exception e) {/ C) ~; s e/ P1 q$ X
System.err.println("Exception updateLattice: " + e.getMessage ());
! W( h. R9 K, B+ t( Y }
7 d) H, Z6 Z/ s; K , p- V+ l2 p! ~
// Then we create a schedule that executes the6 b j7 D$ l6 f( m8 _
// modelActions. modelActions is an ActionGroup, by itself it K& n0 G+ f! u- `9 W0 A
// has no notion of time. In order to have it executed in- {* T, m$ b- s4 Y, i1 W( V; f
// time, we create a Schedule that says to use the
0 R: m* P/ K/ {3 Q' k4 w // modelActions ActionGroup at particular times. This
8 P1 ~# S& s' }3 X% ~/ Z/ N- T // schedule has a repeat interval of 1, it will loop every
! G: L6 c) V' b // time step. The action is executed at time 0 relative to
0 N0 a1 q/ D/ g+ O+ z" l // the beginning of the loop.0 u0 c, r$ Z$ [
1 o, n- Q- `- x/ j6 l. d4 z
// This is a simple schedule, with only one action that is5 n; a5 z7 U' E [+ U
// just repeated every time. See jmousetrap for more- R+ x* b7 p* u# N# [
// complicated schedules.
- S6 c; K8 y6 B. [% o2 ^ l - a7 t! m$ R, _* f) d+ B
modelSchedule = new ScheduleImpl (getZone (), 1);/ p+ j4 P$ r Z& T
modelSchedule.at$createAction (0, modelActions);
9 z( ]1 q3 ~* T! c+ M
5 U, Q9 ]( m1 I4 O( S return this;
" m- p4 R+ o; N( I5 x1 \ } |