HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; J# H; F% |# i/ W j% G% ~" J$ ~! |
3 p+ I' U0 n2 [- P$ Z" t% ~
public Object buildActions () {
) D$ n, u1 x( u( | super.buildActions();
; E% Q0 q: Q1 F) W( U$ Y 6 A: H; M. C' H4 E. R% V
// Create the list of simulation actions. We put these in
$ ^/ w8 h- Q0 y; X; F! y // an action group, because we want these actions to be2 v* f7 ~* t, s r; j/ u
// executed in a specific order, but these steps should7 a4 z4 K: ]* L i, v
// take no (simulated) time. The M(foo) means "The message
" f# K& H* O: q" A$ _* e // called <foo>". You can send a message To a particular
; L+ E2 z$ i5 g1 C. \# K+ D7 v0 S7 z! I // object, or ForEach object in a collection.
/ c5 c5 w# B# x& f" ]9 P7 U
; v8 A& j& ~3 j3 M" N) c // Note we update the heatspace in two phases: first run: M- g" m+ p, ~( {" o: w6 c
// diffusion, then run "updateWorld" to actually enact the, m& @ P" K4 B% W+ u' X
// changes the heatbugs have made. The ordering here is! ^, u1 Q$ `+ H6 m) z
// significant!' t* T' W+ r& s0 m9 w4 ~
& l: a3 x6 B) K( V0 ] // Note also, that with the additional* ]2 X3 q2 ^2 g" ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can3 E( y' N" W! l2 N1 ]
// randomize the order in which the bugs actually run6 \: X' d5 m9 C$ T9 o
// their step rule. This has the effect of removing any8 z) G- f$ l3 T' u
// systematic bias in the iteration throught the heatbug
8 H+ ]1 Y r- @/ j/ D( G3 q // list from timestep to timestep! S8 }; f# i, t8 `! Z5 Z( ^
! b' K" v, o6 u' I
// By default, all `createActionForEach' modelActions have
, _4 N) e4 f3 x( k+ D1 f4 p" A // a default order of `Sequential', which means that the' r w" M+ }' ]
// order of iteration through the `heatbugList' will be3 H/ {0 J- O4 ~
// identical (assuming the list order is not changed
: @) d+ o' I. G* l& p$ v // indirectly by some other process).$ u. a: f+ _+ G4 U
1 P" o; }2 b8 X modelActions = new ActionGroupImpl (getZone ());
. \3 ^- G$ @1 M
* N W' X: a& b. e) e, z& I; v3 X5 h2 C+ H try {
# e( ]0 x; R% X5 F3 m0 g modelActions.createActionTo$message/ z4 Q h1 p: A" D5 i8 s/ {: R% U
(heat, new Selector (heat.getClass (), "stepRule", false));7 H) r4 }! W& y5 F0 r' Z
} catch (Exception e) {
6 [" u( g' ?" U: {2 I0 c System.err.println ("Exception stepRule: " + e.getMessage ());
6 F" v. u2 m4 S% t( p5 Q }
4 J1 ]5 I( t, F9 J! Y: C4 w
2 ?) z8 [; B" e/ R8 T% e try {
0 Z' _4 c) b" F6 U3 k# r! \ Heatbug proto = (Heatbug) heatbugList.get (0);. }9 w: B; {# T7 b; k- E8 ^' R
Selector sel =
4 f# ~* @2 K1 X! m3 p4 P/ b: E new Selector (proto.getClass (), "heatbugStep", false);' @5 M/ t, m* S& A3 n
actionForEach =
2 ~, e2 U- \ J, r4 T$ O modelActions.createFActionForEachHomogeneous$call
9 W: Q# a, B0 ` m% p (heatbugList,
. t) u8 R ^2 n) T. k1 v: V* V new FCallImpl (this, proto, sel,$ L) K+ Y8 R7 {' H3 s2 X3 Z/ J
new FArgumentsImpl (this, sel)));# V7 D2 ?# F4 N
} catch (Exception e) {6 O1 Q" T h# S
e.printStackTrace (System.err);, F& a1 m4 Z1 I+ K
}7 R( L9 s% f k
7 \& E9 N* Q$ u, B) n4 U4 i3 m syncUpdateOrder ();, a0 T i& J- e0 j$ U
# P1 d; I2 Y8 m4 V- ]5 i! _ try {" W/ T4 l' @' q# e) Z" G6 P
modelActions.createActionTo$message
0 U& Q! T2 S* k8 q (heat, new Selector (heat.getClass (), "updateLattice", false));
( ^5 l1 O" y* R+ d } catch (Exception e) {
- ]' ~! M5 M6 b' R. u7 q1 _- o System.err.println("Exception updateLattice: " + e.getMessage ());7 Z1 j2 G" ?5 l7 K3 e% H- V
}
3 f/ t# m0 t& a+ g
8 C2 s& ~; _! N5 i' a // Then we create a schedule that executes the, x/ h' ~" B# B, x) @
// modelActions. modelActions is an ActionGroup, by itself it
4 `" T8 D- D0 m# ] // has no notion of time. In order to have it executed in
0 b4 w& ]7 ~ K- b6 b // time, we create a Schedule that says to use the
6 W G ^5 G# B8 `( n! d! [/ Q0 U // modelActions ActionGroup at particular times. This" a& D6 R! Y: U, Y
// schedule has a repeat interval of 1, it will loop every
4 N8 U; a0 p0 D- \7 e- | // time step. The action is executed at time 0 relative to1 x1 r2 w- L$ m4 t
// the beginning of the loop.
+ A+ Z2 B; V2 Y( s
. B# q& d5 f8 ~' a% L9 ^ // This is a simple schedule, with only one action that is
7 m7 t) N2 N7 F8 T2 N // just repeated every time. See jmousetrap for more' G: {; ^8 Q) d0 I! ?) R- m' B
// complicated schedules.6 e( _; z7 h( h! U" W+ P" u! X
( r- w: \4 U! g# V modelSchedule = new ScheduleImpl (getZone (), 1);
5 Q8 \" \2 G# _ modelSchedule.at$createAction (0, modelActions);' R! V3 ]8 Q p2 v! \/ }
5 Q# K+ W d- `$ j: T) z return this;. h! {4 N! q6 t
} |