HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) A6 }; C6 t$ ]5 ^
2 O% b- B( K+ I- e# d8 I1 J8 B0 n public Object buildActions () {
\6 V8 |; c* b9 t" O" N super.buildActions();
$ k9 c+ _* X% ?" k # B2 D* Y* p) |, { U2 u$ Y" A
// Create the list of simulation actions. We put these in
5 g2 c) I: Z. D$ y+ H! W9 G // an action group, because we want these actions to be [! T' B! `) H# L
// executed in a specific order, but these steps should2 K9 Y; ~# \! F2 x9 D- {& P \
// take no (simulated) time. The M(foo) means "The message
8 A9 q. w ]) O3 P$ _* q // called <foo>". You can send a message To a particular( U9 T+ O2 `6 r
// object, or ForEach object in a collection.9 M( [: @# B: I! _/ h
5 N, y8 \: G9 G, x
// Note we update the heatspace in two phases: first run6 J( [0 B0 q( t) @# O9 x
// diffusion, then run "updateWorld" to actually enact the7 x9 o5 j T0 C6 h0 W' X8 f
// changes the heatbugs have made. The ordering here is
# D$ V2 p% P7 q" R4 |! P // significant!
# R: g/ A' B; X j" Z/ l : |% c2 g- {9 J1 F. O
// Note also, that with the additional
4 P7 S1 q j( c# ~0 c' s2 @/ L // `randomizeHeatbugUpdateOrder' Boolean flag we can
* A- b6 B' Q" w3 r5 Q // randomize the order in which the bugs actually run2 ?4 x! d) ~' E$ \
// their step rule. This has the effect of removing any2 [+ `8 \: T( u& I
// systematic bias in the iteration throught the heatbug+ u+ k2 A* M4 l8 h4 M7 ^/ ^
// list from timestep to timestep
: B3 t; H/ b F0 C4 W 0 Z9 Q. O- ~1 o" [) h" q
// By default, all `createActionForEach' modelActions have
1 U+ b% M3 m* s! I, K7 e9 q // a default order of `Sequential', which means that the
% z0 G2 H9 e& e) [4 y5 L // order of iteration through the `heatbugList' will be
3 K; V6 h/ _" r. `9 r- \# E // identical (assuming the list order is not changed
Z# h# d8 _; H9 z- N // indirectly by some other process).
) \+ h/ ]. e* ^; k 0 x9 B# x+ u P- t) ]3 I
modelActions = new ActionGroupImpl (getZone ());1 ^. w2 Q8 Y; e h
* N# O+ n8 S6 `" O Q3 I8 x try {6 b: f+ B' H3 n' s+ r: g' d
modelActions.createActionTo$message
7 S1 X. k0 Q8 o/ y* h. l (heat, new Selector (heat.getClass (), "stepRule", false));
/ P- o# L& K: O% ?* p: @# b! O } catch (Exception e) {
6 z$ `7 k% N- x% e/ k/ }% U) w System.err.println ("Exception stepRule: " + e.getMessage ());0 \4 U* m) Q E0 }6 O
}
. B. ^( l% B8 c% r( q4 o/ i; u! F& _! x$ _* {- H8 e2 a, U6 ~
try {
b8 T9 Q7 t. C' ]4 B Heatbug proto = (Heatbug) heatbugList.get (0);
: z8 M, @. @0 @7 B' U2 f$ a Selector sel =
" p2 U. b8 K. T ^# H$ z1 F$ y new Selector (proto.getClass (), "heatbugStep", false);
2 N. m" ^0 e, E1 b* x actionForEach =- s& C3 `! p0 H: j) x
modelActions.createFActionForEachHomogeneous$call) }$ v) W- O c- q! a
(heatbugList,
* V' [+ [- |# L! Y new FCallImpl (this, proto, sel,
" ]- \% q& K, p7 R6 K$ T new FArgumentsImpl (this, sel)));
( Y B* @% B; K, ] } catch (Exception e) {
, Y I i: |5 h1 N! A; Y: ~ e.printStackTrace (System.err);
( z }: L Y# c' k$ ]9 y+ p2 W }/ K# R" n% J0 N) p C
3 x/ ]% ` \& w. S( a1 ?, y; W syncUpdateOrder ();% ~' \0 |6 M& m: u
2 I* g1 r; w6 C/ L7 ^
try {$ `7 T0 V% }& S% }7 n
modelActions.createActionTo$message % e2 w, J. R# d+ ~. V& ?, r" y
(heat, new Selector (heat.getClass (), "updateLattice", false));/ [: ?/ v1 S+ k1 }6 d. u: M% t, n
} catch (Exception e) {
1 f1 g3 _& i' Z, P System.err.println("Exception updateLattice: " + e.getMessage ());" U4 ]6 j" i6 j- q
}9 k# n8 n* \& H7 \$ e# R
; O0 r) Q9 w, Z* `8 }. f // Then we create a schedule that executes the3 [" B, @# C {' O5 J2 B9 \
// modelActions. modelActions is an ActionGroup, by itself it# q2 d& s) c5 [+ t+ n
// has no notion of time. In order to have it executed in
- ]- o+ H n% d [" N* T: Q // time, we create a Schedule that says to use the3 i* l: ]& M$ m6 G6 s
// modelActions ActionGroup at particular times. This2 \: K& `$ z" W8 { v' b* A
// schedule has a repeat interval of 1, it will loop every
" Z7 [* S" f! V1 T1 ]8 ? // time step. The action is executed at time 0 relative to
2 n: v$ E$ a1 |3 [3 J8 h5 c' f // the beginning of the loop.
$ c! u- r2 M; y8 U4 Y5 Z8 S$ r6 G9 N' r. A& z. \! @1 u
// This is a simple schedule, with only one action that is
8 N$ b; U1 G( [5 x" D7 @1 ? // just repeated every time. See jmousetrap for more
9 G: Y/ g8 T- U7 j // complicated schedules.
7 V( E+ t# C$ C5 {/ W3 w
+ P1 {: L: }# Z5 u2 f( v modelSchedule = new ScheduleImpl (getZone (), 1);
" j$ ~$ }0 f, A( [- \3 v4 F modelSchedule.at$createAction (0, modelActions);
8 L1 g. }: {& K3 w& F6 F$ p
; |2 }2 R/ o' ^7 I4 ~ return this;
4 a5 K% d4 x. ^5 y: f2 _& v } |