HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, P# K( K- j* ]1 }: \# v- a; S" t8 _) [# v' l# T/ V B7 l
public Object buildActions () { m. Y. f8 x5 @( I) j# k$ `5 v) {: H
super.buildActions();: p* z8 f7 l/ z+ r6 Y
' Q! S8 Y6 n6 W' N! b+ U0 E // Create the list of simulation actions. We put these in" A# w! V, s; x c
// an action group, because we want these actions to be
) v) j. @* @! N+ Q% ` // executed in a specific order, but these steps should
- C" S' @( ]9 g // take no (simulated) time. The M(foo) means "The message
+ C* B2 O. \8 d n# Z // called <foo>". You can send a message To a particular
( J4 z4 ^1 K9 d3 T. t // object, or ForEach object in a collection.
, o9 C }& \' y8 k) ` 1 N# ?7 D& v% g+ n" M' n
// Note we update the heatspace in two phases: first run+ U" Z3 R7 s9 V4 {
// diffusion, then run "updateWorld" to actually enact the5 Y3 h! W* A" y3 I& {- c
// changes the heatbugs have made. The ordering here is
/ {! U0 _+ w9 w // significant!6 \& g/ Z) x, p# c
3 q ?7 _7 b9 x2 R& P // Note also, that with the additional5 D2 w2 `# y d0 ~7 j* l8 g/ ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can% `6 T9 q4 i6 }. E+ y N1 Z7 q
// randomize the order in which the bugs actually run, q' S' K7 h! o/ p
// their step rule. This has the effect of removing any
) k$ L+ p' N2 r' ]* q7 E // systematic bias in the iteration throught the heatbug
8 C$ G/ f" B( t! Q% d6 O8 Z // list from timestep to timestep
7 o, w/ l3 c G 1 a+ I" a4 V2 `9 n- `4 k
// By default, all `createActionForEach' modelActions have4 J, Y, J; e2 @7 l8 }1 F( t; ^
// a default order of `Sequential', which means that the
$ u! f0 T+ Y: p: g6 Z V8 {" w u& N+ w // order of iteration through the `heatbugList' will be
! \) Q* e. f- F6 T8 k // identical (assuming the list order is not changed
+ [2 f0 W0 I! @+ |9 N // indirectly by some other process).
' g( n; l8 k% _/ m6 M
$ k# x" S9 r; E$ M X4 f modelActions = new ActionGroupImpl (getZone ());& |, }# E" z3 E4 U' J
9 B+ y" _* [- [- ^- Y0 I" T. \ try {! ^$ K2 v9 q9 n# O. v" k2 s
modelActions.createActionTo$message3 W6 Q0 K& |+ B+ ~
(heat, new Selector (heat.getClass (), "stepRule", false));
% w9 ?3 G9 ~: ~% P2 g# i } catch (Exception e) {0 \0 p: z) h4 c6 M1 D0 A
System.err.println ("Exception stepRule: " + e.getMessage ());
1 X, M; R# G+ ]% z9 A; \" r4 { }! S G/ U( N0 G7 j; h
* m- p, f! s' S Z. ? ?# b" C M try {
- x" ], t. \) {" q6 Q Heatbug proto = (Heatbug) heatbugList.get (0);
; F2 V x+ A4 \: m! r( c Selector sel =
3 B. N4 S. g2 L) [ new Selector (proto.getClass (), "heatbugStep", false);
% Y: y: W: {4 _; J5 m actionForEach =7 m7 n1 A" B* q0 S8 K
modelActions.createFActionForEachHomogeneous$call! X8 N- f6 m, P
(heatbugList,. f# }5 z. `4 ~6 X8 T
new FCallImpl (this, proto, sel,# K) D: Y! _1 Y( M3 J. ?4 ~$ o4 N
new FArgumentsImpl (this, sel)));& G0 a2 j6 `6 H
} catch (Exception e) {3 ^& G0 Y: m( {# y5 A; ?
e.printStackTrace (System.err);
5 T3 r8 b! {6 b7 R' U3 m5 L3 S }
. ~! E; K1 [& p0 Z / e1 D% u, F B+ Z# o. F
syncUpdateOrder ();5 m, y9 i S/ s( s! M+ F( E5 M
) g [3 q9 K& A% x! |
try {$ S) Z0 |, r6 A% c8 Z
modelActions.createActionTo$message
7 @1 z' O0 T- p6 x C6 _ (heat, new Selector (heat.getClass (), "updateLattice", false));
' l3 E* Q5 \, l; ]3 Y B( G } catch (Exception e) {
% M9 Z3 D$ `, @* W6 l8 T System.err.println("Exception updateLattice: " + e.getMessage ());& t& u, `: ^( v! h6 ?7 z9 W
}$ y; k* m; _% o" x3 {* ^: h/ {: p/ N
- e: ^( C2 d3 n, {
// Then we create a schedule that executes the7 @( p0 n- A" X1 |6 ]
// modelActions. modelActions is an ActionGroup, by itself it1 ]4 K: r* V3 l
// has no notion of time. In order to have it executed in
7 R3 D6 x& o$ }* Z" t1 ?& Y! _4 P8 V // time, we create a Schedule that says to use the- F7 e! L9 y$ q7 V
// modelActions ActionGroup at particular times. This
% j2 f5 p9 K5 J" O6 Z7 ~4 E! _ // schedule has a repeat interval of 1, it will loop every$ C# B$ c* z3 @
// time step. The action is executed at time 0 relative to
: W/ u6 T- d( P& g& j2 K p // the beginning of the loop.- _8 I- W2 ~6 c" s7 S
8 ]5 H0 J) g' \$ Q
// This is a simple schedule, with only one action that is( C' @) r+ K* ]" f
// just repeated every time. See jmousetrap for more
2 ]* [; v$ i* a5 P // complicated schedules.0 e( F# s! ?+ W+ o. j* q: k( Q5 A
$ B' k# X- [: S$ z modelSchedule = new ScheduleImpl (getZone (), 1);9 D. Q- x) Q' z ~
modelSchedule.at$createAction (0, modelActions);
7 e: A4 I9 v! I# M $ a3 X) C! b& T7 ~: b" L
return this;" T7 f2 N7 r7 U! F- k
} |