HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 o0 r; k6 Q# J2 M- @# ]& \! H' o: D `: B/ U
public Object buildActions () {
( x4 t% d. U3 d8 g super.buildActions();% S8 i$ e8 r4 V
: V' q }& O4 t2 u
// Create the list of simulation actions. We put these in3 M& j4 _- r/ ?7 i
// an action group, because we want these actions to be) _ J# [7 X( ~1 }: h
// executed in a specific order, but these steps should; Z, i# t# R5 k0 ?3 |& n
// take no (simulated) time. The M(foo) means "The message
& \" M; p2 m) A, O6 d7 p& j // called <foo>". You can send a message To a particular
: |: p6 b2 V% w7 |& \9 `& V! c // object, or ForEach object in a collection.
6 H! x' N! s6 I, ~" w
- \+ ~$ R' T! a( _/ g // Note we update the heatspace in two phases: first run
$ v8 K( a$ S7 X8 F: L8 C // diffusion, then run "updateWorld" to actually enact the
7 I1 ~' F; s' {; n; Z // changes the heatbugs have made. The ordering here is2 w! X1 @# A& c
// significant!# f5 Y0 P2 P- ]# ?2 e
1 Z! T2 \- q9 ?5 |9 v // Note also, that with the additional
7 d& w5 L7 z* Y/ v& f" I // `randomizeHeatbugUpdateOrder' Boolean flag we can3 b" O& `% e4 j
// randomize the order in which the bugs actually run
" n5 m! G3 e2 P6 k/ w // their step rule. This has the effect of removing any: ?9 p9 ?( M* L9 ^6 b& v
// systematic bias in the iteration throught the heatbug0 G! ]2 G5 D1 y0 ^" n+ T; x
// list from timestep to timestep! \; K5 s# t; P; z8 R" k
$ k+ N) f1 P5 a, h; U // By default, all `createActionForEach' modelActions have
9 _1 o, M0 H* ~! @" ~ // a default order of `Sequential', which means that the
' {/ T& T) q+ [! @ // order of iteration through the `heatbugList' will be
/ {! r# C' @0 H3 m // identical (assuming the list order is not changed- t/ O4 d" z; J: {. m. C$ i
// indirectly by some other process).
2 e" ^# Z9 `% Q6 T9 J% P" H
# ^! \! e9 C# x9 V modelActions = new ActionGroupImpl (getZone ());1 k) B# L! l( L3 p
/ Y) x. k. p) D3 l% z K8 i! w try {
! b3 \3 Y; t3 E& O' m$ M+ V modelActions.createActionTo$message# ^9 V2 ^. W* i0 F. D" Q
(heat, new Selector (heat.getClass (), "stepRule", false));. l# l, K8 g6 O8 r! @
} catch (Exception e) {
9 d3 R. r4 k' E1 o; H System.err.println ("Exception stepRule: " + e.getMessage ());& I2 n( p9 Q4 O1 b! d9 E
}9 q: k" a2 \1 D% E! k3 P
4 M, u/ U+ C/ f6 Z2 L$ t7 b try {
# ]$ g, A9 B- ]" Q" ?; b Heatbug proto = (Heatbug) heatbugList.get (0); e/ m/ y- u5 o) c
Selector sel =
/ V! x+ O% {1 Y new Selector (proto.getClass (), "heatbugStep", false);
: B& @' u6 M( l, ]. w actionForEach =
' @. o# L( H% {, S5 V) j modelActions.createFActionForEachHomogeneous$call g% q% M8 k& l( Q/ Q
(heatbugList,5 r0 y" Y- k( E4 e0 @
new FCallImpl (this, proto, sel,
/ u8 R0 J y5 _0 @8 v" o new FArgumentsImpl (this, sel)));: a: L( A/ O% y1 C0 y* j
} catch (Exception e) {
% X- X# X; ^/ F" r e.printStackTrace (System.err);
( L5 T+ d3 E8 t }
! T8 H& j6 g, M0 O " a9 d& i. @ F* r* ?$ ]: `
syncUpdateOrder ();
$ |6 d$ \" o1 V, Z! u/ `1 Y |3 G3 C- P5 G2 ?7 A% w) c: _- k8 i
try {+ [1 G9 ?& y0 i: h
modelActions.createActionTo$message
+ @8 x$ y( ]+ D/ ?! P. @; H- | (heat, new Selector (heat.getClass (), "updateLattice", false));/ u! X/ l6 m5 c4 f( b& I3 a2 _4 C# [- h" r
} catch (Exception e) {
6 \/ R1 [4 Z( V, R* v$ V' Q System.err.println("Exception updateLattice: " + e.getMessage ());/ I. @0 x+ ]+ r( v0 C, c/ W) w9 ]
}0 J6 D; R; t3 K* ^6 ~
* a* s$ X7 O- Y1 S // Then we create a schedule that executes the& V% p& W! M# w0 S4 A) m* ~
// modelActions. modelActions is an ActionGroup, by itself it
6 @" _7 g- E) @1 A* O' m // has no notion of time. In order to have it executed in3 B* \1 `9 d3 R, C' K* Y9 j
// time, we create a Schedule that says to use the& N7 l9 y: \0 W9 Y" t
// modelActions ActionGroup at particular times. This9 @0 u0 t( e8 X7 h( k. p. `
// schedule has a repeat interval of 1, it will loop every
$ f, h/ B& I: G2 e: T // time step. The action is executed at time 0 relative to
9 |0 x0 G: P1 H2 v- w3 q // the beginning of the loop.# }6 C! D+ [3 f* t, Z
5 j) _$ V( o( y% o // This is a simple schedule, with only one action that is+ G% A+ u; @% Y/ L. r
// just repeated every time. See jmousetrap for more" I: K+ Y( A2 ^6 R" Q" t
// complicated schedules.7 K( G$ I% N+ X5 L8 q* b; [
8 L5 h3 _- @3 I1 Y
modelSchedule = new ScheduleImpl (getZone (), 1);6 O8 F3 O: u3 m/ k
modelSchedule.at$createAction (0, modelActions);2 c0 S' B" F, k) o/ T& b2 L
7 C6 o' i. H, T" k- b. } return this;
. y. A9 }# d& ?9 w3 |$ u% a } |