HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ q; h3 d H n- \0 \, y
8 D+ Z8 D% z3 j+ u$ x. n
public Object buildActions () {% q# o# d$ \& w! G
super.buildActions();% g; |) K& `5 ^5 M: j/ P; y
+ R5 f4 X1 h. O
// Create the list of simulation actions. We put these in
& G/ m& Y7 X7 F- V // an action group, because we want these actions to be( K9 [: @7 @5 P4 V% n
// executed in a specific order, but these steps should9 _# i- ^, M. f! t2 ^
// take no (simulated) time. The M(foo) means "The message, ~8 t% O1 D7 a. X; {
// called <foo>". You can send a message To a particular8 i' M4 y# p8 \ {5 L# L
// object, or ForEach object in a collection.
9 @, m- H6 q- g/ ]& m
7 m& c# M, |- {% P, F# q( u // Note we update the heatspace in two phases: first run
$ C/ h9 X' V. R // diffusion, then run "updateWorld" to actually enact the
; n8 } q& C; ^9 F( X* n, [% } // changes the heatbugs have made. The ordering here is
& n; i/ \ V7 { d // significant!% x) u# C+ k+ m0 m
$ a, F m6 ` Y# A$ x% [ // Note also, that with the additional( T4 w2 P) F! ]
// `randomizeHeatbugUpdateOrder' Boolean flag we can
4 m6 t5 z2 K# x9 w- b // randomize the order in which the bugs actually run# j. |, N. E( m2 L, {: b! `, n
// their step rule. This has the effect of removing any" b) e/ K2 ~3 v9 ^( [ K! `* j" s+ ^
// systematic bias in the iteration throught the heatbug
0 ^1 D& {" W4 [( l# g7 h+ q // list from timestep to timestep
# B" y6 m$ R9 J/ X& ] 8 q/ G- K( J7 ?
// By default, all `createActionForEach' modelActions have
3 @- b% i6 i' ~+ C2 l% j: c$ e // a default order of `Sequential', which means that the
/ l; D8 @, A5 e2 p // order of iteration through the `heatbugList' will be
$ E4 m z, Z2 ]7 h9 X# b2 f // identical (assuming the list order is not changed
; @: L* n( h) t8 t% L6 K // indirectly by some other process).! ~$ Z( V& Y) X- B' i: ^
& c6 @/ U: t. U! ] U
modelActions = new ActionGroupImpl (getZone ());" ?2 L" C7 j5 _5 { B: f
, S. J3 m3 C" P b# z; m! m try {
" g# ^) \5 B6 L' u' m M) }/ R! q# B modelActions.createActionTo$message
1 b; |0 w# l2 e3 p (heat, new Selector (heat.getClass (), "stepRule", false));
) _) G! D, U$ \' K: ]" _1 L. T } catch (Exception e) {: H" z8 T3 I; a4 a# i
System.err.println ("Exception stepRule: " + e.getMessage ());, z3 A+ T( {* E8 r
}
8 p4 W; ^! G J; @! z" _6 b, z: s* P- E* e; V
try {* Z O A. P3 w7 y8 j8 y$ V$ J; N
Heatbug proto = (Heatbug) heatbugList.get (0);( \" a/ m0 P, M$ z
Selector sel =
2 e" L/ e- K; [, k m* i+ k' A! X6 \ new Selector (proto.getClass (), "heatbugStep", false);
, V$ |( e H/ F8 x actionForEach =
# ^9 Y/ Y8 H) t' z/ ~" d: O modelActions.createFActionForEachHomogeneous$call7 d# m- g8 J6 H
(heatbugList,+ V; K. O* A% a" o% r
new FCallImpl (this, proto, sel,& a! n+ W7 T* J2 k. }; T7 [
new FArgumentsImpl (this, sel)));
! S! [3 ]2 o9 | } catch (Exception e) {# R7 a! T0 q5 |9 D
e.printStackTrace (System.err);7 ^2 y8 ]$ C" _. @
}& `1 A! {1 ]# r: _4 \
( ^0 h$ i7 O v1 N/ A2 d
syncUpdateOrder ();
& u; y; d1 M5 L! i
( V" v1 u! @& @- `1 H o6 U: V try {
! B* q3 K5 }$ r8 e+ o% i modelActions.createActionTo$message
/ K1 D+ M3 g+ L. q' y6 i5 p (heat, new Selector (heat.getClass (), "updateLattice", false));6 ~$ [6 n. _0 ]% C3 ]3 _" c" \
} catch (Exception e) {
* w: U/ W9 Z+ P' X" I/ n System.err.println("Exception updateLattice: " + e.getMessage ());
0 i; `! p% H$ w2 K0 K# M7 L0 r$ ` }
2 y3 o8 F5 a: j6 f9 ^ 2 X9 g/ ^% H ^- G, g- f
// Then we create a schedule that executes the+ M: ~& O Q& D
// modelActions. modelActions is an ActionGroup, by itself it
# U3 ]4 d+ Y) e& M // has no notion of time. In order to have it executed in. i" u) f% t0 \
// time, we create a Schedule that says to use the
0 f2 B7 p7 t9 T4 a. u // modelActions ActionGroup at particular times. This2 W z% P' e2 l. C- X* x0 f
// schedule has a repeat interval of 1, it will loop every# B) v& X7 v6 |. E9 I
// time step. The action is executed at time 0 relative to6 l! [$ {# ^4 s$ N& _
// the beginning of the loop.+ f$ b- V: F- M$ w- J6 ~/ G
- M. e% O7 ]7 W, ~/ Z
// This is a simple schedule, with only one action that is
j4 b4 B% r3 b# q( w% d$ n // just repeated every time. See jmousetrap for more
$ Q; C- M% u) c9 p( Z3 q+ Q // complicated schedules.
% D4 u2 l, W* m2 `' V* r% I' u" W+ C
" X+ Q5 N7 y) f4 l" R' I modelSchedule = new ScheduleImpl (getZone (), 1);% s: q$ h! e& ]# I+ P
modelSchedule.at$createAction (0, modelActions); n0 a8 D% N$ Y
5 O7 [) p8 Y* V& H# H6 W return this;
9 l$ l1 a& h, ]. H } |