HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 A' n3 W" l& ? A: m9 [4 ~8 @. {8 ?) Q, r: d# @
public Object buildActions () {" r% p* k0 O4 `5 L( h% I
super.buildActions();6 ]% I$ t% a1 P' R1 T6 z& ^
! S! G) P. K% B // Create the list of simulation actions. We put these in
( b& F3 N8 w7 i // an action group, because we want these actions to be& {& j* \ N& x1 K
// executed in a specific order, but these steps should
% T D. R: f. O' g( U F0 U // take no (simulated) time. The M(foo) means "The message
5 K8 l" y0 e1 |5 g // called <foo>". You can send a message To a particular
& V2 ]2 e2 M; o7 \7 T! r. Q5 \ // object, or ForEach object in a collection.* H/ o& s- f/ X1 f9 E2 _. m
7 T' e2 Z* C3 M // Note we update the heatspace in two phases: first run
4 F. {# H0 Q; g( u- Z9 ]1 t7 j // diffusion, then run "updateWorld" to actually enact the) Y1 o( N! t }0 Y
// changes the heatbugs have made. The ordering here is
# o. H" s1 j: O& v // significant!, R* }/ o8 S" k+ |9 j
8 Q$ e# Y) {, b9 B0 f
// Note also, that with the additional
! @8 B4 V) m- O6 u // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 n6 P' {1 o7 x1 e* X // randomize the order in which the bugs actually run+ k0 c" u; E( i- f- ~2 M3 O
// their step rule. This has the effect of removing any' {3 n: i: F$ M( B# U' n" O
// systematic bias in the iteration throught the heatbug
: ^$ {' s: {1 c5 j- m0 j/ m // list from timestep to timestep
) A& ]5 I6 v$ w* g
' g' B% ^* T/ S7 y, `4 r // By default, all `createActionForEach' modelActions have, M ~# i1 Z3 [. I
// a default order of `Sequential', which means that the. L/ A$ h* V# o7 S
// order of iteration through the `heatbugList' will be& ]4 R8 {* e! I; V/ ?) T D
// identical (assuming the list order is not changed0 w1 T5 g- S# {0 f( \
// indirectly by some other process).
5 z1 T2 l t; P
8 u' @! C( k6 S- L- Y modelActions = new ActionGroupImpl (getZone ());& e% C# x0 h; j+ z9 p
' C3 z5 _' s5 [ try {* X- l3 A v- ]; I- }; @% `
modelActions.createActionTo$message
/ c2 o; p9 a# N8 Y5 V (heat, new Selector (heat.getClass (), "stepRule", false));
" ^8 r$ E* X% ^8 s, j: M } catch (Exception e) {
1 Q& F/ u$ k% ?3 T3 s2 \$ S. ] System.err.println ("Exception stepRule: " + e.getMessage ());0 R) t8 ]! ^, D# E7 l6 A
}
1 D( A8 s0 L) n" Q( |# E- Q3 _2 w0 T0 R( m3 D* F
try {
7 W6 |3 A8 J7 [% @6 b y Heatbug proto = (Heatbug) heatbugList.get (0);
1 y) ^" `" b4 X' \$ x/ \* B( Z Selector sel =
& w% A3 O# i0 K# H new Selector (proto.getClass (), "heatbugStep", false);
0 t# a# b8 r$ L8 z4 P actionForEach =# \! _/ x, T% N. Q4 q
modelActions.createFActionForEachHomogeneous$call
$ j7 C- ~/ B+ H7 q% D2 O A6 L" @ (heatbugList,
; e; r6 F9 Q4 F9 t6 U new FCallImpl (this, proto, sel,* ~; A. {, }+ Q8 z7 w) p
new FArgumentsImpl (this, sel)));6 S8 P( [, Y9 P6 n/ ~( q
} catch (Exception e) {
4 c, c& v4 T9 \# B$ U- G, ~ G e.printStackTrace (System.err);2 F4 T9 _/ @: M7 e9 W/ ]3 v
}/ d6 f& Q7 q; E$ o3 x. w; S$ E
0 t) h: w; s' E$ @0 z8 G- _% I syncUpdateOrder ();( T5 N% s& U: q" h3 Z& ] L% k
9 N% E. k. q- d- N* g! } try {. F) a8 N5 u5 }# r
modelActions.createActionTo$message ( U. b& c* U* v# j& l
(heat, new Selector (heat.getClass (), "updateLattice", false));) g8 p2 p) X% l9 c; I9 L; T6 y
} catch (Exception e) {
& ]8 g) ]1 m5 g) F& O: w System.err.println("Exception updateLattice: " + e.getMessage ());
" O; C6 h% |7 j: g% g+ e# Z }" h* Y# ~# h) G4 L) o
, }* ^# Z I* j# _- G7 A // Then we create a schedule that executes the
- `: j9 x* W' e; O& Y6 R // modelActions. modelActions is an ActionGroup, by itself it( N: a! q+ ]8 p3 E$ Q% Z' t4 m
// has no notion of time. In order to have it executed in) I7 s' y0 L7 N* `
// time, we create a Schedule that says to use the
! t7 J$ K& Y: F# Z# x // modelActions ActionGroup at particular times. This2 e. r; c2 F8 W, K0 n
// schedule has a repeat interval of 1, it will loop every
6 V) d3 U# k# a& \7 X // time step. The action is executed at time 0 relative to
+ ~8 P, t q0 Q // the beginning of the loop.) y. C1 {+ L$ T* D( E; I3 p
d% J( |! G6 S5 Y( b // This is a simple schedule, with only one action that is
+ C& P4 n3 }3 d, o$ g // just repeated every time. See jmousetrap for more
! ?3 i5 p+ ~9 W# `9 R- I0 U5 J5 X // complicated schedules.
' V* L4 D. T" G& i$ r. u C/ C 3 x5 q; O3 x1 G1 q: H
modelSchedule = new ScheduleImpl (getZone (), 1);& D" O0 n+ |3 x5 l6 g) ?
modelSchedule.at$createAction (0, modelActions);
4 i# ?0 u; _- T , {* t! a- A5 U8 A- F
return this;7 p3 ]+ e \ U" p& x/ s
} |