HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" w# p( ~% K0 E! c1 P2 p2 p+ `; `. i7 n- @4 z. R/ H7 k
public Object buildActions () {" K o8 i% x8 P7 h1 @6 ~' |9 m
super.buildActions();
6 v! u& x/ Y$ o* P, Y
6 _" L$ P) m0 k @) q( @! D // Create the list of simulation actions. We put these in
3 I* L8 o, E% W5 Y3 ?7 C. Z // an action group, because we want these actions to be% b. Q" }. I0 q/ e
// executed in a specific order, but these steps should
/ `+ Y0 K- ?9 q; S // take no (simulated) time. The M(foo) means "The message$ s. ?7 T( @+ [+ e5 o
// called <foo>". You can send a message To a particular
6 M( W) v3 F! c: G% @+ Z7 I F // object, or ForEach object in a collection./ Q" y w2 [0 V- z9 O
" s( E3 }9 \ w, G/ b( D2 L5 H // Note we update the heatspace in two phases: first run6 u) ?5 i, ]. M9 Y# W: O- C
// diffusion, then run "updateWorld" to actually enact the
, c8 t( [5 ?7 A, F // changes the heatbugs have made. The ordering here is
6 L- g; s6 Z. K+ s( [3 d: ` // significant!
9 y, i5 N- I. |8 N$ J k6 F1 K! o5 v+ a2 n
// Note also, that with the additional
. C! }' W" S1 @" }+ ?' x2 p& F5 y // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ v* n( p; Y& q' F0 }9 Y, k: k // randomize the order in which the bugs actually run
# X& W' u z$ V9 t. c6 T0 V // their step rule. This has the effect of removing any
M: ?7 l. |! R/ h! ? // systematic bias in the iteration throught the heatbug7 W, [, X+ Y p* l# \: t
// list from timestep to timestep
2 h: A, ?5 e9 G' }
3 o. S; T2 B U) R& P+ M // By default, all `createActionForEach' modelActions have
+ ~; I7 e+ k v- L* K // a default order of `Sequential', which means that the: E/ b5 R& @) ^! a3 u8 V
// order of iteration through the `heatbugList' will be
R" B, e: a; _# l+ b+ d // identical (assuming the list order is not changed
# _, k" ~5 v5 f // indirectly by some other process).
& K' t# t2 P2 O+ E4 ?5 s # [- i) q1 X6 @1 R( X# k& T
modelActions = new ActionGroupImpl (getZone ());
A8 r* R* T3 o
# m! L2 T& R( G* r4 |1 v try {2 p- |8 {% Y& s
modelActions.createActionTo$message. t+ S0 a1 Z1 I- X7 S/ V
(heat, new Selector (heat.getClass (), "stepRule", false));
' N; _/ ^6 @* O! b7 V( B } catch (Exception e) {
: y2 p/ c$ E2 Z1 _ System.err.println ("Exception stepRule: " + e.getMessage ());" O- T5 s: s1 F7 `- E j
}8 I7 E. H, v+ T& m
9 t% v3 ]; F$ A) s4 q# C9 a try {% S. z! R$ _+ b
Heatbug proto = (Heatbug) heatbugList.get (0);4 u! k( X ?/ S* k( G" Z! U
Selector sel = " E5 R9 j& I+ z6 P. b" Z
new Selector (proto.getClass (), "heatbugStep", false);$ N0 g4 N# N! ^* U9 O% R! x
actionForEach =
, f0 p1 |( r% \; ]- Q modelActions.createFActionForEachHomogeneous$call: ?5 \8 I* `3 Y5 ?- a8 ?" ]
(heatbugList,
1 @+ D5 U! h- v$ N new FCallImpl (this, proto, sel,0 i8 u% L, B- A. Z* |
new FArgumentsImpl (this, sel)));
+ a- w k1 P) q" V* w% E } catch (Exception e) {
" \, V u2 E* _ e.printStackTrace (System.err);, M' A% T$ B4 K
}$ d3 m' C# P8 {0 {$ e5 s
# v; G6 {5 ^/ v; ~ syncUpdateOrder ();( g% Z# J* B+ v; M6 e4 @
2 p8 k' z: z; j4 w G try {
! i4 Z- z3 ^2 t& z; U' g- T! W modelActions.createActionTo$message
$ u! @" g; ]. M/ W, n" z (heat, new Selector (heat.getClass (), "updateLattice", false));8 H; {' P" O- x8 J" g
} catch (Exception e) {4 i; H0 q5 `/ t7 ?6 |
System.err.println("Exception updateLattice: " + e.getMessage ());
; n' x1 w% _1 {& @2 T; | }5 y6 I0 a y% V# u0 w+ }
; @5 I. T1 D3 L$ ^) t: [ // Then we create a schedule that executes the8 v, K* n: ^; O8 P8 Q
// modelActions. modelActions is an ActionGroup, by itself it& I! u2 v9 @ @$ B9 P" o
// has no notion of time. In order to have it executed in
4 f$ u3 Y% q$ ]6 B$ y // time, we create a Schedule that says to use the2 i9 |4 g, X2 S9 r
// modelActions ActionGroup at particular times. This
0 Q9 h7 q8 X+ p0 u // schedule has a repeat interval of 1, it will loop every8 \6 k$ D% X7 V
// time step. The action is executed at time 0 relative to' }( i3 K# n! M# ~8 e, v' o2 ~; q1 m
// the beginning of the loop.; n: j& l5 n& }
" ^' Q$ p" S7 |. I+ _( @ m
// This is a simple schedule, with only one action that is
; w3 S8 z; ^9 ~3 f' B/ D, x) u* m" w // just repeated every time. See jmousetrap for more
7 B8 s( U B1 ~$ ? // complicated schedules.
$ L% l1 |4 c6 s+ @$ |4 ]
0 o- I+ J; I- Z: I( Y% a2 d( X% p modelSchedule = new ScheduleImpl (getZone (), 1);( @! [: q8 A' H8 A1 V/ e
modelSchedule.at$createAction (0, modelActions);1 t0 M% B5 T5 M) x! @1 Q" d8 _" d
4 K6 O$ s$ P3 |. t6 `3 f return this;1 s8 _: j- P& E6 [7 T, w
} |