HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ r5 n$ f! ~; U' x3 f4 }
( J2 i- _3 z9 ]: p6 g$ ~
public Object buildActions () {
$ V$ m3 B* L U$ X$ D7 C b. i super.buildActions();' E8 ?$ L5 \& c; ?* m4 d5 P$ S9 V
0 |+ X: x9 e7 F6 Q% H! `$ e
// Create the list of simulation actions. We put these in
% [, R& j1 G! H: Y; U/ e# S // an action group, because we want these actions to be/ ^ ^) I4 _7 y4 {
// executed in a specific order, but these steps should) Z7 J# c0 T( z" }& o
// take no (simulated) time. The M(foo) means "The message
, Q8 N: J& s8 W# x% Z- H8 K // called <foo>". You can send a message To a particular, M2 `. B+ h% S+ a% n% D
// object, or ForEach object in a collection.) c* G% F9 _! F
( c) c" G& e, E6 l, m: k# k" Z // Note we update the heatspace in two phases: first run
5 a5 D" O1 p3 Y. N. c+ G& B; ~: T // diffusion, then run "updateWorld" to actually enact the& m" s% n! r2 {
// changes the heatbugs have made. The ordering here is2 f- ?; S* C0 L _) d* B6 [
// significant!$ B# {" x6 Q: L# O
- R6 j3 A5 F: r* }$ ?0 ?% B6 n
// Note also, that with the additional
' K5 I2 k: I( R, G // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 ~3 C- A/ q$ S. h) y // randomize the order in which the bugs actually run, c- o/ o: d0 T% ^' R) `2 W# x. e% N
// their step rule. This has the effect of removing any0 ]! g s% s* X" _* }/ [. }& x' C
// systematic bias in the iteration throught the heatbug
' j& z. H$ E& o" [ // list from timestep to timestep
& e. F6 b( G2 m' V
6 n6 b6 X) X; T/ Z* n/ I // By default, all `createActionForEach' modelActions have
5 H& _0 r/ e: c: \1 H // a default order of `Sequential', which means that the
" I4 v& I! G+ D // order of iteration through the `heatbugList' will be
6 F. i" I7 i `" d4 c& _/ X9 S // identical (assuming the list order is not changed
0 J3 }7 S5 D" w // indirectly by some other process).0 ]+ g, I# g; e. n6 G! ^
0 E' E4 K7 F3 h" E modelActions = new ActionGroupImpl (getZone ());
) d$ `# a5 k; b2 j: o
7 U, U2 ?) c/ n- O2 [7 I$ t try {- R5 O9 y- \% M
modelActions.createActionTo$message3 N7 x/ n6 T$ c8 O: E! u
(heat, new Selector (heat.getClass (), "stepRule", false));
- B6 n% A1 `7 h) p } catch (Exception e) {
& b8 x) \- T( A. l* C, e4 w$ S5 q9 n System.err.println ("Exception stepRule: " + e.getMessage ());
9 X+ g- I- Q6 W U/ b }
+ O/ Y" T& V; v h/ o- Q
, V- }/ [% {; n& V& C try {
. u# l! T: ~$ E' @ Heatbug proto = (Heatbug) heatbugList.get (0);
3 ]- i! F4 g+ {6 v Selector sel =
2 N; f7 G" N- [! N; N new Selector (proto.getClass (), "heatbugStep", false);- }6 t4 V5 Y3 [0 w) Z* I
actionForEach =
4 m+ B8 ^: d# \3 x% H. B5 x modelActions.createFActionForEachHomogeneous$call) `, z1 N- `6 ]! w
(heatbugList,
4 T! I3 M+ ^6 a2 g5 y5 h new FCallImpl (this, proto, sel,- V+ C1 z! R g2 P3 t! W
new FArgumentsImpl (this, sel)));: H/ B# x# ]4 I: e m! K4 n% K" Y( ?* W
} catch (Exception e) {
# S& D" U* D& g6 ^& }% r e.printStackTrace (System.err);1 M: [! ~& c- V- X: A! M- H
}
! B% {9 [% e% @- u5 g
[" q% `2 C9 |% U5 \$ s syncUpdateOrder ();1 ~& c, O" `* `7 A; k. G' D/ ~
8 D) {1 c7 a5 y8 @ try {
$ b' m. n- I" l5 a modelActions.createActionTo$message
! l- w) ~7 x& H3 e8 M" c v/ M (heat, new Selector (heat.getClass (), "updateLattice", false));% I7 U q+ U. T& P9 C" O6 l8 Z
} catch (Exception e) {! g; l9 c4 z. E5 N5 }
System.err.println("Exception updateLattice: " + e.getMessage ());
+ K* Q' z* Y, ` }* m l- g. p1 P; O
+ R6 ~5 b# U: X // Then we create a schedule that executes the
/ C- [9 ?9 `& f, T // modelActions. modelActions is an ActionGroup, by itself it
# @) W6 g5 H$ I7 _. x // has no notion of time. In order to have it executed in
( j$ h D0 `! f+ w // time, we create a Schedule that says to use the! H) `# C+ q" t
// modelActions ActionGroup at particular times. This
7 ~1 B: D1 v) h+ q$ h // schedule has a repeat interval of 1, it will loop every& f0 A0 g7 F# k/ @3 i
// time step. The action is executed at time 0 relative to+ ] J5 K5 N! X3 `$ \
// the beginning of the loop.
4 r. G" T8 P+ Y- m0 p5 c+ o( q/ e
% ?. u1 K$ f1 k' i1 P n+ P. n: f: x; y // This is a simple schedule, with only one action that is
# a( A1 p1 f( E. t3 Z0 ^7 N f) n // just repeated every time. See jmousetrap for more& x& \" Q# m2 c- p C8 w' o" R
// complicated schedules.
4 A* p1 p' Q* T : |( h3 B' ?7 W
modelSchedule = new ScheduleImpl (getZone (), 1);
G1 a9 W; M+ X' D, p modelSchedule.at$createAction (0, modelActions);0 p3 W' r$ f7 n& K4 }( Z! e+ ^5 o
4 S- N9 ]3 y( j% O" G$ j
return this;
+ @% U2 W. a. c) s1 q0 T& A* f } |