HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, W* w. ^4 H. U& R
; a: F8 y2 W- M! ?0 G public Object buildActions () {
6 z2 v6 X9 U+ U4 u9 S0 V super.buildActions();
" h8 A6 d3 D+ ?) }
* p: p. m: u' }# d% X // Create the list of simulation actions. We put these in/ w/ a9 J6 p# \5 T4 R# p6 C
// an action group, because we want these actions to be& l7 `! A) k; h$ f E% Y
// executed in a specific order, but these steps should! {# K) \( Y% h1 \3 }' N
// take no (simulated) time. The M(foo) means "The message! y0 Q( ]! k) v) x8 X4 T
// called <foo>". You can send a message To a particular8 N. o* a; O4 p) ]7 b# w8 K' T4 U
// object, or ForEach object in a collection.& ?, D( Z9 a" y5 b: Y' F% x; P6 _
) M, U) e/ |9 U5 f- ^& H/ v) D
// Note we update the heatspace in two phases: first run5 I6 O6 y B1 N, ^ e& j
// diffusion, then run "updateWorld" to actually enact the, C- t% x) }4 o8 v
// changes the heatbugs have made. The ordering here is- ?, o" k8 h) o3 I6 }5 C
// significant!
5 v; r: g# e$ O' Z3 u $ i5 n9 R5 ?- Y. ]& R
// Note also, that with the additional
1 N* k% J: s2 m& w // `randomizeHeatbugUpdateOrder' Boolean flag we can
" _" v$ r, u8 i) z) w# t // randomize the order in which the bugs actually run
/ ]% n# q0 u) z- U; l; A // their step rule. This has the effect of removing any6 |- v% F' I8 x- x! M0 H
// systematic bias in the iteration throught the heatbug% j* s" W9 }$ a; @5 Q m
// list from timestep to timestep
' a0 O6 Z0 k5 v ) W: n" N; t4 l" S
// By default, all `createActionForEach' modelActions have+ A0 q6 K4 D. ]5 {3 r
// a default order of `Sequential', which means that the7 @% k4 p3 ?0 G: N5 F8 u [
// order of iteration through the `heatbugList' will be
, N) l7 J8 m/ _4 X* ^ // identical (assuming the list order is not changed
; c8 h2 {4 e) i0 l9 V4 }2 R3 g // indirectly by some other process).# T( t7 S- ?8 m* V
; I3 F# q$ B! I modelActions = new ActionGroupImpl (getZone ());
. k3 `' o+ G+ z2 a- D F
8 k/ S- ]2 R; \# J% Q" ^7 x try {6 {+ s( V: @% Y, S0 c/ }" @7 q
modelActions.createActionTo$message
0 v6 C% M, B) [! R3 s* g$ T/ i (heat, new Selector (heat.getClass (), "stepRule", false));
$ g) Y- C+ M' L3 U, U } catch (Exception e) {
) K- M3 m3 p* d7 H: T# e System.err.println ("Exception stepRule: " + e.getMessage ());) A [1 Y/ T5 Z) l: c
}9 w: u% x& e% N/ R8 W. W" p
E9 b& m, B6 O4 k5 T+ l
try {
* u. R( c q) w4 ~7 { Heatbug proto = (Heatbug) heatbugList.get (0);" y8 y2 }$ Z6 B
Selector sel =
! {6 N* } P" o0 p- {7 s3 { new Selector (proto.getClass (), "heatbugStep", false);+ }9 Q9 I6 D1 S3 B) y
actionForEach =; I4 H! U8 M2 a& b$ C6 F
modelActions.createFActionForEachHomogeneous$call4 }9 v" x# a1 H k+ w" e9 M' G
(heatbugList,. W; ?9 y$ {# h1 E9 i
new FCallImpl (this, proto, sel,
6 l) }% ?2 R- V) X" M new FArgumentsImpl (this, sel)));
) e& T5 Q1 T! ?" I* J, | } catch (Exception e) {
# j4 ?- X9 u# d* ~ e.printStackTrace (System.err);
( x7 L0 [: l7 L. ]# d }5 X6 Q; z V- |0 n& |
7 b+ s: e0 I2 n, g* @7 b. T1 ^ syncUpdateOrder ();
$ |# t& z+ w3 e# b
h) F. }8 g* l: I9 J5 o try {* [; c: d4 r' T# c; [$ [! m# e8 Z
modelActions.createActionTo$message
+ F( o1 C- J6 c2 @ (heat, new Selector (heat.getClass (), "updateLattice", false));6 K g f' s+ K/ b c$ w
} catch (Exception e) {1 ~* n& q) ~' S8 r
System.err.println("Exception updateLattice: " + e.getMessage ());
# Q. w; X" B- q; A5 p4 _ }
. S- n. Q0 a4 m2 s7 v b
0 d7 c8 D; p5 l2 ` // Then we create a schedule that executes the" L& h. r. a6 u8 r# S2 E
// modelActions. modelActions is an ActionGroup, by itself it9 u, Q9 J/ S+ B2 c: H I9 d; Q
// has no notion of time. In order to have it executed in
: I5 [. _6 t: o$ g // time, we create a Schedule that says to use the
% l8 ]( h3 Z( b/ c* O // modelActions ActionGroup at particular times. This
2 B$ C* [, `7 q0 r6 Q% Z& e // schedule has a repeat interval of 1, it will loop every3 B k1 ~* N% o+ n
// time step. The action is executed at time 0 relative to5 i5 { |5 X3 T y! x/ Z0 j
// the beginning of the loop.
, w; P, ~ d; a) [* s$ C5 x L' M" w0 U8 a- L
// This is a simple schedule, with only one action that is' W: {6 x: \, n2 b
// just repeated every time. See jmousetrap for more
! I: p/ M% ]* p5 Q // complicated schedules.( C9 M; x( l r
4 n2 X* q. L9 c' Y modelSchedule = new ScheduleImpl (getZone (), 1);- n* S& b/ x: Q
modelSchedule.at$createAction (0, modelActions);1 g I9 I# J% n+ n7 A1 v8 r# t
2 g* }8 N V) J, O
return this;$ n+ x, z( a& N: o7 f: D
} |