HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& }5 D2 c1 T+ i5 M( E
1 X, B$ S' x4 x, T2 { public Object buildActions () {
, K& }( p: ~. P super.buildActions();
( B( B$ c+ M2 R+ r5 V- I
- ~! {- u- \0 z0 |. r# b // Create the list of simulation actions. We put these in' B9 G# e' b, R. D
// an action group, because we want these actions to be
) E7 X' x, l4 F: V2 S // executed in a specific order, but these steps should
: w' I6 B9 N( q // take no (simulated) time. The M(foo) means "The message
9 a/ R( G/ L% S% }- Y6 H // called <foo>". You can send a message To a particular
. A$ \2 }! ^$ F' _ // object, or ForEach object in a collection.
U! e% w& T1 H! ~( k+ o
o' ^# w, h! _) v // Note we update the heatspace in two phases: first run
/ G1 g" s: h3 e+ P) w7 ~; ]' U3 M // diffusion, then run "updateWorld" to actually enact the2 D0 R. A4 c, J- @& C# U, j, a) v
// changes the heatbugs have made. The ordering here is) ?$ h) f z$ ~. Q& a+ f% h
// significant!# K+ c1 P, `+ N
4 y( P l# T; g$ i // Note also, that with the additional" A5 i# X4 Q4 D. z6 Q- ]( L
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ G" ?& e& R+ j- V; i9 x5 m/ z O
// randomize the order in which the bugs actually run
' q$ |, l6 s( _ // their step rule. This has the effect of removing any9 B! F3 Z7 O2 |0 V
// systematic bias in the iteration throught the heatbug7 d2 @- J0 f. ?4 c8 d
// list from timestep to timestep0 y9 |% [0 |: W' d. A3 k6 }
7 N4 p% Z- p9 n/ V( z4 I) b
// By default, all `createActionForEach' modelActions have$ W3 T. T9 V: r6 P* b$ H! M
// a default order of `Sequential', which means that the" f6 {6 X" L6 o/ E9 j" P0 v
// order of iteration through the `heatbugList' will be
9 F5 H$ ]- N, C g7 | // identical (assuming the list order is not changed
7 K+ y1 ^0 w' r3 m# z // indirectly by some other process).
7 e$ p) y: b2 C% l+ W7 f
& G: t2 v0 m6 H+ ^ modelActions = new ActionGroupImpl (getZone ());
; I! b1 T. K0 W7 x% c* n9 ]9 h
0 |1 \- V* v5 l# V! M6 o try {% r8 J' `3 b& v* p( z
modelActions.createActionTo$message
% b3 f6 v% L# {0 h# _+ X! u/ N/ K (heat, new Selector (heat.getClass (), "stepRule", false));' U; b$ y B$ `6 x1 K$ M- S
} catch (Exception e) {
R+ S7 [+ N L System.err.println ("Exception stepRule: " + e.getMessage ());
u/ |9 k" ?; R: E" W# a }
+ ^0 F3 [1 \: M; D2 B% g' ]- t, m
, W2 {5 r5 w9 \8 d) K try {! T$ U6 S1 `% |, E
Heatbug proto = (Heatbug) heatbugList.get (0);
& c: p/ E7 H8 e' |% a Selector sel =
5 ~9 a% G* _+ j( I! P new Selector (proto.getClass (), "heatbugStep", false);2 w+ O4 r6 t3 X5 `
actionForEach =# Y1 D7 T& P4 i: [+ W7 U4 L
modelActions.createFActionForEachHomogeneous$call
! o# @1 G( O" L7 K (heatbugList,
& m( t8 P7 }1 U1 F( {) d new FCallImpl (this, proto, sel,
/ E& Y/ i% q1 ]4 d3 m Q new FArgumentsImpl (this, sel)));) }! H( E, U' f( D! y
} catch (Exception e) {
6 S1 i+ i9 f/ z5 ]& H7 F4 ^ e.printStackTrace (System.err);1 O0 l3 N" J& W3 \" F8 A
}- \' c& z% ~) [* i% j( ]
/ Y- P0 h: m' J
syncUpdateOrder ();
+ p& b$ q+ u: d& ^& [) @
5 J& t6 V8 R1 l( v try {
! S8 I1 ]; f. X- }! h& o modelActions.createActionTo$message
2 s/ N, V. n9 a$ H" I$ `$ W (heat, new Selector (heat.getClass (), "updateLattice", false));
6 S8 b" o/ E' I, [/ \) z } catch (Exception e) {5 s8 n) o. y! U6 ~$ N6 t
System.err.println("Exception updateLattice: " + e.getMessage ());$ g* o1 J6 Y: R7 O: b$ S
}4 ~, m* B% R- U
6 ~% S& V7 G+ h' h9 u
// Then we create a schedule that executes the- C! E$ l; p4 _" R! B( V5 b
// modelActions. modelActions is an ActionGroup, by itself it9 x8 ^% p9 g* v5 ~, N* j. O
// has no notion of time. In order to have it executed in- H& d; Z1 {% ^, m1 u
// time, we create a Schedule that says to use the) J' W: G0 M/ \. f% b4 x7 w* v) i
// modelActions ActionGroup at particular times. This, z4 B- H. I! h: O
// schedule has a repeat interval of 1, it will loop every' E: M1 a8 E, C
// time step. The action is executed at time 0 relative to, x% o+ h/ t, C9 u
// the beginning of the loop.( |% d# t0 p4 M
+ }# v2 `7 J0 G // This is a simple schedule, with only one action that is
/ b7 g1 ]' {9 j& D // just repeated every time. See jmousetrap for more
* U+ |5 T" F1 Z // complicated schedules./ Z# w+ U9 Z4 u* N+ \! ^
" s! Y4 M( `& D6 n! B# W$ a; P
modelSchedule = new ScheduleImpl (getZone (), 1);
% |/ d. S& ^ F" Z- O @ modelSchedule.at$createAction (0, modelActions);
6 z* w7 ~( c) j% c' O, Z
* d9 w$ R7 N' X return this;
, F$ w0 E7 }* p7 q6 G2 M } |