HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 m; p" a1 n3 S' D! m
; C1 i6 @2 L) n( [! f
public Object buildActions () {
6 Y9 Z% s( ]! _: _6 T super.buildActions();7 X& r5 Q8 q0 P1 a# g5 f& e. O% G
7 v @. r- M. ]1 ?' m4 K! T% o
// Create the list of simulation actions. We put these in
' L2 g5 x& R" Z6 Q% Q // an action group, because we want these actions to be
" e+ u0 W6 |# }$ \* d // executed in a specific order, but these steps should
) H" O; y; M' b5 X // take no (simulated) time. The M(foo) means "The message
2 C5 O8 F$ ~- [0 @1 H# U) w // called <foo>". You can send a message To a particular
( W+ e: l7 x! L! V // object, or ForEach object in a collection.9 I6 c% @) z: s5 N7 w( l9 G" i3 C
1 e5 F" p7 P( k7 n
// Note we update the heatspace in two phases: first run
) Y1 r3 n/ _1 G) M. u" l // diffusion, then run "updateWorld" to actually enact the, b- M) M I- ^: y: e+ T1 D* \1 F) x
// changes the heatbugs have made. The ordering here is
* a9 y% I: l/ \6 d8 F) o1 h c. D // significant!
. c' t5 `) Z8 v: v' R) S
* E% s* Y' m; F // Note also, that with the additional4 `# X& l% ~' \* b8 F; t9 S, c2 h
// `randomizeHeatbugUpdateOrder' Boolean flag we can# }2 q5 r8 t, m+ q- b
// randomize the order in which the bugs actually run- c' F! v! Z u! M
// their step rule. This has the effect of removing any: \) t- q& i6 T5 s
// systematic bias in the iteration throught the heatbug4 G3 E6 i9 N5 o/ W7 _' p* _
// list from timestep to timestep
/ F' H! d. D* |+ d1 p
! ~4 n' H) ]' t+ U# z3 a0 P6 m" ? // By default, all `createActionForEach' modelActions have; P; Z" \- x% {
// a default order of `Sequential', which means that the# e) g9 ?* d7 b, I" _! y3 y
// order of iteration through the `heatbugList' will be1 q$ a" B* k5 U, L, N
// identical (assuming the list order is not changed
- q' [/ \; ]) O/ m3 |: ]1 A // indirectly by some other process).
# s2 ^- q; R! a% [' V
. Q- |9 V9 a3 A" }. B modelActions = new ActionGroupImpl (getZone ());3 T1 {7 I. M6 E2 O/ T) W/ x
# y! d/ D' C2 H# S1 y' A9 I
try {
0 _3 Y0 E! S7 Q" R# M) B modelActions.createActionTo$message
4 ~: _, n% C, V" k) ^ (heat, new Selector (heat.getClass (), "stepRule", false));( @2 w1 ?& I& m7 m$ Y: U
} catch (Exception e) {( |7 }* T" s; K3 ~# w! n# K- Y* I# q
System.err.println ("Exception stepRule: " + e.getMessage ());
* }1 u3 R, J3 G% S5 R% K& L: E }" {9 w3 ^7 ~1 P8 E
3 t+ v5 @( H1 O
try {
& I. b# w1 W& E7 g+ c8 G2 w Heatbug proto = (Heatbug) heatbugList.get (0);2 d1 }+ |7 \ {2 _! K$ K# F/ g+ w
Selector sel = # w6 H& z: L; p, Z. j4 b7 V: x8 [
new Selector (proto.getClass (), "heatbugStep", false);
7 y3 W- }7 l1 l" Y% Y W actionForEach =
! m {, \: H6 y% F9 b1 P* O modelActions.createFActionForEachHomogeneous$call
* y* X& G8 h8 p$ M w (heatbugList,
" K1 W+ T# Z! p: M. y6 S new FCallImpl (this, proto, sel,4 |5 i/ M) h7 `" A* c
new FArgumentsImpl (this, sel)));6 ]( U8 S- q8 r7 a
} catch (Exception e) {' R6 ?3 R( t, J
e.printStackTrace (System.err);
+ n+ j$ G8 i' ~6 {% ]3 g; p }
# ?0 x) L3 q; F0 y' J, S" Y5 Z
2 _4 W* w9 K+ \. b syncUpdateOrder ();
$ z6 @2 `1 c/ D7 A( l5 R0 p3 C6 ]
! U. i! [3 z( c/ o3 ^ try {
/ X0 f0 z% g. |! S modelActions.createActionTo$message / y; M, Y- f7 g5 c9 h1 X1 Y' Q" ?. D1 u
(heat, new Selector (heat.getClass (), "updateLattice", false));
! B D a: G" K' E$ Z } catch (Exception e) {
3 {! c9 y, g7 i' \" G- ~" S- I System.err.println("Exception updateLattice: " + e.getMessage ());
8 J! u1 T# x$ T' J" o1 ~1 X }- s2 W( e: }; o
6 f2 ]( y9 \; B3 K& b* j
// Then we create a schedule that executes the
8 q; \. `. Z9 O& G7 w // modelActions. modelActions is an ActionGroup, by itself it
. s& i4 r( P6 ~0 S // has no notion of time. In order to have it executed in
- q7 ^! ~2 Y6 f9 \5 u* P7 p2 u' _ // time, we create a Schedule that says to use the7 k5 b0 X2 e ?# V1 ^( _
// modelActions ActionGroup at particular times. This
, H+ ?, s6 W9 v) H; s // schedule has a repeat interval of 1, it will loop every
! E) ?1 U) t3 N* R // time step. The action is executed at time 0 relative to5 H0 Z. f0 |9 f2 w
// the beginning of the loop.
, ]2 x4 {9 M, V1 Z+ B9 a' w
: [3 {5 A. m8 J // This is a simple schedule, with only one action that is
* r3 R; C+ ]8 m9 ~ // just repeated every time. See jmousetrap for more
' f! b+ A, Z) ]! P4 R3 j // complicated schedules.5 m/ `0 W6 M2 \
- n+ \/ |% l8 W0 f4 G6 k o L modelSchedule = new ScheduleImpl (getZone (), 1);
- \8 ]1 K8 t8 J$ m modelSchedule.at$createAction (0, modelActions);3 w, v- f3 V) o8 L) J1 s1 o
0 A5 }0 j2 c; t# V3 M; n return this;, {3 D$ Q: [8 ~+ V- D
} |