HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( ?& K p3 N* M! k+ |# P
1 j3 G/ C/ I) ?3 t% ~ public Object buildActions () {, p: t9 a) G: s* m
super.buildActions();
9 l, n* K" L! k7 m
8 @0 v9 K/ J- ]% i2 H5 i, V3 V // Create the list of simulation actions. We put these in
, O9 _$ ^$ K, \9 {5 U // an action group, because we want these actions to be# V" M, f- {3 |) I5 ]) Z8 t( t& i( I
// executed in a specific order, but these steps should6 r' {) Q j* @ z4 r$ u& B" ^
// take no (simulated) time. The M(foo) means "The message: }- u: V1 W$ ]/ Z) E( J; `4 p
// called <foo>". You can send a message To a particular5 h; V, h+ K" y4 \8 T7 Z# {1 o
// object, or ForEach object in a collection.
+ B* C W, H4 q2 o4 p: ^
0 _8 A) E1 ?7 X3 ?/ J // Note we update the heatspace in two phases: first run) V+ r7 ?' n! k k: q3 G3 U. V
// diffusion, then run "updateWorld" to actually enact the$ }3 g1 ^) T/ @7 ~# R
// changes the heatbugs have made. The ordering here is
/ _3 t9 d6 y! `/ \! ? |6 n5 v // significant!
% J2 j& |, w- `2 H9 e
: Z/ C1 ^) [8 H" W7 F // Note also, that with the additional; m9 d' Q a: [9 _' \
// `randomizeHeatbugUpdateOrder' Boolean flag we can
, D; p. |, m1 f // randomize the order in which the bugs actually run
5 b" t9 ~. u: Z" u7 A2 l // their step rule. This has the effect of removing any- k- d7 B& v# B) L
// systematic bias in the iteration throught the heatbug+ K! {* ~6 y, w' y/ s
// list from timestep to timestep6 Q3 _, d5 z0 v8 h
2 J/ f& ^ ^, z& c- G // By default, all `createActionForEach' modelActions have9 N8 p* p. @+ t# M
// a default order of `Sequential', which means that the
9 s3 C" b: U# T- o // order of iteration through the `heatbugList' will be
) C+ m- l2 _% R* Y/ G: ^1 r4 @! x // identical (assuming the list order is not changed
' n p; S+ h5 l5 Y, r // indirectly by some other process).# |5 {- h$ O( U$ l9 r( x- X6 l. y
7 o% E' n3 L1 P& c( S. K modelActions = new ActionGroupImpl (getZone ());# i( i: Y! x; o2 k: X
5 i" m/ {9 z# D) M, X6 B | try {
1 \5 l3 G: ?" e+ e2 e, P modelActions.createActionTo$message
, D/ z; n) Z/ v8 B3 h6 _ (heat, new Selector (heat.getClass (), "stepRule", false));
0 O* G, e# O5 q: I' N1 E } catch (Exception e) {
3 E8 m" E% P6 P+ h# Q5 v System.err.println ("Exception stepRule: " + e.getMessage ());
$ K1 O5 a+ k: M0 V* }( Z }
# [8 l# ^3 N. n- V! N- K
: r3 Q, N7 R2 @4 e+ S" m) F) t try {
% H, A4 s4 i7 B5 c; l. E3 S( i Heatbug proto = (Heatbug) heatbugList.get (0);$ V; k T; G: P$ ^3 t6 e8 A; P1 Y
Selector sel = 5 _3 o& @: x7 E/ k% h
new Selector (proto.getClass (), "heatbugStep", false);. _& T% e# C7 [) `+ p
actionForEach =
6 ?% N& `. m9 S& M/ W modelActions.createFActionForEachHomogeneous$call; k1 @3 y: u" f, j6 b: {' A9 y
(heatbugList,
$ @5 Y, B6 p! y5 |: R new FCallImpl (this, proto, sel,; J) I9 P3 g$ I- ^
new FArgumentsImpl (this, sel)));
5 T" H* v% \% M. e) r } catch (Exception e) {. h: h1 C/ R, n2 l+ ?
e.printStackTrace (System.err);8 G# c) g1 B4 T5 U8 I
}% f3 a- n( }. T6 B' P
( L' u/ f' a+ X2 C syncUpdateOrder ();; {9 f8 \* N, C1 `+ E. g
9 F& ]8 G* J( F; \6 B try {
6 h# y' F' n. W* B8 R7 K! ] modelActions.createActionTo$message , n! N8 U# V/ i4 U1 m ~
(heat, new Selector (heat.getClass (), "updateLattice", false));0 |9 X( {$ }; s7 @. }" W
} catch (Exception e) {
" N; l$ \/ A. O9 l System.err.println("Exception updateLattice: " + e.getMessage ());: S, R4 v) E0 c* @% M% f
} m% o! o7 y5 q1 y2 r
# l: p9 I9 ~" I2 B4 I \& j // Then we create a schedule that executes the, a* l) s0 H" T% ?) h: k9 F
// modelActions. modelActions is an ActionGroup, by itself it0 ]. A; x C: i" B* {
// has no notion of time. In order to have it executed in
0 R1 q8 ]: [; m# |4 |0 Z* b1 ` // time, we create a Schedule that says to use the
* P6 h, H& \8 U // modelActions ActionGroup at particular times. This) ^5 H) ~8 U; p$ `5 @
// schedule has a repeat interval of 1, it will loop every m1 P x. r. d) v
// time step. The action is executed at time 0 relative to" @. E, Q/ Z: L* D" \9 g0 ^0 k
// the beginning of the loop.1 N0 |% Q0 }3 r7 W9 r6 o% |' j3 }
5 _/ U( j. z8 E" B" w- f, U // This is a simple schedule, with only one action that is$ Q3 ]6 z4 M7 r$ }
// just repeated every time. See jmousetrap for more
! K$ [" n, S* P( v" ~. |+ d2 J // complicated schedules.# A1 W; Z- l, Q% {: v
6 V1 p, N) a S modelSchedule = new ScheduleImpl (getZone (), 1);
+ n7 [' F7 w2 u8 i0 G+ \; U+ ^ modelSchedule.at$createAction (0, modelActions);
; l' t: v4 h$ P) k ( g3 `! D; x% j* t7 b7 H
return this;
/ T0 ?. p. T: ^. e8 w } |