HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# O* {0 Y$ O2 ^: p1 \' z. L, C
+ x" m* l% C2 T: @8 d6 y2 H+ B public Object buildActions () {: U- o* f. ?9 X6 q% `
super.buildActions();
- _; C: n+ E- @/ I3 H$ N 1 d3 ^3 O" F9 o. E0 ?) |% o9 H
// Create the list of simulation actions. We put these in( O" l6 z1 Z k9 t' Q- t
// an action group, because we want these actions to be( S5 q6 F6 z: k6 ~# Z
// executed in a specific order, but these steps should
% A: y8 Z# r; |4 k0 ~" `0 Z // take no (simulated) time. The M(foo) means "The message
" K0 d+ ]; p! x) Y: ~7 r0 w1 A+ ] // called <foo>". You can send a message To a particular
: P- m* c# b* E // object, or ForEach object in a collection.0 y7 l+ W9 K# {2 u7 \
. W% A7 ^" P6 D // Note we update the heatspace in two phases: first run7 F; y+ y: Z! j5 o
// diffusion, then run "updateWorld" to actually enact the
* M# s9 s" W& h // changes the heatbugs have made. The ordering here is
- ]. Z3 h. G% f3 G // significant!
) U9 s1 p. X' A1 `, M% j; z# n * J' ] `3 }) A8 ~2 [5 }4 K% I. x& i
// Note also, that with the additional
0 ^. m$ A! N5 U8 b // `randomizeHeatbugUpdateOrder' Boolean flag we can
! y+ w" j4 q% D, t; l% D2 a // randomize the order in which the bugs actually run% }, L/ U+ ]! Z* u
// their step rule. This has the effect of removing any* j# Z& L4 i' A$ L
// systematic bias in the iteration throught the heatbug3 l$ t, x% }& o: S2 w% w! _
// list from timestep to timestep
5 a* j8 U2 ?3 I9 H7 D
' b' _, r7 B+ [7 V+ A, J // By default, all `createActionForEach' modelActions have0 _# i4 D0 ]. L! \8 \/ H( ?
// a default order of `Sequential', which means that the" i4 w# L7 J9 M9 I# A
// order of iteration through the `heatbugList' will be5 o' l5 M- g. @5 n2 a- N$ Y
// identical (assuming the list order is not changed
}! v9 p: C. |7 C8 Z, B% b+ I // indirectly by some other process).$ N2 R# C" E5 T0 p
- T8 _6 X0 b" s% L8 Y) [
modelActions = new ActionGroupImpl (getZone ());( ~& w$ Q% ~+ M/ Q s+ p$ E" Y, y
7 D' k2 @& ~: j" t7 m' r- G5 q
try {
' A9 ~' Q2 R9 w, W" G modelActions.createActionTo$message$ @4 [7 p8 H5 G. b" P6 T
(heat, new Selector (heat.getClass (), "stepRule", false));
+ q/ O/ E) E, a) L4 ~, r! P } catch (Exception e) {
2 p) p8 k: U) j; l, }' }; A System.err.println ("Exception stepRule: " + e.getMessage ());
6 w: @& p) S% u! [, B6 ^$ } }
/ h1 Q n8 S; y2 ?5 \& W3 Z& n3 S8 J$ {3 L, b$ w6 f+ r- q4 h" _
try {' M/ `) _5 M. G* J$ u/ U; ?
Heatbug proto = (Heatbug) heatbugList.get (0);% [7 l4 b7 I k4 k8 I" a
Selector sel =
: d0 X) @, K8 y3 U new Selector (proto.getClass (), "heatbugStep", false);
1 o6 n, s" t: H actionForEach =
$ U' ?$ E6 x9 ^4 _ modelActions.createFActionForEachHomogeneous$call
$ D. Z( q2 { F3 q2 g4 X (heatbugList,$ T7 p: K' @! }% F
new FCallImpl (this, proto, sel,
6 `- x, i! e" W b) o new FArgumentsImpl (this, sel)));3 W9 ?% f/ |& w% ~. ^' W0 f( U
} catch (Exception e) {
B+ F' p1 Z3 B8 d e.printStackTrace (System.err);
6 O1 d/ |6 } n5 e# a' D }7 o6 D7 X6 T) a5 E! Q# @
% ^+ G( m% S% c; F syncUpdateOrder ();8 c R) K& @6 n3 C2 m! P% T
' T' }: l& r$ P9 t- @' }$ k4 W try {2 @7 M( e9 w3 R# F3 A6 D' B7 @8 e
modelActions.createActionTo$message
& r$ l/ H& N; A! r& n- e& R (heat, new Selector (heat.getClass (), "updateLattice", false));
; w: ^4 H# x( R6 ?& V% \# { } catch (Exception e) {
7 I6 O% r. D f) U! ~ System.err.println("Exception updateLattice: " + e.getMessage ());' p/ B: S: b- U
}9 `4 S! Y: u' Q1 C5 Z* m3 D& ?
$ N) x3 p( i2 h$ N) I // Then we create a schedule that executes the7 |" k# X4 g8 c! `; `2 V" [
// modelActions. modelActions is an ActionGroup, by itself it* h2 |, t: R+ B
// has no notion of time. In order to have it executed in, G' i9 `. p0 n9 H; D
// time, we create a Schedule that says to use the$ h* p7 W1 {. j |1 A, Y2 i
// modelActions ActionGroup at particular times. This
* H' L8 g8 w+ R8 b; R5 ` // schedule has a repeat interval of 1, it will loop every
2 H, o |0 u! r3 L' G // time step. The action is executed at time 0 relative to0 K( q# G' ?2 \6 ?1 e; N" r
// the beginning of the loop.
' A K d3 h* j/ w" \8 @% H% [: V. d
0 f: S, A# \' }5 T3 h+ c' Z" c: H, |9 ` // This is a simple schedule, with only one action that is
0 f+ J& X c) u X5 ^ // just repeated every time. See jmousetrap for more
/ y" }4 c8 _! {8 x. e9 ?$ h // complicated schedules.6 P7 v( x$ A; G4 T
! I ?/ L; Q9 v* k6 P, {. S modelSchedule = new ScheduleImpl (getZone (), 1);
& {$ R$ A. Y9 U7 K/ n modelSchedule.at$createAction (0, modelActions);0 p* _! E( Q. t' H
, A4 o; X* L- i7 X
return this;
9 ~ q( Z/ M e' s" m } |