HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 Q6 X4 N/ Y9 n
+ G5 q) R1 H, ^ public Object buildActions () {) P4 c. v4 n1 |# j
super.buildActions();
% J* ]+ M5 |. j \ ! Y8 v$ F4 w4 e- k7 _
// Create the list of simulation actions. We put these in# D8 ?; ^( q) j$ g+ h3 W
// an action group, because we want these actions to be7 i5 p. e$ [) t$ }% S8 S' a
// executed in a specific order, but these steps should# m' M& e7 \# B: ?5 z4 W
// take no (simulated) time. The M(foo) means "The message+ B* g# o* _& a0 e6 u& n
// called <foo>". You can send a message To a particular; U! U9 n4 X# r B! L
// object, or ForEach object in a collection./ j" _5 X% d1 z$ s6 J
4 m, R0 R# U/ y6 ^% Z9 N7 ^
// Note we update the heatspace in two phases: first run
5 _; b- }" U* k5 z+ j7 S // diffusion, then run "updateWorld" to actually enact the
7 U0 n$ z% b/ } // changes the heatbugs have made. The ordering here is
! @* |4 o1 ?* O4 I( ? o; ^8 B // significant!
+ j9 q2 `* m- m/ J! r# G" C
3 W2 g, t# _" v( v- v // Note also, that with the additional% j, m* J5 g* V! Q( c" p" Q
// `randomizeHeatbugUpdateOrder' Boolean flag we can
@+ F- t% \3 p6 z // randomize the order in which the bugs actually run' E& u% i( V1 p$ e" r) v
// their step rule. This has the effect of removing any) ^" f$ p+ d8 }! @5 y9 i
// systematic bias in the iteration throught the heatbug! K6 d/ F& c j) _! h) g: Y/ |
// list from timestep to timestep; V! _) [* t2 X) i$ e5 d7 t
7 Q# B' Y3 H% c, n; b O% h1 B3 {
// By default, all `createActionForEach' modelActions have
" a$ B$ z# V9 u$ v1 | // a default order of `Sequential', which means that the
8 ]1 x9 ~, \% N4 S0 d // order of iteration through the `heatbugList' will be
# c% D# Z( h' Y% I! ^; o9 U E; H // identical (assuming the list order is not changed. Y4 Z; s) {7 e0 K
// indirectly by some other process)., S( T7 P" P: M J0 c
$ v: e' r; d, w8 x, U" a. I* g modelActions = new ActionGroupImpl (getZone ());
6 b" o/ X: [$ O. e. m" q8 N7 o4 U& f# f4 Z; s' j
try {
; ~4 C, C! Z; t( J u modelActions.createActionTo$message
; t/ K" \3 S. U/ H4 s (heat, new Selector (heat.getClass (), "stepRule", false));% ?+ t$ t4 j! }7 j0 J! S- }8 p
} catch (Exception e) {% Z; B, I9 q$ X" _ _1 d2 ~1 L
System.err.println ("Exception stepRule: " + e.getMessage ());4 {3 u( D# c- Z) G0 G
}. \9 g% _+ `6 A
' y" M0 s: ?6 M' N1 K: { try {2 p0 L c8 n4 h2 ]2 ~7 ?: u0 u
Heatbug proto = (Heatbug) heatbugList.get (0);7 ] X$ R# I1 e" |
Selector sel =
7 y* m: C- L9 N# o! @4 Z) r1 B new Selector (proto.getClass (), "heatbugStep", false);
8 ?+ V- w! }0 M( A* t. O actionForEach =
" }( ?4 @ _: Q9 ~" d5 ?2 d modelActions.createFActionForEachHomogeneous$call: M. s) f- Q" ~* v' m$ ]7 E* Y
(heatbugList,, O+ j; ?% D. N, p
new FCallImpl (this, proto, sel,
7 c' \* E; y# C8 d& a. l( j: X1 x new FArgumentsImpl (this, sel)));
* \" g( @9 q& t- Y/ A3 s } catch (Exception e) {0 w' V6 e& o9 z
e.printStackTrace (System.err);
& w) \# G/ ]- q# f% s5 ^/ P }
" p) \$ X' t2 P0 x) A, o9 A
! a0 Z5 T+ K8 X5 m) n- R syncUpdateOrder ();
( ?: }' b/ @) D6 i9 b! J4 @# W
% e) R3 I- ^' }0 F6 K3 p try {5 l- u, @0 q7 u1 W+ M
modelActions.createActionTo$message 0 v, u d% o2 v# m
(heat, new Selector (heat.getClass (), "updateLattice", false));
' _0 w- P! C, L+ T } catch (Exception e) { h2 k5 ]- @* u9 X9 \( z0 e. T
System.err.println("Exception updateLattice: " + e.getMessage ());
( T* d/ e! w2 l& ^2 w }
0 x" B! t6 i d. G# O0 }) k1 l
4 G# Y& p7 p! ^) t/ r3 S- n8 I // Then we create a schedule that executes the
' n& `' x2 H% f' s% R1 A // modelActions. modelActions is an ActionGroup, by itself it
a+ Q& B- Z9 k' _1 g // has no notion of time. In order to have it executed in
Z' t6 _! R) n2 f* H0 X* \ // time, we create a Schedule that says to use the
! y9 x$ y6 u; z/ ~: g- ] // modelActions ActionGroup at particular times. This
4 c# }. `) t( t! F // schedule has a repeat interval of 1, it will loop every
2 I0 o/ C9 B/ R" q6 V) b // time step. The action is executed at time 0 relative to
: `+ z% j* G9 C" ?" w // the beginning of the loop.
$ j6 l T$ d/ p/ W2 |% c! L7 B E; b3 U* u: n& u
// This is a simple schedule, with only one action that is1 g& ]& `$ K: z# Y3 o
// just repeated every time. See jmousetrap for more
* w% [$ G9 c$ h3 B' f- O // complicated schedules.! C" p, f" ~( w! [# t( f
+ u9 P. H& `3 C- d6 I! N6 v
modelSchedule = new ScheduleImpl (getZone (), 1);
3 c# s$ b u1 g) X9 Y- V7 c4 f" |7 L modelSchedule.at$createAction (0, modelActions);/ } g9 T+ T* @2 W, t
1 F2 h5 o' S8 j5 r% B
return this;
0 o0 n7 R1 g% I } |