HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 n$ M: X9 P' o9 r+ h
) V; P6 l0 M2 a6 j" _ l
public Object buildActions () {' p+ V) ], G7 L6 T! Q
super.buildActions();, }8 I7 O, g$ M0 Z+ p
" @, a( C" `! ]/ j7 x
// Create the list of simulation actions. We put these in
- T& G( \* c3 _+ q5 _% C( x+ k // an action group, because we want these actions to be
. O& J* q7 Z; l& F; R, Q7 X5 y // executed in a specific order, but these steps should
' {2 k; k& U( D3 b' T. p5 Z // take no (simulated) time. The M(foo) means "The message
# w0 E( V& s* ]4 v // called <foo>". You can send a message To a particular3 E3 M* p) j) s
// object, or ForEach object in a collection.: B, x9 `6 u: f" @4 ?8 \& A! T6 p
, V4 D, X9 J5 |% X! b: P0 x
// Note we update the heatspace in two phases: first run
' d* a6 D" ^: @- b& ~/ C7 ? // diffusion, then run "updateWorld" to actually enact the, _! \3 q* U L3 r6 \0 D8 F" N
// changes the heatbugs have made. The ordering here is) x8 P8 e6 t0 g4 m3 R
// significant!8 p5 v) m3 M# y. T: e# [: @
% G$ Y. A% u( U; n/ l
// Note also, that with the additional o$ E. W0 O0 [- T$ j- \9 S% M& I/ ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; O. h! q# S+ O2 O7 c/ | // randomize the order in which the bugs actually run' w+ ?7 Q3 T! q" f. v
// their step rule. This has the effect of removing any
, e7 Y) s: ^' R% r1 z: K$ l! b // systematic bias in the iteration throught the heatbug
; V; z% I( g3 U. H9 L; H5 A1 Y // list from timestep to timestep$ z- _- P+ f' g( a2 G
. O U! m8 p6 I8 l8 S; X' R
// By default, all `createActionForEach' modelActions have T# g& l0 v" B! p9 s; y
// a default order of `Sequential', which means that the
1 a( a |; [$ ? // order of iteration through the `heatbugList' will be
9 I4 b( s9 Q& F2 V( l" I // identical (assuming the list order is not changed
% c' }, |- _& P5 W9 {" E4 N // indirectly by some other process).
, ?2 p" l8 o" Z, i! q3 i3 B
# V, o) o* }' R% Z8 P: O1 U# ~1 M) Y modelActions = new ActionGroupImpl (getZone ()); G; V3 E3 `" i0 H2 f5 F
5 z6 s0 z0 a* d9 p/ V4 m" O+ u try {/ W0 K! k+ k6 m* w8 a5 {7 }5 f
modelActions.createActionTo$message
6 y0 t4 y$ |3 {) Q/ D (heat, new Selector (heat.getClass (), "stepRule", false)); u6 J7 E& e' D# c4 ]
} catch (Exception e) {
$ w5 J7 x* r! f* s2 g$ E; d' Q( q System.err.println ("Exception stepRule: " + e.getMessage ());; L/ h2 s! n+ s2 j
}
& W1 L V9 e9 F4 ]: w* @, X
5 r( U- a5 w. J d1 w9 q try {, N- ]8 Q' b3 ^0 Z2 N
Heatbug proto = (Heatbug) heatbugList.get (0);
* g. Y% r. g" t2 o7 {4 H Selector sel =
0 u; `% u+ l: b8 i, y! B' n new Selector (proto.getClass (), "heatbugStep", false);
' N! J8 N0 N+ Y8 f actionForEach =
9 a; J3 ]$ n) h modelActions.createFActionForEachHomogeneous$call( @4 X! S2 b0 z/ ^
(heatbugList,- N2 w! @& p' u8 z, ]* k
new FCallImpl (this, proto, sel,: U; n0 t9 J* @" J+ _: k1 m: R
new FArgumentsImpl (this, sel)));9 I' [" g6 a/ n5 N
} catch (Exception e) {! ]! ^( j) U8 Y- g4 f' H6 s" s1 k
e.printStackTrace (System.err);) O# m$ u4 w2 p
}* A/ E, h2 k& O& }$ B: m7 }5 s1 [8 Q
8 T' w5 W# T4 c( {9 a2 `% {" k syncUpdateOrder ();2 B/ g$ T' X, U/ y- C' f
4 Y- A' c5 S* z: f6 @ O F try {
; @& r4 [" I: ^" q. _4 G. { modelActions.createActionTo$message
. ^" G2 F; C5 S/ s (heat, new Selector (heat.getClass (), "updateLattice", false));
% N! n7 R. v8 O% |2 j } catch (Exception e) {
) q% N6 h9 Y& c/ l7 _1 Z9 f0 g# s System.err.println("Exception updateLattice: " + e.getMessage ());- H. F+ V& \4 m, y( Y
}
% N2 _. n) W" [2 ^7 C8 W c! Z 1 ?4 F1 x3 j9 U# ^% s# b9 w
// Then we create a schedule that executes the+ J1 A: [4 Q1 ^7 }, P
// modelActions. modelActions is an ActionGroup, by itself it
. ?/ O% C/ t( N9 b4 } // has no notion of time. In order to have it executed in J5 q% K/ q. f4 M$ r, d4 K! x. Y
// time, we create a Schedule that says to use the; e2 s/ \, U& {( O+ t
// modelActions ActionGroup at particular times. This; k9 Y9 H5 \" a" S+ S: Y3 R
// schedule has a repeat interval of 1, it will loop every
. E3 h2 V+ `# q: n' E: j9 m // time step. The action is executed at time 0 relative to5 L0 M- D" U8 L; T! \5 Q
// the beginning of the loop.; D/ _; k& ?8 ^4 Q" Q- I
' p) v) N( X' |% n
// This is a simple schedule, with only one action that is7 c8 q. v ?0 |7 ?0 p7 j4 W6 z
// just repeated every time. See jmousetrap for more
% }9 }* ?) I9 {* l9 C- z, _ // complicated schedules.8 v' j# q/ ?( R+ Y. s3 j4 T2 u
( _8 F$ h2 w# v modelSchedule = new ScheduleImpl (getZone (), 1);0 B) U5 O7 N6 n% H+ S D8 M
modelSchedule.at$createAction (0, modelActions);
2 N$ y8 f, `% v+ I% c
2 i$ |0 D6 ^$ t# c' ~( m! B/ {* V return this;
8 L3 D" a8 D2 x/ k2 \3 e } |