HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& ~4 x! W1 n2 N3 |6 V0 @
# h0 Y/ _! M. L- [, m( k, G N public Object buildActions () {% L5 l1 K) ^+ \
super.buildActions();
+ ~+ ~2 e) r5 n9 Q % |0 N' p5 |. M9 H' V+ d' S7 v. T
// Create the list of simulation actions. We put these in2 m! D/ o6 w: t& b$ h9 @
// an action group, because we want these actions to be l2 V! G- ]$ u, G4 a- W; v# ?
// executed in a specific order, but these steps should
7 w& S) L) ]5 Y8 u0 z% z // take no (simulated) time. The M(foo) means "The message
" C! V: {2 w% L* l7 O$ ` // called <foo>". You can send a message To a particular
: d$ b8 e- w4 a$ u: p& J+ {. @9 r // object, or ForEach object in a collection.. L/ P% r& C/ f8 a& G
/ _) ?; t: b" ?+ j, Y: g. C, o
// Note we update the heatspace in two phases: first run' x; D9 i, p1 Q& x7 o2 X+ Q
// diffusion, then run "updateWorld" to actually enact the( O+ F( _8 E& |. f+ r& f! J
// changes the heatbugs have made. The ordering here is) }& S1 B9 q, G# X$ d( n
// significant!' p6 a$ Q% c1 D1 i6 ~3 L
6 l9 L0 o2 ?) Y8 X/ }7 X( B
// Note also, that with the additional
4 D" o$ w! ?# f$ k" W* o/ c2 g- T // `randomizeHeatbugUpdateOrder' Boolean flag we can* \, i! q, z6 |
// randomize the order in which the bugs actually run
% H. H$ G* t/ M" D // their step rule. This has the effect of removing any
0 }9 p# \; M. ]$ x7 C9 Z // systematic bias in the iteration throught the heatbug3 ]* t" m# ~. n& D
// list from timestep to timestep! e. ~7 B( }$ B' H; U: @9 V: J1 O& c! {
; f3 O* a* k0 W) Y: j+ { // By default, all `createActionForEach' modelActions have2 `! z! X; e7 E5 L& a3 e5 z
// a default order of `Sequential', which means that the
7 {$ |5 h9 x3 X4 u- m // order of iteration through the `heatbugList' will be
. G& t) \% u# a4 q K% f& s // identical (assuming the list order is not changed
" m: ]" H% n* \2 u( ^7 e/ s/ d // indirectly by some other process).
. N( i3 u, k9 P0 p
* V! y0 h6 ]/ \. { modelActions = new ActionGroupImpl (getZone ());& A" J6 t/ `* x
- p: K/ N" m8 w; D( U* i! I% _! g
try {/ E6 h# ~# J; v1 V5 z( [# u+ z' s
modelActions.createActionTo$message
# E* X# x1 S! s/ l5 y. E9 l, A (heat, new Selector (heat.getClass (), "stepRule", false)); r, b' k* n# [6 U
} catch (Exception e) {/ _9 Y+ { H" s. L5 M* M
System.err.println ("Exception stepRule: " + e.getMessage ());% `5 C0 {+ i, ^6 [
}
4 `% c, S3 N- s9 `4 m- f7 o. L- H: C: x$ {5 v: s
try {( d+ A3 p1 h( P p2 l0 ]( F) d
Heatbug proto = (Heatbug) heatbugList.get (0);
\( J+ L$ v' j* J8 ^& z5 i Selector sel =
0 d b9 R# x" ~( L new Selector (proto.getClass (), "heatbugStep", false);
' r' Q- R- O% D" Y* ]6 Y& w actionForEach =4 M% d* o+ [1 N6 @% v
modelActions.createFActionForEachHomogeneous$call3 F) Z0 Y: n7 w
(heatbugList,3 }! E+ U' L3 X$ `% `/ ]% B# ]
new FCallImpl (this, proto, sel,
$ w) U: V5 h8 C) V. P new FArgumentsImpl (this, sel)));: c# h5 i( X* U1 [, p
} catch (Exception e) {
4 {* |5 Q' N6 G1 Z; K% s e.printStackTrace (System.err);2 g# }: t; M/ t1 y4 M
}
. u/ D, X: y% r3 S
5 C$ r$ f1 y- H# O+ G$ s syncUpdateOrder ();6 Y6 y, A) W% H9 W
I& N+ _: F% q) X7 A8 r
try {7 S6 }# ^! C" D# p/ _2 ]6 v; k
modelActions.createActionTo$message
# J9 T ?) R& E% @% { (heat, new Selector (heat.getClass (), "updateLattice", false));3 s3 o; N: k: H- S2 o/ ?, s; p8 W
} catch (Exception e) {
& w% x& b3 K/ @# g1 b+ B5 Z System.err.println("Exception updateLattice: " + e.getMessage ());
: ~' n, x; E. ^" v3 Q4 e }) w( ~$ B5 x( V1 X* B( D3 r1 n/ {0 p7 ^
- Y4 p& u) F2 o6 P, G // Then we create a schedule that executes the
4 B$ J. C" S/ _; A. d' j: n# I% x // modelActions. modelActions is an ActionGroup, by itself it
. `7 e6 R* c& [4 F. n6 c m: o f% e4 X // has no notion of time. In order to have it executed in5 g* _+ T- t! `" M
// time, we create a Schedule that says to use the6 T; [1 z0 w( c
// modelActions ActionGroup at particular times. This
7 f1 p4 Q" T6 }1 i // schedule has a repeat interval of 1, it will loop every' \+ `6 u/ k0 L$ e$ |; s6 x* `
// time step. The action is executed at time 0 relative to" P; \8 `9 E4 Z7 k/ ]- r+ P
// the beginning of the loop.
( s9 \9 @ n: r3 O! ]. f2 P# o( I( @" \( O; Y% Y8 r' m
// This is a simple schedule, with only one action that is7 S! B0 ^, ?6 A$ N! V, o9 J
// just repeated every time. See jmousetrap for more" y. `8 E1 r) P' Q4 y$ Q
// complicated schedules.9 r' { q- o; `
( d3 R$ [1 X$ z( S# U modelSchedule = new ScheduleImpl (getZone (), 1);/ ]" T8 ~* ^* z; p2 h
modelSchedule.at$createAction (0, modelActions);: t. y) B4 u7 U5 V
; o' s! ?, I: k% V5 O1 d7 `( W return this;
& W& l4 h! Q8 P8 ]% K/ @ } |