HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" c; i8 [* t1 I5 {' ~$ l+ o: P4 P
# w- }: y3 h9 c0 ?2 a* q
public Object buildActions () {" r& L8 g1 }+ K$ J6 B
super.buildActions();* a5 K- Z9 P! `4 p. n% R
& N M! a3 C9 l4 L# t `
// Create the list of simulation actions. We put these in
: @/ N$ F6 h# q' D6 E2 k [ // an action group, because we want these actions to be6 w3 d4 R6 @: M1 R; \( h! v- V
// executed in a specific order, but these steps should5 n; S7 B9 p4 z$ }" j. {$ m8 m$ `# D
// take no (simulated) time. The M(foo) means "The message
0 {8 `# A2 N' {6 w // called <foo>". You can send a message To a particular
3 Y6 G/ Q7 D& n: @. C; C2 b // object, or ForEach object in a collection.
$ e! A# d# I5 Q$ T/ A$ y
- U0 ]! u. F: H9 _ // Note we update the heatspace in two phases: first run
6 u9 d2 n. G- e5 X // diffusion, then run "updateWorld" to actually enact the. }9 `3 ]: g9 B8 H, f' z; j% O
// changes the heatbugs have made. The ordering here is2 c6 f! c: Z1 b3 P n r* B& T; j& o
// significant! O. Y' M1 A7 k2 f' c
0 x7 @. Y" k# I9 B+ o // Note also, that with the additional, N* d& _1 J" |) _
// `randomizeHeatbugUpdateOrder' Boolean flag we can
8 q" h/ [5 a. N y // randomize the order in which the bugs actually run8 q/ Y5 a, K7 q1 j' u v% B/ B
// their step rule. This has the effect of removing any
8 c+ e1 A( @1 o8 U- s8 d( |9 \/ G // systematic bias in the iteration throught the heatbug
N9 V2 H# }3 Q0 o( Q, e6 Y1 x // list from timestep to timestep% K, T1 p0 A5 |9 d$ I$ D
% n8 `# L; S8 \6 U; s% K$ _ // By default, all `createActionForEach' modelActions have
% p. d6 q3 a* @/ J9 n, q5 }+ g) q% ? // a default order of `Sequential', which means that the6 r+ ]# h3 v4 D: Z3 U2 A
// order of iteration through the `heatbugList' will be* t( d% L! r: T
// identical (assuming the list order is not changed' q" E& _! M5 q$ K: r4 S* C
// indirectly by some other process).2 I2 p: v/ v2 V5 H
! I$ U* j' n6 V+ u3 a2 X
modelActions = new ActionGroupImpl (getZone ());
4 k' V2 f! U! Y7 p$ Y- ^- n n. } {! _* }# L
try {
( I% D8 I' p4 h( y modelActions.createActionTo$message% I3 s; o. }- M9 g2 P' S
(heat, new Selector (heat.getClass (), "stepRule", false));
: ]- d% b6 Q5 ?* ]9 n' z8 _ } catch (Exception e) {
" M( d+ V, Y8 T( L: R3 n8 j System.err.println ("Exception stepRule: " + e.getMessage ());
+ { J8 P4 d" U; F }. h0 Y/ k+ v) @ d( Q3 F7 m: p
& X" x, o# j( c6 s- s/ J5 c- A try {
# g* w- k/ ]* G* M5 a: X4 f Heatbug proto = (Heatbug) heatbugList.get (0);
" ] o' I- w, W% _% C/ H Selector sel = 6 W, J& w+ J7 d$ s9 P
new Selector (proto.getClass (), "heatbugStep", false);. d7 C3 O/ q, L
actionForEach =
! Z! S2 O9 ?" h& q, N& J# \. v modelActions.createFActionForEachHomogeneous$call: X+ k- ~# r& J$ i# h( J
(heatbugList,6 ^; Y& b5 n; ^/ P7 V
new FCallImpl (this, proto, sel," p) g. e; G! \8 C
new FArgumentsImpl (this, sel)));9 [* i) q: u4 q/ e$ I7 @4 M8 e
} catch (Exception e) {
4 o2 w. r5 M' s% u) _ e.printStackTrace (System.err);+ V! B+ i6 M% L% v. x
}+ \: ]/ K$ z, o( r( b2 i+ d+ |
/ H2 M& Y" ~, ?4 ~2 `" ~6 c
syncUpdateOrder ();# [9 e" Q# n& Q" k
0 W7 s* H$ S5 I try {
. d, H+ B4 V2 ^; z# [7 s modelActions.createActionTo$message
2 y9 _& q+ J w8 ]; F/ ~0 K (heat, new Selector (heat.getClass (), "updateLattice", false));
* m: d4 G/ ^5 B3 Z, C } catch (Exception e) {1 k& e% t1 h. x' d# G
System.err.println("Exception updateLattice: " + e.getMessage ());0 b" Z1 x7 ?5 W, j. {
}
4 f& T8 ^$ b9 z [! g" W3 [9 O, F' S
) r; C+ d0 t Z- ] // Then we create a schedule that executes the- w f$ j+ N8 }. o& R% J
// modelActions. modelActions is an ActionGroup, by itself it
. n/ q/ `' X. Y8 | // has no notion of time. In order to have it executed in
2 `& F+ H" y; B6 c! b" t9 w // time, we create a Schedule that says to use the
- v8 T! H" i: K5 f! w% o // modelActions ActionGroup at particular times. This3 f8 z, G- @9 U
// schedule has a repeat interval of 1, it will loop every
2 Z- ~1 d5 l2 H, d) n // time step. The action is executed at time 0 relative to4 k5 L. s2 I+ y' l* k% E
// the beginning of the loop.2 b# g4 c; R' k9 P' H
7 U0 p1 F3 T, K
// This is a simple schedule, with only one action that is" x9 {5 r. W% T X6 d! p
// just repeated every time. See jmousetrap for more
: X x( d& S4 t% D# t: } // complicated schedules.1 @, ^0 C$ P: @; k( A
$ l( l9 h( _. j7 V7 p
modelSchedule = new ScheduleImpl (getZone (), 1);+ U2 ]7 m! E+ M# c6 R/ g" k
modelSchedule.at$createAction (0, modelActions);! }' {9 {. i& }4 z
! z) ]+ ^* F" F& k return this;
) C" l. s. n5 c! t6 ]7 q } |