HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; I' E& x2 L8 S! }/ s
) D O b1 D* M public Object buildActions () {) j- Z4 P o$ T/ t
super.buildActions();
2 k& O1 t( R/ M* @$ m6 o ) d% m) I6 ~, k/ V- n
// Create the list of simulation actions. We put these in
5 R' r1 l/ y# ]& q // an action group, because we want these actions to be
' [8 ?+ l7 d: x2 A // executed in a specific order, but these steps should
$ J. J5 |/ u% x( v3 N // take no (simulated) time. The M(foo) means "The message
7 ?8 ^6 R! M* m- [3 g // called <foo>". You can send a message To a particular7 @9 w% D9 L/ X% K
// object, or ForEach object in a collection.
; x7 I2 r* h6 U' \
5 F; }0 B+ n: U; w6 |5 m5 `) Z) L" E // Note we update the heatspace in two phases: first run% e5 {. Z6 ]6 G7 ]0 B& K
// diffusion, then run "updateWorld" to actually enact the& u7 B, @! ~# F5 f8 h
// changes the heatbugs have made. The ordering here is
0 i" I! v3 Z3 Y& Q; l // significant!
; ~, h* _) m$ d% Y3 ^+ Y, Q
: u+ Z, u+ k( ~+ V // Note also, that with the additional7 N0 y- J0 K0 B) N) K2 G
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 e( D0 d" @4 {) \9 d# V+ r
// randomize the order in which the bugs actually run
) j( B2 ^4 U0 N0 n // their step rule. This has the effect of removing any
1 q0 u% s, p; O2 U- A) k) d // systematic bias in the iteration throught the heatbug' S+ [2 v% C7 j
// list from timestep to timestep
+ r' o9 C+ L" t
3 w, Q& o+ z& u% q // By default, all `createActionForEach' modelActions have' u2 [. v4 @: p8 C# `5 w
// a default order of `Sequential', which means that the+ j+ n5 J* V# y7 S
// order of iteration through the `heatbugList' will be/ a) `' ]; X4 W% z
// identical (assuming the list order is not changed
* ], ~* |1 B; {! p1 M* L // indirectly by some other process).
; n1 R% {- D1 @% _
# G. r! v# G% @/ ` modelActions = new ActionGroupImpl (getZone ());
7 a3 N$ a* |6 i, | S4 z7 [ Y3 m2 o8 _( a
try {
8 Z" Y' Q/ X. ~/ @2 n# w modelActions.createActionTo$message+ {$ d2 T( E( v) Z8 ?! z9 v
(heat, new Selector (heat.getClass (), "stepRule", false));
+ q8 _! B* L( g- P } catch (Exception e) {
1 i0 Q+ N# a4 X3 i. m8 ]# B5 n System.err.println ("Exception stepRule: " + e.getMessage ());
) c# X0 A Q7 ~) a }
' s b3 l( S2 t" O! d* R5 w( F, y7 n' Q6 f
try {
5 z- }" u# I! e/ F7 j3 y+ Y Heatbug proto = (Heatbug) heatbugList.get (0); Y( y9 T5 }' J5 W3 A
Selector sel = ; X' p2 D y" V h+ k
new Selector (proto.getClass (), "heatbugStep", false); J6 q1 R6 v$ N! x2 r1 t$ r) S
actionForEach =
: G. r$ ]. d; Y2 k4 T: Z modelActions.createFActionForEachHomogeneous$call' Y- _- g5 w. K; y
(heatbugList,
; s* s) c( E1 E new FCallImpl (this, proto, sel,
& G- a" @0 h* C# C8 b; g new FArgumentsImpl (this, sel)));
- ^& v. P5 e. \$ { } catch (Exception e) {
# c, t$ O% a" w1 h e.printStackTrace (System.err);3 Y' {) v: l# ~1 W
}
6 E+ Z6 ^ O/ M
6 F6 q' [' Q4 ]" n* a syncUpdateOrder ();. K7 T6 B9 M2 p9 C
5 ?& R- X2 |3 t0 ^9 V try {; Z M+ O) \* d. A. x5 p( R
modelActions.createActionTo$message
0 O. n$ A ?' n. y8 R) I (heat, new Selector (heat.getClass (), "updateLattice", false));* d4 {. m- ^: q
} catch (Exception e) {
$ A0 Y& z1 ^4 r3 k% h b System.err.println("Exception updateLattice: " + e.getMessage ());
3 k; {( x0 r- `; [ }
* ~: o7 V* t( o% t: T0 K! \8 c* @4 i ; @1 o! d( W* X V8 ~+ {
// Then we create a schedule that executes the% ]/ J1 R. Y( A/ q0 S8 l
// modelActions. modelActions is an ActionGroup, by itself it
# y' v" X9 V" {; l1 g5 ^2 M // has no notion of time. In order to have it executed in
2 \& i% B7 R- h2 ` // time, we create a Schedule that says to use the
( b% U6 r3 f6 S L# U- c // modelActions ActionGroup at particular times. This+ v# b% w$ \; Q4 `" c1 L
// schedule has a repeat interval of 1, it will loop every
7 _1 ^% u9 O2 Z6 d( O t& |$ k8 w8 n9 Q // time step. The action is executed at time 0 relative to
, H5 @3 k# l0 o9 P9 F+ m2 ` // the beginning of the loop.! d) R( ? l) ^
* F2 l! L4 O% r- \& v$ R( O
// This is a simple schedule, with only one action that is; T$ w) Y- o& f% X$ w3 e, i- N
// just repeated every time. See jmousetrap for more. _, A6 ]. w+ o- N4 \
// complicated schedules.
% Q* y/ ? Q# F X& V' {
5 }" F$ {3 }+ c) [# {2 o/ I' O modelSchedule = new ScheduleImpl (getZone (), 1);6 u0 {9 U2 z8 `9 \) t
modelSchedule.at$createAction (0, modelActions);
$ K( }7 B3 P2 |3 \# g
: p/ w7 G8 s) D# A }& N1 Q7 N6 z$ h return this;
1 o! f0 R K1 h4 _. J& v } |