HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. J. _6 z: M7 s! K- i/ P% T9 Z/ p
* N% R; p: c3 G! {- I+ n5 j1 D
public Object buildActions () {
' M5 t, A/ n6 I' ?1 [) o# c super.buildActions();5 Z; Q, i: r) w1 h- ~; p% D
: R/ w( l3 d- P5 M // Create the list of simulation actions. We put these in
$ ~ ^( a; i: i: G9 y ]6 i // an action group, because we want these actions to be
; D3 c* s5 Z6 y6 q' Z // executed in a specific order, but these steps should
W* O. P5 p3 u# I // take no (simulated) time. The M(foo) means "The message
4 |$ C( Y! }0 l2 V // called <foo>". You can send a message To a particular
) D9 S2 l) Z% K! ^' m // object, or ForEach object in a collection.- I* }1 B, E9 s W2 D1 s
$ m& I5 U/ m! @ // Note we update the heatspace in two phases: first run
9 k; g7 K/ g0 N6 {& A7 Y9 U" Z( |$ C8 p // diffusion, then run "updateWorld" to actually enact the
; ]- X5 H: p2 Q( E+ o/ a3 `9 L' S // changes the heatbugs have made. The ordering here is
' i* a; I7 s; H, V. l6 O" x `/ i& ? // significant!: o, x: H3 g9 w% @; `; e
- o" d. w/ p( t! Z
// Note also, that with the additional& N0 E5 @( u) V: O
// `randomizeHeatbugUpdateOrder' Boolean flag we can3 ]! B7 }" \+ i" y+ Y, I
// randomize the order in which the bugs actually run
( i" g2 v7 X; d" p // their step rule. This has the effect of removing any0 P% f+ T8 T5 ?' e
// systematic bias in the iteration throught the heatbug
- {: V: ~ `9 t( G% c // list from timestep to timestep( X2 x; B g. i; w% |
5 J X" c$ y& ~' _* _7 w: X
// By default, all `createActionForEach' modelActions have
; p! o/ P' l2 F4 M // a default order of `Sequential', which means that the9 p2 i) Y: l; E) p/ J5 _5 a, e3 `
// order of iteration through the `heatbugList' will be) V! I3 o) v$ g+ J
// identical (assuming the list order is not changed& T' r) N$ T- Q7 c$ n3 `
// indirectly by some other process)." k9 c. S' r8 i+ z
2 F. K2 ?7 v6 E6 i' T! d
modelActions = new ActionGroupImpl (getZone ());5 O( P% B3 h5 P
/ F, |7 s. Q( s5 E
try {- K; M7 a n! e, M5 ~6 }
modelActions.createActionTo$message J* u8 k2 Q9 ~$ s
(heat, new Selector (heat.getClass (), "stepRule", false));
' r' R( c* R# c S, V% } } catch (Exception e) {
) _# x4 ]# s, a System.err.println ("Exception stepRule: " + e.getMessage ());2 ]+ C$ g* Q$ g
} A8 M$ L) O8 s7 p2 m
/ {7 a9 U# }! {0 m try {( U+ i% b8 }% i( n& d
Heatbug proto = (Heatbug) heatbugList.get (0);3 c" d$ ]* X% G$ `2 c
Selector sel =
* |% w9 U+ X. D9 v/ h* L new Selector (proto.getClass (), "heatbugStep", false);8 e1 A+ I$ W" r3 U$ _( A+ @
actionForEach =
- l# ]) [3 _8 K3 v, l, G/ { modelActions.createFActionForEachHomogeneous$call8 _, Y/ [: c3 ~2 U2 B
(heatbugList,5 E9 u4 [7 x/ ]/ s
new FCallImpl (this, proto, sel,
: x3 K& L1 Z4 h& N) ~4 J/ j new FArgumentsImpl (this, sel)));
5 H1 l+ g2 j9 Y& H } catch (Exception e) {
: o) \& k4 K* U6 m5 j+ _: l e.printStackTrace (System.err);
* U8 U5 P# r$ Q$ ^7 r$ W7 f }
% T4 d2 z1 M: ?9 C
( b8 j, r% V9 B \) X syncUpdateOrder ();
. A Z$ j. C, P- U& T- k8 }
$ x/ |, } z- \% G) | try {- _5 k* G0 x" ~( p. u4 b) J: o: F
modelActions.createActionTo$message
5 B2 G6 y" } _# }6 } (heat, new Selector (heat.getClass (), "updateLattice", false));
; C& s% O, a* H$ w } catch (Exception e) {
, q( B# x! b! t. P- C5 z2 S W System.err.println("Exception updateLattice: " + e.getMessage ());" D4 a! W: s! e. z( h
}
3 a; m8 a" @) G4 Q# t' l7 ^- {' C) \
9 b* E- v) X/ s" \- \ // Then we create a schedule that executes the+ D' m2 O. p% ?3 J# H% A0 u# V# Y1 Q
// modelActions. modelActions is an ActionGroup, by itself it
* v' z) I ^: {& ?( A // has no notion of time. In order to have it executed in
n- R6 _$ ^7 E* U* P: A# l. A) w // time, we create a Schedule that says to use the
' j' n7 ]" Z! j" y8 M // modelActions ActionGroup at particular times. This& Z8 U: I& p- F. U$ h2 @, j
// schedule has a repeat interval of 1, it will loop every8 w8 w$ t! c7 Z( N8 q) e2 @! h
// time step. The action is executed at time 0 relative to+ L6 D. s# y+ s3 |, S! G1 e; A9 e% a
// the beginning of the loop.
1 W& [ M( H# h# z' w
( x- m8 i& D8 V; p0 ~, _" F // This is a simple schedule, with only one action that is! y4 g7 t$ c; M) W. H4 a
// just repeated every time. See jmousetrap for more& m' k1 e! R" T$ Z- S$ L2 {4 g
// complicated schedules.
, E0 O. u2 `; V2 j7 H) Z ) { R, O4 b( X2 @
modelSchedule = new ScheduleImpl (getZone (), 1);& K/ Q7 a$ o6 F) {- d$ K9 \% H
modelSchedule.at$createAction (0, modelActions);4 O4 k, u* ]/ e' f2 }
2 r* T8 M* \9 \) o2 ? return this;
! g, Y- w' z* o" b } |