HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" X* V( ], e3 y$ t# S7 q' \7 m+ d$ [3 B. \( f4 l( ^
public Object buildActions () {
8 w, N! K. l( \) s super.buildActions();0 Q# Y* C9 _% r( E
3 [( `% Z8 L2 L9 @5 o2 G3 O
// Create the list of simulation actions. We put these in0 \3 R9 q4 y2 V' J4 i
// an action group, because we want these actions to be
/ Y2 D! |; d$ d6 h3 P$ U1 E7 z8 q // executed in a specific order, but these steps should
3 B- o5 l9 ]8 V) o; \! ^) M+ h // take no (simulated) time. The M(foo) means "The message
1 I3 E1 {# d/ O5 D, T( Q$ k7 p // called <foo>". You can send a message To a particular4 P0 b( ] b8 P9 v/ m+ \
// object, or ForEach object in a collection., o) A2 _- d' `4 I% _4 s% y
* m. i3 S: e+ }# Q0 D- ~) K" q // Note we update the heatspace in two phases: first run1 P: j$ c5 D! L0 m; w
// diffusion, then run "updateWorld" to actually enact the
3 m" S+ }# P) U8 S q8 [1 K, E( ], C# R // changes the heatbugs have made. The ordering here is
% F% Q- W! z+ T, c) H9 Z5 ^/ A // significant!9 Z8 N) E% E9 }! b. Y6 [; V
1 g/ m0 m9 f; t // Note also, that with the additional: {1 U; z' [ J" V
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 V( O: O- w& d2 q; I // randomize the order in which the bugs actually run# J( t) v* A4 ]. ~3 G2 }9 A& w8 T
// their step rule. This has the effect of removing any* p! `; n' k& O
// systematic bias in the iteration throught the heatbug
$ W0 g' i4 z5 R% ^ // list from timestep to timestep
1 M; u4 j0 N3 m 8 l/ g: \+ t2 Y [
// By default, all `createActionForEach' modelActions have
) A) c9 k+ J! H- V // a default order of `Sequential', which means that the+ m9 P! x( C: k1 q) p
// order of iteration through the `heatbugList' will be ^7 j$ j' H0 e, H+ s
// identical (assuming the list order is not changed
7 S5 Q" [- \* R' g6 [" D // indirectly by some other process).
6 H, H; R K4 l% R
, r! Q3 p( B/ V modelActions = new ActionGroupImpl (getZone ());
/ u' S, M. n* O+ F
: O" l* r* b5 M1 h try {5 g3 D" i4 `- Z' @& k0 Y
modelActions.createActionTo$message
r0 l# l& K. N! z5 m (heat, new Selector (heat.getClass (), "stepRule", false));6 w8 s% W# E% o) j6 f
} catch (Exception e) {, o' T. a3 o) E) o* M0 H
System.err.println ("Exception stepRule: " + e.getMessage ());
2 p, [+ w) }) D* H0 q }2 s0 E) i) W0 ]" r# E% g' ? e8 I
2 e3 H5 S4 w0 A: v
try {
- I8 t8 i4 @' F+ `* m; Z Heatbug proto = (Heatbug) heatbugList.get (0);
" S |; L, a: ~$ }' H Selector sel = ! ~0 K5 I) ?) q- D) X
new Selector (proto.getClass (), "heatbugStep", false);
5 T- Z' D/ K" ^, I' b actionForEach =
|$ B- j+ t- d' T4 t7 u modelActions.createFActionForEachHomogeneous$call
' ~ C2 Q' ?; p% @7 c3 u% h4 X" e2 ] (heatbugList,
( U' o. M4 I& x; K' g! V: s N- W new FCallImpl (this, proto, sel,( ?8 ?6 c5 I4 U% v4 m! h# r6 s
new FArgumentsImpl (this, sel)));
" X) G% b$ d1 z# I } catch (Exception e) {
+ {2 C* D( g: @; T* l e.printStackTrace (System.err);
0 R* k+ n/ q& X( V! g }% ]9 c- ~* [+ ]3 K+ A/ L0 J
9 V( X3 _7 \ n1 F* [, ^8 i
syncUpdateOrder ();
$ @: Z- D4 t, b$ i) z. b7 \3 a' y7 b w
try {
' w( q* ^# e& u modelActions.createActionTo$message
; R, c: \( X) E( m* m (heat, new Selector (heat.getClass (), "updateLattice", false));
8 l; @; W. i, G6 f3 w } catch (Exception e) {" N3 k* B/ N& B9 Q8 k9 v
System.err.println("Exception updateLattice: " + e.getMessage ());
y' \+ g3 U: d }/ o1 q6 }1 K0 R' I+ D! n
3 G+ H1 Q/ t7 ]. u // Then we create a schedule that executes the5 K/ c1 n0 d: o6 F6 b5 r
// modelActions. modelActions is an ActionGroup, by itself it/ L$ C! N1 t- i" g% Z
// has no notion of time. In order to have it executed in
6 C# s2 a% M$ D1 c6 h3 h3 S7 |2 r // time, we create a Schedule that says to use the$ m; O4 U1 {6 z
// modelActions ActionGroup at particular times. This
$ t' k' W, v) \1 o) | // schedule has a repeat interval of 1, it will loop every
4 s5 z8 Q1 K% {( ?! o$ o8 ]# H // time step. The action is executed at time 0 relative to7 h- \4 f- N5 o
// the beginning of the loop.
7 f+ J* b0 P, o% f( }6 z, a! o' `. q% \+ G: r6 T! F7 Q0 n5 I% X6 Z
// This is a simple schedule, with only one action that is
1 |8 e/ |5 h* f // just repeated every time. See jmousetrap for more0 C8 R4 _; M5 ^9 h+ \" Q
// complicated schedules.' u# P1 A; J* q3 n* W" ^
6 h( m- T5 g* V9 F' B2 Y d U; T( Z modelSchedule = new ScheduleImpl (getZone (), 1);- S8 F, j' W l' U3 U# t* l/ L
modelSchedule.at$createAction (0, modelActions);7 H& g( `, {; [! m8 Z4 S
5 \% }2 A' _, A& \ return this;2 U- K' d. y- `5 `. R8 j1 C. L6 t
} |