HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: g' p$ n7 r; g% E6 V( c1 u, G1 L! t- ]) Q
public Object buildActions () {" J* T, p- F% G6 i
super.buildActions();9 ^. s; J4 M+ W+ l3 I, H
# x" L$ P Y) v3 R+ @8 K // Create the list of simulation actions. We put these in5 c+ ?5 x! a) T4 {9 N8 f6 d
// an action group, because we want these actions to be
4 }( _6 R5 L3 T // executed in a specific order, but these steps should
; `" c, W, a+ q6 j; V; ?( n& T // take no (simulated) time. The M(foo) means "The message
, v: M1 v* p7 U3 o // called <foo>". You can send a message To a particular
2 N9 j9 e, M$ N! H2 f6 ? // object, or ForEach object in a collection.
. Y2 ^2 f& q4 i
4 k$ B2 z: \6 U: Y5 m2 W5 d // Note we update the heatspace in two phases: first run
. z8 n- w+ o: a& N' _6 j6 F" S // diffusion, then run "updateWorld" to actually enact the
9 z1 B$ s+ P, ^6 t2 k // changes the heatbugs have made. The ordering here is
' I" Y2 O6 m0 X0 @6 o // significant!
& @. Y3 F' |! c) R 4 [9 m' |9 N1 D Q) T) Z
// Note also, that with the additional
. G2 x2 O, x9 U6 n; { // `randomizeHeatbugUpdateOrder' Boolean flag we can0 z1 o# R9 O$ S- A
// randomize the order in which the bugs actually run
1 a' C l# p4 B6 N3 F$ e3 Q // their step rule. This has the effect of removing any
6 h2 P* j ` ^; @) r0 `3 N# }/ ] // systematic bias in the iteration throught the heatbug% B6 N* f& |6 ~! w+ V' u
// list from timestep to timestep
; I8 Y% Y) T2 `1 p
2 Y0 P# O& U2 O2 L" @ // By default, all `createActionForEach' modelActions have
4 e! |- Y" c! S. b7 B$ W // a default order of `Sequential', which means that the
. f* `/ B" h/ { // order of iteration through the `heatbugList' will be
# R* y' O0 I2 X // identical (assuming the list order is not changed0 G0 x3 S7 P: r
// indirectly by some other process).7 v; \* f7 L3 {9 q' ]
( ]4 X1 ?2 A; `" X! D* U modelActions = new ActionGroupImpl (getZone ());; t8 b" x: p% X1 b
B" r1 p5 U; x7 L
try {
5 L' p1 J' {- m0 R- a u( o1 f modelActions.createActionTo$message8 u# n' o( T3 A# k% h
(heat, new Selector (heat.getClass (), "stepRule", false));) l* L, j: s0 E; j
} catch (Exception e) {- T. O6 U( v. T& o7 X
System.err.println ("Exception stepRule: " + e.getMessage ());. i: b, R; V/ j. u7 Z4 F: r
}+ l v8 ^, S. `3 F0 e. t3 x* `
# g4 K% r. M" l( y* V; l try {0 Z- M: D; ^$ }6 c7 R
Heatbug proto = (Heatbug) heatbugList.get (0);/ C$ I" _/ `* S. t G& f7 o
Selector sel = 0 T' L4 B' i8 t" Y% s1 a
new Selector (proto.getClass (), "heatbugStep", false);
0 j4 b5 r5 m9 _; S* I+ {$ A1 \0 D, m actionForEach =* D9 H$ x) y5 j, k5 F
modelActions.createFActionForEachHomogeneous$call
) H% z" {0 M% N! e0 q- b (heatbugList,
( c% `, R- K& ]: u" ?. ~$ m! x new FCallImpl (this, proto, sel,
! A6 A4 R f- \" H9 ^ new FArgumentsImpl (this, sel)));
1 V5 S2 V, D0 _ r } catch (Exception e) { v8 [1 e, R% a9 W; S
e.printStackTrace (System.err);
9 h4 `! Q% P Z }
% K" F/ R# p0 c) C9 m
9 b3 V) w, J+ S5 l syncUpdateOrder ();" b) V" R Q# t0 Q& T( d
" m' z. d3 A2 G$ }2 U3 J try {
: \" _& K0 K: k) @# ?8 k9 a* x K modelActions.createActionTo$message % q0 S3 T. M+ Y; P) Y# g
(heat, new Selector (heat.getClass (), "updateLattice", false));4 S/ H1 `5 ?$ Q* S( @4 A/ \# i
} catch (Exception e) {( I& ]1 G. G: H3 G2 |
System.err.println("Exception updateLattice: " + e.getMessage ());
+ ]' i6 k/ R* m9 Z }. H, s4 y; w d0 c- |
7 e8 R8 y# A1 Q
// Then we create a schedule that executes the
' Q6 |% H* y5 @+ ^1 X0 |9 @& ^ // modelActions. modelActions is an ActionGroup, by itself it
6 Z* ?. S" C, W4 p9 q* b: n& | // has no notion of time. In order to have it executed in l, z/ y* j, y; O+ g$ [
// time, we create a Schedule that says to use the# H2 n/ H* R# Q6 o9 a- u
// modelActions ActionGroup at particular times. This$ ~/ Q% ]3 z# L5 M( N7 J) ?
// schedule has a repeat interval of 1, it will loop every" u0 R! [8 d Y7 g( c% }9 Y
// time step. The action is executed at time 0 relative to1 a q4 R' q. |; a* ]% [# ^$ x' {
// the beginning of the loop.+ X$ t1 H3 ]# N- `8 p
; E7 W3 H+ Z- V! U
// This is a simple schedule, with only one action that is$ }; m3 _- \6 I0 ?. ^! O9 B
// just repeated every time. See jmousetrap for more
3 k( h: @6 N9 i# i; M# M // complicated schedules.( ~" A; K$ Y# b) l
" `. w5 O( ~4 A+ S( _
modelSchedule = new ScheduleImpl (getZone (), 1);% G3 z6 l: {- o+ Q' j6 e( t4 U/ f& @
modelSchedule.at$createAction (0, modelActions);
7 }, o$ C$ g' C( O* a$ S# C, j
9 L, z) Z6 X& ?1 f& R return this;
# o* j! f, I: E/ Q } |