HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 M6 f# S" K# }% Q4 |
6 E S* n( |' p' v" Q& l public Object buildActions () {' J- S( [: l+ u
super.buildActions();
' W7 F3 q* t; G3 \! S & q) E+ o6 v7 c9 o
// Create the list of simulation actions. We put these in& U' E, u6 X5 u0 c% h9 l6 l9 V
// an action group, because we want these actions to be
, H1 a8 }/ ^) e // executed in a specific order, but these steps should5 }2 I1 _! g; L# C
// take no (simulated) time. The M(foo) means "The message( J* c4 t$ B; I1 T# b& E
// called <foo>". You can send a message To a particular
& B" {2 c$ E" T- w // object, or ForEach object in a collection., z. e& N; o0 q& [) F. Q
/ C. v1 l7 @5 \3 W7 p: V6 |' i // Note we update the heatspace in two phases: first run
2 Y3 c, \7 \% q' D$ | // diffusion, then run "updateWorld" to actually enact the
' @3 S% S/ R" C! N# n6 D // changes the heatbugs have made. The ordering here is! |* u$ e0 N' O4 k. M
// significant!
' W6 |$ c' f6 X8 V) {: D' C: D0 F) [4 h \
; @7 ]; t0 a# P2 ~8 P8 z/ X // Note also, that with the additional& g5 w# F5 O0 V8 _0 M' E
// `randomizeHeatbugUpdateOrder' Boolean flag we can! [$ m3 H: }1 g y# l. j/ }
// randomize the order in which the bugs actually run1 h Q& g) q; Z
// their step rule. This has the effect of removing any7 p+ V2 k2 I3 J* ~8 C0 _
// systematic bias in the iteration throught the heatbug
3 i7 f/ p1 v5 \' ^' ^" b // list from timestep to timestep
/ J, V9 s% W7 w* u! ?
2 C$ r3 Q& I$ m // By default, all `createActionForEach' modelActions have
4 L/ x) F* t7 f- Q9 ` // a default order of `Sequential', which means that the
7 e& m7 U' x* A3 U7 b6 q // order of iteration through the `heatbugList' will be+ O! E/ A `8 z; T* a3 P
// identical (assuming the list order is not changed1 b, v* ~; {5 d1 P S& c9 q9 B8 U: a, C
// indirectly by some other process).: I8 ~7 g f. ~. C$ q
6 {3 ~, O' L; g modelActions = new ActionGroupImpl (getZone ());
! m8 p# C$ y& W/ g/ B. w# l( X v; c. l P: V! U: Y4 u) G0 a
try {% _0 Z9 E1 \' T. P
modelActions.createActionTo$message( [& B9 F' M) |5 _/ ?
(heat, new Selector (heat.getClass (), "stepRule", false));0 q* S( l) _/ p7 U# ~
} catch (Exception e) {, i- K. {# H$ g+ n- h- C& W
System.err.println ("Exception stepRule: " + e.getMessage ());
8 q- l' r" S) f! i, M }% z& V" ?9 k5 p* {' {- g" ~" M
" R+ L9 V1 _% y* b A
try {0 s( l/ ?3 S4 o8 X% X% t4 t
Heatbug proto = (Heatbug) heatbugList.get (0);
* x* C5 X* Y* o& q! O# }! _( S Selector sel = 0 P- y/ W$ G; A y; F- w
new Selector (proto.getClass (), "heatbugStep", false);
0 Y% ? Y% }/ k! t8 |2 V( d, _ actionForEach =
% ~/ s% S5 U! B modelActions.createFActionForEachHomogeneous$call
8 L8 }+ D. J; N D) w1 R1 x, D; Z (heatbugList,
. }2 Q2 N {) B, D0 ^ new FCallImpl (this, proto, sel,) N' W3 b: _+ m% ?0 z
new FArgumentsImpl (this, sel)));6 {* r: X0 s5 L
} catch (Exception e) {: ~0 \, o- \& R5 u
e.printStackTrace (System.err);0 f) s) w& p- {! r
}
9 U+ w: g/ y7 [1 i , w2 a; r2 ]7 S2 x A( {
syncUpdateOrder ();4 g& i9 h: q% J3 b* I
, ?" @" y' L0 y% l8 M, u$ E* ]
try {& @ w4 {- g+ ^& E; f
modelActions.createActionTo$message 5 q7 V# u4 k0 {9 i( A+ O
(heat, new Selector (heat.getClass (), "updateLattice", false));* i0 J( M( P% {* e3 v7 i& k
} catch (Exception e) {- F& f1 m, ?# k2 S: ~
System.err.println("Exception updateLattice: " + e.getMessage ());( e2 l1 f; f9 r3 u1 I5 s
}
# |7 [3 o$ X' n& ?( f, b# t * n* z( V. F+ f* {1 T8 g
// Then we create a schedule that executes the
: I5 U/ V& b# l3 ~ // modelActions. modelActions is an ActionGroup, by itself it
$ ^' E6 z$ i- e, F& |/ {; H3 ? // has no notion of time. In order to have it executed in
5 @/ q& ]- d" P1 H5 l( b. O0 f6 C // time, we create a Schedule that says to use the7 u( T) b4 b# f" O, h
// modelActions ActionGroup at particular times. This
& o2 P3 f- ^0 [7 b% r1 v. G$ k6 m7 s7 y // schedule has a repeat interval of 1, it will loop every
# V9 [+ J2 }: V: m // time step. The action is executed at time 0 relative to
; a; W2 u) }# l! C // the beginning of the loop.- M2 f5 |2 T3 w1 m, V
7 i' k# ?6 o! b
// This is a simple schedule, with only one action that is j* d, R0 M7 d# _6 }9 p
// just repeated every time. See jmousetrap for more4 o, ?1 b$ W6 J7 ?1 D2 w' \
// complicated schedules.0 j, Z" ?3 `; g8 l' v8 ^+ F
# a+ u' V" a* K4 { modelSchedule = new ScheduleImpl (getZone (), 1);5 t0 p+ Q. n f/ P1 j2 |/ f
modelSchedule.at$createAction (0, modelActions);
) }: Y; i! q Z0 G5 k * e0 n" Y) A p
return this;
1 R/ |4 S% z5 U" B, A- j, _: S } |