HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, [6 a9 t# K0 c/ h
0 V, `& e6 ^8 c
public Object buildActions () {
- p' q7 w: c3 O super.buildActions();6 X9 g% O% \5 ~( ~8 J2 W- d
, Z/ o, G7 ^5 P4 Z% |+ p$ u // Create the list of simulation actions. We put these in- R$ o l5 k& B E, ?5 }! r( {' J
// an action group, because we want these actions to be
/ E2 P/ x4 ^$ b9 ^4 W5 p // executed in a specific order, but these steps should
2 C1 U! P1 V3 S5 L // take no (simulated) time. The M(foo) means "The message& G& _# Q4 _/ u+ ]) Q7 Y. [
// called <foo>". You can send a message To a particular
! r. p2 I/ }& w* i$ R& A8 w // object, or ForEach object in a collection.
! s# E% f8 f' d9 C4 K! o $ S: X# l: Q/ C+ X4 B6 ~
// Note we update the heatspace in two phases: first run
# r2 s4 H% F. a% Y3 q3 \ // diffusion, then run "updateWorld" to actually enact the
% z# _/ a, R8 f7 U) } // changes the heatbugs have made. The ordering here is
) h; U) V. F" b7 f3 J! w // significant!4 t& B% z9 L+ s f
: h" l5 J! N! e
// Note also, that with the additional
+ Y" M9 h! M# F" l, S( G+ n; V // `randomizeHeatbugUpdateOrder' Boolean flag we can
3 ?: S- W: Z) S% u6 y // randomize the order in which the bugs actually run
. P% x4 y9 {( T6 L) a // their step rule. This has the effect of removing any# a& t4 _/ z! f$ U1 e, a
// systematic bias in the iteration throught the heatbug b4 s3 _& d; r5 z1 V, U
// list from timestep to timestep* e, I. _3 Q% D7 i
" p5 z, p" Z. q% @
// By default, all `createActionForEach' modelActions have; T4 y( i/ r) Z/ l# c* g, u
// a default order of `Sequential', which means that the
# R8 k9 |8 b+ {; e( w // order of iteration through the `heatbugList' will be
8 q, B* d0 @% u1 j // identical (assuming the list order is not changed
6 h6 u( O; p. h6 Z4 s k0 z4 D // indirectly by some other process).) L* q# C D( i
0 v( K: G# A! ]1 I/ A, f$ Z modelActions = new ActionGroupImpl (getZone ());
- M5 G" e& f' v/ s% P* _* ~) w
& y# f, u3 _3 I" p Q try {% g0 g3 {1 r( p$ E4 B: W) z
modelActions.createActionTo$message2 P& }* _& C& V7 l: r
(heat, new Selector (heat.getClass (), "stepRule", false));
( U( I1 @+ }+ R+ g } catch (Exception e) {% m3 X4 ^ T& ]4 n
System.err.println ("Exception stepRule: " + e.getMessage ());
. z1 }# f7 f5 ]" k6 ?* M- A& K }
1 N# h% o2 v2 S2 }% T0 u! m& |) J) z* n' w
try {
+ J" k- C9 \& E0 N1 J0 P Heatbug proto = (Heatbug) heatbugList.get (0);' c$ n. G5 v7 }- o
Selector sel =
C* H" Y+ i* t1 {6 a; E new Selector (proto.getClass (), "heatbugStep", false);
- a* e* ~& E& b: x0 @# H% h6 V3 @ actionForEach =6 F( H0 [! T4 U* I8 Z) d1 {
modelActions.createFActionForEachHomogeneous$call
8 {1 r( i) _: Y! ?: _3 c (heatbugList,5 F) g) q5 q% P. n m% p, v
new FCallImpl (this, proto, sel,! P* F% P! M! P% K. @
new FArgumentsImpl (this, sel)));% O+ f3 J) a, `+ G. H5 R+ x k
} catch (Exception e) {9 i2 _! { ^# B7 O
e.printStackTrace (System.err);7 a& w/ a# N f4 k* I. b! O t& }# M
}) J3 M5 Y( W, U, [
6 N# _! x( s4 v, Z( a
syncUpdateOrder ();
- T) S$ b! P. _' f
3 \ f+ F; I! H- ?9 b5 a try {
4 z) r2 @8 P' @4 m modelActions.createActionTo$message
U2 O& X5 E5 F x1 X5 B (heat, new Selector (heat.getClass (), "updateLattice", false));/ L# |1 e$ q3 K) Y6 z- ^, v
} catch (Exception e) {' c, n7 u& H; C0 V7 A0 j7 A
System.err.println("Exception updateLattice: " + e.getMessage ());2 `. A/ {( U8 Q/ n9 M
}
3 z6 Z9 I! R/ A3 A* `! X0 b& K % V! |/ f; ~% s2 s! O6 f' V2 Q. n! c
// Then we create a schedule that executes the
- P/ ?7 k8 J$ g* ~, n // modelActions. modelActions is an ActionGroup, by itself it- s: ?' J: f6 `' ~0 A
// has no notion of time. In order to have it executed in4 j) i, U- n7 i+ b9 m6 a: T: C
// time, we create a Schedule that says to use the
% X* Z* F, y) c# L! F // modelActions ActionGroup at particular times. This
+ C5 i5 M, l U+ _0 ]8 T$ ? // schedule has a repeat interval of 1, it will loop every
1 F0 |/ V1 f8 H% E* D( M1 n$ l // time step. The action is executed at time 0 relative to+ R! c% B2 D( C; _
// the beginning of the loop.2 E4 g- z- L: e0 c1 s- F
/ C1 g' \5 ^0 s9 J // This is a simple schedule, with only one action that is5 h$ \) @# ~# I" I, x1 Q' b
// just repeated every time. See jmousetrap for more5 g4 G0 X# \* G; F
// complicated schedules.
" W2 i# { D2 k7 N/ ? A3 }9 o1 U
9 W8 f# X5 L+ m( i modelSchedule = new ScheduleImpl (getZone (), 1);' }' ^9 I1 P) z" i* n" M- Q
modelSchedule.at$createAction (0, modelActions);" s# l7 L# j- O5 x% I
* s' O z) d8 y- d* k, O* E
return this;
m3 [# E1 t9 {5 R } |