HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 ~6 F: t* L( w1 {6 o, Y7 h7 V
* b! x/ n n& o3 @/ _
public Object buildActions () {! q& ]; G) z; z8 r! w8 o, Q6 G
super.buildActions();0 r" ^, O/ t/ z! m' J5 d
* l, `4 M/ L9 ?& s6 e
// Create the list of simulation actions. We put these in z/ b9 \8 ^, R( [% U
// an action group, because we want these actions to be
7 ~. P8 l! G3 |# n0 _4 m' R+ x // executed in a specific order, but these steps should
7 O) l/ W% ~8 l1 M // take no (simulated) time. The M(foo) means "The message
! x9 _$ K6 \9 k* i0 c, h // called <foo>". You can send a message To a particular v- s" y" Q h9 n7 R
// object, or ForEach object in a collection.3 k# F, o* L& n7 y$ O) }
5 L8 P9 q0 t# {! B6 O \ // Note we update the heatspace in two phases: first run
3 n' S8 V5 J( W+ {6 |- f // diffusion, then run "updateWorld" to actually enact the- F/ S8 R9 \, X4 y
// changes the heatbugs have made. The ordering here is/ N; y! d! g' y L6 `
// significant!* b' M* u _7 J. D2 \
+ g; I: s) q+ m; L. D // Note also, that with the additional; c3 `! {, k! o& |0 H6 t0 h) G; G1 `
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. j3 R* I4 u! J7 D/ n# z/ a8 {9 Z // randomize the order in which the bugs actually run% h6 V+ A: l# F/ V- W p' K v5 D
// their step rule. This has the effect of removing any
; O# v) d5 h6 L9 H+ x9 ^ // systematic bias in the iteration throught the heatbug# b8 V Z e9 x! X
// list from timestep to timestep
" @/ B9 n* S& {/ m' I6 u
1 p1 A$ j$ }' `% X6 w // By default, all `createActionForEach' modelActions have+ H. z- R6 V3 ~2 }" k
// a default order of `Sequential', which means that the
3 v% R* o- o0 t9 | // order of iteration through the `heatbugList' will be0 O' q( I; T+ U8 w
// identical (assuming the list order is not changed
4 l7 z- h& q7 @" c // indirectly by some other process).. W. ]4 A9 n2 t$ q% l( K* w
. l3 v2 D& n: `7 W0 H+ z! E
modelActions = new ActionGroupImpl (getZone ());( ^: k" m. x/ o' W: [% M
; `0 F u1 S3 Y; _
try {
0 _# X8 I+ \& b* I; g, N! G modelActions.createActionTo$message
% z: s, w# w) Y# g# Y8 e (heat, new Selector (heat.getClass (), "stepRule", false));( X+ g8 M* M( r4 @. d. O' ]5 @
} catch (Exception e) {6 w, o& m8 w" G9 _5 t# O2 N
System.err.println ("Exception stepRule: " + e.getMessage ());
: n# B6 a+ Q! B2 @3 h n }
. P2 r! W1 ^2 p: Z, Z
/ s* N e" N9 q: P2 S- A try {
0 i8 j- n2 j$ U7 T: _+ Z3 { Heatbug proto = (Heatbug) heatbugList.get (0);
: E9 V2 t' D( p. T3 m; X Selector sel = 3 O' u' g! r5 K- Y
new Selector (proto.getClass (), "heatbugStep", false);
- U+ H% W& \3 V( _ actionForEach =
& n! w% s. i% x* o0 g9 S: F modelActions.createFActionForEachHomogeneous$call
" g. `/ A8 }/ R; C" m% ]' l (heatbugList,
) y7 f9 L6 f- y6 ~ new FCallImpl (this, proto, sel,
/ ^! t0 F5 e$ } new FArgumentsImpl (this, sel)));/ _6 N8 G! p, o S: Y
} catch (Exception e) {& j2 S4 S/ U( x% ?
e.printStackTrace (System.err);6 Y- I M. W" Q O0 k
}$ @) Z7 o; t5 j/ U3 J/ d
" Y0 C* O+ Z7 |4 @ syncUpdateOrder ();
! b1 c2 b! d2 M5 [: T$ K) i( t8 W3 b
try {
; {: N; d/ }; }) i* U6 `4 t modelActions.createActionTo$message
6 T) w& w$ ]0 _ {0 X, t (heat, new Selector (heat.getClass (), "updateLattice", false));" Z$ M+ ~) O8 F6 A5 w. ]# D
} catch (Exception e) {
S. |/ ?+ n! U: ~4 d. J$ k System.err.println("Exception updateLattice: " + e.getMessage ());
. q. B) l3 I& r! O9 |' S. m }& \& }. l% N6 ]
6 z- o0 }5 |6 n4 w
// Then we create a schedule that executes the$ V @ _% u' W, B
// modelActions. modelActions is an ActionGroup, by itself it; B6 A5 {$ z- Q9 A
// has no notion of time. In order to have it executed in
" P- t! r0 X5 r/ E // time, we create a Schedule that says to use the4 O9 q/ @- E; G5 }1 x
// modelActions ActionGroup at particular times. This
0 y) X; h4 j3 H6 W6 e0 U, N // schedule has a repeat interval of 1, it will loop every
! Q8 m8 t, ?3 c ^$ y9 k3 B* u3 O0 Q // time step. The action is executed at time 0 relative to: s4 f6 N# ], [
// the beginning of the loop.6 J, R$ x7 Z4 J
4 E3 u6 X8 f0 w' u R // This is a simple schedule, with only one action that is& _5 _* e9 c# ~& u! p9 @3 Y8 i8 Q2 k% X
// just repeated every time. See jmousetrap for more
, K* z' I/ l$ ^) k5 l // complicated schedules.( Y% M, F' s+ O4 m" ?2 D' W6 t! P
( o& k) k. b! [" K
modelSchedule = new ScheduleImpl (getZone (), 1);/ b' t7 i0 l3 p
modelSchedule.at$createAction (0, modelActions);
5 } t2 x. O; W+ ~ & K$ G0 P: O2 s& ~( z# D6 k8 K% l
return this;
u5 k4 O6 C$ R; h" X; Q } |