HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: a% z* c' C7 N; b/ ^4 J* u4 D' @* E) g& d
public Object buildActions () {
) o7 R, J6 O) v$ u* F3 `* U. h9 B super.buildActions();
" e+ G" U- @! c6 t; P
4 s/ m8 X7 F% d& B- ]" K) z& V // Create the list of simulation actions. We put these in
8 h1 K* r' v5 V2 u0 r0 _+ T2 k7 C // an action group, because we want these actions to be
7 V6 [6 g! \' R // executed in a specific order, but these steps should( C8 R; q- J3 J( y
// take no (simulated) time. The M(foo) means "The message% F0 f" M: f' a8 d' s2 `
// called <foo>". You can send a message To a particular
' \* a) e2 A3 ` // object, or ForEach object in a collection.8 e; z1 V/ g. o+ L" i# W: }! K
; P: N- k3 m8 ?$ c1 E1 L0 n // Note we update the heatspace in two phases: first run a- l9 C+ L8 U
// diffusion, then run "updateWorld" to actually enact the
# n" F2 k6 J: ]9 P @/ J% s // changes the heatbugs have made. The ordering here is
" l3 @# ]$ l& @! h2 E- u // significant!
7 D9 T2 N3 r3 @0 M3 L
$ c& C6 Q- P8 G) X) N // Note also, that with the additional
* Z' R$ ~, s# i {9 m6 g! \2 d // `randomizeHeatbugUpdateOrder' Boolean flag we can
- z; W3 ?. T F // randomize the order in which the bugs actually run
) z2 S$ G: h: v7 J* T8 M& M // their step rule. This has the effect of removing any, ^; ?9 w8 H+ C4 X: `! R. o
// systematic bias in the iteration throught the heatbug8 \1 G( @5 f+ q7 g
// list from timestep to timestep
0 J/ _+ l! G- f& I' R9 y* h, G / X! P& |7 T- R7 d# R3 j
// By default, all `createActionForEach' modelActions have
" I X" n7 T" q0 Q5 ] // a default order of `Sequential', which means that the
4 y: e; |! D9 k3 u& b! A // order of iteration through the `heatbugList' will be
: Y) o5 Y1 O+ L, q6 L+ M // identical (assuming the list order is not changed
" i. t9 f% h/ M* d" V. [ // indirectly by some other process). }% M( F& ~- @: S! W r
+ l; K: @$ T+ ]% j) Z
modelActions = new ActionGroupImpl (getZone ());
1 r. m! P+ R( v7 h4 V
X: M3 I! h) M1 R& t try {. N7 q6 f* t7 H L
modelActions.createActionTo$message- D5 `* {3 X- u0 N( }( B
(heat, new Selector (heat.getClass (), "stepRule", false));
# _- _4 X* [& [' I7 n( } } catch (Exception e) {4 s( E& j' `7 X& @% d, P$ d6 M; h4 \
System.err.println ("Exception stepRule: " + e.getMessage ());
; f4 b( ~ V& J% U9 _ }
9 k* [' w X# X7 [7 G" g, t' ^! r2 T- J# Q3 e
try {
/ @6 b- S+ f1 _4 [1 G0 b& H Heatbug proto = (Heatbug) heatbugList.get (0);' m0 E4 J- u: g0 h: y
Selector sel = 5 i- }! t+ {2 l0 t
new Selector (proto.getClass (), "heatbugStep", false);
8 B0 }7 ]+ Q! W actionForEach =! g! M2 S6 d3 m
modelActions.createFActionForEachHomogeneous$call j5 s, P' g- J! ^- {, j
(heatbugList,
4 V' i, |$ T' p1 W; ~( O. K% _ new FCallImpl (this, proto, sel,: B; D- I( X! w4 M; J8 ~ b) a0 _
new FArgumentsImpl (this, sel)));) j+ W5 \9 x% i
} catch (Exception e) {
. M3 g* d' y4 L1 l e.printStackTrace (System.err);1 T5 K. C0 A% B8 Q9 @
}, L+ }5 A$ W( a5 y1 n8 N. N
# w4 A/ F) A$ J4 I* E) M5 I( \0 u$ o syncUpdateOrder ();+ U; r% q5 g! D* C5 [5 }; ~
. e% n3 {, g. Q4 T' Z- h3 u4 c: ]' i try {) c& w; p# c j
modelActions.createActionTo$message % b- o. Q' Q/ Q6 l
(heat, new Selector (heat.getClass (), "updateLattice", false));# G$ C* \8 d/ D2 z3 U; _4 `( Z3 R
} catch (Exception e) {8 Q5 F1 n0 @/ m. c5 k8 v
System.err.println("Exception updateLattice: " + e.getMessage ());. U* X: q9 q5 s; V* y$ k) t& Y
}
# d+ c3 o5 d7 k. N , d0 ^0 v1 w- w# C
// Then we create a schedule that executes the' ?+ A5 X- _ ?0 ~4 t! e
// modelActions. modelActions is an ActionGroup, by itself it' A! q- v! [4 n" ~+ z1 [3 p2 J: b, N
// has no notion of time. In order to have it executed in' A% g2 V6 U) p3 K; `) f
// time, we create a Schedule that says to use the
' I) o7 H8 Z- R `) U5 ^$ c // modelActions ActionGroup at particular times. This
7 N2 r( f7 {4 X- q: \) l7 w6 \) U // schedule has a repeat interval of 1, it will loop every
" z x" q. D. [. t6 ~ // time step. The action is executed at time 0 relative to. t- R! R; F% r5 P
// the beginning of the loop.9 M- n1 o3 h6 B* ^7 u& ?
# v. E6 J) D( f1 o- X- l9 H // This is a simple schedule, with only one action that is
' f7 n* T: ~/ D9 @ // just repeated every time. See jmousetrap for more& x8 t4 C2 d& i
// complicated schedules.
- s4 N0 N+ n+ P& o% j5 q
& E7 `. ~. \, ^ modelSchedule = new ScheduleImpl (getZone (), 1);
: K2 B% E' q- K( R modelSchedule.at$createAction (0, modelActions);
8 Q% ]" _- ^, O: Z7 H8 U
+ m- |$ E8 R, [" S4 j- X3 [5 C- [) F return this;
- m3 v# H2 J" G/ ~5 F: c } |