HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& w8 X: u; H3 A/ J
1 k$ c$ `% B& c8 v* \' Q0 C public Object buildActions () {
; M# N: B0 D5 a" |7 ]; g super.buildActions();; n' x* D F+ W3 h* U" Y- p% A
/ q0 [# i4 E: W }! e% ]5 l
// Create the list of simulation actions. We put these in
0 ?7 _! |7 j8 ~/ [' r* Z4 q& Y // an action group, because we want these actions to be
6 Q! ~* |& |, L# M4 f0 n% H // executed in a specific order, but these steps should7 b1 p( ^/ G( ?" }2 N
// take no (simulated) time. The M(foo) means "The message
! s) ?: n- b' I6 y+ Q: | // called <foo>". You can send a message To a particular/ f6 Z% s% P( x, \
// object, or ForEach object in a collection.2 O$ m" _& ^( U: l+ t! l: U/ i! N
. M- G2 W$ Q) ^& D3 ~7 j+ E$ l
// Note we update the heatspace in two phases: first run: ]" C6 N' [$ _$ _
// diffusion, then run "updateWorld" to actually enact the2 O; { t0 U0 \$ b
// changes the heatbugs have made. The ordering here is- k& q! y" S8 ^/ s# l& S
// significant!0 z5 J& M/ c9 }
: L! ]3 p# C% k6 K, a2 I# A; \% P& b
// Note also, that with the additional
! g) l4 I2 r" A/ b // `randomizeHeatbugUpdateOrder' Boolean flag we can6 i' K. g% o8 R( r# m# ]
// randomize the order in which the bugs actually run; Q* r- C" D1 a. F) j
// their step rule. This has the effect of removing any
9 e9 B; m$ B" P# O3 }" o // systematic bias in the iteration throught the heatbug
- N/ P/ d! k: a // list from timestep to timestep
6 R5 m7 e& t o8 M " ~$ z6 Y5 V+ P& n2 A3 U. ]
// By default, all `createActionForEach' modelActions have2 ~7 F' ~1 V' f9 S: |
// a default order of `Sequential', which means that the
6 m" B# s; q; c // order of iteration through the `heatbugList' will be
7 i7 r' N4 t3 {& a l // identical (assuming the list order is not changed% M# {$ d+ Q- H: \& [ j% G- ]' E
// indirectly by some other process).3 M" U6 z: L+ H5 a) ~/ f
$ o0 z z, Z/ b0 ~% S9 X modelActions = new ActionGroupImpl (getZone ());- G, ?: h" V6 M5 [' m9 j/ S, p
2 `. ^7 b$ E8 w, k% N, `% k
try {
" E; B$ R5 R: d6 F modelActions.createActionTo$message
, W: _* f; U7 \# M2 t (heat, new Selector (heat.getClass (), "stepRule", false));$ a. G! z5 F% [7 d$ C, e1 i- H# O4 d
} catch (Exception e) {- U) @$ }2 `: e. E% r! A8 b
System.err.println ("Exception stepRule: " + e.getMessage ());
+ S, d' s* a8 H) M0 _# y/ U5 \- Y }
6 i+ S9 X3 C. C U1 S% A0 t: ?
* |+ _& x: C ?. c$ C# h5 V( C! w try {
; B. G* _& T) v+ A2 z Heatbug proto = (Heatbug) heatbugList.get (0);
/ B" |! R; O9 f3 u8 C( M' I Selector sel =
5 Y+ f* w! J6 y4 _+ X. C new Selector (proto.getClass (), "heatbugStep", false);
; w$ D; I+ k8 d0 w* C3 a5 j) y actionForEach =
3 i A4 z. I0 A" p1 a modelActions.createFActionForEachHomogeneous$call
1 T* q- z* L/ O. J (heatbugList,' m( t) h, b% A2 N* S# V. a- e
new FCallImpl (this, proto, sel,
% C: u9 L, k: B new FArgumentsImpl (this, sel)));* H" C; M- U- G
} catch (Exception e) {* a+ y0 Z! ]$ o( }+ s
e.printStackTrace (System.err);- y1 F+ k9 h! A
}/ R, o( B! X! U! q
% a' S) K4 i ^: g) R i: [0 P
syncUpdateOrder ();+ v: d9 N4 o. Y7 E* K) e7 l* R; A
+ O& D$ B$ ~& @. H# j
try {
! `8 j% t' @" X* D- r/ o modelActions.createActionTo$message 7 l' L7 I, ]) c C( v/ m' h/ @
(heat, new Selector (heat.getClass (), "updateLattice", false));; q% ^. R4 [0 r% X# k$ q: R5 G% O
} catch (Exception e) {
9 _9 ?! |* Y' Z* L* u) H System.err.println("Exception updateLattice: " + e.getMessage ()); a+ J1 ~# ^- s. |! q8 Y
}1 w5 o( a3 d6 L8 u
# p. Y; t+ j8 }6 ^' E8 |5 O8 j
// Then we create a schedule that executes the
7 Q; y. v3 l& ^; \ // modelActions. modelActions is an ActionGroup, by itself it
" F% {1 Q" y) M // has no notion of time. In order to have it executed in3 J1 w S8 N2 @
// time, we create a Schedule that says to use the
3 j# g8 G2 i; T. F$ G! { // modelActions ActionGroup at particular times. This
( o$ p! C( Y: o% ~% J! E! A/ k i+ \ // schedule has a repeat interval of 1, it will loop every, H% ?, m! a6 e( \/ t5 x0 q9 F- Y) X
// time step. The action is executed at time 0 relative to
- q9 Y( f5 E) ?0 C& s8 s // the beginning of the loop.
, S$ f& |, V( |3 m, a% a
; Z& K3 W4 A: y L9 f6 r2 e2 i! q8 w1 h // This is a simple schedule, with only one action that is
+ T; v/ T: `& B5 y8 @ // just repeated every time. See jmousetrap for more* S, m. ?7 O# |& C' b) d4 V1 ^
// complicated schedules.
; f9 z4 L+ @0 [$ V; R A 1 J8 U3 W; ]; ?, ~ f$ w) p
modelSchedule = new ScheduleImpl (getZone (), 1);
9 Q0 K. S' b' O; ~- b& F modelSchedule.at$createAction (0, modelActions);
1 |$ Q9 ?( V8 q! ] 3 Y3 R# `9 w( |# ]; l( D; U
return this;. a' _2 I' Z' Y- j5 _5 l- V, A
} |