HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 M2 \, A {+ C4 K+ z0 N" W) r' d+ @
public Object buildActions () {
$ b9 k7 _1 v& P. p/ b% L( r% A super.buildActions();( |/ ^! `3 [# f0 r) X/ p
2 B; C1 H) H2 E // Create the list of simulation actions. We put these in# e) b# L; z3 X( M
// an action group, because we want these actions to be: y) W: g. ^. f% s, y x t- C7 `! N
// executed in a specific order, but these steps should
) W- S' [1 A" K0 J' {5 P e // take no (simulated) time. The M(foo) means "The message/ z0 i1 x. X5 p/ w. }
// called <foo>". You can send a message To a particular
# L1 |+ [$ C, x // object, or ForEach object in a collection.
6 O/ j" o3 ^( e# x7 }. J$ h. i % J7 V9 V/ r; y' r6 E, |. S
// Note we update the heatspace in two phases: first run
4 a; M# b0 s$ {4 h, R7 Z // diffusion, then run "updateWorld" to actually enact the5 w" H( G( T* Y1 ^: T: p
// changes the heatbugs have made. The ordering here is
* {5 c; E1 T0 y3 @ // significant!
' J0 Y( S' q2 I$ t! F7 a
0 d( F+ p. c. k) e. W5 S. d // Note also, that with the additional2 {, r+ U9 H! P& j" L7 n p
// `randomizeHeatbugUpdateOrder' Boolean flag we can Q2 |0 S5 y0 w+ D7 i* t5 |6 ?: y
// randomize the order in which the bugs actually run
; y7 G' s$ k& B$ P) m" K/ d! f1 | // their step rule. This has the effect of removing any+ @7 L, r: K7 v3 {- D) `8 q0 d
// systematic bias in the iteration throught the heatbug
- I0 k" I; s: J9 A$ g6 r. A% H // list from timestep to timestep, d# i& Z1 T% P/ y
* D8 f5 `& Z/ s* ?0 j% b0 s. u1 Q // By default, all `createActionForEach' modelActions have
) p3 T& I( \: ~1 W9 ]6 a. `. F // a default order of `Sequential', which means that the
( K2 H2 _ N) r. x$ A/ y- j4 w' e& y, a1 E // order of iteration through the `heatbugList' will be6 `8 S7 Z* K/ t
// identical (assuming the list order is not changed. B( o4 K% C, x6 u) u
// indirectly by some other process).: K- Z( s( P; y! x* g6 n% K
4 i K7 ] t5 x: O0 L8 p+ d% O3 H modelActions = new ActionGroupImpl (getZone ());. m! f$ ^) s1 o3 @; H
5 i+ O4 o! ~& c& W# D% N) t; G try { |* v" k2 i( G c
modelActions.createActionTo$message
5 e/ e' B" M9 S8 K7 v (heat, new Selector (heat.getClass (), "stepRule", false));
5 k. T; b# D/ G' J& ~ } catch (Exception e) { Z, T3 r$ j8 a; P
System.err.println ("Exception stepRule: " + e.getMessage ());% E) ^; v- U/ e: V. ~
}/ P4 I! v$ S* l* m4 D
0 g+ [ X! g" }% \+ f
try {
3 J5 y% |: C# l5 c& [0 \ Heatbug proto = (Heatbug) heatbugList.get (0);0 C% Z: J; Y0 |8 U; _# h+ C
Selector sel = 9 W) T6 @% p o0 J6 Q3 S1 f
new Selector (proto.getClass (), "heatbugStep", false);
5 g9 C' O X- D" n/ r actionForEach =3 z2 J) r* F M' @
modelActions.createFActionForEachHomogeneous$call
' q# `2 F" M; [1 D# B; r (heatbugList,3 A* F; u( z# ^* N, L# v
new FCallImpl (this, proto, sel,
4 L8 G8 f3 R' A s8 Q* K0 G' b6 | new FArgumentsImpl (this, sel)));
" d. U' q, C" m7 E) ^' h } catch (Exception e) {
; g& p$ t- S1 g e.printStackTrace (System.err);+ n# E# M* W% b3 `( Y; o5 t j$ i
}# p9 q) D& Z) b8 ~( i
_: C C" k- x1 X4 i
syncUpdateOrder ();) e2 R) g: o0 A. t
4 S+ \1 C5 c1 a; e1 a try {8 a- d% E. C- J* {5 b: X: n Q2 J
modelActions.createActionTo$message : @( w& c3 q% K: u" ]* i9 p
(heat, new Selector (heat.getClass (), "updateLattice", false));
% y- h3 X. V) d) r* N } catch (Exception e) {
/ r$ {* q& W) l System.err.println("Exception updateLattice: " + e.getMessage ());
& l" X/ a5 Y7 ~5 F }- _! \. ] K, R4 ^7 o$ W
& d# I3 p& \( s/ |' D9 u // Then we create a schedule that executes the
: i( N4 y' ?. \/ `: l0 Z' W // modelActions. modelActions is an ActionGroup, by itself it8 ]8 ]2 C) W& l4 c3 R- Y& z
// has no notion of time. In order to have it executed in
4 [* P8 O9 q, h( u$ S% l3 f- b% g // time, we create a Schedule that says to use the% V+ H! _ E6 \8 ~4 i/ q7 P
// modelActions ActionGroup at particular times. This
" W7 J; ~4 N4 K7 Q // schedule has a repeat interval of 1, it will loop every6 o8 s1 `6 ?3 c$ G. Y
// time step. The action is executed at time 0 relative to; s* O# `+ i u$ S1 q
// the beginning of the loop.
* \2 W; t2 Z' A% P7 i7 O$ f( F5 C# d8 `% T
// This is a simple schedule, with only one action that is
8 `& g0 R \0 X" L& _: j // just repeated every time. See jmousetrap for more/ ]& D9 J* o5 T4 G R
// complicated schedules.
" p* ]1 _/ |1 f" F1 ` 7 w( m$ b) \1 {( i! V& V- X
modelSchedule = new ScheduleImpl (getZone (), 1);' w6 \& a% h r2 h1 x% s6 \+ c
modelSchedule.at$createAction (0, modelActions);$ e3 P6 m: I, P e5 J
1 R0 K; k8 j5 f+ h2 ~' A return this;
0 d' h* N8 w$ c' ~9 b } |