HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 G- R7 b7 u5 z: n& p* o! i
" ?5 D" u# B9 |$ _- o5 @9 d public Object buildActions () {
' p! W( P0 S. n0 y5 N+ v( [& c super.buildActions();
U9 V7 I$ h/ S2 J " v; W3 @( [! R0 Q. N+ Z) L
// Create the list of simulation actions. We put these in
4 r7 s- |: I6 S, s5 j0 d+ ]" X // an action group, because we want these actions to be
! r& w; _% q: I* h; j! H! F // executed in a specific order, but these steps should* ]9 l: Y2 T& A
// take no (simulated) time. The M(foo) means "The message& {8 t G8 y. c& K e
// called <foo>". You can send a message To a particular- b' {2 i+ p+ C, t3 J- x
// object, or ForEach object in a collection.! A+ M0 w& d8 W+ G
, u, {# l. v9 n0 f; _" u
// Note we update the heatspace in two phases: first run* `8 i6 i) \1 K) H% o
// diffusion, then run "updateWorld" to actually enact the$ b; q- }! i3 N5 z
// changes the heatbugs have made. The ordering here is2 B* e4 r$ @9 m- ~
// significant!
& O0 y! T( O0 ]7 c% D( a- H 4 `& i. @* T! F" e" G3 H. V" R
// Note also, that with the additional( s2 ^ V/ t( J1 B7 p# ], [
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* h8 X4 o/ W3 b- Y7 {' I // randomize the order in which the bugs actually run
2 j( N6 _/ M( t9 Q' F# `, ?4 ? // their step rule. This has the effect of removing any
6 e! x' C# K; R% B9 { // systematic bias in the iteration throught the heatbug7 I0 P6 ^$ m1 w) L& T3 x* u( L0 L [
// list from timestep to timestep0 _6 Z( I5 r- q+ c* S2 y4 X/ c b
X3 a, \7 |3 i4 n+ \( F // By default, all `createActionForEach' modelActions have: S' f* l. f! x
// a default order of `Sequential', which means that the
! }) u# s! O, _) u2 U+ U // order of iteration through the `heatbugList' will be' E- i0 x! F; V, z$ Q% s3 \- z
// identical (assuming the list order is not changed( Q5 n) ?; U. F# t3 a$ D
// indirectly by some other process).9 J0 H8 \8 J- C9 l0 o
9 t- D6 }! T. \4 u
modelActions = new ActionGroupImpl (getZone ());
( f1 I1 V& M* _( @+ S
' l- m& |; g/ ^* u7 s' ^$ N try {
6 g$ {% r+ S. }! M modelActions.createActionTo$message t- M. `7 X" k0 b+ @% j5 I
(heat, new Selector (heat.getClass (), "stepRule", false));- r j8 ^" r. f, W- }3 K
} catch (Exception e) {
3 H! w+ g3 W! C+ v d# K! ^* a System.err.println ("Exception stepRule: " + e.getMessage ());$ m; m" B* T2 ^: `% \
}
/ K; C2 t. W2 ]/ |+ R& f: s: f! G3 I; j8 U" y H, t5 Y
try {
( Z/ P( z/ t5 \$ x% ? Heatbug proto = (Heatbug) heatbugList.get (0);; r/ `* ^2 o0 x/ j
Selector sel =
U4 Y M# a! [5 [* g5 {6 ~3 C new Selector (proto.getClass (), "heatbugStep", false);
: G) I; z$ r0 ~ j" u0 m actionForEach =
! d+ [; \ B" q4 l( x7 ~ modelActions.createFActionForEachHomogeneous$call2 b$ h' [- _6 L, X6 G6 {
(heatbugList,
' D( M; s1 Y8 F) Q _4 V3 | new FCallImpl (this, proto, sel,
; }3 _' g! P; q# w7 p! h" S7 K5 l/ C new FArgumentsImpl (this, sel)));% k2 E# f6 k L+ o% {1 |7 o
} catch (Exception e) {
9 T, a% B; M/ t8 ~7 _8 X e.printStackTrace (System.err);
0 o; H0 U) G( ]0 X7 K }2 b. V2 e) P) N! g J; M
: r5 ^! H1 y" a( n
syncUpdateOrder ();
/ a/ M, ~% J% A9 k* Z
. G+ M7 x+ E- m2 D try {
# E+ V: A/ g: U' ~" l modelActions.createActionTo$message , K. c9 B0 }5 _3 T3 N& j; q a X
(heat, new Selector (heat.getClass (), "updateLattice", false));5 P6 i$ s) K/ z6 `% |
} catch (Exception e) {
0 M6 i& r+ H/ F System.err.println("Exception updateLattice: " + e.getMessage ());( K1 s& n# o& }0 Z. l
}$ u; `1 m# L) g3 x
: y/ N! r. m( B
// Then we create a schedule that executes the* c/ H+ A* U" l) B6 B5 l- \
// modelActions. modelActions is an ActionGroup, by itself it8 D- x* O7 `* s: P
// has no notion of time. In order to have it executed in
% d: g1 C% a5 F // time, we create a Schedule that says to use the. F2 l- c5 I/ ^1 o
// modelActions ActionGroup at particular times. This
9 c" L- k& e4 {, D$ V+ _ // schedule has a repeat interval of 1, it will loop every
$ u7 L* z& g6 n // time step. The action is executed at time 0 relative to w+ T p* ?$ R( u' y
// the beginning of the loop.
) C6 W, f' M: n! K: f. B5 }, X/ k) ~5 }+ i3 H
// This is a simple schedule, with only one action that is. q* D% p; O' x6 P
// just repeated every time. See jmousetrap for more
. G( ~. T( v2 s2 U% |$ c# h9 e6 n // complicated schedules., T4 I" `7 H) o9 q
% H. ?4 ]8 r/ O) b/ ?/ X modelSchedule = new ScheduleImpl (getZone (), 1);- C$ S& q' E" G. b, u2 H7 O
modelSchedule.at$createAction (0, modelActions);! J% O+ b5 [" m d
" r- Y. p# Y# N! v
return this;
! p7 `4 u; i0 K$ D3 K! S } |