HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; U7 z- E+ o/ i6 I5 M5 e4 a( o8 ?2 Z2 W
public Object buildActions () {
T, @4 k5 x6 ]6 l" @2 U super.buildActions();5 s* z- |, f: f+ U- [& E( I0 M) L
$ m. o8 @% r8 E* A
// Create the list of simulation actions. We put these in" J# \* T& i/ d e6 H
// an action group, because we want these actions to be
- K7 Q% X m( V1 f+ b: [ // executed in a specific order, but these steps should
" M$ S2 z O: Q [9 M! u8 d; m6 W // take no (simulated) time. The M(foo) means "The message+ {" V8 q0 B3 Z% k% d0 Q4 V4 f) G8 e
// called <foo>". You can send a message To a particular, u0 U Y7 s; P4 G, Y: X# ]
// object, or ForEach object in a collection.! }2 F6 [5 o+ c( P+ r6 w1 Q
' |; e+ }5 J% X2 a2 J) N // Note we update the heatspace in two phases: first run; T* ~- o1 P* z
// diffusion, then run "updateWorld" to actually enact the
& B( n! I, W: |+ k, x // changes the heatbugs have made. The ordering here is
8 y4 q) m0 G0 \2 o1 B" j; Y // significant!
& M1 ^ S, c/ [: n: ~* s
6 X1 r! L0 {+ t1 C! i // Note also, that with the additional/ K& p1 k8 o8 ?4 Y5 [
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 I8 ~2 l3 O: D! L0 i9 D' u+ ?
// randomize the order in which the bugs actually run1 | K+ ]( h% V* n! G1 D& i
// their step rule. This has the effect of removing any3 C$ B/ V: E+ ]4 G3 O
// systematic bias in the iteration throught the heatbug
+ I3 ?, V4 }+ b0 d& R% U1 x r // list from timestep to timestep l1 ^* }$ M/ r: Z7 _. C
6 s6 V& q! Q" Y // By default, all `createActionForEach' modelActions have
0 ^3 X, P: X+ A# c/ { // a default order of `Sequential', which means that the7 a" ^8 J/ o6 v6 a- Q! s+ J
// order of iteration through the `heatbugList' will be
' U! i$ ?) t9 `' |* T% d // identical (assuming the list order is not changed
$ z4 W' k9 U* P9 Q" n // indirectly by some other process).
; P- Q* C5 O9 @0 C
/ r( U9 N3 Y8 M, h X3 A: k' [ modelActions = new ActionGroupImpl (getZone ());0 m. e6 U/ r, s! h) V* j9 b! ]# l& O! C
+ ?2 c. E w6 ^4 r# S
try {. |+ I8 C1 v4 W" n# X2 F) d, l
modelActions.createActionTo$message
/ _; ^* G; n1 Z (heat, new Selector (heat.getClass (), "stepRule", false));
# C `$ X2 ^; d# J% h! h" l } catch (Exception e) {
) V. \' Q( P* n Q: U System.err.println ("Exception stepRule: " + e.getMessage ());* J& k! s Y4 }
}! N# `2 s/ x0 ~2 L$ `' y9 e7 Y
3 C6 |( j9 T, a/ M; S& L2 `; c try {
( @' M. q" _# l1 h. b5 u( d Heatbug proto = (Heatbug) heatbugList.get (0);
: H5 z+ |9 \8 u$ h1 s Selector sel = - `- ?/ o2 V( [4 P; Y' H$ R
new Selector (proto.getClass (), "heatbugStep", false);
# | c0 ~3 m9 }! E: {" _$ ~: h actionForEach =
% e- y+ _- N& A modelActions.createFActionForEachHomogeneous$call) L- ^" j* h1 d: |8 ?! h- ~' t& T
(heatbugList,# U3 |9 W# Z# k8 P! H
new FCallImpl (this, proto, sel,
7 G1 G+ k8 o4 z# l new FArgumentsImpl (this, sel)));
4 Q% I$ C8 B! U! ~ } catch (Exception e) {3 ?. c1 H) s, p6 _
e.printStackTrace (System.err);
7 m( k# P# w' A& ^ }# z( c) @- p: v! F
2 P% v$ y" y( K syncUpdateOrder ();
6 U. F* D) n2 n& H
( S' a* j6 y' l/ e: P4 h try {
, o, Q4 }0 p6 v. G modelActions.createActionTo$message ' L+ q! `( w0 {5 S' Z
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 N c9 ]) p9 j } catch (Exception e) {# g4 K2 s5 S" ~7 g( F
System.err.println("Exception updateLattice: " + e.getMessage ());
. y- O: i7 M; F( q" }# \ }9 t C4 u# T6 B) K% C
" L& U+ F% R% l- F // Then we create a schedule that executes the/ H. b! @+ X$ n" T5 c
// modelActions. modelActions is an ActionGroup, by itself it9 A v( j0 N& D/ n( W- S2 h
// has no notion of time. In order to have it executed in8 X3 @3 k* u5 W4 u3 b; Z
// time, we create a Schedule that says to use the
% s1 l6 { S* I // modelActions ActionGroup at particular times. This
. W( z% z6 f# A N3 p* P; c H // schedule has a repeat interval of 1, it will loop every+ H, b1 f! N& Q0 l+ _
// time step. The action is executed at time 0 relative to
2 ]8 V2 L9 o* O! A! N // the beginning of the loop., {! Q. }; F+ w4 O3 J) P/ V
- Q2 B9 G: Y) [; d- S3 W
// This is a simple schedule, with only one action that is- b& C. q1 M$ r: s2 z% a$ M+ X$ ~
// just repeated every time. See jmousetrap for more
* i- W7 s5 `! n. S9 v" N // complicated schedules.* I" D2 g m6 }$ E+ x$ F6 l- i3 i, x
/ M8 ^4 h% k/ ^% W* t* l
modelSchedule = new ScheduleImpl (getZone (), 1);
) h4 T; _3 V2 K8 ^; j) S* \: B6 ?: h modelSchedule.at$createAction (0, modelActions);
, N3 n# n: w" d. R& P+ j: T8 x; E
# w& i9 M) C5 ~- } return this;
. M! w) n! \% ?8 W } |