HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. s' i! u! Z. O; w# u! Y( }
% b9 X8 p! i6 R& c: ^, g public Object buildActions () {& C3 d3 @) }5 n& V
super.buildActions();: o% |( I6 M8 C
; k, N3 J0 y5 k; k2 N% M$ ^# ?, a
// Create the list of simulation actions. We put these in
) D8 Y) }. Y3 s% R, K# A // an action group, because we want these actions to be
* S% t% B+ `2 |& T! _+ F ? // executed in a specific order, but these steps should
& [! d% A2 h& ]4 k. o2 ? // take no (simulated) time. The M(foo) means "The message# p2 [ G' z7 b
// called <foo>". You can send a message To a particular( i- f/ \# ~# q+ {6 x$ \# F
// object, or ForEach object in a collection.
" Z' h! b' u. l* i 7 ^* @; f x- P2 h$ h S- |& k! h
// Note we update the heatspace in two phases: first run; w4 l2 b, ?% A3 m7 v% M
// diffusion, then run "updateWorld" to actually enact the
: I# O2 s. N9 u$ P- T' B U0 A. @ // changes the heatbugs have made. The ordering here is
, m' d- w( _( G# g // significant!: E: M5 m& X& z1 r; {9 G P
, W2 h& C' ]0 v1 H, ~0 Q3 k // Note also, that with the additional
" H/ l7 o) _2 Q- }1 G+ @' w3 w C // `randomizeHeatbugUpdateOrder' Boolean flag we can
3 B5 c0 e& Q) \: u, f$ }( ~! }' @ // randomize the order in which the bugs actually run& W' |1 u1 A% f8 Y
// their step rule. This has the effect of removing any
1 S7 s5 t6 A+ h7 W( M3 t // systematic bias in the iteration throught the heatbug
9 @- j# f- M* [$ Z' ]+ K! h6 D6 n // list from timestep to timestep1 E5 n A* v4 y, F. A$ m" @% |
' x# Q/ i4 _' o
// By default, all `createActionForEach' modelActions have
& {" d O, t# ]! s // a default order of `Sequential', which means that the
' Z( g% O0 l* H: R; ~$ x // order of iteration through the `heatbugList' will be
& y) ~& j: z" U+ x& c$ \ // identical (assuming the list order is not changed
( K# N2 h' ~. q4 J' C // indirectly by some other process).
; a7 X$ {* H* o( r4 h
4 c( D; t7 K( L1 {* d* @6 V& D7 P modelActions = new ActionGroupImpl (getZone ());
* @: ]- U) n% j' b8 E3 j* M
' y# \5 n- ^/ W8 C, g/ m$ X& W try {" x1 p) Z8 L. v7 ~5 `& G" E: `' H; M
modelActions.createActionTo$message7 b/ O6 X: K+ \+ ~7 P) k5 T3 o
(heat, new Selector (heat.getClass (), "stepRule", false));
2 J0 L7 w+ r! \0 r) E6 |! g } catch (Exception e) {
) J; I. V* U( t0 |2 o System.err.println ("Exception stepRule: " + e.getMessage ());& u# x! e) c/ w+ W
}& A$ w! b: B) s
4 v' v$ e1 i9 ~- ~% J4 D1 d+ S try {
* k3 R, D1 m$ m8 l Heatbug proto = (Heatbug) heatbugList.get (0);
, }# j# ]4 V. I& w2 J Selector sel =
8 o% P2 V2 ~/ ^: w new Selector (proto.getClass (), "heatbugStep", false);
7 Q+ g- q9 p$ b" t) \5 W5 j' H actionForEach =! M$ p8 `9 n5 c7 H+ V
modelActions.createFActionForEachHomogeneous$call+ l+ {( M; |3 @! t4 T/ ~# K7 M6 i
(heatbugList,
h% i( \9 f7 x: k new FCallImpl (this, proto, sel,
* w0 J9 l" M* y0 F new FArgumentsImpl (this, sel)));
) ?- K' y6 ?8 B& D0 n5 g+ F } catch (Exception e) {' z. w" k) L" V7 E
e.printStackTrace (System.err);# b& e i$ S: |5 ^
}
* S6 F0 H" \* H$ [7 e8 K
9 t0 l9 D4 g7 B1 b+ T7 f syncUpdateOrder ();
8 E; D! p& w" O/ k1 n' N" F, u1 n
& E: F8 S$ T+ [/ m& B- q$ F try {
^4 v" Y7 H! t9 W, q: O4 b1 m modelActions.createActionTo$message
6 s1 u" v3 q; v4 U I% e (heat, new Selector (heat.getClass (), "updateLattice", false));* ]" t* P \/ o- A }6 f) C
} catch (Exception e) {
+ H, Q, y% Y* }4 V+ J! r' S7 @ System.err.println("Exception updateLattice: " + e.getMessage ());
+ I' U m( Z5 U# ?% u& r }. S9 i. k2 F7 ?" S6 W' K/ z1 M' ^9 B
0 q& U; Q2 U/ i! x
// Then we create a schedule that executes the1 D+ \! \) a+ n1 m
// modelActions. modelActions is an ActionGroup, by itself it
2 p5 T) ~. E0 i' h3 }5 |9 h$ U // has no notion of time. In order to have it executed in: s) q# u2 j" f9 a/ o0 i) R9 E
// time, we create a Schedule that says to use the# D9 a& v# Q9 N
// modelActions ActionGroup at particular times. This7 a: J" p6 d. n
// schedule has a repeat interval of 1, it will loop every
: V- `: c" B4 j* O7 `9 q // time step. The action is executed at time 0 relative to
; o/ X$ t' q( f1 |' }: o, z // the beginning of the loop.
4 {( n+ |/ `( Q, z7 ?8 E5 n4 V; O' q4 n: q
// This is a simple schedule, with only one action that is1 y. i+ g* z) X& |( P9 R
// just repeated every time. See jmousetrap for more! V2 }0 @! F5 B$ y
// complicated schedules.* Q( t/ ~- Z* F0 y" g: Q, K+ T& J
7 K3 z$ y: P8 {( p( P: b# l modelSchedule = new ScheduleImpl (getZone (), 1);
. {: k0 h! w- S. V, q modelSchedule.at$createAction (0, modelActions);6 G4 C# ?8 T/ Y" ?- w
. q% B1 v* C& j, R$ u return this;
" Z) [1 S6 e7 S% g* I0 w } |