HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 g. u" X: ?( F- |& C
6 H# {' c- _& W public Object buildActions () {% {$ P) f R& q! c' e
super.buildActions();$ p" g5 x; l3 k& T# i, Y' b4 f* u/ R& l' c
$ C6 z" k' {! a ^7 f9 Y( T3 K0 x
// Create the list of simulation actions. We put these in0 w$ F! A6 G6 S% F4 N
// an action group, because we want these actions to be& j1 I8 _3 [7 Q- e+ {
// executed in a specific order, but these steps should6 y; F! y& B1 x
// take no (simulated) time. The M(foo) means "The message# u" w& p6 y1 h. a- w
// called <foo>". You can send a message To a particular$ d" C8 O7 \- |1 B2 d8 Z
// object, or ForEach object in a collection.
+ M5 f; b! P: t 6 p7 g' f3 v9 `- k8 q
// Note we update the heatspace in two phases: first run$ w6 j% U- j7 a* Y Y$ q
// diffusion, then run "updateWorld" to actually enact the( B+ {) G% j5 C4 B5 M; }$ w
// changes the heatbugs have made. The ordering here is
J$ H& _0 j+ @3 v( p // significant!& G- l& E" j7 A4 S; y! a8 O1 B$ q
4 D D" o+ l7 s+ g; q
// Note also, that with the additional
_* l9 B- Q, k // `randomizeHeatbugUpdateOrder' Boolean flag we can0 i4 |' A& @7 i; ~+ ]
// randomize the order in which the bugs actually run0 K. ?' G% j$ O( T7 s- P
// their step rule. This has the effect of removing any
7 l( c5 i$ M$ Q; `- v8 h1 S // systematic bias in the iteration throught the heatbug
: T: y6 k; |6 C3 E' @$ s // list from timestep to timestep
( j/ a, Q, H/ O
/ P4 ]6 J" K$ R4 f // By default, all `createActionForEach' modelActions have2 q! ?; j7 V2 [2 |& N A
// a default order of `Sequential', which means that the5 u& i3 ~! \- |* p5 Z+ V
// order of iteration through the `heatbugList' will be
4 I8 F8 I2 j/ m6 Q2 T8 O1 }3 f // identical (assuming the list order is not changed
( g% x, h3 [5 f$ O$ I' W // indirectly by some other process).
8 f- i( g' J$ {3 b
! q' z3 Y( U' T5 |/ C modelActions = new ActionGroupImpl (getZone ());" I& s" e- F+ D4 T( o
% F3 P1 z% N$ U1 ]4 Y F0 \ try {
p' m! T. _! z# o* ~( W# @ modelActions.createActionTo$message& c/ c7 V0 G! O- A$ w. G
(heat, new Selector (heat.getClass (), "stepRule", false));
; j s: a" s' P# y0 x% f6 i } catch (Exception e) {
+ ]; j9 e0 Y' ~ \% l System.err.println ("Exception stepRule: " + e.getMessage ());
3 _' x0 m8 j) n' T" z$ a }& f( k* k: a6 r# }% ?" f0 d
1 J2 \ [, ~$ j: \0 C" { try {; I8 f1 V( g. H V% K( ] \# k3 e
Heatbug proto = (Heatbug) heatbugList.get (0);
( K, x$ r0 F' ~7 l$ [2 j Selector sel = # d( E. N2 v- E5 h6 H
new Selector (proto.getClass (), "heatbugStep", false);
3 v$ m$ L4 e$ ]( b u actionForEach =0 q9 C: `! w: |3 ?
modelActions.createFActionForEachHomogeneous$call
( b' ^1 Q7 f L( Y, t (heatbugList,+ y8 x: M0 i4 A% G# T; }
new FCallImpl (this, proto, sel,
' c7 L8 q+ ]7 ~% |. T% [- F" R3 Y new FArgumentsImpl (this, sel)));
, X$ f: h j( O+ K } catch (Exception e) {
2 @1 _+ g/ _4 s d7 I e.printStackTrace (System.err);
9 I1 N2 o& a/ v1 f; k: Z }
- x! o k) ?/ Z3 T
$ W7 |/ M7 u/ ~" j0 v syncUpdateOrder ();% O9 f1 i( h9 F: q4 `
- U3 d5 i, A8 @2 h5 F% K! k& O& ?
try {/ r9 q$ V" R) h- c0 C
modelActions.createActionTo$message
& k0 c0 y4 W5 z8 A (heat, new Selector (heat.getClass (), "updateLattice", false));! {( x% ?, B: B! L! c* `7 y3 H
} catch (Exception e) {* s; V1 w% l3 F1 h" E0 w
System.err.println("Exception updateLattice: " + e.getMessage ());
& \' q" K# D) }' B; Q* J' A$ h# j }+ l) p- L" ?: E ~8 a. n0 D
( s6 T9 r3 S" }6 E$ N
// Then we create a schedule that executes the
7 P* ]' Q% m# b4 a! X // modelActions. modelActions is an ActionGroup, by itself it. d; k/ Y5 N" G9 Y9 y
// has no notion of time. In order to have it executed in
. k2 h K% F% ?) {3 A ?% q // time, we create a Schedule that says to use the
4 M; E3 ~; a, _1 ~8 G+ a2 n // modelActions ActionGroup at particular times. This
. i9 q9 M% R$ ?7 S& r // schedule has a repeat interval of 1, it will loop every
- Q p; A( |& K/ A. W // time step. The action is executed at time 0 relative to
2 _ i& L. y- k t* Z/ e5 s. d // the beginning of the loop.
% |: c& s8 p$ B% U1 F8 h: t* P; T. y2 N% P/ O) `5 q4 z
// This is a simple schedule, with only one action that is! O( s1 f1 G& V
// just repeated every time. See jmousetrap for more
1 A9 J8 L1 d8 \% v/ ? // complicated schedules.+ F- b7 q/ \# X4 |/ D
6 l. @8 S+ @* d8 N g
modelSchedule = new ScheduleImpl (getZone (), 1);
; m) d; ^/ F5 c" y5 ] modelSchedule.at$createAction (0, modelActions);
3 {# E+ R' K0 O. x, w ( Z" N+ K$ C x1 | t' M
return this;
. O8 V* ?6 }" B# m r4 L } |