HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) u1 k# B' Y7 k& S
( r, T9 P( M! s
public Object buildActions () {# V r6 H+ F# g Q" Z
super.buildActions();5 G; D8 Z+ h6 v! y' I
/ `1 M% p+ w/ [) m" V+ T
// Create the list of simulation actions. We put these in
0 F, K" s- S/ r* U1 a5 P // an action group, because we want these actions to be5 E- V. D) J* W( e% L$ V" h
// executed in a specific order, but these steps should; X) T; X0 ?* f4 y" s" Z$ t
// take no (simulated) time. The M(foo) means "The message3 t: o8 U* l! P$ [
// called <foo>". You can send a message To a particular4 ]( K7 m+ z# a' s; I& g5 a
// object, or ForEach object in a collection.
" V5 G- B+ n$ m: m* X3 r 3 a3 b4 v: e! ^) F1 {; R! ] w
// Note we update the heatspace in two phases: first run
+ \8 v7 x$ z ?1 h9 [8 M5 l // diffusion, then run "updateWorld" to actually enact the
' ~ o0 K( X7 H // changes the heatbugs have made. The ordering here is
; N, x0 J6 z$ Y+ [1 x* K // significant!
/ K* G5 R& z6 R" e) `' T" R( z, ]
/ P' A3 l3 |" q7 n // Note also, that with the additional
' A4 E$ [ d7 h) f: k1 a# | // `randomizeHeatbugUpdateOrder' Boolean flag we can1 i* H2 p5 W: {8 j' H6 V' m
// randomize the order in which the bugs actually run8 l f% x) u$ L9 U# Z
// their step rule. This has the effect of removing any0 i3 k" c3 ?! h. G. }" k
// systematic bias in the iteration throught the heatbug$ k' Y: I; w6 P" B
// list from timestep to timestep2 g/ j4 t4 d( y: w z# R
. [! O, _. K. ~0 l: P& o: q
// By default, all `createActionForEach' modelActions have
2 l0 u( G& K" k m) v" U // a default order of `Sequential', which means that the
/ z$ w# b5 _) P$ b* ` // order of iteration through the `heatbugList' will be/ `" `: q3 n% k' }
// identical (assuming the list order is not changed" A' M4 f2 ^8 ?4 j
// indirectly by some other process).
) R. s6 X& d& ]* \: i . y, `! i, e0 ~! s2 L
modelActions = new ActionGroupImpl (getZone ());
5 K6 o1 p2 o, k1 F. k9 N' T, x5 R) J4 U! D2 v
try {9 h( w: Q7 j {, n
modelActions.createActionTo$message
6 Y# @5 l4 q# g" _ (heat, new Selector (heat.getClass (), "stepRule", false));" Q/ D2 x( [8 w4 ~+ }8 k3 n6 J8 V
} catch (Exception e) {
1 I/ o: [8 T, N1 E* } System.err.println ("Exception stepRule: " + e.getMessage ());: j& }! ^# d$ R0 Y+ s: `
}
; ]% B! [% s/ b
) z) h( e: f" M$ _ try { y* B: Z- I4 y- I" e# H, ?
Heatbug proto = (Heatbug) heatbugList.get (0);. J; K( G! b& h# a. e: j
Selector sel =
/ W, B! `: Y9 O4 T new Selector (proto.getClass (), "heatbugStep", false);1 \& K2 ]: J, X; t Y8 ]
actionForEach =
& ]$ v, H$ o0 F: _/ `* ~ modelActions.createFActionForEachHomogeneous$call! I$ F B) r) r
(heatbugList,
+ ^: d# q: ]* n5 g$ `- b0 ?$ Y new FCallImpl (this, proto, sel,. y4 M! P9 B- q
new FArgumentsImpl (this, sel)));3 Q* L7 Q3 b, O( g, x( _
} catch (Exception e) {
4 x! t. V/ m7 _- _# v e.printStackTrace (System.err);9 e7 i! C' Y O) `% P* R3 R( O1 x
}
3 `5 J+ |# a7 X4 S ' ^" T' j- K! Y6 _; y6 X
syncUpdateOrder ();
, T0 j9 ]9 V2 c. T' h2 C) q7 H1 w% J! Z- E2 {: K6 i& \2 H4 o: S; {6 {
try {- w' w" p @" n. t- N0 \' i
modelActions.createActionTo$message : K5 P" |& h0 K: Q$ ^0 r
(heat, new Selector (heat.getClass (), "updateLattice", false));* M, r2 w/ `% W# m% b# |
} catch (Exception e) {- F" D y) Q6 q/ w" C
System.err.println("Exception updateLattice: " + e.getMessage ());
# s9 E# W1 q& [9 u" [' E3 f2 L }
3 N) x' X7 q+ M, b; k3 c
) x0 _' M( ^* R5 E1 F! ~6 @, Z/ @ // Then we create a schedule that executes the
1 ?6 x. g, o1 ^7 J* @5 e // modelActions. modelActions is an ActionGroup, by itself it' w1 m5 M1 B- M! o! _5 e9 j; [
// has no notion of time. In order to have it executed in
) X1 H& F. x, n" V# k // time, we create a Schedule that says to use the
! V# S+ y6 ]* H3 ]5 u o! { // modelActions ActionGroup at particular times. This7 l: |8 r0 L _) J
// schedule has a repeat interval of 1, it will loop every7 ^* j2 y8 K$ s3 e, e% z& r
// time step. The action is executed at time 0 relative to
( C. T9 S6 A% d2 ~9 l // the beginning of the loop.
i5 H* S3 n9 K; Y& q" e$ k: m% ~+ V6 E& w# W. ^5 T% k( x& n# x
// This is a simple schedule, with only one action that is
5 M' p+ i, K7 X9 F // just repeated every time. See jmousetrap for more
- n# e h2 i/ O/ E* x3 B // complicated schedules.
) ]2 h% d- [! l- i6 f l, x, i( {5 v# @. Z
modelSchedule = new ScheduleImpl (getZone (), 1);! N! P g) t9 ]
modelSchedule.at$createAction (0, modelActions);
; Y/ b% [8 x B) A* r 9 _1 U5 j. S/ v5 p+ p Q0 j' a9 T
return this;
8 o6 ?" F- ?% {- e- K7 W+ l } |