HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 y3 W8 ~! p# `9 c# |
+ f+ {: c4 C2 v3 C9 ~/ o) \( Y1 q public Object buildActions () {. p$ k$ z8 D: F8 r& @5 |9 `2 e0 q
super.buildActions(); h+ `4 ] h" d
/ p$ N4 l$ B0 F2 Q // Create the list of simulation actions. We put these in4 k$ @# R* w7 [6 T1 Y- x% S* h
// an action group, because we want these actions to be5 A" ]' K2 f' [+ w" f
// executed in a specific order, but these steps should
$ Y. i) d9 e( M! V# f // take no (simulated) time. The M(foo) means "The message) i" L* z* `; f; G& r! J g6 |0 l
// called <foo>". You can send a message To a particular: A, ~+ t8 z* H
// object, or ForEach object in a collection.$ j! ?) ?; `" f7 {. D. D
9 Y. p5 P7 O8 ^2 }& ?) c // Note we update the heatspace in two phases: first run
3 t" S9 h8 @8 z4 y9 l( i" F // diffusion, then run "updateWorld" to actually enact the
$ a; ~0 Z* V& O: `# }! B% c // changes the heatbugs have made. The ordering here is
+ p3 M; F, W+ e3 y# a: s/ C% W( c // significant!0 w7 z/ t$ C# }# [
- \" \1 ?4 u6 [) R9 {
// Note also, that with the additional, }5 a9 C. U& S- E/ P
// `randomizeHeatbugUpdateOrder' Boolean flag we can) ~0 ~8 N; j |! n$ a2 y1 N
// randomize the order in which the bugs actually run' o* f1 n- q" }9 w. H4 v
// their step rule. This has the effect of removing any
0 m; O- j0 a8 c% S1 L // systematic bias in the iteration throught the heatbug7 I; {# c4 R8 Q0 ~2 c
// list from timestep to timestep' b3 e( K- V9 q/ q
G" m9 j; t* K& C& n. w& h3 T1 G
// By default, all `createActionForEach' modelActions have& @+ R+ g3 x. i
// a default order of `Sequential', which means that the
! x$ g" F, v6 \, J, G7 K# g // order of iteration through the `heatbugList' will be
) D% G' t# ^" t# D4 X // identical (assuming the list order is not changed
; V0 X! _1 y8 O7 ]/ ^9 } // indirectly by some other process).
7 {' a5 v- F) h% a: P 5 H) t0 A+ F& j3 R& b
modelActions = new ActionGroupImpl (getZone ());
, O( f0 V- J5 R4 V j" c, z% X' U7 D1 `4 y C; A. P! r
try {
! U/ P! M3 R; \+ w; V+ y modelActions.createActionTo$message+ a* u$ ]0 y8 J8 u; q* N' h
(heat, new Selector (heat.getClass (), "stepRule", false));; A" I9 x1 Z/ L& f& t5 M* x
} catch (Exception e) {! `- T# ^$ r( ?9 p0 y7 A" R
System.err.println ("Exception stepRule: " + e.getMessage ());* g6 \! r6 |$ J n, q
} k s$ r8 q+ g9 I l/ i/ [
) _- Q' C! _( G# z try {$ Y! j4 G9 t: G! N1 g3 Y$ L4 B
Heatbug proto = (Heatbug) heatbugList.get (0);
, I$ s1 n, U! w- Y3 a, z Selector sel =
8 {% I2 @4 x1 w2 g; h. Y1 e& W new Selector (proto.getClass (), "heatbugStep", false);
* q7 E, T; B, F actionForEach = N% i' Q% {! n! E+ w
modelActions.createFActionForEachHomogeneous$call( E; ~/ T5 S/ H) U3 ^ b
(heatbugList,3 N9 _2 _& F; Z. Q5 g! J
new FCallImpl (this, proto, sel,
- Z/ R$ X, D0 V, J, N: A6 g& l new FArgumentsImpl (this, sel)));" s8 u4 N' B: B) a( u
} catch (Exception e) {
; M" m) Q* B6 s) d& J% j- k5 C e.printStackTrace (System.err);
?1 o9 C8 ^" W2 Y }" W5 K* C8 s8 b
+ h+ z) k4 M2 C1 A7 ]
syncUpdateOrder ();! \/ r8 [ r w, C( q+ S0 G
- g: Z% X6 ]% I) t: ~9 M: E
try {# A( H }# T( b5 v' f3 h
modelActions.createActionTo$message
, o7 g' C4 ?3 h5 M (heat, new Selector (heat.getClass (), "updateLattice", false));
+ P1 Q! j5 [+ [5 E2 I' C } catch (Exception e) { N1 ~9 o" O, {$ |7 ?1 r! q T
System.err.println("Exception updateLattice: " + e.getMessage ());2 D2 j( m2 y0 k8 e) K6 S+ s
}' j: p! W7 f2 w
9 S* E# ~/ F; i. y I& o' p* |9 Y
// Then we create a schedule that executes the
/ t: i2 N1 b5 q2 R! ?* n // modelActions. modelActions is an ActionGroup, by itself it
$ c4 r) ~. G$ G4 T // has no notion of time. In order to have it executed in
$ w6 \+ w% }: W7 W; X // time, we create a Schedule that says to use the! F* K9 l! J, ?/ F: f
// modelActions ActionGroup at particular times. This% d: e" a5 v, f1 V
// schedule has a repeat interval of 1, it will loop every
6 X3 d5 ]2 u- J. U // time step. The action is executed at time 0 relative to
) C+ |2 T! |, K) M3 w // the beginning of the loop.) g: _' T& z+ u* I, R& Q' V
1 _' t' P3 P# u9 i, q- ?
// This is a simple schedule, with only one action that is
; x# k, v( B% } // just repeated every time. See jmousetrap for more. k7 n W( Y. Q
// complicated schedules., V+ U9 I' Y! H/ s$ \
- c# t0 j8 D. }& ^3 D
modelSchedule = new ScheduleImpl (getZone (), 1);% S9 g3 P* V' o1 ^& O
modelSchedule.at$createAction (0, modelActions);" ?: m4 {) \5 s7 n( P
6 {+ T9 i2 H; h X return this;1 r. T3 ?1 j. \; V. X
} |