HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ o \6 V' G4 Y- z' [
( c9 Q1 \, L9 Z# F* y# d) Y public Object buildActions () {& Z9 n/ \8 {* T% [ A
super.buildActions();
5 {3 A( I, P* I% R* F, s6 m
1 x! X' G$ ?7 Z2 H! F- [. R // Create the list of simulation actions. We put these in7 _0 g$ P q, s' m/ n
// an action group, because we want these actions to be8 d* f8 S+ s+ f; @# K
// executed in a specific order, but these steps should
0 g; _# P& H: ~. u$ ?7 G% n+ E% a, Q // take no (simulated) time. The M(foo) means "The message/ Z" ?; I. r/ q
// called <foo>". You can send a message To a particular
: p K z/ L( U+ ? // object, or ForEach object in a collection.
, c7 [& @9 e% y* [3 @ + a1 X# Z& y2 l) i
// Note we update the heatspace in two phases: first run
9 L, ^$ Q0 M8 I6 Y# y // diffusion, then run "updateWorld" to actually enact the
( g) ~3 o. Y E3 _ // changes the heatbugs have made. The ordering here is
; d y4 R' r( w0 @' Q' d // significant!% ? {% q3 z4 N9 r+ f* v
5 b- r0 c0 I3 m! R- e
// Note also, that with the additional: j8 i9 h0 T/ Z8 i3 p! u4 A( n3 a
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ c- c5 }& a3 Q# A // randomize the order in which the bugs actually run8 B% N* t1 t) l- V
// their step rule. This has the effect of removing any' B0 r3 R& U9 U' |
// systematic bias in the iteration throught the heatbug S+ V! W& R# N6 D+ F3 z
// list from timestep to timestep! J- [# `! z1 K& n `9 c
d5 }4 \- h6 ~" J9 _# f3 |$ `8 V // By default, all `createActionForEach' modelActions have
+ z: C! h3 H" [2 D- J3 A // a default order of `Sequential', which means that the$ D$ }5 y: t6 j! L
// order of iteration through the `heatbugList' will be
: o% S) @- U4 P' e6 T+ O/ V2 P- h // identical (assuming the list order is not changed
/ T$ n& G; j T, ?* q3 M' E // indirectly by some other process).* }/ X+ B1 h; {# Q( \
1 T/ K* S- b+ P2 y; d! m
modelActions = new ActionGroupImpl (getZone ());
' y [4 F9 O) L' p" v
# X" c$ J6 d M" @: [ try {, {! u# }! G+ O/ k4 f4 z
modelActions.createActionTo$message
4 x! `& j/ I) p& m' D7 K0 o+ ` (heat, new Selector (heat.getClass (), "stepRule", false));
: J( Q1 Q# g3 m* r } catch (Exception e) {
! |: ~4 T q9 i7 A% w8 i9 B5 ^' o System.err.println ("Exception stepRule: " + e.getMessage ());: Q& H: ?6 Q+ K+ A; W& Y. f
}
5 K ]$ f3 X, E- O7 |, {# X! i5 c
) p1 P0 c: b* ], |+ e try {
" ~$ N8 R7 l; r! j, @+ k Heatbug proto = (Heatbug) heatbugList.get (0);
7 m( Z; Z9 W% @1 P4 b* j- w Selector sel =
$ r) B5 z* U: _' ~, K+ ` new Selector (proto.getClass (), "heatbugStep", false);/ [" S: R) E* S$ G) F j
actionForEach =, d/ F) {' h! P4 T
modelActions.createFActionForEachHomogeneous$call
: K2 c9 ]# L8 ]& M+ E (heatbugList,
3 i3 z% O: m O8 a new FCallImpl (this, proto, sel," `. _' ?' V" z9 ^
new FArgumentsImpl (this, sel)));
; v( B( g; i1 W9 |$ w } catch (Exception e) {2 l$ R/ h1 H) X8 ^
e.printStackTrace (System.err);& o; a3 Q: }& b& t7 i
}
7 Z( {6 x( l8 J8 ~
3 F: U0 F' F; l syncUpdateOrder (); n7 E g' y) U% l1 M" v# Z5 w
1 @0 k, ?# _* x( z try {4 A. Q+ c9 r# T. o5 k$ R
modelActions.createActionTo$message / p& P9 E* h; Z8 [1 l0 n
(heat, new Selector (heat.getClass (), "updateLattice", false));' T3 O7 i% y! m; A2 b6 r
} catch (Exception e) {& d! b0 D. |& N7 c1 b7 F7 l( i9 ]
System.err.println("Exception updateLattice: " + e.getMessage ());/ {" e( N: H. h- m1 ?& {5 y+ ^* M
}* D" ?8 x1 ?- I$ B ]
@. j& h0 b' {( ~: d M; ^9 U // Then we create a schedule that executes the5 u2 \" ~9 ~. _
// modelActions. modelActions is an ActionGroup, by itself it- A: s4 Q. V U$ J9 Z1 E
// has no notion of time. In order to have it executed in
5 K; q# j# w, Y( u: p // time, we create a Schedule that says to use the
2 U$ z9 }( t- p" X7 a // modelActions ActionGroup at particular times. This
( d. @* h4 j7 Q2 l" K% n, \ // schedule has a repeat interval of 1, it will loop every( e, U0 \. c7 t
// time step. The action is executed at time 0 relative to+ T: `, t9 \) R; X7 A% P4 ?% _8 z2 C
// the beginning of the loop.8 [8 H# \% g+ R4 ]& }( ]5 Y% R8 d
% b: y4 | @- p: A, `
// This is a simple schedule, with only one action that is2 g: ^/ b) H& B, @3 ?
// just repeated every time. See jmousetrap for more
+ b2 @' w8 X9 N& g& { // complicated schedules., F% S7 D z7 `" @# n1 _' d* l% T
8 K' {0 ^9 `/ X modelSchedule = new ScheduleImpl (getZone (), 1);
6 W; u" ~3 R* `! @. t8 G modelSchedule.at$createAction (0, modelActions);
8 C T4 q2 E( A) c( q
3 r! V t- e" B5 Q+ l5 M. } return this;
& X2 k8 f' j7 K* x. H$ Y } |