HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
Z1 ~# S1 N6 u+ v' W
6 V& A: K" c1 D& p9 O/ q5 B public Object buildActions () {
Z$ W w6 `- N- Q8 o8 N super.buildActions();1 ]7 O2 X& v1 p& Y/ q; ~# P
9 U# N8 Q- x3 l/ j; K // Create the list of simulation actions. We put these in& {# o* {/ g$ f {# h
// an action group, because we want these actions to be: R! J( A: N9 M1 v9 ]( s) s7 ?
// executed in a specific order, but these steps should3 {- x) X+ T4 y2 E
// take no (simulated) time. The M(foo) means "The message# X+ H) j) ^8 Y' V- k! L
// called <foo>". You can send a message To a particular
! f, \& m. U( }# R, Y7 { // object, or ForEach object in a collection.* S1 G' K( u5 `3 w- v( ^/ B
, Y$ o2 j' J' m q! Q
// Note we update the heatspace in two phases: first run
) e. i5 K2 H6 ^. @' d! Y // diffusion, then run "updateWorld" to actually enact the
7 g3 @+ p/ @6 H# t U X // changes the heatbugs have made. The ordering here is8 e4 M/ W. G* |1 D* p: Q" t) n8 N
// significant!
. c, I, [1 ?/ Z& G! { ) c" o* i4 o( B+ w. k
// Note also, that with the additional
7 T" a h4 c# z7 N7 N" z* { // `randomizeHeatbugUpdateOrder' Boolean flag we can& H1 c6 z( q1 m, v
// randomize the order in which the bugs actually run
: P/ W. c; [; S/ N9 W5 o& P // their step rule. This has the effect of removing any) m5 D2 p$ l, h* ^
// systematic bias in the iteration throught the heatbug
( F3 C8 |, E' H // list from timestep to timestep& b+ `* y$ B, X
% \1 k% z7 s/ E3 z // By default, all `createActionForEach' modelActions have2 m2 S$ X; x/ p) N" N" f
// a default order of `Sequential', which means that the& [0 f' z, m5 W: X* y0 ~
// order of iteration through the `heatbugList' will be
# U! u. q/ H. |( h$ S: [ // identical (assuming the list order is not changed
0 V) ^; f2 b1 W% s+ ]/ d; ?2 o3 ^ // indirectly by some other process).
3 w6 Q: S' k- n3 n! g' {
0 k/ `; }, c% L modelActions = new ActionGroupImpl (getZone ());$ {* b3 q# y6 N: U! b$ [, _
7 W# A6 z. n+ L) e: ~
try {
( a* [( E. a+ ^% t" ? U$ g modelActions.createActionTo$message4 y8 E: h" l8 d+ @7 u
(heat, new Selector (heat.getClass (), "stepRule", false));
+ {% t1 S8 i, _- r$ ?) w: y, u6 s } catch (Exception e) {
& s2 K# x, W9 h5 \$ r& V System.err.println ("Exception stepRule: " + e.getMessage ());& c4 Q* {" y: a/ x$ m5 b
}
, `1 ]1 A+ v7 c0 R, M! ~* m( A, l7 a% G% U8 o
try {5 J" k% F, }4 ?7 O% [3 h4 }3 j" i
Heatbug proto = (Heatbug) heatbugList.get (0);
$ H) `# B+ |2 u( V Selector sel =
8 v- g8 ?$ K8 w; ~ new Selector (proto.getClass (), "heatbugStep", false);
6 v6 M0 D% q. L$ C actionForEach =
6 K+ H4 @/ A; b( e8 U modelActions.createFActionForEachHomogeneous$call6 K5 q) p8 I6 N8 c6 h
(heatbugList,
6 P, \, [1 g9 v* b4 c. n n new FCallImpl (this, proto, sel,/ c1 v/ X" V5 A
new FArgumentsImpl (this, sel)));2 |9 l' l- T" `( N- f& I2 q
} catch (Exception e) {
( `# O7 l# j' S5 H& y4 y e.printStackTrace (System.err);
1 U4 c6 S+ |2 A, L }) k( ]( u8 m; }( `
* I- d: S3 ^% _6 H8 j; X6 k1 I
syncUpdateOrder ();' [/ i8 V s- C# b: o: |
F1 |8 i% o7 P& q try {7 X3 I& L4 I {- y5 H& Q
modelActions.createActionTo$message * E0 k! _# p. A0 B- Q6 q
(heat, new Selector (heat.getClass (), "updateLattice", false));
* a4 I4 ^) ]& ]% t } catch (Exception e) {
; m6 A8 w/ k, e+ `1 [# N7 V' M, s System.err.println("Exception updateLattice: " + e.getMessage ());, z6 h/ P# G7 O0 @
} v) J& }! h$ s8 ~* ?5 s
- J) e$ ]/ X( G // Then we create a schedule that executes the
; \* w. Z/ {/ y9 \8 L. R$ Q // modelActions. modelActions is an ActionGroup, by itself it
. t4 X; _8 |$ u4 X6 Y3 b // has no notion of time. In order to have it executed in0 f$ c$ g- T6 v& m, [9 a ?0 F0 L
// time, we create a Schedule that says to use the
4 F1 T: T5 w l% \; \ // modelActions ActionGroup at particular times. This1 H4 O6 x; m' b5 x: V* Q
// schedule has a repeat interval of 1, it will loop every
6 L$ Y) ]6 Z9 A: p& e# U // time step. The action is executed at time 0 relative to
; Y: ]- c J9 T# N // the beginning of the loop.* } G9 G$ D$ ^/ ]# [
, s" S2 J- Y8 A7 D
// This is a simple schedule, with only one action that is: a" N) Z+ ]; }& F, h# v* Q
// just repeated every time. See jmousetrap for more
' K( j5 B) P* }+ M& }3 K' E. h1 E; j // complicated schedules." {6 U- e7 Z' u3 e X. N5 w' x; w
7 H* I( e2 e: c- ^
modelSchedule = new ScheduleImpl (getZone (), 1);
, o& k( A7 x O. L$ a modelSchedule.at$createAction (0, modelActions);
) H5 ~1 [4 y0 Q2 }; L $ X1 U: t9 B( Z3 H0 u
return this;
3 Q1 y$ g! R" a' G } |