HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 Q9 j3 W1 B. \, J( d1 M
5 B1 t/ G& \$ n k public Object buildActions () {
0 r$ ^8 `( \8 S1 D0 N4 ^, @ super.buildActions();% g# h5 q8 `6 a
# _& x7 }- h5 P2 q4 H# o& \ // Create the list of simulation actions. We put these in
# y. Z! q& y E# U+ i- m // an action group, because we want these actions to be
- s8 w) n U4 n, E5 W. ]3 e // executed in a specific order, but these steps should6 l) P% ~$ T$ Z4 O! z
// take no (simulated) time. The M(foo) means "The message( R, U- M$ ?* o
// called <foo>". You can send a message To a particular( G+ N$ \$ d% H
// object, or ForEach object in a collection.& f0 @& h/ S/ G% @
( O3 g& X$ L' b l# `! q // Note we update the heatspace in two phases: first run9 U; F0 ^+ i5 [. G# H
// diffusion, then run "updateWorld" to actually enact the
, O4 V+ A6 C L( W' \ // changes the heatbugs have made. The ordering here is
c' G* g- H7 B // significant!
' a/ e$ m9 Q( y) Y. o 7 A& j, ?3 f7 c1 m6 u8 i/ K2 q
// Note also, that with the additional/ G+ l3 Z8 l. ~: i5 ?" y5 p
// `randomizeHeatbugUpdateOrder' Boolean flag we can
% n" \7 q+ F+ |; K2 u- J // randomize the order in which the bugs actually run
( }& q* Z' j' \9 D# v0 P+ u // their step rule. This has the effect of removing any. m# N$ `6 P d- q
// systematic bias in the iteration throught the heatbug4 Q- A0 o1 V) R
// list from timestep to timestep
1 }, |3 x, S9 K4 h/ R$ d. o
! z' u# N0 j4 j3 k4 V. E) z // By default, all `createActionForEach' modelActions have
0 r- \/ R z, x3 T+ r' Z+ ` // a default order of `Sequential', which means that the3 I3 N. @7 {; H- _! S" A* W% K
// order of iteration through the `heatbugList' will be+ X, S8 C: _! d; J+ Y
// identical (assuming the list order is not changed% ?( U0 ^1 E" \' h1 s/ E; g
// indirectly by some other process).. _( I' v% t, |* L+ W# H. i
( b/ y( ~( O, [; Y& N0 h3 ^& n
modelActions = new ActionGroupImpl (getZone ());( {& R% b5 y; z
) J) y+ _, @6 l try {1 I- G' ]$ {1 u' g% C" L9 A
modelActions.createActionTo$message+ H9 w8 I$ i; K3 q. s
(heat, new Selector (heat.getClass (), "stepRule", false));. v( U# Y2 y* G, S, c, D
} catch (Exception e) {6 ~: n: Q( v* W% A7 z: O7 C! j
System.err.println ("Exception stepRule: " + e.getMessage ());8 M' J0 X/ }( ^8 e6 |
}
& ~, _4 R; n R: T9 w# W
/ m6 E5 P4 c8 M. D v; t7 y try {
! D8 e. N y- H$ c- H Heatbug proto = (Heatbug) heatbugList.get (0);* W6 a$ d5 R: y
Selector sel = 1 @2 \2 j2 g4 h
new Selector (proto.getClass (), "heatbugStep", false);' b2 e1 B, I/ m3 d; s, o
actionForEach =( ?0 B0 b( i4 e' w& {* e2 Q1 a2 M2 h: q
modelActions.createFActionForEachHomogeneous$call- w$ M2 |8 J @% b
(heatbugList,
* S5 G! {1 S- S/ b% E* j) d new FCallImpl (this, proto, sel,
' d' I: ^ U! G: o* ~3 }* A new FArgumentsImpl (this, sel)));2 f& }0 J- {6 u; {" D b! s# F
} catch (Exception e) {5 _- e: c, a( {" d# J
e.printStackTrace (System.err);+ N4 w& A! Z+ i0 V& {9 b
}- ]9 J0 C+ c0 V2 s" D/ {
: [& q6 r3 R/ H: X, A, K/ e
syncUpdateOrder ();
, D; @: n# a# C% O" q* q7 V6 s& P7 z" [
try {
$ }4 K3 P) U0 E) W modelActions.createActionTo$message
! k2 W5 q4 z0 a' L$ @) s1 U (heat, new Selector (heat.getClass (), "updateLattice", false));' m( E7 f# H9 A |: I: \( u4 t- a
} catch (Exception e) {
" j2 F O: ~! w1 ] System.err.println("Exception updateLattice: " + e.getMessage ());; X7 O- e! G# f- Q
} g$ ^8 O; p8 W5 d
& K2 T8 P8 T. k: W
// Then we create a schedule that executes the/ F' g# W# o* J4 S1 L
// modelActions. modelActions is an ActionGroup, by itself it4 m1 D( A0 [! c* z- w
// has no notion of time. In order to have it executed in
0 p5 o. z' g6 C$ P% x // time, we create a Schedule that says to use the+ t, L3 h" T: X
// modelActions ActionGroup at particular times. This
5 l9 N/ H# ^/ V/ _2 P // schedule has a repeat interval of 1, it will loop every% J6 B) ?" D; D, B, w: T8 l6 ~* q
// time step. The action is executed at time 0 relative to) c, V# z' j; |$ Y5 U0 K# \
// the beginning of the loop." J9 F- c/ f9 K$ ?1 K: o( Y m
P- k8 w, q: L) f: _$ j // This is a simple schedule, with only one action that is
5 ]4 A# e0 i9 }( i6 I8 ? // just repeated every time. See jmousetrap for more4 }- J- o$ o3 M. F
// complicated schedules.% b p/ n( q- a$ E8 `/ I6 R
8 M S% n. J6 V
modelSchedule = new ScheduleImpl (getZone (), 1);
3 }% [( x5 F, T1 x# A% H+ j modelSchedule.at$createAction (0, modelActions);. E* S4 O; l3 s
7 @6 h9 v% j; t- s: z6 t2 Y
return this;
, h' t& ^- Y% y6 h8 p9 L4 p; D } |