HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& s3 [3 T4 b' v
1 s; u8 a" C3 q# O public Object buildActions () {8 f* Z2 F, S+ r1 Y, O
super.buildActions(); }$ ]: D+ Y* a$ h: G
1 z$ M: |# A- A
// Create the list of simulation actions. We put these in
# E3 S8 I8 D9 j F0 N% P // an action group, because we want these actions to be7 l. C* s/ U1 m6 c. Z
// executed in a specific order, but these steps should
# i' b% i4 E6 E // take no (simulated) time. The M(foo) means "The message
& u1 X2 r5 g+ ^# k. b // called <foo>". You can send a message To a particular& M! q' O+ Q( O7 y
// object, or ForEach object in a collection.3 x k, X$ k; L! i6 }5 j
5 R( P* E J/ |
// Note we update the heatspace in two phases: first run
. _$ ^7 i. k3 q8 Q& T7 E+ o // diffusion, then run "updateWorld" to actually enact the9 A( U o9 M- W1 w8 w. U
// changes the heatbugs have made. The ordering here is5 D, \% m, `" H' q
// significant!
: _; ^0 i; n# j+ _0 W) ]. j2 z1 X
. q4 o8 B' A+ `3 m9 p // Note also, that with the additional: e6 A- B# j1 o- {0 X5 X3 Z% |
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 M s# T: m$ O a6 ~* e. o3 W; N
// randomize the order in which the bugs actually run
/ }, ? x4 t& K! j* g1 w // their step rule. This has the effect of removing any* T4 ]! t V8 ]- {' E5 R
// systematic bias in the iteration throught the heatbug
1 v1 a$ [2 n- Q8 N' \$ n3 [- X // list from timestep to timestep3 b: H) P+ [1 F+ a: W- Q4 @. Z6 H' J6 ~
5 ]! B0 t5 M- x) b0 M$ [( g
// By default, all `createActionForEach' modelActions have
1 S4 I- O5 Z, l7 Z3 K C // a default order of `Sequential', which means that the& s. t4 u5 M$ Q6 J- C% e
// order of iteration through the `heatbugList' will be
2 ^: ?5 T, w8 c6 Q% B // identical (assuming the list order is not changed' g) L2 P; {- l% t0 Z( m% K4 b
// indirectly by some other process).6 |! ?$ a2 ?# P& @# C
; o' ~" B8 V) n; \/ \/ u; F
modelActions = new ActionGroupImpl (getZone ());) ]7 H! X( ]# U; ]8 k9 X, M
& d* ~0 i& g% }/ g try {
' n, |( E) L. A b' \ modelActions.createActionTo$message
, h; d- ^: N# @4 C/ t (heat, new Selector (heat.getClass (), "stepRule", false));+ r! j5 p( ?1 U- M
} catch (Exception e) {5 c+ C. y# {; l* U: J
System.err.println ("Exception stepRule: " + e.getMessage ());" n: G! O" w# j
}
9 V3 @; e. A2 d6 T9 z' V
& ]! K' K" L8 b* m. f4 v try {9 T0 N* P" y7 ~
Heatbug proto = (Heatbug) heatbugList.get (0);
# V+ [2 W* B& Y6 B5 p) Q( P Selector sel = ! k4 S/ l3 V1 C2 s4 P3 Y
new Selector (proto.getClass (), "heatbugStep", false);; Q1 G' J K3 \6 _* w+ c5 n9 N
actionForEach =' }5 S7 f8 K( n! ^9 k; J! I
modelActions.createFActionForEachHomogeneous$call' |& K5 H- Y* q/ b4 _
(heatbugList,
7 c+ p7 D: Y n new FCallImpl (this, proto, sel,
9 W, z" f# ~! f+ I new FArgumentsImpl (this, sel)));7 ~) ^. C! o2 x5 @+ ~
} catch (Exception e) {$ U7 o" M8 v: S( H& T9 @
e.printStackTrace (System.err);
) B9 u0 s. ~8 s) B7 M D }
7 I- B' s- O! Q# W3 D
: d$ y: z$ J! E1 C( g syncUpdateOrder ();
) _+ y$ O' o& @" u9 u' h6 }) i& Y2 l+ Y2 y' T7 u- {
try {, v4 ?6 Q1 v, s, k1 x' S- ^
modelActions.createActionTo$message
- h# q: f' A% Q9 \; Q' A# u (heat, new Selector (heat.getClass (), "updateLattice", false));
' L' \# ~% m! E! B } catch (Exception e) {7 F2 V" v+ Q' Z1 U0 V
System.err.println("Exception updateLattice: " + e.getMessage ());
" s% a c. Q1 N" K/ v6 @ }* k- k7 x% z$ R2 }
; x3 E/ I5 A: h# T
// Then we create a schedule that executes the
( h0 ~, e5 E2 k9 x: d+ z! k9 G3 {' W // modelActions. modelActions is an ActionGroup, by itself it F' N% s% R# J7 g: f
// has no notion of time. In order to have it executed in6 W% ?* H) s/ ? i+ Y
// time, we create a Schedule that says to use the
! C/ ]5 E6 i& @2 x; o+ v! Q; z // modelActions ActionGroup at particular times. This
- ^: X# s$ w& g" [. p- S- a // schedule has a repeat interval of 1, it will loop every( r- Z' r& p; e
// time step. The action is executed at time 0 relative to
1 R* y3 z% m) T; q# c" \% { // the beginning of the loop.- M( W v/ ]! x; i% X5 R/ s1 C: w
; B& u" J8 e! e0 [
// This is a simple schedule, with only one action that is
1 i- t" A1 R$ C' o // just repeated every time. See jmousetrap for more
, q2 E4 Y. f5 ?# c( v7 Y. ~ // complicated schedules.. l/ J$ P- l. h# {3 w1 G- i3 A
" N4 d' G+ }% f modelSchedule = new ScheduleImpl (getZone (), 1);; ~7 S5 e# N3 T, _* t$ t
modelSchedule.at$createAction (0, modelActions);
- U& a6 Q! @0 k: b' x, b, M ?
2 { L8 w; P+ T! ^- a6 ~4 i return this;
+ ^+ ?. F8 {. V0 k0 e } |