HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" E2 s3 z9 G. c$ o( k" y( L1 x! H4 E! [& e% h4 s% {& Y
public Object buildActions () {9 V8 x, H& c. H- Q# V! b8 o. C
super.buildActions();
2 S V) q7 m1 l8 o, u& i6 V; i4 O
7 V, T2 O! l) R0 ^, a S // Create the list of simulation actions. We put these in
, C! l6 a7 p, z& m // an action group, because we want these actions to be
) |/ \: d( r. H7 N // executed in a specific order, but these steps should" u9 l/ a( {. |, g7 e
// take no (simulated) time. The M(foo) means "The message; l( Y1 D1 F: x) ]5 _
// called <foo>". You can send a message To a particular
4 k y: L( c8 N // object, or ForEach object in a collection.; R2 j( L: Z7 p$ e/ a, t
& _1 n0 ]6 c9 L1 v2 ~' m5 W; i- M // Note we update the heatspace in two phases: first run
- S7 ~ ^8 `! t // diffusion, then run "updateWorld" to actually enact the
/ ^- p1 G, @ h3 P4 u // changes the heatbugs have made. The ordering here is+ j4 Q! v _5 v7 M! S
// significant!
& V4 x* Y& t) w5 c$ @+ K 5 H" P# t' p! m8 }3 g
// Note also, that with the additional- f% U- @6 P5 l( }
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ R8 a4 S! u8 X# H) O
// randomize the order in which the bugs actually run
/ E3 m" H- M' W9 |! ` // their step rule. This has the effect of removing any. z) D1 i: w/ _2 R( E2 t, r Q
// systematic bias in the iteration throught the heatbug
* {2 v8 c1 e. c9 v2 E9 e( Z8 d& M // list from timestep to timestep
v( f8 f. k9 l! d. E* I7 j ! H( d* d( M8 U% L i3 o2 T
// By default, all `createActionForEach' modelActions have! ?( x% L1 d3 S. h
// a default order of `Sequential', which means that the
' R5 K' j2 F" r# O$ ^0 ~ // order of iteration through the `heatbugList' will be
. `# {# @" D4 m3 w // identical (assuming the list order is not changed
$ V' U$ i2 m, v/ @3 Z& u1 n: R // indirectly by some other process)./ a+ O, B0 {# \0 u& M: w
' w% F/ r+ ?* x, W
modelActions = new ActionGroupImpl (getZone ());4 B2 {4 }6 q4 S" Z/ H3 [
; z- [. W6 A' W9 L3 B: x try {% S- L! e# e- `! X! F! U7 g4 }( }& z
modelActions.createActionTo$message
6 G' g$ |$ l$ s& K (heat, new Selector (heat.getClass (), "stepRule", false));6 u) h0 \5 f# N# u2 [% w
} catch (Exception e) {
, O+ ?( _4 Z+ Q9 r2 k+ W) n System.err.println ("Exception stepRule: " + e.getMessage ());
Z4 [" h& u' b# c/ Y }& U( M! ^# [4 ]0 c& U9 T4 [) [
/ l: l) C4 N: g4 L
try {
3 z" n. G- J9 }- n9 |5 F0 G Heatbug proto = (Heatbug) heatbugList.get (0);6 R1 S9 D) E* Q* T' G
Selector sel =
8 H# Z7 R( E0 y1 ?9 J new Selector (proto.getClass (), "heatbugStep", false);
+ d! f0 c+ B E, [8 B3 Q. m9 C actionForEach =
, J9 {( K; ^ g5 I8 H; U modelActions.createFActionForEachHomogeneous$call! ~1 ]( [" V3 M: I2 F: ?' Q4 \$ x
(heatbugList,
. o" e8 O- i+ v. | new FCallImpl (this, proto, sel,7 l* l w9 }6 j- }7 P0 i z$ P; H
new FArgumentsImpl (this, sel)));# L0 ?" k! v7 ]# O
} catch (Exception e) {
& k: c3 f% ~: E; ? e.printStackTrace (System.err);
+ [1 J2 y& X! G. M7 W. R* h1 A }
; b- R9 Z, x$ @3 Y # w0 R! r5 B! ]* u C. r
syncUpdateOrder ();
% t" T [/ T/ w ]- g" `/ m! y* r
try {. v, S# F9 a& f+ \- b5 W
modelActions.createActionTo$message
5 R% i1 ~% _- O5 U6 ?( ^: } (heat, new Selector (heat.getClass (), "updateLattice", false));# d% T# ^1 F7 A1 @6 Z% ^8 n4 i
} catch (Exception e) {
$ k+ L4 z& O+ I5 S" `* R System.err.println("Exception updateLattice: " + e.getMessage ());
( A4 T0 U. ]7 q: F9 q }
& q6 v3 H6 Y+ z! K8 \. ? % d9 c! K* ^8 C- B4 g
// Then we create a schedule that executes the
6 g5 s, \ Q) @1 v0 ?5 k // modelActions. modelActions is an ActionGroup, by itself it# ], t% h: D: Z3 D6 U% i) e2 B
// has no notion of time. In order to have it executed in) f9 K- Y1 C0 b9 ]- d
// time, we create a Schedule that says to use the& |0 U2 n% L& ?7 d9 Y7 U( U5 `
// modelActions ActionGroup at particular times. This
( G6 o% R2 \5 H7 r! |8 Q1 `/ Y // schedule has a repeat interval of 1, it will loop every
) V% z9 Q. J- f" q // time step. The action is executed at time 0 relative to
* T ?* v+ _6 p5 t! l8 K // the beginning of the loop.
2 a% i _* L" J3 e# x1 I6 v
# K5 J! Z- c4 C& h6 H8 y( [ // This is a simple schedule, with only one action that is& _% k4 X1 a' j* X$ E z
// just repeated every time. See jmousetrap for more
+ Y# C9 K, W9 j8 n. r // complicated schedules.! |: [% K1 \' l9 U9 U7 p+ j& G/ X4 p
5 ? B( ]7 m* x9 W2 x" ]! U modelSchedule = new ScheduleImpl (getZone (), 1);6 c* w. T1 C( {
modelSchedule.at$createAction (0, modelActions);! J- K8 o( e) z( z3 u& K
) @1 q1 r4 @) E- n0 k return this;
6 ^: Z: n- I% P7 a7 s( t } |