HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& E: @8 M8 G |3 o# P: H( H. Z" C$ x# U/ J* }5 M
public Object buildActions () {
; A1 m2 ~! R0 G \8 E' K" m- K super.buildActions();
( X6 ~8 r3 ]: i1 S9 e , ~+ _7 t! I1 U/ Q: s; h
// Create the list of simulation actions. We put these in
2 q$ W7 A% ^0 o/ E // an action group, because we want these actions to be
) A. p1 g W, i // executed in a specific order, but these steps should9 _, l; \ Z. p+ [" U
// take no (simulated) time. The M(foo) means "The message
. K" E* s1 M9 v* G7 F // called <foo>". You can send a message To a particular
7 i; v5 S6 a* _0 o/ J+ v; W, g // object, or ForEach object in a collection.
% N( ?0 R9 l6 h" Z
. z% a1 O6 e' W/ I5 q // Note we update the heatspace in two phases: first run
; x- _ R- U) B/ g8 r // diffusion, then run "updateWorld" to actually enact the/ y K o, g* G% Q/ q1 U; r0 [3 ]
// changes the heatbugs have made. The ordering here is
1 }5 h2 u* w% ^9 n( K // significant!
6 h# x% O' n/ F# c3 k/ G- \# }% C
8 F$ N8 h% _3 I" R // Note also, that with the additional
3 v+ b b' P" n" W# e I" | // `randomizeHeatbugUpdateOrder' Boolean flag we can/ a. q1 M h6 _* c; Y7 f
// randomize the order in which the bugs actually run+ T2 X) j0 e" z1 H' ?. O7 k/ A
// their step rule. This has the effect of removing any
" E! d7 H% N+ z& b // systematic bias in the iteration throught the heatbug
( _4 @+ n' R# }0 k# c // list from timestep to timestep5 w2 L- A. b2 V% }) \& Z9 p
8 @7 P2 ]0 ]% X: `* J( T // By default, all `createActionForEach' modelActions have5 H3 ]; o5 g5 H/ z0 s {
// a default order of `Sequential', which means that the
9 }8 ~4 V' u% E" N# i // order of iteration through the `heatbugList' will be
8 H: d. \$ H2 a+ W' B // identical (assuming the list order is not changed. k* l2 O+ {7 t1 m0 t8 y
// indirectly by some other process).
- ]" ] L& W6 Q / e1 }! {: x; G0 V0 S* [6 k
modelActions = new ActionGroupImpl (getZone ());6 B1 U$ U9 s$ W3 C0 M
|" u! t/ k6 h) N, L2 ^+ t try {
. s- k1 Z; d& F$ j modelActions.createActionTo$message6 u* h# S; [5 h% Q
(heat, new Selector (heat.getClass (), "stepRule", false));6 T# ]6 W6 A w8 H( Q: c# Y
} catch (Exception e) {
5 q' c) b: N4 d1 G9 r# k0 _8 o System.err.println ("Exception stepRule: " + e.getMessage ());
9 M' z. e' b$ G: i+ C- J }
: l: L$ A9 A. H* B2 f; O. Y/ O, R; T% s0 i) T
try {7 l6 `7 f$ q# W
Heatbug proto = (Heatbug) heatbugList.get (0);
8 l R! p7 E* e1 E+ o# I$ F6 } f2 f' E Selector sel = 5 J/ t* F% E+ t5 H9 g
new Selector (proto.getClass (), "heatbugStep", false);
( {: r' z$ T8 q actionForEach =
& o" h4 k& U" J modelActions.createFActionForEachHomogeneous$call0 g3 A7 K: j) n5 Y
(heatbugList,
! f+ b) _ Y/ |, B; m- ` new FCallImpl (this, proto, sel,; x% d* z" _ O, D+ J8 G
new FArgumentsImpl (this, sel)));1 T) h0 K7 s; \/ B9 X/ w9 V
} catch (Exception e) { q7 F h5 u* F
e.printStackTrace (System.err);
- L$ r8 \( ?; ]3 A( I+ i }( r# A. B. K3 F& {: g) A
, {% E: k: _* M/ n( r7 _ syncUpdateOrder ();5 Q! C2 F- k+ `3 C2 \
' a3 y( s& Z& `7 c: E try {
0 v& Q6 u7 [ K+ o8 q modelActions.createActionTo$message ! j1 L5 i& p$ v
(heat, new Selector (heat.getClass (), "updateLattice", false));
; V- x- p2 @: o0 O& ] } catch (Exception e) {2 @/ ~4 R( z. r) p+ l+ P* p. x
System.err.println("Exception updateLattice: " + e.getMessage ());
) W) h9 k, Q g2 Y1 Q }
+ I- a2 ^" L2 d: D b3 u$ I5 {9 C
! d+ ]& m2 u5 w1 M // Then we create a schedule that executes the
; u! A$ v' @3 z- a8 }: C" u u, k // modelActions. modelActions is an ActionGroup, by itself it1 f( X. W! e1 Y- F, W
// has no notion of time. In order to have it executed in
) N0 u. z3 l1 i, r# h // time, we create a Schedule that says to use the
; a4 }8 W& ~4 Z/ N2 I% C // modelActions ActionGroup at particular times. This' F6 f" `( a9 w* \ w
// schedule has a repeat interval of 1, it will loop every
' R2 e1 k1 Y4 ?/ I4 K2 s# }, m // time step. The action is executed at time 0 relative to% w$ s4 W# c* x' v# f* Q2 J3 K! m
// the beginning of the loop. C/ K6 S! e4 b! @; N- @9 l
( V) {2 F$ X! G // This is a simple schedule, with only one action that is* A" n# i; D$ i4 X0 s* z, w9 G
// just repeated every time. See jmousetrap for more# ]+ G- s9 B7 c
// complicated schedules.
! ~8 f7 C# E z5 y& d
- }; A% ~2 g4 a modelSchedule = new ScheduleImpl (getZone (), 1);& J, _) Q$ ~: D4 E1 ?. a8 q* ^
modelSchedule.at$createAction (0, modelActions);, f" N# G9 \2 o: x
2 s- _8 X' }3 y; j& |* h' D
return this;9 M# ^2 V* q1 M4 I7 {" P @" {
} |