HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% |* Q" L" y8 M4 a$ ?8 }- z$ o6 a+ ^' L4 l% Y# A
public Object buildActions () {1 T8 u: T. Y9 ^, p, z1 P
super.buildActions();
: Y- ]* y: X4 [+ }: _; Q
) c( P9 t0 K* O // Create the list of simulation actions. We put these in
' r" j8 l1 k9 P // an action group, because we want these actions to be8 m5 a5 {0 d/ a
// executed in a specific order, but these steps should# }2 m6 L) T+ I5 W$ t* |/ I6 F
// take no (simulated) time. The M(foo) means "The message
+ Q$ V$ \$ Y: L' K' l4 ~ // called <foo>". You can send a message To a particular" t, p" p$ g. [- {
// object, or ForEach object in a collection.
) D& I Z, b# l4 E
7 c- I2 S) _- h" L; \# R // Note we update the heatspace in two phases: first run
6 e f' E2 Z5 A2 Z$ c! e2 Z // diffusion, then run "updateWorld" to actually enact the$ [6 w% m# g% i: H9 B8 P9 u8 Z: |
// changes the heatbugs have made. The ordering here is. ?. A+ j' t2 e; S% _/ N$ z
// significant!
( E9 b% l. v7 ]! X : k, E6 i9 _, a8 t
// Note also, that with the additional
% b9 }2 ]& o* G // `randomizeHeatbugUpdateOrder' Boolean flag we can
) \5 n0 i- R& a6 U/ C // randomize the order in which the bugs actually run5 H5 _, V+ {1 |8 u- L- |
// their step rule. This has the effect of removing any
6 {- n9 D- k1 k1 N0 \3 |+ v; o9 b // systematic bias in the iteration throught the heatbug
5 ]/ M# [1 N) v) D8 A5 H' U0 E2 T // list from timestep to timestep
. h5 @4 c! R6 [9 B ) t: ?, i1 b7 y& O% X
// By default, all `createActionForEach' modelActions have' W5 H* k: Q4 O
// a default order of `Sequential', which means that the* r1 E; ~" _* T/ f
// order of iteration through the `heatbugList' will be
+ E" Q) M8 m0 h- M4 p t$ S // identical (assuming the list order is not changed
[& O& @# U6 S2 N5 Z // indirectly by some other process).
; y' D) l& f0 t" { ( B. z4 J+ u: b( d; t1 j- u0 C7 W, X
modelActions = new ActionGroupImpl (getZone ());( [6 l* \% J9 x7 W$ L
( m& G$ x# p8 a* O- b try {9 t. B& b1 K; a- |
modelActions.createActionTo$message
2 ?0 R$ s# W' o0 G; a (heat, new Selector (heat.getClass (), "stepRule", false));
; D: J0 y7 M$ L# H* Q1 Z! W- x } catch (Exception e) {
F1 E$ `: z% p8 C System.err.println ("Exception stepRule: " + e.getMessage ());; y" C; c1 D: c# Q
}
, J0 Y% G& F4 E9 U# Z. X8 E1 c1 e8 _: A) |- U: e5 Q
try {
7 L* u* ?/ r5 \4 W: |4 O, } D Heatbug proto = (Heatbug) heatbugList.get (0);9 c& p% Y7 G; @% V
Selector sel =
: ^% T. J' E* p+ w3 L* X# L+ G U new Selector (proto.getClass (), "heatbugStep", false);9 h9 r) A6 X ^5 O4 ?% [. ` P
actionForEach =
- u% T7 k3 u* Z modelActions.createFActionForEachHomogeneous$call( F% S/ Y3 X' T. f! {
(heatbugList,
- h5 W2 U: } s" A5 @ new FCallImpl (this, proto, sel,
( \( i3 N. ]9 i1 H new FArgumentsImpl (this, sel)));
! @' l Z8 Y7 H" d- D } catch (Exception e) {
: V; B3 ?' C& T4 }1 h/ X; L7 j e.printStackTrace (System.err);8 J8 R1 K/ y# z1 u
}; E+ } F$ v, U4 ]
: Y' C: m4 n) V. h' h syncUpdateOrder ();- R) }7 W0 Z1 e4 ^3 c; N$ j& S1 q
( W' p' |8 W" ]4 ^, @9 J. j. a try {
# \6 N) h/ Z) V modelActions.createActionTo$message 1 S. g: w, K2 z X T; x
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 z% ?- N" a' k( P$ M& h3 y: l0 n } catch (Exception e) {3 W/ \! N2 K1 f/ U
System.err.println("Exception updateLattice: " + e.getMessage ());% \; c6 v1 r8 O8 }/ @: G6 z
}* Q8 {2 `/ U; G2 n
: R+ d9 l4 Z, m9 d$ R& g" }
// Then we create a schedule that executes the
4 I1 ~! a; }: b: Z2 I. p% G // modelActions. modelActions is an ActionGroup, by itself it
( T6 n) ?2 o" | // has no notion of time. In order to have it executed in
" r$ U+ f6 O1 c1 I! e- H // time, we create a Schedule that says to use the
0 q" D3 J/ w H // modelActions ActionGroup at particular times. This0 ?. X5 _9 y; {) V; k& t: U
// schedule has a repeat interval of 1, it will loop every E% [- S' Z) Y9 q7 U7 m* v
// time step. The action is executed at time 0 relative to) E5 W# j4 u0 U2 u3 `! R
// the beginning of the loop.
2 _* k, I8 I/ A. R0 x, W
/ w/ W# o1 H+ p5 `6 }1 l; k // This is a simple schedule, with only one action that is. N. ?* f" n# [% E) K: x) @1 k: u
// just repeated every time. See jmousetrap for more
3 {; q( v& m# G; q& s+ W // complicated schedules.
, A6 |& U- ^* T8 f2 t) | & B% y. ]8 o4 W1 Y3 i
modelSchedule = new ScheduleImpl (getZone (), 1);
* k7 y# A, v' L o modelSchedule.at$createAction (0, modelActions);
% X! v& w+ Y Z, L1 p6 m % m6 x% k6 f9 O) X& A6 _
return this;! Q# K; f3 K& k9 G* M5 t
} |