HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- r5 ?# f2 B& j0 W w( f* p( P; y# ^% g+ l1 K9 [
public Object buildActions () {/ S8 J% D/ y7 |: Y1 Z( a* S
super.buildActions();) o2 P8 u) L- G& Y; u, ~4 r
/ A: G% N% ]% D8 M9 d) O // Create the list of simulation actions. We put these in7 w0 C1 V% t4 R
// an action group, because we want these actions to be
$ v) i. O; i' ?, t U // executed in a specific order, but these steps should
3 V, r* u/ Y0 x: j // take no (simulated) time. The M(foo) means "The message
. L+ o' H) O. f // called <foo>". You can send a message To a particular8 C% `; n \4 x* ~6 O0 J6 o
// object, or ForEach object in a collection.
3 V0 h" C0 n; d+ t W- w' O 1 h: i, m' y; q: Z2 Y( i6 A9 z) |
// Note we update the heatspace in two phases: first run$ s. l5 r/ q: \9 A
// diffusion, then run "updateWorld" to actually enact the
, T! H# W/ o2 n! U2 i9 w. h+ ?( R9 i // changes the heatbugs have made. The ordering here is1 r* r( \! e4 J- J2 C
// significant!( T: `, @6 T m
A+ v8 n) k7 [
// Note also, that with the additional
9 u( ]# m" n9 G8 b' K // `randomizeHeatbugUpdateOrder' Boolean flag we can8 G$ @3 g" R$ F* S% |+ \7 R% Y
// randomize the order in which the bugs actually run" M' Q9 N G: f
// their step rule. This has the effect of removing any
) V9 Z# ^ ~: g& K' r // systematic bias in the iteration throught the heatbug7 b' ]! P! q% K# r7 C& ~2 D S
// list from timestep to timestep1 ]/ N2 m9 p( X( p; O
- z' t* b; Q4 s8 r1 @' h
// By default, all `createActionForEach' modelActions have- o% Z6 `* |1 w# r, P o+ o6 ?
// a default order of `Sequential', which means that the8 @/ ~& m p* S2 b5 B3 i
// order of iteration through the `heatbugList' will be5 F0 f! @5 \3 K. g! k3 E
// identical (assuming the list order is not changed: x* _4 E5 Y% y4 R' @1 ]" k! T5 O
// indirectly by some other process).
! X3 U( p8 M; O2 b. A
4 R) }6 E4 U! E$ } modelActions = new ActionGroupImpl (getZone ());7 m1 ?- v# j/ b5 q; Y$ y1 @
4 r6 R8 p" U6 S! S2 t try {# P, m. J* D: `, Q
modelActions.createActionTo$message
' r2 X; H+ B4 y* c0 C (heat, new Selector (heat.getClass (), "stepRule", false));
; m% e; n4 i! K; i o } catch (Exception e) {# G1 p. E+ f# W5 W& |% x
System.err.println ("Exception stepRule: " + e.getMessage ());: X6 q9 p- i+ E! @! Z! Q4 ?, p( P) d
}
/ b# b- e' M0 y9 [. M' U" w8 W) o
try {
; C0 ~" s g& f7 k4 g0 |5 e+ T Heatbug proto = (Heatbug) heatbugList.get (0);
- n' P9 j8 L8 C+ g% j, {0 e Selector sel = + g4 ]/ y z! M* d: w% G, q% E
new Selector (proto.getClass (), "heatbugStep", false);8 m$ x* {# C% b! ~' l
actionForEach =4 r9 x" F; G2 r. U$ i! t$ A5 N& l
modelActions.createFActionForEachHomogeneous$call, g! M8 h3 D7 y" s
(heatbugList,
2 o$ I; h* s1 q0 D5 y* x new FCallImpl (this, proto, sel,# G9 }% A/ f) \# O# p" a" `
new FArgumentsImpl (this, sel)));$ s3 Y; e5 W: D
} catch (Exception e) {
9 H2 Y: o: n3 X# `% P: M e.printStackTrace (System.err);% r2 @- g0 s$ ~/ e
}$ I. w% N+ N3 a+ b2 H) J
. l# x2 i7 L, s$ A/ [% M9 Y
syncUpdateOrder ();
O# ?/ N4 U+ M* m
$ w; h6 M8 P+ F try {
% z3 {- ?! P5 N, { modelActions.createActionTo$message
/ M% o/ k% C+ j. h- g3 D (heat, new Selector (heat.getClass (), "updateLattice", false));
+ P/ q: ?" n3 u: @# r! o& U' g } catch (Exception e) { `$ I% U! m) L. [( |
System.err.println("Exception updateLattice: " + e.getMessage ());
- t3 C8 D, D9 C9 t& y }
( d/ P# m5 n N& `7 r
& N8 Y( L# P4 z // Then we create a schedule that executes the
! ^& Q n- G$ l! \ ^ // modelActions. modelActions is an ActionGroup, by itself it) i8 m+ w9 } h
// has no notion of time. In order to have it executed in
3 v& l. T! T5 {. R9 n/ U! i // time, we create a Schedule that says to use the- b; q' x" F! s
// modelActions ActionGroup at particular times. This
2 C7 Y' U3 l! I3 ?& w' ~# G) \ // schedule has a repeat interval of 1, it will loop every V$ q; _' g& o* ]
// time step. The action is executed at time 0 relative to
# |( E: z9 b/ U0 F3 r& H$ t' f // the beginning of the loop.
- f, G3 v9 G: B, Y
; Y0 u, Y" L. D' ~ // This is a simple schedule, with only one action that is P( H1 s, t" Y/ d& \; k6 c
// just repeated every time. See jmousetrap for more
% ~# b+ |% N3 f // complicated schedules.
& P5 e" D; P1 | W% a
. p, G/ _- L" ` modelSchedule = new ScheduleImpl (getZone (), 1);
4 {/ k/ M0 y4 D) p4 X) N) ? modelSchedule.at$createAction (0, modelActions);
1 U: B4 V" X- W$ c$ |4 f+ p % c. ]1 n+ d! ^# F3 O* R9 c4 m
return this;
+ N f0 ~$ \7 Q* D& L2 p8 | } |