HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# B$ Y. G! | T0 v( N
. q1 }8 N* D2 L$ n5 o$ Z% i* | public Object buildActions () {
" x0 v% d$ J7 s" q super.buildActions();( J9 g! E* j' v; {3 c
$ x# \ g$ _ v9 b9 O8 |" m$ e // Create the list of simulation actions. We put these in
/ e( u$ x; v( l$ R" r! v4 L // an action group, because we want these actions to be
4 N& j1 B$ t# V // executed in a specific order, but these steps should: s K- e/ z) a9 w: b; q' p; F! B
// take no (simulated) time. The M(foo) means "The message
% {( l! N8 S1 q* W$ ^' o6 a& p // called <foo>". You can send a message To a particular: D' P: }; B, D
// object, or ForEach object in a collection.& K# a0 x8 _$ M
6 o6 M% F& D* [1 | k // Note we update the heatspace in two phases: first run+ L: h# r; N4 c0 K2 f
// diffusion, then run "updateWorld" to actually enact the
- N" o5 ^3 ~; { // changes the heatbugs have made. The ordering here is
% G; G* o* q {& D7 T // significant!
# {" [, \8 n0 _5 H1 r0 x. V* H % S3 b, {# C, m2 L+ l- T% O$ ]
// Note also, that with the additional. S' f" m- R6 w
// `randomizeHeatbugUpdateOrder' Boolean flag we can( N/ g4 w$ j6 Z
// randomize the order in which the bugs actually run4 @( D; V( |/ p
// their step rule. This has the effect of removing any
+ ^1 x+ w% Z/ X G // systematic bias in the iteration throught the heatbug) C# `& o& i; Y7 G& H' q! b4 d
// list from timestep to timestep
5 `* f1 ~7 f, h2 D8 F2 e" M) f
' J. y# f. Q* w0 z // By default, all `createActionForEach' modelActions have
! O0 l' V7 c! X! }- Q3 K // a default order of `Sequential', which means that the. j+ g; x% [1 `$ L! p3 a# _/ r
// order of iteration through the `heatbugList' will be
) t/ d7 _6 ?5 [3 u1 d+ [ // identical (assuming the list order is not changed
, C3 A3 v: c. D# `, J O // indirectly by some other process).6 \5 p9 ]9 m/ X2 [
! L5 J. ^2 O6 g/ V, u1 I5 Q7 y
modelActions = new ActionGroupImpl (getZone ());
/ I( j3 x% }/ M4 @/ ~) O) j+ [/ K( [; D- E
try {
- ^. ^9 g1 ]5 |+ m$ {3 |' e modelActions.createActionTo$message
: `1 u3 ~* O1 a/ n0 D7 Y! [, X (heat, new Selector (heat.getClass (), "stepRule", false));
" G9 x7 t3 v& Q$ c) R- [: l } catch (Exception e) {8 B1 L' X- I$ u" e5 F7 F/ ]# e( X
System.err.println ("Exception stepRule: " + e.getMessage ());1 N! l8 p: \3 y6 [
}
+ W# P6 {2 I' B4 Z( x: ]
8 g3 p6 J5 G6 q, | D5 c try {
. h# g: H) d9 M8 | Y Heatbug proto = (Heatbug) heatbugList.get (0);
; f# l( [9 }! s4 R E; U; B Selector sel = . i7 I' m- o* t; R; V; U$ b( U
new Selector (proto.getClass (), "heatbugStep", false);
5 o/ R, I; P, w9 E7 c+ K actionForEach =. x4 t0 U0 `7 `6 A
modelActions.createFActionForEachHomogeneous$call1 J# x( @1 G0 t; Z0 g+ T
(heatbugList,0 x: i; {/ o9 M; k7 E$ }1 C& _8 }
new FCallImpl (this, proto, sel,( X$ z+ A3 Z! A7 }' M k1 V$ q: W# S
new FArgumentsImpl (this, sel)));
" w- F ~& e, t0 t. r3 p6 { } catch (Exception e) {# }4 w4 F8 a& z" {4 r' x
e.printStackTrace (System.err);6 v( ~% F, E* H3 d; F
}# i" q* s) P% e6 l6 n# v
' C, |% `6 j1 F8 j/ U( O$ f syncUpdateOrder ();
a" N2 I# T% j! E% W( O# K
0 u' p2 n2 J2 E$ s: N9 c; t try {/ ^. R, c2 W) P3 p) z3 O! X
modelActions.createActionTo$message
& ?, T* _) Z9 G (heat, new Selector (heat.getClass (), "updateLattice", false));
H+ M0 l b& h9 A } catch (Exception e) {
: t+ H6 q1 g* l& Z% E# ^ System.err.println("Exception updateLattice: " + e.getMessage ());+ `" g, s5 \* I
}' ]" O3 d/ [0 I' L+ W5 `3 @
$ o1 G8 f& D: W // Then we create a schedule that executes the, R" M6 a: ~! ]* V6 H
// modelActions. modelActions is an ActionGroup, by itself it
1 W+ i3 l6 ^* q$ F! S // has no notion of time. In order to have it executed in
' i& S/ L. b5 e1 c; y+ z8 x1 Q0 F7 U // time, we create a Schedule that says to use the
( C* f! o, H$ j // modelActions ActionGroup at particular times. This0 C, ]: ~: O* t5 K$ a; ^
// schedule has a repeat interval of 1, it will loop every
' K8 g" H% N. J& ~9 J // time step. The action is executed at time 0 relative to
; |& d+ l5 A3 n- |9 b9 @ // the beginning of the loop.
7 B( E# M0 k/ a( L! f0 a9 E2 |7 s) ^! j: O& {$ B, \) B* x) C8 m2 r6 i
// This is a simple schedule, with only one action that is
. U% q! R, Z+ ` // just repeated every time. See jmousetrap for more
' |) A$ e" v i. g // complicated schedules.
; {/ l6 C9 g6 d5 Y! `( z* L9 s 8 t& T4 T$ T, t9 p3 P
modelSchedule = new ScheduleImpl (getZone (), 1);
# T5 F, Q* ]0 X6 f( U modelSchedule.at$createAction (0, modelActions);3 r( K" J- j. W, b
" h( D! _$ A4 x$ P' w+ {2 e* N
return this;" d* P$ L3 o' t+ {0 D9 N
} |