HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# u! W" D, W4 h1 S% c: Z/ d3 Q% p( O
) Q& w4 \7 G% j+ `, t5 t1 H$ d- [$ n. }
public Object buildActions () {+ z' H) _$ X7 @8 z9 R* x/ h
super.buildActions();
/ |) w' q: j" S3 M5 r3 @& d
; m0 O- \+ ? a8 \9 r // Create the list of simulation actions. We put these in
! p4 c& x. C2 F- [$ @4 ?9 t // an action group, because we want these actions to be; q$ r8 f' L+ h5 ^6 c7 n0 P9 ~- X" g
// executed in a specific order, but these steps should- O: ]4 S; }7 M2 H. e- s/ D3 t o
// take no (simulated) time. The M(foo) means "The message! I! {( A. z! x9 T3 s) X! F
// called <foo>". You can send a message To a particular
) z8 n- m5 f. i' v9 p" h // object, or ForEach object in a collection.! ^. t0 E) {. b l2 [: S9 {8 r3 W
9 l/ u& j- u5 p& ^ // Note we update the heatspace in two phases: first run5 f$ o& R: U# _6 m( V% ^9 X3 Q2 t' F& Z1 R
// diffusion, then run "updateWorld" to actually enact the
: f, g* a, ^" r5 @& K W // changes the heatbugs have made. The ordering here is
- c6 ~3 s" c( @3 j+ K // significant!
9 ^$ V) e8 A& @/ { E 0 T- ^+ X# e1 X. j
// Note also, that with the additional4 r- M- l7 A8 k4 U$ p) v- o
// `randomizeHeatbugUpdateOrder' Boolean flag we can
Q4 V |3 ?$ F* _ // randomize the order in which the bugs actually run
5 V9 V& ?7 T% j: I- Q7 q // their step rule. This has the effect of removing any9 ]" I" i6 F d3 N/ I% i/ a3 s+ |
// systematic bias in the iteration throught the heatbug
7 a: H9 H9 L6 ~1 B# Z) e // list from timestep to timestep
7 A& A! i0 ^2 `5 s4 \) Q
* ]2 f8 h1 Z7 f/ P* |! g% L& e- C // By default, all `createActionForEach' modelActions have
}! u- R6 M% B4 Q0 i1 N // a default order of `Sequential', which means that the
$ u7 G+ X8 K0 ` H8 v // order of iteration through the `heatbugList' will be9 J. U e) }& _1 @+ z* d, X$ I
// identical (assuming the list order is not changed
5 O# a4 J4 A; s. \) J2 e) G // indirectly by some other process).
9 a8 }- m5 ?) N! w ) W4 l. z' _2 s0 e) m5 t
modelActions = new ActionGroupImpl (getZone ());
( [; z4 ]5 |, \# r, q( [# a7 ^: `1 \7 r
try {
! Q- U6 }0 J% K" L! K6 ]2 n2 n modelActions.createActionTo$message7 T* d8 H* J4 I0 R) M
(heat, new Selector (heat.getClass (), "stepRule", false));
) O* n6 ^! x, [- Y4 `. g } catch (Exception e) {
4 D' ^4 n4 K1 X7 k' }+ T System.err.println ("Exception stepRule: " + e.getMessage ());; C. G. f: U3 }- q# s
}1 K# x8 r$ d- o. H9 P, G1 p
9 A' h' J; K4 ?" s try {
% b" G0 d- |9 y2 F$ r7 V# ~1 H Heatbug proto = (Heatbug) heatbugList.get (0);/ ?; B4 y" F. o: H
Selector sel =
% [- a1 |# h8 i" O4 f& ~ new Selector (proto.getClass (), "heatbugStep", false);
% D1 ^5 n6 M' M6 H5 y, @ actionForEach =* f, r, @" y# o8 w" q; l9 e; t
modelActions.createFActionForEachHomogeneous$call
) Z9 ]: \1 B# N2 N5 m4 @7 _! y (heatbugList,0 f. I9 m& Q+ b
new FCallImpl (this, proto, sel,1 o+ N o3 I. K
new FArgumentsImpl (this, sel)));
0 U+ `) ?$ D$ ~7 o1 _) C } catch (Exception e) {, k3 B8 j L5 G3 I
e.printStackTrace (System.err);3 C4 L! @% x' O: Z5 l4 K
}
& M% A9 b- ~ E' f1 g 3 l# l6 O" _$ e/ F( q
syncUpdateOrder ();
+ a9 K$ }$ F: `
# l8 g9 ?8 C$ u& F" ^( M try {9 Z& y- W5 w; P" a* K
modelActions.createActionTo$message 3 N* q! b0 N* D/ k4 c/ k/ j
(heat, new Selector (heat.getClass (), "updateLattice", false));
1 y7 f5 n$ f' E8 ]9 Y2 e } catch (Exception e) {( V% F, a+ a! ~
System.err.println("Exception updateLattice: " + e.getMessage ());* I3 K* ]2 H5 Q& K3 w0 Z
}4 u+ A! J0 I: G
% L. Y5 v# A8 g+ ]4 A6 R2 a& i
// Then we create a schedule that executes the5 b" [7 [7 M+ J9 i# R! p0 b% y
// modelActions. modelActions is an ActionGroup, by itself it A1 j: Z4 M* b+ S, q2 q
// has no notion of time. In order to have it executed in
) n( H p$ \& C // time, we create a Schedule that says to use the
& y) R0 N% w1 O n6 `0 Y // modelActions ActionGroup at particular times. This) R7 n0 ~" w$ J/ x
// schedule has a repeat interval of 1, it will loop every
- B% ~2 ~) S' x* E' p // time step. The action is executed at time 0 relative to
9 A7 B0 V, v2 E8 r ]2 c% y // the beginning of the loop.
3 g1 |) `/ Z# ?
9 S! c; U& q( M) ~6 ^3 E // This is a simple schedule, with only one action that is
. j) }- N9 W# O5 ? q2 i) E4 P // just repeated every time. See jmousetrap for more
+ m' t! v. f3 C* B- \ // complicated schedules.
* d0 F0 m/ A7 a- N4 ^6 o9 w
) F5 R, b! K, n% s modelSchedule = new ScheduleImpl (getZone (), 1);! |) u/ m* v; R
modelSchedule.at$createAction (0, modelActions);
1 S4 |3 c/ K8 E8 N( Q
3 t1 u& q2 u, P+ Z2 |! y' I return this;8 a$ t; U: M0 u* [
} |