HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 B! z' u. P# s! ^
! k+ o) o/ o. n" a* c4 [
public Object buildActions () {
/ d! \: o. e, X, c super.buildActions();0 Y2 i- Y3 M! ~$ k
# a5 S% J, h E0 n
// Create the list of simulation actions. We put these in
4 f0 v! i) @- K# U5 l // an action group, because we want these actions to be
8 k/ p: n* p1 L/ g/ w- p( N6 A // executed in a specific order, but these steps should
) g: l7 ]$ M1 Z& n7 A. i- [6 q // take no (simulated) time. The M(foo) means "The message7 W. {6 o8 T; w7 `
// called <foo>". You can send a message To a particular6 W# `8 S& i5 S/ N3 T
// object, or ForEach object in a collection.9 b& x9 G7 G0 p
8 i3 l& T6 X, b6 x' y( w
// Note we update the heatspace in two phases: first run4 y: Y/ u% \ ]; f) ~8 e' A
// diffusion, then run "updateWorld" to actually enact the
! W: ~/ O' N, A Q( c) b // changes the heatbugs have made. The ordering here is
. t: D3 o* P% E% h // significant!
; Y' `! S$ _1 O5 ^0 [( i % ?; V9 d5 C$ _! u7 v( d
// Note also, that with the additional1 S/ A0 M, n8 w/ X; B) Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
9 [0 {) ~1 s1 \; e' D" U8 g5 H // randomize the order in which the bugs actually run
X, G- b$ Z6 t // their step rule. This has the effect of removing any
3 r0 _& Y- }9 r% H* v0 R // systematic bias in the iteration throught the heatbug
1 z0 _0 Q j4 v @; X // list from timestep to timestep6 h. ]! D+ a7 ]2 M/ f
9 H' K8 V" e/ f, p% C6 n# a5 q // By default, all `createActionForEach' modelActions have' A5 a0 [9 J& h) ]( W
// a default order of `Sequential', which means that the8 ~4 t0 f: k5 g$ e$ M$ U3 u
// order of iteration through the `heatbugList' will be
0 h) n) t! g" g3 G, m( U" ]1 H // identical (assuming the list order is not changed. b5 R9 r/ B) T3 x( P2 P p/ w6 a
// indirectly by some other process).
. |* ?( S; D Y2 T0 ~
$ f& [* Y$ ~+ ~4 T% f, m) q- V modelActions = new ActionGroupImpl (getZone ());
/ P: V& y% x( P
" N' X+ H1 T9 j7 T try {
4 ^7 N- E9 B, I' F: U/ q8 w4 B modelActions.createActionTo$message
! P8 y, g6 y9 T( ~0 P' J5 H (heat, new Selector (heat.getClass (), "stepRule", false));
3 v! p' b! ~" Q# b H6 E } catch (Exception e) {
0 J7 o O, J. I/ r3 ~- ?0 d( { System.err.println ("Exception stepRule: " + e.getMessage ());. J. `9 z0 l) v2 W
}& r* } m8 d8 y. {' O/ B
9 E( C I6 m4 N/ y try {
/ C7 L, W; D, |! { Heatbug proto = (Heatbug) heatbugList.get (0);
, B- X' r9 \8 ? Selector sel =
% Z v" }0 p' _4 R& u c g5 P new Selector (proto.getClass (), "heatbugStep", false);
4 V; L) y7 O: ?( p actionForEach =6 L" v/ [+ L6 R
modelActions.createFActionForEachHomogeneous$call
; z! y% `$ q1 h9 ^, i- Q (heatbugList,. b9 U3 a' N' Y5 b. h
new FCallImpl (this, proto, sel,/ u: \4 k# L$ a E* m
new FArgumentsImpl (this, sel)));
' _! R( [ i% W9 j. V1 g' T! k } catch (Exception e) {0 G4 T5 Z# J8 F9 Y
e.printStackTrace (System.err);
! Z' M/ e( h' r. v6 {: T. K7 { }3 C6 [. H8 x6 K' o
! s n' L* b* q' T' _* O+ U) _4 K
syncUpdateOrder ();
# `. ~& J4 I8 B9 o1 F' q' e. S; }; F5 f# R% E2 l; X
try {: \ Y- P* X+ D! n! }
modelActions.createActionTo$message 6 O$ o5 Q z$ z+ m, p
(heat, new Selector (heat.getClass (), "updateLattice", false));
. Q0 k4 U' i8 M- z: ^ } catch (Exception e) {
; S" [# ~0 u" w5 p; c' T System.err.println("Exception updateLattice: " + e.getMessage ());8 s. U) t1 U/ X. n2 [
}' ` T/ \+ g+ ?. u* \( G, O
6 T' J" A$ J9 l // Then we create a schedule that executes the8 A$ c6 P* E% G W# d; p5 m0 ^1 k% U
// modelActions. modelActions is an ActionGroup, by itself it! Q* ]" \: U( W( `
// has no notion of time. In order to have it executed in
# c9 S% \9 l3 ^ // time, we create a Schedule that says to use the4 M& N$ ~9 s/ t
// modelActions ActionGroup at particular times. This
; G, L5 t1 X1 E2 L! U& G // schedule has a repeat interval of 1, it will loop every9 K5 g' F( w3 c. V9 @& x
// time step. The action is executed at time 0 relative to0 b, \7 n3 ^* ?! |, I! W
// the beginning of the loop.1 S0 y% f! q" V& k( l0 E: Q
3 o6 l# O: h4 k. s& ~% t* t( \' I
// This is a simple schedule, with only one action that is
$ n* ?% R* s4 ?& ^- d // just repeated every time. See jmousetrap for more
1 R& d- U' s. K0 k* m, g- i+ o // complicated schedules.
+ r- j) P1 v$ B h+ `0 c' p0 j n% e 8 v& r0 p- q$ W. A. |* E
modelSchedule = new ScheduleImpl (getZone (), 1);4 E% d2 @' S j1 Q$ e1 @; d
modelSchedule.at$createAction (0, modelActions);
' g# G" C# H* f \; ^ 8 s; v0 b; P4 X0 }: \
return this;
+ L2 R3 l5 ]; B' k4 L# P } |