HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% g6 \4 c3 }7 {* L, X, S r
. F$ l: T1 ~0 S. f" _ public Object buildActions () {
' a9 L8 `) \ ~/ t7 f super.buildActions();* ]! C# P) o# z- u
4 |. z' z3 v/ b" j8 c& _2 l // Create the list of simulation actions. We put these in" F% K: C2 j. |0 [% v$ i, S& U7 @
// an action group, because we want these actions to be
! C2 U7 A. M* h6 O5 A% Q ?# w$ ? // executed in a specific order, but these steps should
- z' p( E% U$ ]) f5 }8 Z // take no (simulated) time. The M(foo) means "The message% _* Q$ I: l" M3 K' m. c
// called <foo>". You can send a message To a particular
! ]$ E1 |1 G4 I) D- t // object, or ForEach object in a collection.% f; f0 W: H8 v0 t2 _: ?
8 o. l5 V5 U/ l& c- @* r // Note we update the heatspace in two phases: first run1 b# M0 g$ J- q
// diffusion, then run "updateWorld" to actually enact the
: _5 z9 q, P) l0 f2 U, I // changes the heatbugs have made. The ordering here is% H/ g6 H8 c+ N+ `1 x, |
// significant!* T; x8 b5 b% h# Y
[; [9 Q: h4 u
// Note also, that with the additional
8 p1 L( Y3 b) S6 N! O // `randomizeHeatbugUpdateOrder' Boolean flag we can3 H" H# m6 n0 n, N( S
// randomize the order in which the bugs actually run
" x% C3 J( T! ~+ X- E // their step rule. This has the effect of removing any! @ i- |, r9 x6 n
// systematic bias in the iteration throught the heatbug
! Q4 P2 @+ n. V# r$ S2 q9 J. `# k& N4 s // list from timestep to timestep
( e1 V, [) I0 W8 K3 e5 ~3 ]2 i
3 y \$ t+ |# r# N! s! C // By default, all `createActionForEach' modelActions have! L/ B. y' E! ~0 W/ U) D" p' {
// a default order of `Sequential', which means that the
( p9 A9 W0 w# o( u; `2 G // order of iteration through the `heatbugList' will be) {9 v1 k m. t ~$ @' Q7 ?
// identical (assuming the list order is not changed2 ]" C [. [* h( ?9 L
// indirectly by some other process)./ X; ^; e7 N" P) l
4 f. u4 f, S- U( W& X, J
modelActions = new ActionGroupImpl (getZone ());
1 K$ m; `; j" N+ h
% |0 Y* A' J# x try {
! B% C, h& \/ ~+ g3 k b modelActions.createActionTo$message
7 Z! A: n# }4 t# N9 h0 G% F D (heat, new Selector (heat.getClass (), "stepRule", false));
! d$ h% b) V- |& b' c } catch (Exception e) {
% _7 o- J2 d. @ B System.err.println ("Exception stepRule: " + e.getMessage ());
; `6 O, n* u ?" v8 G }
$ o# E& x1 J" k% C8 K' |$ o
! `1 m; V3 X# f) D7 g" t. V try {# k4 E$ h7 `/ i) \" s
Heatbug proto = (Heatbug) heatbugList.get (0);
, P! [6 ~$ m" a4 J5 \! u5 A7 N/ [ Selector sel =
5 M& J9 h. H7 F! z new Selector (proto.getClass (), "heatbugStep", false);$ t: b7 y0 D% {: |
actionForEach =
$ f! A- T& g0 N6 {& y+ c7 r modelActions.createFActionForEachHomogeneous$call+ J( N0 a" T2 k/ b3 g
(heatbugList,
4 Q% G. Q3 Q/ D/ Z. a new FCallImpl (this, proto, sel,7 ]- Z5 _ r ^4 v" N7 Z6 Q# Q
new FArgumentsImpl (this, sel)));2 D# z( c6 v: x2 {
} catch (Exception e) {
6 x2 a* T# M$ R* f9 R$ ~; p/ b e.printStackTrace (System.err);% r4 c* M1 R' n
}
# j; @$ ^3 n$ T: O, N0 _ 1 _+ m p! m. `% Z& o% ~
syncUpdateOrder ();
, j) r8 t; J$ G0 m+ P, ~( S; \7 g8 ~$ R2 I' W
try {
- q1 @, r. d: L) D7 { modelActions.createActionTo$message / k- F$ @, O" j
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 m8 t7 T4 | S0 Q4 p9 t } catch (Exception e) {6 K% s, t R6 P
System.err.println("Exception updateLattice: " + e.getMessage ());+ P: a, `+ `- B3 S/ N0 u- Y
}. m/ ?7 c: p9 U& {
- i& |4 y* d. f // Then we create a schedule that executes the0 ]. ]8 h- E G; Y6 V
// modelActions. modelActions is an ActionGroup, by itself it) o z) ~1 G% s% Y6 W
// has no notion of time. In order to have it executed in. m! q7 g% H" {7 [$ E
// time, we create a Schedule that says to use the. B3 B4 N" P$ a' [4 {- @0 X1 B
// modelActions ActionGroup at particular times. This- v7 x' ?0 P2 u H3 p8 Q: S% M
// schedule has a repeat interval of 1, it will loop every
8 A, |: j- j. a- Y& D; t // time step. The action is executed at time 0 relative to+ }' T. i9 m* w+ C( l
// the beginning of the loop.8 i4 e2 ^& S7 e" |
# j4 j# u; W( L p. \ // This is a simple schedule, with only one action that is
8 F1 h% Z/ i* V; y0 A l6 o // just repeated every time. See jmousetrap for more
) j! r1 L* Y8 W |; v; q // complicated schedules.) o, Z! T. B$ r8 f9 G+ d8 T/ o$ \
6 W* `( c3 l; I/ ?4 E2 s
modelSchedule = new ScheduleImpl (getZone (), 1);
) e2 ~# a* M' t modelSchedule.at$createAction (0, modelActions);
$ C/ k# N; t( ?9 W6 `4 S
8 V) [5 f j9 W* c return this;+ t5 z- X3 T0 Z% m' R
} |