HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 N9 |2 P5 `- X, V" I, Y$ H* L9 E
4 I& w8 w ^8 b3 E5 m" n( x, k
public Object buildActions () { W. d0 X3 h- ^8 p2 }% N- r
super.buildActions();: \" }& Z0 q0 x' ~ d9 L0 c
9 S7 F. m4 h7 B; s, [ // Create the list of simulation actions. We put these in/ S% O. D: z& ?( y( n# ~. d: i: k
// an action group, because we want these actions to be# ~, X9 i4 U( d- {2 K
// executed in a specific order, but these steps should/ `9 Y6 e, ]3 {+ W4 x8 s
// take no (simulated) time. The M(foo) means "The message R' T+ {! d k& ]2 \. q
// called <foo>". You can send a message To a particular) i1 l( j# K. P6 t
// object, or ForEach object in a collection.# g# z' `3 G. O1 T# Q: c
2 @. A8 M* L% ]* k1 R$ J" e+ A, Q // Note we update the heatspace in two phases: first run; @& @1 D6 _$ f
// diffusion, then run "updateWorld" to actually enact the! j- w) l' E. D
// changes the heatbugs have made. The ordering here is5 V. q1 H# r/ u, o" s" I
// significant!
5 j- r& t9 g7 x @ ! g8 G D1 R5 x: x) O
// Note also, that with the additional
6 a6 |( Z0 Q, \' x p1 V // `randomizeHeatbugUpdateOrder' Boolean flag we can5 m, B# [! d) i# p
// randomize the order in which the bugs actually run j. Y* z" q6 U" W7 v
// their step rule. This has the effect of removing any$ P, J0 j5 W; U2 `6 v R$ e
// systematic bias in the iteration throught the heatbug
; r z F/ P- X- z# t$ O, a // list from timestep to timestep# L2 s6 {8 m# c' K/ q' P# y
$ b" p# v2 |. Z
// By default, all `createActionForEach' modelActions have
i# J3 x7 R& B+ m* e // a default order of `Sequential', which means that the) T% E) i7 @: ~; P; t8 g6 f
// order of iteration through the `heatbugList' will be) x+ B5 b3 G ^2 }* Z, L/ Y
// identical (assuming the list order is not changed
, e: _/ d6 J3 a. v1 H2 }" Z // indirectly by some other process).; l6 L' a9 o3 w! S, c
( S8 M+ Q c+ }0 Q& b. @6 [' S
modelActions = new ActionGroupImpl (getZone ());- n) {4 }( d B, J; L$ J# g
& v' c7 D/ I7 C6 S' T try {
2 K+ T( A9 k& ?/ {) g6 A! o modelActions.createActionTo$message9 I* R4 `8 r/ g
(heat, new Selector (heat.getClass (), "stepRule", false));- m5 t. }# r; Q0 k# I( E2 [0 j
} catch (Exception e) {
& n8 G1 m: u5 K( T# { System.err.println ("Exception stepRule: " + e.getMessage ());+ d8 I6 m& ^, t, j% a( {* J
}
9 i" b+ z$ i/ }; M. \* V, P& l, c0 {& F* ?8 X
try {7 X7 A& k; t1 z* c
Heatbug proto = (Heatbug) heatbugList.get (0);
4 j: z. M1 N/ P# @- E Selector sel = . M# P% O7 V" o U, _1 t
new Selector (proto.getClass (), "heatbugStep", false);4 W1 h+ U, z5 X, n3 p# n5 Z& e
actionForEach =
. ^" ~1 r* v5 P modelActions.createFActionForEachHomogeneous$call
, r8 z/ }5 r! z (heatbugList,
4 q0 P6 e9 L4 P% U. W7 Q; a1 l new FCallImpl (this, proto, sel," \- E k' P. A; _% U9 d
new FArgumentsImpl (this, sel)));9 G" W. S7 R! C- i
} catch (Exception e) {
. Z6 A. r7 M5 G; H3 ?0 F" K e.printStackTrace (System.err);
7 c6 E3 j7 C. X( G7 J2 e. t+ i& t }
' Z% c! ~ D2 D* d) D* f4 }1 M' G& l 5 x9 n% q, Q7 ~7 Q2 t
syncUpdateOrder ();
5 L" A$ d0 D! b$ L+ T: x4 v, L" X% Y4 I
try {
- w3 c4 D* B7 f% w$ S) G modelActions.createActionTo$message ( m4 o" a; Y- Q. C1 R
(heat, new Selector (heat.getClass (), "updateLattice", false));6 s; `3 z% W/ ]* R7 o
} catch (Exception e) {
+ R- U+ y6 y) @% a. n1 ]' E6 F System.err.println("Exception updateLattice: " + e.getMessage ());6 v- }2 m' {- K5 k+ Q4 x
}; L9 k3 e! y1 s5 k, ^ D% D0 v
( _( _. b$ I U( |$ r% [
// Then we create a schedule that executes the" k# u& I7 `/ Y0 Y9 Y2 n
// modelActions. modelActions is an ActionGroup, by itself it
6 F3 c* q. w+ g9 d" s% _ // has no notion of time. In order to have it executed in
; Q4 ?6 }/ _( x- f j! z0 G: n // time, we create a Schedule that says to use the! }) {5 R, ~) m$ L6 _: |% b
// modelActions ActionGroup at particular times. This5 ]1 E5 ?1 P# ^. n0 S
// schedule has a repeat interval of 1, it will loop every
% K: n9 C) N1 D( x0 E // time step. The action is executed at time 0 relative to
* j" C; }+ Q. o+ v // the beginning of the loop.
# ~& ?' l/ P5 H! T) i" }- E& V* k* i3 t8 v* z! A3 w
// This is a simple schedule, with only one action that is% q3 [, U5 f" K T4 Z; l
// just repeated every time. See jmousetrap for more% m/ K! M' x- x( u* ]- o3 D5 P
// complicated schedules.; t7 @3 |6 n; L& N' e2 e' }
; I) v$ a; M/ ^. g+ E7 Y: h modelSchedule = new ScheduleImpl (getZone (), 1);+ x, R1 p" Z9 L7 A R" ?0 w
modelSchedule.at$createAction (0, modelActions);
9 \0 v5 v) v3 v. Q
( q+ c3 m w, x- k# P2 h& U return this;, _/ K! D: {! p" s- ]. V% d5 e; C
} |