HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 w' B8 y H' s" h g4 @: ~8 g
3 v$ Q+ d9 L& c, v/ A6 Y public Object buildActions () {3 q# T, i4 e6 }3 O, P$ V& [* M
super.buildActions();
, [. B! Y1 t7 e5 O & H8 g; L5 q- ^$ N
// Create the list of simulation actions. We put these in6 v! i+ M9 V. K: ^" B
// an action group, because we want these actions to be+ s7 n, M# [1 }/ V" \5 ^
// executed in a specific order, but these steps should9 J; {% @- [' L; m! P0 F% @/ G
// take no (simulated) time. The M(foo) means "The message
; I0 L3 A9 d+ ?! t' \ // called <foo>". You can send a message To a particular
! I8 P m! V; S // object, or ForEach object in a collection.
/ L3 O: W e4 G1 }* R2 K " I- Z$ B& `' j. z
// Note we update the heatspace in two phases: first run
6 Z: H \; h$ h2 h // diffusion, then run "updateWorld" to actually enact the+ m" B7 l8 W! F& ]
// changes the heatbugs have made. The ordering here is
/ J4 W* g2 O. |" g! a7 \& f // significant!
0 f' Y0 `7 @- ^$ E. x+ F1 R8 q ! {/ [7 F2 d# v2 ]: _
// Note also, that with the additional
, R7 j' d" L: Q! ~- D. F // `randomizeHeatbugUpdateOrder' Boolean flag we can i4 p! M; @: S% E$ V, |- B+ l& G1 L
// randomize the order in which the bugs actually run
7 I g `7 {; Z/ H // their step rule. This has the effect of removing any7 j3 c3 N/ G: S5 }# T' S
// systematic bias in the iteration throught the heatbug
9 k; V& D: Q! i/ K! `" p // list from timestep to timestep
0 C7 b& o3 x( B1 }0 ?: [0 ^2 t
+ B$ l. n2 p2 c // By default, all `createActionForEach' modelActions have$ _+ [9 X; M5 N- z7 J, K! t# X
// a default order of `Sequential', which means that the( ~: u; J+ z" e; i5 y( x% |3 r
// order of iteration through the `heatbugList' will be
8 p2 U, N0 d# s2 Q7 N // identical (assuming the list order is not changed
* ?6 }6 V1 V- x2 t. \7 c' Y // indirectly by some other process).
& a2 S- V! M E- Q6 z9 G
- P" t' q' ?, B5 P modelActions = new ActionGroupImpl (getZone ());
; h1 d+ P( v. B- j! @. e/ Y. @
* U. b, _3 m' o' I* H try {
6 {& U* N5 N! s& F modelActions.createActionTo$message
! e4 M- j0 v+ L$ f9 z p) v0 H (heat, new Selector (heat.getClass (), "stepRule", false));
8 u0 a$ l( C- @/ E( i% _ } catch (Exception e) {
& T t1 B" a& A; b, M( t System.err.println ("Exception stepRule: " + e.getMessage ());5 \9 _- k+ i! ~& g- E
}' L* Y1 w$ Q& K$ x
9 p% i! A. c0 A
try {
7 `/ _2 Q% {5 Y% \$ p2 @9 g3 V Heatbug proto = (Heatbug) heatbugList.get (0);8 _$ g3 ?) I: H( o
Selector sel =
9 g* y! Q( O6 @' u8 \3 f$ H new Selector (proto.getClass (), "heatbugStep", false);
9 D$ p- c J4 D0 M actionForEach =0 F3 W9 t9 Z. L* D0 \4 m; b
modelActions.createFActionForEachHomogeneous$call' G- h7 R8 r* o% w+ X
(heatbugList,; J9 X6 Y* r) H5 n
new FCallImpl (this, proto, sel,
+ X7 l6 } ]8 z# P# B new FArgumentsImpl (this, sel)));! I: ?1 I5 g% u; w: k
} catch (Exception e) {
, S% `6 m6 \5 N2 S6 g e.printStackTrace (System.err);& t7 x) S N+ W( S; Z5 L8 R1 [. E
}: r" n6 ?# O( G% g2 [
5 J. w5 d9 m3 ^& z5 J
syncUpdateOrder ();
; U y3 j0 Q( n* _4 L* `9 w7 {3 x. o2 D% V! F3 {
try {
6 _" c" B% m" \ p9 w0 p5 b8 W$ z2 l modelActions.createActionTo$message / X# C* L& ]7 g" }2 v
(heat, new Selector (heat.getClass (), "updateLattice", false));
~' Y7 v, p' h0 t5 k } catch (Exception e) {! r4 s4 R; @4 R! C" R+ T4 ]
System.err.println("Exception updateLattice: " + e.getMessage ()); Z% J5 Q6 ]' j& B, k* }7 `
}
4 S+ p2 k& G- \. A5 [2 q+ w
/ g# B8 ^4 P- b( }8 s$ H // Then we create a schedule that executes the5 o$ ~* {3 L9 _" \
// modelActions. modelActions is an ActionGroup, by itself it
# h1 S- \( v3 ` // has no notion of time. In order to have it executed in! f0 l' D+ G9 E& \) W9 E; f
// time, we create a Schedule that says to use the( }1 ^: Y2 Q: S) f! T2 h) i6 P- G ]
// modelActions ActionGroup at particular times. This+ X2 N+ d3 |3 p4 c
// schedule has a repeat interval of 1, it will loop every! x( ?# k# o! c8 u
// time step. The action is executed at time 0 relative to
3 {7 Y i, u) t& b // the beginning of the loop.7 H+ Q& s* ], C: V
( s. j0 ?7 ?+ ?8 ]4 U7 Z$ k5 u! W p // This is a simple schedule, with only one action that is- d/ Y; a/ D, Q5 S! K' r
// just repeated every time. See jmousetrap for more
$ w( e; s/ ~' o0 Y% _. G // complicated schedules.# D/ J, x& Y+ G0 C/ b5 C
' ]- s' J1 V' b3 K) V' X
modelSchedule = new ScheduleImpl (getZone (), 1);6 {/ D$ B! ^ d" @; b% V
modelSchedule.at$createAction (0, modelActions);& E% d0 }. L3 `3 ?$ z' P
1 y; h$ Y* F: }0 o; U; q
return this;
( ~6 ~$ J" f* U } |