HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 Z' c4 p7 K. b5 X- J5 C
9 }$ m7 E O& J public Object buildActions () {# m# K, F$ @4 V. y
super.buildActions();
' n: }$ l$ v: h/ k1 E5 q
4 P0 g$ N% t0 N L // Create the list of simulation actions. We put these in# M w( t j( o: a
// an action group, because we want these actions to be) q n) }' G0 D% S6 V" j
// executed in a specific order, but these steps should
, y w, L4 W6 V // take no (simulated) time. The M(foo) means "The message
- ]3 X: k; G( W" _ // called <foo>". You can send a message To a particular
' f5 t! L+ K8 C( W: J // object, or ForEach object in a collection.1 h$ q$ J; k- i4 y4 q# E7 N( I
+ Q+ ]6 I) {( _, L( ?9 U
// Note we update the heatspace in two phases: first run3 ?7 g8 o% Z3 N5 R% O/ O
// diffusion, then run "updateWorld" to actually enact the
( F- t' s% P; B; j; G; _8 e // changes the heatbugs have made. The ordering here is) m- N9 p# y) ?' @ [( Q$ l; C
// significant!8 P' k# ?: t4 \7 v D- h
+ l9 K) t# m" j' R* ^ // Note also, that with the additional
3 |7 e9 f) h+ L0 Y4 V/ V- ]0 K; G // `randomizeHeatbugUpdateOrder' Boolean flag we can" v% A& T- y) D, H, O O: E
// randomize the order in which the bugs actually run
# D" j, q/ q0 Q3 p // their step rule. This has the effect of removing any
5 V) V3 |1 Y3 W! D: ?8 }. |2 o! D // systematic bias in the iteration throught the heatbug8 |, X/ H' t- I
// list from timestep to timestep2 H4 S& k" ?& n' N. h3 S
. X8 a! e/ p$ W1 `$ N5 b
// By default, all `createActionForEach' modelActions have
. }" Z, C* g1 |+ E0 s // a default order of `Sequential', which means that the
0 I8 N% Y$ `) _9 c+ `5 l0 y' A // order of iteration through the `heatbugList' will be/ U( i( |/ Y2 a% ?# Y6 y
// identical (assuming the list order is not changed
. N4 a$ k8 d+ T' d+ y+ d3 M3 s // indirectly by some other process).% e. x3 Y( P7 W5 z8 \+ b& x
# l% d3 l" m; Y1 Q modelActions = new ActionGroupImpl (getZone ());
9 |7 Q/ R4 B! F3 S5 F
( X+ y% ~' L- m5 g. l* k try {
4 H+ k, D% i# } Z; h8 j modelActions.createActionTo$message( Z- ?9 J+ V( ~+ k$ Y( |3 c
(heat, new Selector (heat.getClass (), "stepRule", false));" L& Z1 p5 p) M# U5 p3 u7 d% {
} catch (Exception e) {
e, d4 Z( @) V System.err.println ("Exception stepRule: " + e.getMessage ());
# T; q: L' o* t! ~+ d }
' Z! C+ U$ g$ j9 i# N0 D/ K
" S. e% M. O6 x) U/ W, G try {, Q" `7 y7 k6 x" f
Heatbug proto = (Heatbug) heatbugList.get (0);
: O% n& c' }$ k7 M3 x$ l Selector sel =
`' t( ` z* T* p# S) x new Selector (proto.getClass (), "heatbugStep", false);1 C: y/ E6 R; b9 E, U2 {
actionForEach =7 d6 ?" q4 l( u% Q) T/ c+ Y( _! H" W# u
modelActions.createFActionForEachHomogeneous$call+ C; j) S5 f; Z' G1 m% u
(heatbugList,
, @, {! E$ z* S" a- ?! G8 b new FCallImpl (this, proto, sel,. j8 S' l' Z+ u0 ]! M
new FArgumentsImpl (this, sel)));, z7 i. j) u6 J# [5 J, c2 c
} catch (Exception e) {
) b, X/ c+ g5 B1 b' I( [2 O" r+ [8 B e.printStackTrace (System.err);' j) |6 m) j e3 C0 ~: n' f
}
- ~, i/ |6 E7 c- N% @2 D H - s- D% i5 b# u: i+ L; [2 b
syncUpdateOrder ();/ K- G) F6 c' m' U/ S8 k ^
b) D; X- d7 ^: O. V: P$ J
try {
9 E" m( A3 G* `- u, W$ l F modelActions.createActionTo$message
" m+ q+ T" k3 K8 B1 h (heat, new Selector (heat.getClass (), "updateLattice", false));; y' T+ e+ q6 U; \; c
} catch (Exception e) {: k2 Z) `' m; l1 Y
System.err.println("Exception updateLattice: " + e.getMessage ());
9 ?/ E/ k* B# h7 I& h7 [ }# x& x0 s' \4 n2 ?$ K0 ]6 E* h
3 r' m% J# l r9 c // Then we create a schedule that executes the" t! Q6 E- C/ M+ }
// modelActions. modelActions is an ActionGroup, by itself it/ a1 q2 c7 Z7 n3 ^
// has no notion of time. In order to have it executed in
# Q, _( x* |! J( c // time, we create a Schedule that says to use the
" ~; N6 u' V2 P. o // modelActions ActionGroup at particular times. This9 W) }: ]8 y4 j
// schedule has a repeat interval of 1, it will loop every4 D t1 z }) w: t/ Z
// time step. The action is executed at time 0 relative to( V( J3 g3 _4 X# |: f$ s0 }
// the beginning of the loop.9 F% J1 i5 S3 I( C& p! f2 R/ V
" _& |; K5 g; O2 {3 w3 K
// This is a simple schedule, with only one action that is
, `) |2 a; f* k) f& U, g, b // just repeated every time. See jmousetrap for more4 E e4 }8 c! U0 q; m. x- G
// complicated schedules.
1 E6 ~, u4 x+ }; T* l. {
6 ~! D- ?2 C6 N; b; M: R# ] modelSchedule = new ScheduleImpl (getZone (), 1);" r1 y: E5 d7 b9 L! J. _; ]
modelSchedule.at$createAction (0, modelActions);( w9 I' | s1 d, {
% v3 w5 x% N8 G* L+ r7 Z, M. ~
return this;+ n7 k0 R3 @- k$ O3 A
} |