HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 B5 r. `7 F- h
' b# p8 s6 A6 W1 |& \ Z& x0 e public Object buildActions () {: B' N% b$ ?, I: D/ I9 W
super.buildActions();2 e. P0 Q! B" R; p0 `. p( c; M
0 X/ w2 L ^; @7 N# w! p // Create the list of simulation actions. We put these in
- M3 b" U1 P- S& }$ l4 t: o // an action group, because we want these actions to be
2 Z8 {; F4 ?. G! D // executed in a specific order, but these steps should/ `0 T. K9 [/ T: b$ ?; `7 q
// take no (simulated) time. The M(foo) means "The message
' ?2 r: m/ _. v4 n // called <foo>". You can send a message To a particular
* _$ U8 v$ t( r' I0 H4 k // object, or ForEach object in a collection.: [& e7 b1 A2 f$ N
' a/ m( u* z: P- W" e
// Note we update the heatspace in two phases: first run
5 x7 m- h8 [' Q* r H2 G! }- { // diffusion, then run "updateWorld" to actually enact the
, V: P/ `: C% x- u // changes the heatbugs have made. The ordering here is2 W' K- O9 v6 w6 b1 b1 o
// significant!
' o0 J: x" P# S- \, ~+ {7 s + s! @+ n5 \5 W g# C! F* s5 e4 P
// Note also, that with the additional9 u5 ]7 |* m" k' J2 z; K- `
// `randomizeHeatbugUpdateOrder' Boolean flag we can# K$ a+ V1 x5 ^
// randomize the order in which the bugs actually run
, S: H! N' r( F0 S$ Q' ? // their step rule. This has the effect of removing any
: i$ w/ |/ ~; k8 H: v // systematic bias in the iteration throught the heatbug) d" O1 c$ I% |- i& }7 y
// list from timestep to timestep$ ?7 ~# S' \7 C) D& \) s2 t
9 Z* x: K+ [4 `0 e* N; s // By default, all `createActionForEach' modelActions have
( o+ |5 n6 `1 ~7 Q) U // a default order of `Sequential', which means that the
, |# {) j/ Y9 D* m- S // order of iteration through the `heatbugList' will be
" @( q: c4 h, V2 [ // identical (assuming the list order is not changed
* ]( \5 W) d! F- b0 b // indirectly by some other process).
; U1 L- I# |9 {, |4 h1 X; l# g 9 O5 Q% V' K" u: ]* _3 V9 g
modelActions = new ActionGroupImpl (getZone ());
5 Q7 j" y$ f$ W2 y" s, {
7 ]: S2 p0 s: @* [; ?/ Y try {
* j* e' J) z9 G/ [8 a% J$ ^ x modelActions.createActionTo$message, g, M6 c5 Q4 M3 b
(heat, new Selector (heat.getClass (), "stepRule", false));& l( O: a' s- @7 A0 N
} catch (Exception e) {! n- v* F4 \- Z" Y
System.err.println ("Exception stepRule: " + e.getMessage ());
1 o+ Q7 o8 x3 y( _% A4 u }
; Y7 p9 ?8 T* u- @9 g5 Z
: K% |1 M) n; m try {* K) y% u6 E: m/ }# N8 z
Heatbug proto = (Heatbug) heatbugList.get (0);, t; p" ^! U6 s/ a8 i0 O
Selector sel = 3 o* D. ` D; u6 J
new Selector (proto.getClass (), "heatbugStep", false);
* B; ?. u6 D, H& T! q' l1 A actionForEach =: G! N6 ?( b# e1 r
modelActions.createFActionForEachHomogeneous$call
+ }$ d; n% t: V0 K+ i$ x (heatbugList,8 h# C, I# c2 I2 b1 s( S
new FCallImpl (this, proto, sel,
: |4 P, ]6 z- e9 c new FArgumentsImpl (this, sel)));0 P2 Y6 _: ]' J
} catch (Exception e) {7 w, K: V7 O# t' m
e.printStackTrace (System.err); {& S0 Z! C9 _1 l$ E' I+ `7 \
}
8 x: O k- q1 b5 y0 n ) {- a* t/ x* u/ n: p; H3 {
syncUpdateOrder ();6 a4 r, }) E' ^9 G1 v# ?3 o$ m' V
k1 F4 P& y$ j4 V
try {% x9 t) u n d8 J2 j4 ]. k
modelActions.createActionTo$message
; v: k. q% K6 @ s( a5 q (heat, new Selector (heat.getClass (), "updateLattice", false));& z% @- X! k: X$ m. x
} catch (Exception e) {
# W/ t* v8 J* V D5 A System.err.println("Exception updateLattice: " + e.getMessage ());
# ?: i4 p& p# H2 |/ W! t% G }
; y8 x3 p6 p- [& r+ n5 y$ E . e' k0 U: F, u; d0 V$ d7 q' }/ V
// Then we create a schedule that executes the
2 I. J" @4 k8 ~ // modelActions. modelActions is an ActionGroup, by itself it
p! k: V+ f3 Y9 `' } // has no notion of time. In order to have it executed in
+ t1 C" z9 |* I // time, we create a Schedule that says to use the
; Y4 ~4 J; M9 c% a% S" j- ]3 m // modelActions ActionGroup at particular times. This' X$ a0 b2 N' v- O) p
// schedule has a repeat interval of 1, it will loop every
1 ?, S, i4 j2 ~/ D1 x7 [ // time step. The action is executed at time 0 relative to
1 i/ R0 y# F4 g) G9 a8 J2 O // the beginning of the loop.* y( \1 K% c3 X0 k
, U( F/ i& p3 Y4 u( T: |! ^6 F
// This is a simple schedule, with only one action that is" c5 @/ e$ N! o6 m
// just repeated every time. See jmousetrap for more2 J6 c) g) B( T; _& I+ o/ Y* x
// complicated schedules.
; D* k8 v0 n' q6 V9 C. I
( m4 O% X5 S' e8 _" j9 p- \2 b modelSchedule = new ScheduleImpl (getZone (), 1);
& B1 U! f5 d7 P modelSchedule.at$createAction (0, modelActions); t" I6 H6 \4 d. M
% r5 T$ i' \9 _, V1 K return this;; g$ b7 k1 @" ]. y+ S
} |