HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. R) F8 _ L- l+ W# |; F! @
' s( q8 k K/ M3 [2 V- P/ ^" F
public Object buildActions () {
{* A' c$ G8 a- V6 v2 i- @$ i p super.buildActions();& M: y% P! V3 }, [" n- L* h
2 a" z6 q F! `' X9 p
// Create the list of simulation actions. We put these in
# W4 e1 C( V Q, V2 L4 Y // an action group, because we want these actions to be
! n5 \$ | H* F7 m+ n- O8 e% ?, ^ P // executed in a specific order, but these steps should6 a: j, F# Q4 l! t1 p1 O
// take no (simulated) time. The M(foo) means "The message V, r$ P1 s9 X+ n
// called <foo>". You can send a message To a particular
" O# x6 S ?' P0 f1 T2 v$ N/ h // object, or ForEach object in a collection.
7 C& |( s; A8 U, r* Y- _9 C( E/ c 8 B8 E' g! P* H0 i
// Note we update the heatspace in two phases: first run1 B( w t# Q7 c2 o+ x2 O
// diffusion, then run "updateWorld" to actually enact the7 [9 ?9 D% I* Z9 N4 i$ Q/ j
// changes the heatbugs have made. The ordering here is
" i/ p$ f3 @2 k1 b2 U // significant!) q% f6 d( ]$ \* g& P5 [0 f
7 V$ U5 p0 W+ V0 z6 R6 f // Note also, that with the additional
& T l/ h& k3 b& n6 D9 {/ B // `randomizeHeatbugUpdateOrder' Boolean flag we can e2 v, e8 D' w. ]4 s4 y9 ~# {
// randomize the order in which the bugs actually run F& n8 Y; c9 U" _" B- E" q- p
// their step rule. This has the effect of removing any
4 q" x; }% p3 F+ J9 N- {- q# \ // systematic bias in the iteration throught the heatbug
! [* t, ~1 g* k2 I8 g/ ^ S- r$ `6 D // list from timestep to timestep
6 t$ j7 Q5 a" [" v( T5 w6 v+ K9 i ' Y) l4 ^) }7 D
// By default, all `createActionForEach' modelActions have7 }' t5 _ ?, P: P
// a default order of `Sequential', which means that the
$ c3 B2 n1 S N // order of iteration through the `heatbugList' will be
1 b( V3 A4 D* u. z+ V" w // identical (assuming the list order is not changed
7 |4 @4 c3 d/ R- l$ L4 r // indirectly by some other process).
9 @- i7 I; s- x1 u
# t5 I! d' ^5 z% R/ ?+ _' J7 j9 H modelActions = new ActionGroupImpl (getZone ());4 ?: e& d. V1 i6 G: D
# h- [* D7 Y5 Y7 d
try {' T* t* w# U/ X. G
modelActions.createActionTo$message" j% D) w* C, Q
(heat, new Selector (heat.getClass (), "stepRule", false)); W0 D7 [3 S$ w1 M
} catch (Exception e) {: R, v3 r$ m3 h) p! C$ u
System.err.println ("Exception stepRule: " + e.getMessage ());
U3 o1 d0 U/ j) I; U* f1 ^" d) v( G }
7 D: T* a; y/ s! s; _# u. G3 M6 t! V; e: o
try {# ~$ Q+ m0 I3 X6 B! i; y1 ~
Heatbug proto = (Heatbug) heatbugList.get (0);) j( E; O. n+ A, e% I
Selector sel =
- H; N3 E5 N& u$ [: D new Selector (proto.getClass (), "heatbugStep", false);
' A& P' a ^7 W1 t; I0 J actionForEach =
( e; i5 M/ u0 S7 T N modelActions.createFActionForEachHomogeneous$call7 @" i0 l+ Q0 N/ b
(heatbugList,. H+ B! g* C0 y$ s0 x1 L
new FCallImpl (this, proto, sel,
) z6 @# y6 X2 f. ~) p: j: n6 |$ d* q3 ?! _ new FArgumentsImpl (this, sel)));
* m& M6 D) u5 Y2 G0 P$ d0 Z } catch (Exception e) {
2 ?9 T/ B9 t, S n5 U3 i) @1 s e.printStackTrace (System.err);
6 a: d% I. e8 i: E } N3 \; y4 s$ j- I9 [( C
7 `9 i; {) e2 V, C/ e syncUpdateOrder ();& Z6 c: H$ n2 _. z
6 ~% F% h, K4 l. K- w( A3 B try {3 @ X* ]- ]: G9 P9 O. R, L. q6 P
modelActions.createActionTo$message
. ^& y6 N% p2 T: F# f! I) g2 ? (heat, new Selector (heat.getClass (), "updateLattice", false));" x( i" Y7 X' b. ~& f
} catch (Exception e) {. T S% z% l$ C+ J& D( A
System.err.println("Exception updateLattice: " + e.getMessage ());
3 q* Q! F ^5 X" D! V }8 ~! F% E7 r7 x0 [! L# B
6 N. V8 \* O: E- I% B+ l // Then we create a schedule that executes the
) y1 Z" r5 c4 @" _$ t v" r; { // modelActions. modelActions is an ActionGroup, by itself it, x. I d5 O9 E) h! O2 P" [
// has no notion of time. In order to have it executed in5 ?; B% |1 a0 G2 C
// time, we create a Schedule that says to use the* l! ~& {4 k4 u9 o: M& e2 }( ? D
// modelActions ActionGroup at particular times. This
% F" }( O; [7 y! c, X4 T% { // schedule has a repeat interval of 1, it will loop every
( u0 t1 x" n. U7 L // time step. The action is executed at time 0 relative to
$ L) _2 T3 T' R3 e) m4 N7 P // the beginning of the loop.
1 P8 z: I O5 c
# `! ]3 L; J; P1 T6 T$ N- b // This is a simple schedule, with only one action that is
0 f9 L- h0 U* ?' u // just repeated every time. See jmousetrap for more
$ \* Q' v; e$ b' v1 y z1 |$ o' H // complicated schedules." c2 c, G& I4 A: F; r
9 p* O- \6 V3 `- `% B modelSchedule = new ScheduleImpl (getZone (), 1);2 P/ u3 ~0 l$ X# {2 K9 b
modelSchedule.at$createAction (0, modelActions);
# a1 R' M/ \0 Z7 u1 V b
3 c* M& l, [8 Y8 k$ f5 A; m return this;& a; W2 e# k' t1 e
} |