HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) f9 z) \8 _& ~( B- T
/ ?) b/ [+ i% J3 I public Object buildActions () {5 |$ Y4 }: }$ H/ O. Q5 L: e6 L
super.buildActions();
3 ?/ a0 r3 b* X3 ]$ T$ ~ ( A! w' H; d6 @4 [, Z0 T
// Create the list of simulation actions. We put these in. X( [" b0 _2 O6 h- f! d: p" c
// an action group, because we want these actions to be( ?; E$ A4 e1 p4 {* h
// executed in a specific order, but these steps should
- @. s' P# o" w/ Z4 U3 x // take no (simulated) time. The M(foo) means "The message
% s1 h$ Y9 ?% h // called <foo>". You can send a message To a particular9 Z$ n- g- M1 K5 c9 P
// object, or ForEach object in a collection.
% m3 u0 V) A7 l2 v- r ^ / k8 ^ y! N0 W, v+ ^$ y8 W
// Note we update the heatspace in two phases: first run
, c. I, D" a4 A // diffusion, then run "updateWorld" to actually enact the
2 E$ e% q) g7 g R // changes the heatbugs have made. The ordering here is& l% Q/ V0 O% f6 X% Y
// significant!
6 ?9 ? l4 {- L O. N6 ]: `
" G1 h% ?7 K6 K1 q3 o; n; O/ X; x // Note also, that with the additional% J5 M) n$ I' s6 M% e# ?* I
// `randomizeHeatbugUpdateOrder' Boolean flag we can
! r j3 |. _, i* e' W // randomize the order in which the bugs actually run: o) S0 v) g. _* Y$ D9 ]
// their step rule. This has the effect of removing any
/ v) a# ?, O* G/ ?- x1 ? // systematic bias in the iteration throught the heatbug3 k: Z9 G: C q
// list from timestep to timestep
8 H, H; X- _ u! l/ c" B1 \
7 K1 L7 T' m( y$ J G0 |% o: G // By default, all `createActionForEach' modelActions have
5 p5 \" Y+ o s; m3 Q9 ~+ n // a default order of `Sequential', which means that the$ ]1 S, q$ |) Y) m! N
// order of iteration through the `heatbugList' will be
- _* g2 ]" E; U1 Q* |& A" k( s // identical (assuming the list order is not changed7 ^% V! y( n4 y0 P- u- K" r, m# b9 B
// indirectly by some other process).
+ O+ \! b2 ~( O0 K* _% c4 S. |. e% }
6 f6 ?* F2 \/ g4 x modelActions = new ActionGroupImpl (getZone ()); v5 W9 l: _5 e3 ]) L
/ ~, O. D% ]: e
try {" h1 r! A3 A6 \7 B
modelActions.createActionTo$message
1 b: e& X$ E* Q i" O8 a# } (heat, new Selector (heat.getClass (), "stepRule", false));0 P! @* |: e) h; ?) y6 E5 u& f
} catch (Exception e) {
! f+ b: k! `1 E8 q w& t; s, N System.err.println ("Exception stepRule: " + e.getMessage ());
- r2 N2 t) S# B/ S, l }- a( p8 b* t$ m) i0 P' d" J" V
i4 P! z x2 ~2 N( L try {
0 L7 o/ L- Z2 I/ h Heatbug proto = (Heatbug) heatbugList.get (0);: C4 j5 j& {7 ^1 w. b
Selector sel = & k/ I% _+ |- M- D8 W
new Selector (proto.getClass (), "heatbugStep", false);
- a: Y D3 E4 f4 z/ @ actionForEach =7 {0 U3 i- O& U7 k0 l
modelActions.createFActionForEachHomogeneous$call
8 t& N( `. Q# r8 |5 x( Z" | (heatbugList, x" E( y$ l+ o5 F+ y
new FCallImpl (this, proto, sel,
+ Y% M3 n8 ^- d k! p new FArgumentsImpl (this, sel)));( \& g* t$ F6 `- L* W8 b) X- G7 ^
} catch (Exception e) {( B& U+ ^: S! o
e.printStackTrace (System.err);
; z& S2 o. y% c. O7 {9 [( I }- P( |$ ?% p" Y1 Q. A; r$ C8 y
+ A4 A- p# T" t* W7 p syncUpdateOrder ();
# I+ q" x5 G, c8 H
}" J9 V5 ?9 f2 y% H try {
4 \" f/ N: Q' ~ modelActions.createActionTo$message
$ T) X4 N% m$ f3 M, O$ N% _ (heat, new Selector (heat.getClass (), "updateLattice", false));
" t4 H6 l! z' I } catch (Exception e) {% V7 n$ e m) x/ ]/ p" G4 x( J, c3 f
System.err.println("Exception updateLattice: " + e.getMessage ());, F1 G) L H* l- t
}
* @( p1 K/ e: w+ X- p: f! a
# N K, k0 M2 _) j0 V" e6 \ // Then we create a schedule that executes the
0 z0 F) c( o! c4 I3 b; \0 Z // modelActions. modelActions is an ActionGroup, by itself it
: H' Y6 W8 [) B4 o4 m C8 ~ // has no notion of time. In order to have it executed in0 v+ ]2 c1 q' D8 [
// time, we create a Schedule that says to use the
' R& w8 J! m- U+ d // modelActions ActionGroup at particular times. This
8 n& G& _# g1 `. W5 A // schedule has a repeat interval of 1, it will loop every- J% O; n5 F$ b9 J
// time step. The action is executed at time 0 relative to
- f, e' l2 f; O J. | M // the beginning of the loop.
3 H5 H( [1 G! G; J6 n3 F# \9 ]6 `% D b+ E- a/ P
// This is a simple schedule, with only one action that is
) g5 r* r2 ~/ a) T8 L& ~% x) W // just repeated every time. See jmousetrap for more
4 ]% N( f: h: ~+ T" I8 w // complicated schedules.( a8 p" a; q: V4 e* r
7 _. \& o) d7 t& b/ z8 ?. s, Q
modelSchedule = new ScheduleImpl (getZone (), 1);
! F8 [ V; ~% M+ D4 H+ y1 a modelSchedule.at$createAction (0, modelActions);
: |7 M" C- c/ G
( x1 i" I' M( k5 k' w return this;
: E- g t s& S } |