HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( t6 R1 `$ S! `: s4 M. T
* L2 ~& X* a" i; k d public Object buildActions () {3 c# U5 g( R& ?( U; \/ L& N Y# J
super.buildActions();
- V# h& |- a' F; M: m
7 E! e; x+ Y4 T' f* v" \1 n // Create the list of simulation actions. We put these in
8 c" u. F8 C% j- U+ V+ c: ^ // an action group, because we want these actions to be" T+ t. j4 u) g; X
// executed in a specific order, but these steps should1 F: u4 B0 V5 K# L& f1 f5 u6 u! B
// take no (simulated) time. The M(foo) means "The message7 L/ H0 O0 b- e: X4 d' C
// called <foo>". You can send a message To a particular* B1 _# g0 w1 n1 K, f/ ^8 p
// object, or ForEach object in a collection.( L( c, u3 M0 }, w! D* |
5 N+ \6 w- s2 ]
// Note we update the heatspace in two phases: first run/ T D0 T) P- b: ^; E+ m4 t( |# `# W
// diffusion, then run "updateWorld" to actually enact the# J/ \# B/ m- b6 ]/ h
// changes the heatbugs have made. The ordering here is% ?* b+ W! M, v1 I
// significant!; \5 W0 D' e# O- I0 I4 G
0 S; ^4 G' j, ~: m+ P // Note also, that with the additional+ ~2 c6 `6 V3 R2 \8 I/ _
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. E7 F7 W; z( j" q$ [ _2 f; Z& U // randomize the order in which the bugs actually run
9 ]; _) X7 S, A* ~" n5 g! w // their step rule. This has the effect of removing any
/ e' A1 }2 E5 J3 ~+ l; _ // systematic bias in the iteration throught the heatbug
# X" i$ r9 w2 V7 P6 y // list from timestep to timestep
0 y' F8 V T$ ?$ E ?% J ! V/ i' a8 p0 z5 D8 d) j1 C
// By default, all `createActionForEach' modelActions have
! P Y1 D# N7 X3 C* G // a default order of `Sequential', which means that the
& s% a* b: I! G, { // order of iteration through the `heatbugList' will be. s4 v+ d- ~% @9 l5 U' X8 T! b
// identical (assuming the list order is not changed
# m0 i2 y- T0 } // indirectly by some other process).9 U# r0 C, z! H1 @
+ ?9 ]' \# z7 h! G5 P) Y
modelActions = new ActionGroupImpl (getZone ());
& x I; O1 l. m7 ~& |3 x) F8 w+ Q; O2 o8 `! n( q
try {
7 i" c; j6 k& x modelActions.createActionTo$message
+ {% i3 V3 j# `* x (heat, new Selector (heat.getClass (), "stepRule", false));- R* F1 _6 t7 P+ d/ h6 n1 A
} catch (Exception e) {
7 o* u( Q. S' { System.err.println ("Exception stepRule: " + e.getMessage ());
& J7 e& V8 q* g: Y7 G# | y }& L% E$ X2 r; p8 O+ h- p7 j
, ?8 J ^2 N6 d: i' o$ j try {2 a8 e- L" w; f
Heatbug proto = (Heatbug) heatbugList.get (0);; y% b( U o) k
Selector sel =
" \; Z. e" Q- i9 C, \ new Selector (proto.getClass (), "heatbugStep", false);3 l, i: d2 p, Q3 z, N8 b
actionForEach =2 b( M9 \5 W/ n
modelActions.createFActionForEachHomogeneous$call
& ?9 {; J9 {+ i* _" C5 {+ b (heatbugList,
0 E1 N% H5 F8 a) v% V new FCallImpl (this, proto, sel,0 J4 X5 w) `$ o3 r7 }/ g
new FArgumentsImpl (this, sel)));; p, ? [" R( z! [3 N! [) ^/ j" H' w: `( \
} catch (Exception e) {5 w; q3 C+ @3 X6 ~
e.printStackTrace (System.err);. u: c& N) ~! B* x
}
+ D Y' y# Z! Y2 S" e3 J
! x4 t; q( X+ V! s3 i0 F syncUpdateOrder ();
! p6 Q9 Q" A2 H& |, Y' {0 \3 `) f3 Q6 ~1 ~+ }- @, D
try {. f6 J: z! z2 d/ U
modelActions.createActionTo$message
1 |7 {! j6 F* n; i9 G (heat, new Selector (heat.getClass (), "updateLattice", false));
, k/ N+ B2 M7 o6 Q: R; B# K$ o- z } catch (Exception e) {% O/ D& ?7 C5 ~0 b4 V
System.err.println("Exception updateLattice: " + e.getMessage ());6 d3 Q: [8 R; j8 d# a. g- [
}7 A o5 j/ m+ |* D. G
1 d, }& [/ V4 Z# ?8 j
// Then we create a schedule that executes the
( A/ p/ j6 e$ N8 ?. y9 O6 J // modelActions. modelActions is an ActionGroup, by itself it6 e1 d9 x3 L8 {* g
// has no notion of time. In order to have it executed in
]* `; o( \) [ _6 ? // time, we create a Schedule that says to use the( r' z! |" x- {
// modelActions ActionGroup at particular times. This' X7 L) L+ Q- Q! }: U% C" o6 Y
// schedule has a repeat interval of 1, it will loop every
, g3 W+ ?3 s; P6 Y* U# P* Z // time step. The action is executed at time 0 relative to& f- ~7 l" ?" Y9 T P+ I+ {7 y
// the beginning of the loop.
- z' N% j7 s9 @$ a+ q; r# c# u* E( e
// This is a simple schedule, with only one action that is y% _/ N. P/ E- j1 R, @$ ^' K
// just repeated every time. See jmousetrap for more" n& e( ?. l* r9 y
// complicated schedules.
4 q0 k+ [$ ^; C+ j; j
( C; R" _* E8 n$ M/ w modelSchedule = new ScheduleImpl (getZone (), 1);
8 z' o7 `8 c0 ?) }1 Q# a) P modelSchedule.at$createAction (0, modelActions);8 L B( t+ t- Y* G
4 g: t) b& e Y1 G# S. ?* ~
return this;
3 X, y& h% N" @! J } |