HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; p! S. M9 P' y" }
4 ^" A% Z) a' [. W ^ public Object buildActions () {1 b; G) s8 q% t
super.buildActions();5 m$ C6 m8 R( j# O
7 i$ y# A! b( x9 e
// Create the list of simulation actions. We put these in% m: E6 q, P A F
// an action group, because we want these actions to be; t1 p! ?. p; `+ { Y1 a! I- u2 p4 Z
// executed in a specific order, but these steps should* y% A$ {, R3 A" L2 }" t
// take no (simulated) time. The M(foo) means "The message& i+ O; f; q$ D7 w0 B
// called <foo>". You can send a message To a particular: A8 L0 r1 Z$ k: H
// object, or ForEach object in a collection.. h; y& f8 z. f+ P9 h$ M
: P& Q. l# T1 C, x/ V4 ]" n
// Note we update the heatspace in two phases: first run
& N( E+ v5 y7 m- l // diffusion, then run "updateWorld" to actually enact the. t; M1 O( Y, @ ^
// changes the heatbugs have made. The ordering here is
7 _& D5 x/ K: [# o4 X1 } // significant! N8 T/ i" ~8 Y7 N; d7 c3 {% }% r
5 q) V5 x# b& E
// Note also, that with the additional
/ L v" y: `) s2 O2 { // `randomizeHeatbugUpdateOrder' Boolean flag we can
' J3 u4 Y8 E3 a) p3 X- ] // randomize the order in which the bugs actually run6 T; \. h, y4 [1 E$ y: ^
// their step rule. This has the effect of removing any) Y- A9 ]$ Q; o5 }
// systematic bias in the iteration throught the heatbug
' |- p3 k, T. {7 {- M7 q // list from timestep to timestep- @; W7 |0 P" j& t+ K( u
7 @) V/ w# M; W, ]" T& {+ X, p
// By default, all `createActionForEach' modelActions have# m0 |0 z% Y4 s$ |
// a default order of `Sequential', which means that the
/ r( ^/ a2 q# v8 s; I // order of iteration through the `heatbugList' will be+ N6 U: Y' H+ J T1 a$ \9 D* P# R
// identical (assuming the list order is not changed
* y4 Y. o+ O( [ s" J" D // indirectly by some other process).
/ A/ U2 l- v5 r8 F( A# x9 N8 o8 d6 N / b# e! n! r6 W: h. x( R" v
modelActions = new ActionGroupImpl (getZone ());
$ O: `5 J- U) V/ ^
5 D5 g# h- I: }) T$ W: _. E try {( }7 B9 S1 z y3 o4 k
modelActions.createActionTo$message
& i! q6 ]0 W8 i4 [5 U0 j (heat, new Selector (heat.getClass (), "stepRule", false));& b; ]6 \4 r: y0 o/ ~% o: p
} catch (Exception e) {
* w P0 E/ h4 M" W8 N, | System.err.println ("Exception stepRule: " + e.getMessage ());& V u8 w% C9 ?6 ^% e1 U2 C
}
8 _5 p& K# q* A2 {
; e J( } U2 W try {
4 H9 D N7 |. R: l( j Heatbug proto = (Heatbug) heatbugList.get (0); P. i/ t5 ^ Z6 G( @. ~3 C
Selector sel = 4 t4 Z/ p& G& w9 v4 N
new Selector (proto.getClass (), "heatbugStep", false);
6 u7 P) p; k4 a' R) n/ z actionForEach =0 F* m4 a! Y1 o, |1 }2 d
modelActions.createFActionForEachHomogeneous$call
* ], |: R) G* @( ^/ O" E( d (heatbugList,
5 M0 [1 T- ]$ z A$ p( m3 X- B new FCallImpl (this, proto, sel,
# a2 y3 w$ m& ]9 g4 Z new FArgumentsImpl (this, sel)));
; Q2 D: [, Z* J- ]5 m } catch (Exception e) {' r$ G0 y/ X" j9 t" j7 y0 _
e.printStackTrace (System.err);' Q% n, p( m1 i8 f G
}( ?7 h9 _4 s/ r' a8 H I; G& _+ M
8 ?8 t1 n- w4 i. B$ A syncUpdateOrder ();
5 i5 T3 p0 g) ~1 r% j
4 i8 b1 b/ z0 S2 K9 N try {! m! A; a% B5 ^9 @; B
modelActions.createActionTo$message % L+ k. d3 U1 ~; y
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ R. ]6 \ H/ A* m* A. s% M! t P } catch (Exception e) {1 s+ D; \8 r( z/ {& q
System.err.println("Exception updateLattice: " + e.getMessage ());6 g% Y5 b# N/ |7 \' b6 m
}
u+ R$ a: {% |( n" p- t& _/ K
+ K& b3 p2 \" L; g3 I // Then we create a schedule that executes the8 C( U, S# w2 u ?4 A9 W
// modelActions. modelActions is an ActionGroup, by itself it
$ t4 Q$ ^* S5 `1 l, U8 C/ Y // has no notion of time. In order to have it executed in# Z" S& l0 v4 a' e! i( N! t5 | t
// time, we create a Schedule that says to use the
: `0 Z" H0 q0 J( V, s4 w. M // modelActions ActionGroup at particular times. This6 t4 l& [1 E ?/ ~4 ~- |
// schedule has a repeat interval of 1, it will loop every1 I* s! {7 y/ B+ m7 `7 x
// time step. The action is executed at time 0 relative to2 J% }) L: J- J- t! {
// the beginning of the loop.6 l5 X, C8 N$ A
. U8 C2 D( W& j
// This is a simple schedule, with only one action that is
1 T4 w# R: _0 W7 @ // just repeated every time. See jmousetrap for more- [8 i6 V, B2 G/ D; k
// complicated schedules.
$ H; K5 }9 l) A9 T0 f % M3 a' u4 n; M0 [% U
modelSchedule = new ScheduleImpl (getZone (), 1);
$ f0 r1 A' G O5 m modelSchedule.at$createAction (0, modelActions);
& Q n: g0 `, f! ]; N" r$ p
) y, g% C0 t9 a5 `8 Q* a return this;
% L# J5 ~. \. ?3 O8 W. F } |