HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" q5 I+ ^" h% Y7 D. I
& H' D. L5 W+ q public Object buildActions () {, T; ~3 e, s' N1 G; Z4 [7 N
super.buildActions();
6 b( G2 g9 G8 H- E+ q5 v8 j1 K0 ?
@4 |! z* v3 p* H% L: d // Create the list of simulation actions. We put these in
- C# N4 h' w0 D! N7 { // an action group, because we want these actions to be7 `) ?( B! s) i+ v; G
// executed in a specific order, but these steps should
( ]: ?8 T0 s z0 g9 O0 M: _- l // take no (simulated) time. The M(foo) means "The message' P; r, ?9 a9 q$ D- u- I
// called <foo>". You can send a message To a particular
2 K3 I) W/ e1 y/ c. ^ // object, or ForEach object in a collection.+ ?; I6 _) G' e( l% D
6 \5 @. M" @1 C* j7 L
// Note we update the heatspace in two phases: first run* W& S; B* D# z3 u1 n6 P
// diffusion, then run "updateWorld" to actually enact the
+ Y- I: T3 ?/ T) _/ K // changes the heatbugs have made. The ordering here is
1 j) L) V' p" r3 q9 X5 G" S9 }5 q9 k // significant!( n/ H4 \4 m4 Y! R. N
6 n5 J! E- S, ~
// Note also, that with the additional( K" q+ P7 h+ W% g* U; c
// `randomizeHeatbugUpdateOrder' Boolean flag we can6 s# e+ t" L: p& h( d' M, c4 N' B. P
// randomize the order in which the bugs actually run) n5 Z- P! d8 ?7 D9 K
// their step rule. This has the effect of removing any
( G3 k ^ g/ k! O* Y // systematic bias in the iteration throught the heatbug( W/ M- U/ {7 Z# t
// list from timestep to timestep
) z0 v) Y0 A2 O0 x
& T& u- B! d) ?) c Z // By default, all `createActionForEach' modelActions have
: C6 d; s" d9 b, O // a default order of `Sequential', which means that the
6 W2 W/ d& A7 Z // order of iteration through the `heatbugList' will be
! d( a" I# E; E% g9 a% ` // identical (assuming the list order is not changed
" @+ _/ D& p8 ]& A( t, [ // indirectly by some other process).- p( n. z& `2 v! r5 l6 Y
# w+ N% a1 k1 @! y6 ^% } modelActions = new ActionGroupImpl (getZone ());" A& T! o9 n! b$ v; A2 j
6 J! ?: i2 H* C% f0 Q
try {
. h3 Z4 E1 G: |9 j- j# y6 z: ]8 [ b modelActions.createActionTo$message
8 C7 D1 r; ?# a8 K (heat, new Selector (heat.getClass (), "stepRule", false));& _! }( W. _/ S
} catch (Exception e) {
- q1 p! |8 b$ m4 P, n4 p# L$ o System.err.println ("Exception stepRule: " + e.getMessage ()); } D3 a0 \& W9 T
}
$ V( \/ V7 R) o! n& O, J2 a# ^' F9 V! [7 B, ~' t
try {
. Z) D" g$ ?+ `# ], d Heatbug proto = (Heatbug) heatbugList.get (0);" | f+ s2 [8 ~8 g
Selector sel = * c9 h* s9 D+ ]) i
new Selector (proto.getClass (), "heatbugStep", false);
p, e! t* E6 c V7 U7 c& g& W& b6 s actionForEach =
# q/ e4 L( X5 c4 _! w. }. @ modelActions.createFActionForEachHomogeneous$call
d, T3 d" z" Y0 M. E% C4 g) c (heatbugList,& R& z7 ^( i9 X. M# E
new FCallImpl (this, proto, sel,
* |9 C: b; i' z new FArgumentsImpl (this, sel)));
% m k$ B- r8 i7 X } catch (Exception e) {0 J2 Q' ]7 C- z \' h4 ^0 C
e.printStackTrace (System.err);
W" ]$ U& g9 D/ k8 u9 F }
; W/ Q% x6 F# `( u3 t9 D
6 d+ G1 j4 F4 v" | syncUpdateOrder ();
' [7 u/ w" n8 d* ]0 D& F& ~
* Z) @- @" m' X: y3 t( r+ K% f try {! U; L. J9 r5 y) K6 r! @
modelActions.createActionTo$message
; O; R/ K2 F' M8 g* @1 P (heat, new Selector (heat.getClass (), "updateLattice", false));
+ }9 ] [) F B b: K# Q8 K" C0 ? } catch (Exception e) {
- l: ~: S, _9 D System.err.println("Exception updateLattice: " + e.getMessage ());
6 r8 h6 V; x8 a( z j }9 `9 @3 Y- f. v" N1 O
: n9 w5 n& q9 r
// Then we create a schedule that executes the4 V4 R$ n: _( r/ `
// modelActions. modelActions is an ActionGroup, by itself it' {( K+ Z' _7 N: l- d1 @
// has no notion of time. In order to have it executed in& g+ ?, h0 i& h5 E0 q/ B# G) ~
// time, we create a Schedule that says to use the4 P* [7 p* a' o l9 y# ]
// modelActions ActionGroup at particular times. This
$ H4 e, e' v! T1 p9 [9 V9 j // schedule has a repeat interval of 1, it will loop every. d$ @# }& L J
// time step. The action is executed at time 0 relative to5 M) o/ v. m/ d
// the beginning of the loop.) I8 _6 K. F- n+ Q
# N% E/ F' B+ N% S% k- Y
// This is a simple schedule, with only one action that is
0 {0 X. Q+ j0 Y. l3 a q1 z // just repeated every time. See jmousetrap for more
) O- |, t: _& R( {( V: t- @4 C // complicated schedules.+ m; m. D* N# p
K, a8 F) s# a3 E6 X modelSchedule = new ScheduleImpl (getZone (), 1);
5 {4 m2 S7 H8 B! u% ~( |! k modelSchedule.at$createAction (0, modelActions);
: Q8 G+ O1 r7 {9 I* P% g$ J 3 _5 q9 n5 ` u7 q% H5 E
return this;
: F' X2 a: s, \$ }5 a$ ?) }( U } |