HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% Q- l8 ]9 A2 K7 i
) T$ |8 D) }+ G+ ^' [, H public Object buildActions () {
) N' W9 a3 u9 i7 l; P3 Y super.buildActions();
% m! B* A5 W! y0 e& {( \. D 3 A+ l7 V, Y4 k( _3 e) y, F
// Create the list of simulation actions. We put these in
" |/ G r! Z; P // an action group, because we want these actions to be: V) M. f" a" @7 J% q
// executed in a specific order, but these steps should0 n, n* V% y5 K, [8 k
// take no (simulated) time. The M(foo) means "The message W1 o* ]) G t
// called <foo>". You can send a message To a particular8 s6 M0 ~8 ~; \7 l/ F4 x Y
// object, or ForEach object in a collection.3 \3 Y9 P2 L. X2 ?# F2 N
/ n7 ^ h$ E; ^6 L$ @: B$ y
// Note we update the heatspace in two phases: first run# c. E3 Z3 B+ J) \! i* G7 c
// diffusion, then run "updateWorld" to actually enact the( T F1 |' h7 l( O
// changes the heatbugs have made. The ordering here is
, ?" v: V- I& D5 x6 U) {) ~ // significant!
~/ w) r! }, G# k( \/ W
. w! v9 l6 K8 R // Note also, that with the additional" n7 ^8 f( N. w& c
// `randomizeHeatbugUpdateOrder' Boolean flag we can( m$ C7 i8 k8 ]1 W+ m' [& r5 j( d- P
// randomize the order in which the bugs actually run# ~- Y5 m& ^! D3 e% g& G5 t4 Y
// their step rule. This has the effect of removing any
8 S/ ]3 o# D& Z // systematic bias in the iteration throught the heatbug
# t! S6 t( v' x1 q // list from timestep to timestep- w8 Y9 S" f1 W/ @4 @2 p
+ u8 t! L! }4 f/ w // By default, all `createActionForEach' modelActions have
+ A- b8 l# o5 S) ^8 g* N // a default order of `Sequential', which means that the
5 k) F+ @: \; L5 F: Q9 A& ~- y* _ // order of iteration through the `heatbugList' will be
; U- p' A8 X9 k2 e- h // identical (assuming the list order is not changed
& W7 j3 I5 ?2 i3 l2 j# w2 E6 k // indirectly by some other process).& ?1 F( E! V! I5 p. n% H$ s
% N [* Q: w. k. Y1 n: H modelActions = new ActionGroupImpl (getZone ()); |6 B% s4 g c+ }# |
$ a& K1 z- `! p6 e/ L
try { _ D* g+ S1 K6 D* s d" ]( ]( s
modelActions.createActionTo$message# @- @, F% q* Q, a: t/ D
(heat, new Selector (heat.getClass (), "stepRule", false));* |+ i2 m0 ?7 y0 X
} catch (Exception e) {
) I6 b1 H0 H) P0 x* i$ V System.err.println ("Exception stepRule: " + e.getMessage ());( `! X: m+ J* l
}
% z4 y+ ]% b+ Q, N3 T
; H' K* R& @; C2 R h4 O6 M8 M try {
: ?8 c' b! @8 u1 ^* a1 P/ g7 D Heatbug proto = (Heatbug) heatbugList.get (0);5 C* _. f$ I' V4 }+ k1 _5 F& Y0 {, l
Selector sel = ' G' m' p8 g6 G
new Selector (proto.getClass (), "heatbugStep", false);
3 t' y# n+ D! y$ v' `7 ? actionForEach =: l" ]9 y2 |% ~
modelActions.createFActionForEachHomogeneous$call3 w5 G% a/ ]. Q
(heatbugList,
& {. R s; k& z new FCallImpl (this, proto, sel,
+ V, {7 ?, x0 B new FArgumentsImpl (this, sel)));0 Y5 `" s: k! \! F; C% c
} catch (Exception e) {& A A0 Q+ p4 X! Y. w* T; a
e.printStackTrace (System.err);
* I& M9 `$ i1 ^% [$ @4 R }! F! E, P* p; _* \8 ?) A- Z
; f4 y: N/ r" U( }6 k4 d
syncUpdateOrder ();7 n) @! }. A; a" F( a+ C9 C
# H5 s- u4 _- T. X+ x try {
( J" s x) W0 V3 z2 o5 ^ modelActions.createActionTo$message
5 n8 @6 E" ?" }# N8 l6 ]1 y+ h (heat, new Selector (heat.getClass (), "updateLattice", false));: i4 N, a+ P( f, u
} catch (Exception e) {& V; O" ~3 ^7 }& w1 J; a$ r
System.err.println("Exception updateLattice: " + e.getMessage ());
1 @1 P, b+ J1 |8 _2 \8 |7 p: f }
( f) z, x# g5 G- K& w K ' h8 b; k/ B0 {2 D8 ?* l* I0 A6 ]
// Then we create a schedule that executes the
! u; F# [/ n/ L. c2 U: G! @5 U // modelActions. modelActions is an ActionGroup, by itself it2 g. V7 D( D, R9 m4 W X
// has no notion of time. In order to have it executed in
9 U) U* ~& H3 t // time, we create a Schedule that says to use the
$ h: P7 X: |2 c1 }9 Q% S* o3 T // modelActions ActionGroup at particular times. This* D* b4 [9 N% W a# @3 b
// schedule has a repeat interval of 1, it will loop every
6 Z, @9 A3 J! Y1 v" S& }' j // time step. The action is executed at time 0 relative to
: @% N6 t3 R8 r. X% a+ G // the beginning of the loop./ t1 o P0 D3 }3 c; o* r
# p/ u5 U" L, D+ c% L9 z9 P
// This is a simple schedule, with only one action that is
/ E+ H1 `7 q* r1 Z( N // just repeated every time. See jmousetrap for more
2 K# m: N# w2 ^! w6 L- D* C1 X // complicated schedules.
: I- t/ L! ^/ P7 o0 Q, C F
9 q% ]+ L0 t |( M& H* ?/ Y* C modelSchedule = new ScheduleImpl (getZone (), 1);
4 H+ N0 o4 L$ F/ l modelSchedule.at$createAction (0, modelActions);1 ~# _, p2 p) a
3 O1 Z. J, J% t! g% e' W! C% V return this;
) B- U9 Z/ Z. ^ } |