HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, g' v$ `- Q5 U1 C& X1 _8 N9 U
4 `3 Q/ |2 \! A9 F% k& k+ N. D public Object buildActions () {& q5 A5 j+ S! I( k
super.buildActions();. ]' P) a0 x( x: [
: F. |% V5 ^) s! R. U // Create the list of simulation actions. We put these in/ _9 @% {, T. [
// an action group, because we want these actions to be, e# a3 N- @+ o3 U8 }
// executed in a specific order, but these steps should
* z) J+ [3 f+ d& r! J7 a- p% `% O; g$ [ // take no (simulated) time. The M(foo) means "The message
, N) N) H, t6 B5 ^1 {8 B8 U // called <foo>". You can send a message To a particular
* Y& t2 n) L" d* S) j // object, or ForEach object in a collection. A! h; ~& ~* K2 W1 ~! b7 j
7 A& Q* S: T. u // Note we update the heatspace in two phases: first run4 a8 e7 O, ?8 a7 m: k+ j2 g. P
// diffusion, then run "updateWorld" to actually enact the, J6 E7 n+ R& t6 H% Z% ?0 U# `
// changes the heatbugs have made. The ordering here is3 |9 D/ c4 {9 D h, [ k, |8 c
// significant!0 x. k: o- z- `0 j/ {
' D h, N& K7 A8 ~ { // Note also, that with the additional
6 V6 b0 ?5 {2 F6 @ // `randomizeHeatbugUpdateOrder' Boolean flag we can0 ^4 f0 a" u- ~* V4 U6 i9 {9 {
// randomize the order in which the bugs actually run, [9 m" Q" z' }% D# l
// their step rule. This has the effect of removing any
' q% V8 O5 h* [: o/ G# T9 T2 u // systematic bias in the iteration throught the heatbug% n/ s1 N0 f: Z: | ~
// list from timestep to timestep ~4 D% D( G: v* ?! n- ?" J
, d' e( O i4 D+ C' b0 Y: \ // By default, all `createActionForEach' modelActions have( }$ B, G" R. L; f
// a default order of `Sequential', which means that the
$ {- C% S/ S" N' s* J4 N1 @% w // order of iteration through the `heatbugList' will be
5 d$ [3 A! n8 [2 Z // identical (assuming the list order is not changed. f ]: P+ g3 l1 s0 P. j+ q
// indirectly by some other process).
! R6 s+ r0 U4 A8 S % P( I1 I, D( |+ ?
modelActions = new ActionGroupImpl (getZone ());9 W5 c' ~& H8 Z2 ]8 C: m4 Y" |
0 C! C% d! c" N' f2 M try {
# P. x$ }$ G. c modelActions.createActionTo$message
+ _0 ^, `6 `* X! ^/ l (heat, new Selector (heat.getClass (), "stepRule", false));
- ^; `/ h3 z! I } catch (Exception e) {0 o5 O2 z" F" {
System.err.println ("Exception stepRule: " + e.getMessage ());1 y# n% b! D5 J1 ^( g! Z) g
}/ r) T! T V: D$ Y9 c5 O# N6 g
' d, l6 }8 E4 v
try {
& B2 U- o5 f: H Heatbug proto = (Heatbug) heatbugList.get (0);' _7 k1 x' W$ Q: p+ k" _! _% @, w
Selector sel = 1 K0 f+ v" x5 q9 Z/ p- s6 d" c: B: v
new Selector (proto.getClass (), "heatbugStep", false);
8 u) `- m! t$ L5 I' i5 c0 c1 n actionForEach =* h, d8 K7 p9 {# l. r8 C4 y' l
modelActions.createFActionForEachHomogeneous$call9 Y; |; M. s5 L5 z3 k
(heatbugList,
4 W0 E/ Z8 y2 q new FCallImpl (this, proto, sel,5 t1 R) `; N% V8 p: J
new FArgumentsImpl (this, sel)));
' Z" T- P4 }" l& o1 X) A) k } catch (Exception e) {/ I, q/ @0 H. G G4 l
e.printStackTrace (System.err);0 n3 @4 L( ^% z( ?( d' O' y
}4 o! V, f4 k- M& w) c
?3 W) a4 @- ^0 I+ X/ }! \+ p1 q
syncUpdateOrder ();
: h5 b& G+ Z& e/ V0 e* R2 H
M, ~$ E/ n# t2 H try {
9 t, N. `4 i$ c# R1 k! L: v modelActions.createActionTo$message
% k, F9 w2 D6 o" u4 z (heat, new Selector (heat.getClass (), "updateLattice", false));
* }4 `7 e8 }# m+ V } catch (Exception e) {
6 m2 H1 e. G3 `$ E3 D System.err.println("Exception updateLattice: " + e.getMessage ());
$ q: c0 r% c& [ }
; I0 j: f; p! p
) ~# t" ]5 }+ {, [- m7 ` // Then we create a schedule that executes the* T- C- Y- z' p+ s& S: |6 U4 p3 Y a% N
// modelActions. modelActions is an ActionGroup, by itself it! I( r' _8 n9 X+ W ]" S# w
// has no notion of time. In order to have it executed in
/ b% g% U5 L/ {' R$ ]9 n // time, we create a Schedule that says to use the: @; ]# a( c5 |- @) j1 f$ i
// modelActions ActionGroup at particular times. This
8 | e- Z! S1 `5 L9 ^ // schedule has a repeat interval of 1, it will loop every
# @- k! Y* S6 L+ k' b3 F // time step. The action is executed at time 0 relative to
7 N, n* e! j0 Y4 b) _, X // the beginning of the loop. S4 X1 @ ~% j8 R3 g, h. y
2 Q( b" C/ J* o% p2 ]" I // This is a simple schedule, with only one action that is
( b5 @& T. j5 G- G. M // just repeated every time. See jmousetrap for more
4 \$ [& u/ q. p3 F: W. g+ p // complicated schedules.
6 B" h- k v/ D+ F' V
! R) f% w: _2 g' Z) \5 u& x( z modelSchedule = new ScheduleImpl (getZone (), 1); `0 ]/ ^3 w$ m4 f9 F
modelSchedule.at$createAction (0, modelActions);; c w& [' G, Z: i9 a
' r3 S2 T4 l& k; C9 V
return this;4 J1 G& ]5 W' R" h+ H* s
} |