HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 w6 T( S+ V3 W- J
9 t+ J8 c/ ^0 j0 }) H, ^! N, ~3 `8 X public Object buildActions () {
4 i- N5 G2 g: ?4 P super.buildActions();
% T1 ]1 w" i$ n6 o9 C3 F! }
5 [# D& ?3 q6 z7 \3 C // Create the list of simulation actions. We put these in
, i( V2 @8 ^1 f! j5 w' M // an action group, because we want these actions to be
7 s0 s Z% y4 j: ] // executed in a specific order, but these steps should+ H+ @' P- n. a; H
// take no (simulated) time. The M(foo) means "The message- R, @$ ^( k% k
// called <foo>". You can send a message To a particular1 w& o* ~3 s9 F) W# I8 A
// object, or ForEach object in a collection.
" y. {# ~6 R& [0 L6 N# x * f" l: M- m0 q
// Note we update the heatspace in two phases: first run
; b; I. M- S* r% f // diffusion, then run "updateWorld" to actually enact the
$ t* G3 x6 a$ h n2 ~ // changes the heatbugs have made. The ordering here is; q1 Z9 w7 I& {! W& L. K! J
// significant!( m% u2 D0 `) g2 z9 @/ D7 _
! o. g! j; f# V- H. b4 ] // Note also, that with the additional! J9 |& x' K( [( Q( E7 m
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ \! d6 M& N j$ W
// randomize the order in which the bugs actually run- D7 ?0 S, ?8 W( R+ |
// their step rule. This has the effect of removing any
% i1 t7 ~+ o$ K- D' B! a // systematic bias in the iteration throught the heatbug
& @: J) f$ l. [3 ?$ E6 b3 u // list from timestep to timestep; y7 w1 L+ L1 i6 k! S6 D2 x
. j1 R# D; }0 H6 u& a+ a // By default, all `createActionForEach' modelActions have1 h* Z( u' K. \5 C# B/ r( @
// a default order of `Sequential', which means that the! c/ h2 P* a. ~0 n! i/ w1 G, g1 C
// order of iteration through the `heatbugList' will be4 A9 F/ [. g7 h+ k" e) J
// identical (assuming the list order is not changed1 p8 ? M3 |' w5 e# x8 j* X
// indirectly by some other process).- L" Z8 l' J) g% \$ n1 G0 e
$ Y$ g/ {$ Y2 F% U
modelActions = new ActionGroupImpl (getZone ());6 }8 X6 }, K5 x& h, ^9 ~3 W
5 Y7 ?( U$ F( ^' a) b
try {
- i1 L' x% s. a6 m/ T modelActions.createActionTo$message& t3 ~6 g$ N3 [0 |& I
(heat, new Selector (heat.getClass (), "stepRule", false));
# Z0 e& x4 x( s9 \ } catch (Exception e) {5 G$ t5 U3 h& y, X {
System.err.println ("Exception stepRule: " + e.getMessage ());! U: [$ R# G2 ~/ K& T' x, z: ?% f' J) L
}
: O9 `$ J' A( U
! Q/ M* K. {/ ~; N4 Q0 A9 N2 L try {
3 c7 k7 E! a6 x+ A/ T! N Heatbug proto = (Heatbug) heatbugList.get (0);
% R: L3 F- W. m; t6 d Selector sel =
9 V1 s. @, T1 p5 \* [3 P4 e1 d% H new Selector (proto.getClass (), "heatbugStep", false);
" X: A# n3 J7 c actionForEach =
5 O# m- Y+ W" C$ f# T, u/ w modelActions.createFActionForEachHomogeneous$call4 o6 \- M" ]: j! l# f8 }) M
(heatbugList,
" X* L2 q; w, M) v new FCallImpl (this, proto, sel,) [& A& S. q1 N* H8 u
new FArgumentsImpl (this, sel)));
4 S: u8 e3 R! p# T1 C1 y0 |6 i& b } catch (Exception e) {: Z1 p1 Z. W8 Z' R [
e.printStackTrace (System.err);
1 |+ P7 L( C) q, p+ K8 m }
9 D# d% v2 H" B: U/ R% b
3 G" @6 R$ }6 |! I1 d/ ^ syncUpdateOrder ();# R! _/ q6 L/ [4 U' Y) Y
7 m" D% W7 n. {( ~# N' l, w
try {% V/ C, ], B6 C2 _+ n
modelActions.createActionTo$message
; a# V( `& ] V (heat, new Selector (heat.getClass (), "updateLattice", false));( n( p/ h5 `' b. [! e; L% B* B
} catch (Exception e) {
& N' I- r, I/ m; L, q System.err.println("Exception updateLattice: " + e.getMessage ());
/ X+ J' f, ]& N7 _4 k }
, c! [& K7 s" m6 w; v; `! a
0 }, W, J$ ^' ?( H/ x. n% J* e // Then we create a schedule that executes the" c1 ?/ ]$ ^/ W2 c$ y! k- j
// modelActions. modelActions is an ActionGroup, by itself it
7 J, Z6 Y! `) k // has no notion of time. In order to have it executed in
. q5 W! z& R) r S9 z // time, we create a Schedule that says to use the
( j; j0 w5 W) x# |) X+ r4 l- G6 S // modelActions ActionGroup at particular times. This( @, g c6 H$ @. K: t1 e& t
// schedule has a repeat interval of 1, it will loop every
) s6 \! T0 |; o7 o0 T' P // time step. The action is executed at time 0 relative to: p) i' k6 T5 u7 E
// the beginning of the loop. V% y3 R) \' W! v) Y1 o8 U* n
1 F2 }4 J( W5 M" T+ k! l // This is a simple schedule, with only one action that is
7 r, n9 n6 Y, z9 [- d; K // just repeated every time. See jmousetrap for more
+ c& O% S) D. l" z# j$ k' i: ~ // complicated schedules.: L; j% ~0 w, G1 r; D! ?; |
0 K% R2 m- O; N, J% i
modelSchedule = new ScheduleImpl (getZone (), 1);
) l) S& g/ x9 D1 ~5 K5 D) @5 r modelSchedule.at$createAction (0, modelActions);" G, }7 n$ i+ _( |( I
. L+ T- ^4 w) W+ Y return this;# M! V6 o8 H# f# s1 O2 Y0 c
} |