HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- _, V7 B- t5 s4 y2 Y
) [& Q" V6 u* l! p$ {3 O public Object buildActions () {
_1 V* e) F5 Z9 {# M( G super.buildActions();
% R1 f$ E1 `; H4 }
4 ?3 g( j9 b& [2 I4 @$ E4 @ // Create the list of simulation actions. We put these in! T9 H" p& i8 [: e+ G! E
// an action group, because we want these actions to be
! v {, R. C/ D9 ]- U! k# J$ f // executed in a specific order, but these steps should
) d- {* s. Z# e // take no (simulated) time. The M(foo) means "The message- X$ c& x0 D+ W2 M
// called <foo>". You can send a message To a particular
% P! @/ J$ q% m // object, or ForEach object in a collection.
- M* p9 l+ f: E9 Y' n# i- A( J
4 P5 o5 a( ]1 s; E$ C // Note we update the heatspace in two phases: first run1 W$ o8 q& {3 S L" M
// diffusion, then run "updateWorld" to actually enact the
$ @" q" g `6 F7 v2 j0 Q // changes the heatbugs have made. The ordering here is) v+ V" B4 S9 h" F+ u
// significant!$ E6 L. Z. h) m/ B. \7 V
% j' e" o# Q+ x; d# `6 x8 X |9 u0 X6 U // Note also, that with the additional. l( ~* V) x. C9 V. F9 ^: ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ Y2 g4 u( e& a. k // randomize the order in which the bugs actually run4 P: m0 G9 D$ K+ f' n
// their step rule. This has the effect of removing any1 h K8 W& q+ @# B& r
// systematic bias in the iteration throught the heatbug$ x8 W! W" M" d" S1 z6 j
// list from timestep to timestep
! I1 M* I# P/ b `) V( ]! {
% C7 K( H9 [' p: p // By default, all `createActionForEach' modelActions have/ ?# V' Y8 C8 _+ Z6 {/ X
// a default order of `Sequential', which means that the
( a$ `; [; ^, r8 f( L' ^ // order of iteration through the `heatbugList' will be$ d, \( {7 }% j4 q7 W8 {
// identical (assuming the list order is not changed
# g8 i4 F2 W* G' f" a8 k // indirectly by some other process).7 M9 N1 b: P4 L# D$ [2 K, ?" Z
* |' p9 W2 J1 L, R" ]8 u: Q! ?3 ^9 L
modelActions = new ActionGroupImpl (getZone ());
" O) m# G }8 p
2 j) o* j3 H5 e! [/ z try {# U1 H3 }* g! o& }
modelActions.createActionTo$message
1 D* P6 l/ ~& B2 R/ b3 { (heat, new Selector (heat.getClass (), "stepRule", false));
# t+ T6 a" j* \# [$ D- y } catch (Exception e) {
. Y, f' y5 p" k3 i System.err.println ("Exception stepRule: " + e.getMessage ());: C- u; ?2 z/ Z. Y0 v
}# h2 P$ g2 C) x3 A' Y
7 T1 m; b2 R7 W$ u9 k4 } try {
4 r1 O: _# I+ D3 V Heatbug proto = (Heatbug) heatbugList.get (0);1 Q- B4 X& ^) a0 g7 f
Selector sel =
3 B* U9 z& g$ m+ M new Selector (proto.getClass (), "heatbugStep", false);5 J! I( ^7 ?8 _$ v
actionForEach =
0 @8 ]2 t4 v! A2 J modelActions.createFActionForEachHomogeneous$call7 G6 M5 o$ B/ A; g6 E2 b7 w
(heatbugList,' {2 j1 h' x6 y; r1 f' i, N
new FCallImpl (this, proto, sel,
0 R$ H, e0 g' P1 b, P) x new FArgumentsImpl (this, sel)));2 N7 H" Q% h9 _* q
} catch (Exception e) {1 Z. I: H! Q+ \( v7 o0 @; G
e.printStackTrace (System.err);
5 V" e k, D' x4 N) W* f. e }; \8 q Y0 N" u; B' E1 P
+ c' c# I+ ~! ~9 h syncUpdateOrder ();
# v: A F7 g2 Y3 Z& h* E) B) d4 ?& b/ G8 n$ d
try {
- G; U/ Q8 s2 O2 i modelActions.createActionTo$message ( ?& U9 I& J% Y9 v2 a/ E
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ Y6 O2 v8 w- h) U5 S" J# h } catch (Exception e) {
; `# v% g, A! f/ C: `# ` System.err.println("Exception updateLattice: " + e.getMessage ());
" |+ g" {# M7 J# _9 D4 H }
% I, o: o4 _/ J: L- j( ~/ t, s ! {" j0 p0 `- o6 O) i
// Then we create a schedule that executes the% V- J+ N- L9 h) M( k
// modelActions. modelActions is an ActionGroup, by itself it
' m. m5 Y2 O, s. N: o2 M // has no notion of time. In order to have it executed in6 N% G' `$ E r9 K% B2 w
// time, we create a Schedule that says to use the
! `1 f; s4 c/ L7 q$ L7 t; H // modelActions ActionGroup at particular times. This
1 j% V1 d0 I8 |0 u( x. u1 b // schedule has a repeat interval of 1, it will loop every+ |% z/ S% A; ` ?0 y7 J1 G
// time step. The action is executed at time 0 relative to
8 G- F" q" B6 A5 z* I, w // the beginning of the loop.7 {( w0 X/ r3 [. T7 }
! q i- A2 V' Z+ U // This is a simple schedule, with only one action that is) C0 s8 `2 ~( O6 k
// just repeated every time. See jmousetrap for more
2 |9 ]3 U; O& `% a // complicated schedules.
+ N1 Q8 ?: Y4 W3 ]* {$ r2 I l* n$ p: H7 t* g4 I9 X3 f- _ G
modelSchedule = new ScheduleImpl (getZone (), 1);
+ S" P7 G8 c' H" s; b$ B- a modelSchedule.at$createAction (0, modelActions);/ q, Z2 F0 }: ~7 L4 l5 D' {$ q
, _; e& i0 e* a; S! K! m return this;
" o- O8 [% g+ M* R3 ^2 c& k } |