HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( H: \ A" {: @- {5 b
. t9 ?- o/ n2 e: s \- B public Object buildActions () { V: F' v- f) ^2 J: \# D
super.buildActions();" D* X: d0 |" \
+ r" v! Q' f1 Y4 n // Create the list of simulation actions. We put these in6 \ S0 f' h+ n& b6 c1 R
// an action group, because we want these actions to be
' Z2 V1 d5 E! Z& r( W; a% Q2 f // executed in a specific order, but these steps should% R( i, J6 o- X
// take no (simulated) time. The M(foo) means "The message: R* s, u6 _+ k5 {- z6 h$ e4 t
// called <foo>". You can send a message To a particular f' y" T, S7 R7 o) F9 Y' Y; j2 n
// object, or ForEach object in a collection.! |6 w) Q8 @5 n
; \# ?; v; M- k/ x, t5 Q. T: S // Note we update the heatspace in two phases: first run
, }9 W& [2 z7 K/ z J. o // diffusion, then run "updateWorld" to actually enact the
, n5 b" ]; ^2 V& e* I/ Y // changes the heatbugs have made. The ordering here is2 h. B: i. Z: U
// significant!
5 g# T& S1 ~ T ~8 `+ v ' s- z) p" V8 ]( M# M% Z/ H
// Note also, that with the additional
) r1 h! ^0 V# l5 P) |+ ?; Q/ g# k // `randomizeHeatbugUpdateOrder' Boolean flag we can
; v' x% C2 d1 y // randomize the order in which the bugs actually run( c% B6 a: f8 A% T: c: _
// their step rule. This has the effect of removing any
5 {! c# \7 N+ ]; g* Y* W // systematic bias in the iteration throught the heatbug7 a7 Q* j# G2 j$ T" o+ S
// list from timestep to timestep. A) y- T. `' m. p
! M8 S! a5 O" X // By default, all `createActionForEach' modelActions have( x/ C6 ^8 V- C1 S/ O
// a default order of `Sequential', which means that the4 v2 _* V( y1 M# s- P3 k$ R
// order of iteration through the `heatbugList' will be4 a. D! |5 F* \; E+ r
// identical (assuming the list order is not changed
6 }, H# a$ O1 z2 j1 K- u% @ // indirectly by some other process).' J) F2 ^ i& G4 E1 G
& z/ R$ v0 ^/ G$ f [, [7 B modelActions = new ActionGroupImpl (getZone ());
: O' x' v& |' @ E% }6 F0 T0 w" }" Y
try {
* o" f f5 ?' p2 U8 p) r modelActions.createActionTo$message2 {$ S) _8 g% f
(heat, new Selector (heat.getClass (), "stepRule", false));# ?: u% _3 _# g& P# I
} catch (Exception e) {
* b6 ~% }! d" f! K6 K) f2 v& ^ System.err.println ("Exception stepRule: " + e.getMessage ());8 Q5 }! ^6 C$ b5 a' P
}
9 N3 z+ ^; v& P) v: O0 L2 l3 ~
4 w# z4 p' }6 C2 H$ i try {9 w1 ~4 T Q) C8 n$ ]- X
Heatbug proto = (Heatbug) heatbugList.get (0);
4 f9 }! e1 D# Z5 |% U Selector sel = 9 L9 F+ U6 g- r) t2 K
new Selector (proto.getClass (), "heatbugStep", false);2 D, f* E7 U, X3 \ q3 i0 N! t! [; g
actionForEach =
1 B7 ^8 h# I. b" i1 A2 a2 t9 U. i" J modelActions.createFActionForEachHomogeneous$call( g& D9 i8 B1 }+ Q$ I, U+ \
(heatbugList,
* _+ K X' t, E new FCallImpl (this, proto, sel,2 L! s+ T2 _8 B% B. T
new FArgumentsImpl (this, sel))); d- @5 V* b0 Q( V B( o' M- ^
} catch (Exception e) {- B' l, _; ~5 l: I/ o% M1 v
e.printStackTrace (System.err);
; a( C$ Y J C: M( X ? }
" [2 J8 E+ y6 N) r
) l* _9 k* O0 W8 |, L" T0 q- A syncUpdateOrder ();4 s# d0 \. F! G% C. l: c- g
) d% Z f* J' o3 R. k
try {
. j2 ]7 I& ^ U+ R$ e/ K1 H modelActions.createActionTo$message . p3 X! {: n0 u
(heat, new Selector (heat.getClass (), "updateLattice", false));
9 ]5 X$ g9 }5 R: i } catch (Exception e) {
; m: J5 k+ T" S/ W System.err.println("Exception updateLattice: " + e.getMessage ());
L. H) b& {6 y$ y0 C }
" K9 [' U% D4 F8 x& T $ c9 |+ h+ z/ F. I0 ^7 Y5 X4 [9 |
// Then we create a schedule that executes the: J6 I) ^5 ~! w
// modelActions. modelActions is an ActionGroup, by itself it1 d F0 E" `( ~& Q& r. }+ Q& p9 ~
// has no notion of time. In order to have it executed in0 N8 l) n# F) }8 ?
// time, we create a Schedule that says to use the
/ G% Q7 T: M4 Z# c- T# g Z o7 m // modelActions ActionGroup at particular times. This
' A& X: `! B' y5 c2 \/ j' a1 D // schedule has a repeat interval of 1, it will loop every3 J! ]4 m6 J f, r
// time step. The action is executed at time 0 relative to
; D# a4 n- H: I) e) ^: B% p+ P // the beginning of the loop.
' F. {! i4 W. {4 }
$ l4 ^$ Y0 ~9 s5 N" ^ // This is a simple schedule, with only one action that is2 X( C" `3 k, H2 l4 t/ {
// just repeated every time. See jmousetrap for more
2 [2 s+ N1 F+ r0 C. z. s; E // complicated schedules.
% x, t! f0 k/ q- C- a7 d7 X 0 p4 M! t% G6 m! A1 g
modelSchedule = new ScheduleImpl (getZone (), 1);& C' m: {. n, e4 h; t
modelSchedule.at$createAction (0, modelActions);- `, y/ v# S/ O. T# B
1 r( P4 ~7 D/ F' Z& K5 s. @ _
return this;* o! |7 S/ _2 S# m; O$ I
} |