HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 C! d5 F3 I, S. f" M% `7 G
9 Z* {0 n& ` _4 l# d' c0 E
public Object buildActions () {/ I5 ]/ t3 N1 \9 y* s, ^
super.buildActions();! y# w" t5 a2 |" w) V* u) |
1 p$ v- a: ]! m' Z( R
// Create the list of simulation actions. We put these in- F+ g8 I, m5 g( n
// an action group, because we want these actions to be1 r2 E T& o9 F1 e2 ~
// executed in a specific order, but these steps should* X4 D* @+ L( g& B g5 n0 s+ R
// take no (simulated) time. The M(foo) means "The message
. `9 ^4 ?8 v( c" Q9 j9 ^& o // called <foo>". You can send a message To a particular
& K: Z9 L x- A5 \! m // object, or ForEach object in a collection.! I9 s' M- E1 P, @6 Z' z
7 U* [# J$ Y% ~2 Y8 q; V // Note we update the heatspace in two phases: first run
# ^5 A8 A) c' C5 V& K4 v // diffusion, then run "updateWorld" to actually enact the; d! O- ~" Q8 q
// changes the heatbugs have made. The ordering here is% K- t0 @9 q1 V
// significant!
; I8 d0 w7 d: g' Q" O
. l& r' J+ Q y9 \, M* \2 |" ?6 H // Note also, that with the additional
4 U; L6 z$ e0 h5 ]. t6 S9 B% R // `randomizeHeatbugUpdateOrder' Boolean flag we can/ l8 ~7 `6 p* b# {& Q! v. T
// randomize the order in which the bugs actually run
9 }4 r4 G- w0 t* Q // their step rule. This has the effect of removing any
- N1 |# w ^/ U4 m; W/ T // systematic bias in the iteration throught the heatbug
9 ]/ u1 E* m% ^ // list from timestep to timestep/ [; z9 j0 S! n7 [0 ~1 [; P
8 U- P0 S* p# j0 M7 i3 t0 d
// By default, all `createActionForEach' modelActions have
9 {$ ^' H8 v, u // a default order of `Sequential', which means that the
, q& n& ]; |" S+ {( U4 n" ` // order of iteration through the `heatbugList' will be% s/ g5 V& o W7 R1 m; T+ L& U7 Y
// identical (assuming the list order is not changed
8 O: [. X- D6 j( ]% ^! N. ? // indirectly by some other process).
) m+ K& c6 X2 D% j: m2 ^0 J
6 c" o; G6 e& K5 Z+ r modelActions = new ActionGroupImpl (getZone ());
2 O- U5 |' L$ F6 C& A4 F" h0 J# J( d' O* o- E/ `( A. ?
try {
. H0 G0 Z& z# l ^ [ modelActions.createActionTo$message
z7 Q5 k9 z0 I9 m+ K, ~ (heat, new Selector (heat.getClass (), "stepRule", false));
8 x6 r1 Z4 q- c0 I& T' L- W/ k } catch (Exception e) {
# h* j3 f' \ Y9 H! |8 w System.err.println ("Exception stepRule: " + e.getMessage ());
8 H4 f2 B8 L# v& Y7 A& f. a( o }! W4 X i; D4 g6 R3 D# Z! [0 ^$ P
2 b. e y. H: D1 L' q% Z5 D3 n7 ~! w9 m
try {, [; F0 ^# ?, k/ h% w7 I
Heatbug proto = (Heatbug) heatbugList.get (0);
# J* R1 R& r& z% g( N X Selector sel = ) k/ |) }" p u& i* t
new Selector (proto.getClass (), "heatbugStep", false);: O/ q! o% \6 S' `2 R
actionForEach =
. Q; D u, F9 s+ S9 g: ] F modelActions.createFActionForEachHomogeneous$call
! h/ v$ S3 x; D" m5 w6 M (heatbugList,4 o' h- ?% E- m& w- |; X, D
new FCallImpl (this, proto, sel,
g. {% h6 [) o5 i* W" }( V new FArgumentsImpl (this, sel)));
. x3 O6 p0 q& O6 U; z0 k- j s0 Z3 I } catch (Exception e) {# N, i- c7 @* E) G
e.printStackTrace (System.err);
: H' {& U4 `6 e" w1 ]. z- @6 j }
5 b: H- r' H! q { . T4 `5 t! [' d- e) q
syncUpdateOrder ();8 r; b1 U' W: a# k" _
0 I- K# Z+ S4 {- L7 n! ^ try {
0 |, I9 c G7 M2 m* Q: j/ i modelActions.createActionTo$message 2 e/ }( T) ^1 `8 X+ q
(heat, new Selector (heat.getClass (), "updateLattice", false));; }8 P! j8 E: m
} catch (Exception e) {
8 I- S% O# i \9 t0 u* K. e System.err.println("Exception updateLattice: " + e.getMessage ());; \. X4 m5 S6 w" }
}
/ @* J7 b" D' [8 \3 w, b. Y# i 4 D3 |9 R/ a/ B6 x. ?
// Then we create a schedule that executes the8 |# K8 a1 ^/ j! p
// modelActions. modelActions is an ActionGroup, by itself it
: s4 q9 _" D6 K2 N0 _; T, r // has no notion of time. In order to have it executed in
6 ]+ E3 G0 C- _ // time, we create a Schedule that says to use the. J0 ~& h& q( T, f2 f
// modelActions ActionGroup at particular times. This
) t: g% ~% l6 ]2 ^0 ^2 s* } // schedule has a repeat interval of 1, it will loop every
+ I- L$ v. Z# {3 ^. ~* M, v& I( q% @ // time step. The action is executed at time 0 relative to) n$ i9 ^! O, k) V; H
// the beginning of the loop.8 o# @. p0 A7 X0 `
' _6 N* l2 |4 S& ]$ B: x // This is a simple schedule, with only one action that is
; Y2 O2 ~+ N2 @; S6 c6 L // just repeated every time. See jmousetrap for more
4 I9 [" A' P1 A0 I( v# ^$ s // complicated schedules.7 ]) y0 _0 P6 ?4 l
; D4 }- z9 Z% f) E! [) u
modelSchedule = new ScheduleImpl (getZone (), 1);
; Y! Y9 P* B0 A T/ l$ w8 {9 M modelSchedule.at$createAction (0, modelActions);5 O7 t j9 w3 U8 Z+ U, S
0 v2 z. F- t. l q return this;
; e* V- d, _) @) X" D# k } |