HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" n. R# P4 d+ S6 m, m
3 ^; D& c e. { {0 [8 s; l
public Object buildActions () {
4 {. N8 X& ~: p! V& ^/ V$ m super.buildActions();4 k: `$ v9 B$ h* b7 L5 d9 _
% o; M$ J- F' w3 J: o+ `) F
// Create the list of simulation actions. We put these in
2 x, [0 s: Z8 \. j- h' g1 m // an action group, because we want these actions to be7 E( g t4 M$ h" Y- @. e: q
// executed in a specific order, but these steps should3 s. H) P! u3 ?1 g8 d
// take no (simulated) time. The M(foo) means "The message* {5 k, Z) F- Z8 o: X0 p
// called <foo>". You can send a message To a particular3 @" o* a6 ^3 U! r6 z
// object, or ForEach object in a collection.
; r3 n6 k4 r! R% f5 @ 2 ^1 A( K0 O u6 G& n/ _1 e
// Note we update the heatspace in two phases: first run2 d+ k' g( g/ \3 J" m# J9 o5 K% m0 E
// diffusion, then run "updateWorld" to actually enact the
3 |5 o. E7 M" [: y* c F. m9 b // changes the heatbugs have made. The ordering here is
8 C$ c+ n" R; | // significant!7 o5 X1 Q. K- x) K0 l9 B
' Z+ \3 P( C' w% |$ q9 T) W
// Note also, that with the additional
3 C% u0 W: g7 X3 A4 |2 ~ // `randomizeHeatbugUpdateOrder' Boolean flag we can, _$ S7 _' G1 H1 Z. D1 G
// randomize the order in which the bugs actually run
w2 G" r1 \( {: @( { // their step rule. This has the effect of removing any/ f( J* ^9 _" Z
// systematic bias in the iteration throught the heatbug* I3 R) Z! K. k+ j3 o
// list from timestep to timestep5 P8 F, N- a2 x6 Y% i
$ k# c" C' l- f // By default, all `createActionForEach' modelActions have
" g$ R, f( m( S // a default order of `Sequential', which means that the! s- h; u, ?& C9 b V; I6 s
// order of iteration through the `heatbugList' will be
5 y& E. B" j% m* m // identical (assuming the list order is not changed
+ h5 e5 P& ~9 V. L // indirectly by some other process).
6 s! X/ w: h+ i% S% g& Q
]" N7 g1 u3 u' h O modelActions = new ActionGroupImpl (getZone ());
* @. j3 z9 H0 {1 e: `, X5 j$ K# p+ @4 A# ?" d0 X5 n+ s/ N
try {
2 r% l9 \# q' d7 Z& u modelActions.createActionTo$message! J" ]4 F9 J8 `$ L
(heat, new Selector (heat.getClass (), "stepRule", false));1 {, d4 }. L7 `2 h/ e6 e1 p; F
} catch (Exception e) {' W' o' v& j R) t; b/ g9 ?7 N- I
System.err.println ("Exception stepRule: " + e.getMessage ());1 @, h5 o# I& e6 B( i
}
+ Y- K5 y" c! p. Q2 |* D3 q
/ \: a5 H; V* |' f M5 C9 b# A try {
: g5 y& q5 v: h. ?) Y* X/ n% H$ | Heatbug proto = (Heatbug) heatbugList.get (0);
* N* O6 p6 d+ H9 d% q6 c6 X+ u Selector sel =
% @. I/ F' s$ G. m6 _4 S1 r: q new Selector (proto.getClass (), "heatbugStep", false);9 h! J6 P0 t, E3 u' p
actionForEach =
% \8 i/ w! E1 `! y2 G6 h6 V. N8 V) M modelActions.createFActionForEachHomogeneous$call
0 k! ^3 W E9 O1 m& \4 T5 r (heatbugList,
8 {' u* X9 X ^* @ new FCallImpl (this, proto, sel,
0 c/ N1 p+ D& a6 r9 w4 d/ \ new FArgumentsImpl (this, sel)));0 |6 j1 S- {$ n/ u
} catch (Exception e) {5 N* T6 \- L$ _6 p, Y7 b( l3 W$ T
e.printStackTrace (System.err);! t- H5 d! J! Z9 `7 B; u
}: B! j8 C% c0 Y2 a
% J2 R! ?* r. M5 X" n) k syncUpdateOrder ();
% L" ^% _2 t4 x: F K8 N5 _( w- D, Y* u+ ~. N+ ~
try {5 F0 [) s* n2 L( I1 W8 Y& K
modelActions.createActionTo$message
% n: r3 W$ `! S9 G* | (heat, new Selector (heat.getClass (), "updateLattice", false));
- R( s0 c g- t# Z% K } catch (Exception e) {. F h; S& e, w- D$ S B! N, {
System.err.println("Exception updateLattice: " + e.getMessage ());
" O( ~2 m' h) H" G }
- h5 L) M2 t& y" F3 L: R- E6 \
& P- Q$ Y; J1 H$ B // Then we create a schedule that executes the
2 |1 i, R4 d! C; s3 q2 t6 m. J // modelActions. modelActions is an ActionGroup, by itself it! L" S# r1 Z* i* y3 m! \
// has no notion of time. In order to have it executed in
5 M1 O# S$ F7 Z6 P9 X // time, we create a Schedule that says to use the
! b& E# J" L# \2 @ // modelActions ActionGroup at particular times. This
+ I& x5 ]) ?& f( d+ ^, t% F // schedule has a repeat interval of 1, it will loop every
+ h' o- n& w6 E* s // time step. The action is executed at time 0 relative to
" f; {% a$ x& A& y! Q8 G. a // the beginning of the loop., |1 }8 U7 E$ d+ h& ]) A
; N. [8 C2 |2 C7 W // This is a simple schedule, with only one action that is! g) J' b7 J% {& u! l4 j
// just repeated every time. See jmousetrap for more2 W8 `) E2 y* L9 Y
// complicated schedules.
4 q1 a/ @* _2 e- {& T$ d9 }1 Y
7 w2 u2 [. E* d modelSchedule = new ScheduleImpl (getZone (), 1);1 G& P& i1 w Z D& F
modelSchedule.at$createAction (0, modelActions);" ~, z, B6 O2 a2 f, l* u [# L
1 |& ~8 L' q/ R% X9 u
return this;- O3 [ o4 n$ e& O# {/ s
} |