HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
N/ h* H( D- N6 t: L
{. p0 _& V U- g2 \" T public Object buildActions () {$ O+ M" y/ R. Z g$ ^( E
super.buildActions();
# b( N W. j- i! X$ a; d1 Y $ I: j0 [3 p9 _% A/ O8 B1 O
// Create the list of simulation actions. We put these in+ p5 K# A \& N* I
// an action group, because we want these actions to be) n M2 j+ w, q9 t. O2 P% y
// executed in a specific order, but these steps should; h, q# O6 i0 ]5 Y2 Z
// take no (simulated) time. The M(foo) means "The message
" L8 f. [ V3 {% J0 K% D // called <foo>". You can send a message To a particular$ c8 z" J( K" z: _; a8 _* l* p
// object, or ForEach object in a collection.4 e2 \& \4 S: Y) u5 a& B# X9 K; [
* S4 u% Y, X4 ^6 W' j7 Z$ @& V5 a // Note we update the heatspace in two phases: first run
+ g5 c9 V) C0 w- b; } // diffusion, then run "updateWorld" to actually enact the
+ O0 {7 R) Q% R- ~9 R' n# { // changes the heatbugs have made. The ordering here is2 q9 n; k' _# N+ L' K& H
// significant!. e$ ?* D, W7 X5 i7 N5 n X
% C# I' q1 ]* ?0 `% M v+ g5 [ // Note also, that with the additional
. J3 C/ b! ^6 g! V$ l2 q c$ F1 a // `randomizeHeatbugUpdateOrder' Boolean flag we can
8 G M- i4 F* m) u. ~9 u // randomize the order in which the bugs actually run4 g6 N1 G9 j- C! Z. `+ L# B0 d
// their step rule. This has the effect of removing any; \. m3 d' r- V A
// systematic bias in the iteration throught the heatbug
9 n- i. u9 S7 ^/ @% l7 d // list from timestep to timestep6 r& m9 x. `' y) O
3 x8 t, k6 l1 \ // By default, all `createActionForEach' modelActions have
4 D3 e( w& @5 c // a default order of `Sequential', which means that the
) D9 R4 C8 H1 Y: { // order of iteration through the `heatbugList' will be
4 e& a8 o3 k) G) U" n // identical (assuming the list order is not changed
7 N& ?- m9 ?3 C% ?7 k1 h6 b // indirectly by some other process).
/ A4 M B; f2 }0 } } 6 l# |: Z8 j" A; s9 ^
modelActions = new ActionGroupImpl (getZone ());
' K8 ~4 ?3 W8 a7 u0 V! G. \& Y4 t9 E( B! J7 x" |
try {
% y1 }5 h9 ]2 f+ M& i+ f) Q# V% z modelActions.createActionTo$message
, _& h4 l0 m4 o! l (heat, new Selector (heat.getClass (), "stepRule", false));
% \) j3 W. N6 u- L' E; ^ } catch (Exception e) {
5 E1 D. F* o- i% ]; m System.err.println ("Exception stepRule: " + e.getMessage ());
1 W8 o6 Z# M* t% j1 Y6 R }6 K7 g. k. \; T2 E1 o
5 Q- u, @6 k6 a2 H try {
# I( U( r5 j+ w/ R Heatbug proto = (Heatbug) heatbugList.get (0);
5 r6 b! W) V; \- f$ R Selector sel =
! [ P! s/ R1 l3 L" h9 R new Selector (proto.getClass (), "heatbugStep", false);8 P# k0 ?0 R) w1 h9 T
actionForEach =8 D% v7 m, \. C* \% H% `" ]% u
modelActions.createFActionForEachHomogeneous$call2 ?$ j) o, |8 m6 ~' d0 t; A" N8 K( d) O
(heatbugList,
5 f3 G" f* k4 t/ j8 V {- [( G new FCallImpl (this, proto, sel,- I$ e( ^5 ]' q# L9 s
new FArgumentsImpl (this, sel)));
& m' {9 ] B! @$ ]. ~6 [; q3 Z } catch (Exception e) {; e) t1 ~* P- g% R
e.printStackTrace (System.err);
0 h: V9 M& I2 H7 [ }: o) ?/ y% g% b2 ~8 l9 ]2 k
: V) `: J1 X v- {: X
syncUpdateOrder ();' s- p+ F; c% H7 k# J
- O1 R# L/ Z5 r
try {
. o5 s/ p2 e6 M# w& @ modelActions.createActionTo$message
- F u4 Y0 A" w5 g (heat, new Selector (heat.getClass (), "updateLattice", false));
6 {( |9 |% ?. W7 u2 s: r } catch (Exception e) {/ x2 b# D" X, `. R
System.err.println("Exception updateLattice: " + e.getMessage ());8 j5 |; { N8 k. B) s# {+ d( U6 x, O
}
. }9 x' J2 y/ ~ 9 E3 s: ?5 x. q9 Y4 s6 z' O. z
// Then we create a schedule that executes the% q4 k- q2 W' J1 L7 q
// modelActions. modelActions is an ActionGroup, by itself it
6 s( t6 ]# K# d& ` // has no notion of time. In order to have it executed in' J2 v' d7 Q& ~4 R! [
// time, we create a Schedule that says to use the
$ U C) E3 `- p; X) X' c$ L1 O // modelActions ActionGroup at particular times. This& x8 X4 ]3 {( i! _# L! ~
// schedule has a repeat interval of 1, it will loop every4 E0 F% i/ c9 b* v( W
// time step. The action is executed at time 0 relative to
9 L: a* g3 D& Y# } // the beginning of the loop.
8 H y0 ~% y! R1 e' j" z1 y
5 x# I" k% P! ~/ i. ] // This is a simple schedule, with only one action that is
" `( n9 c% W3 \# o // just repeated every time. See jmousetrap for more: r4 B, W- d+ Z
// complicated schedules.$ q# a4 {9 k! I& W2 w+ \3 z
, |5 x3 x$ ~2 ^; C ]8 W2 c( p4 r modelSchedule = new ScheduleImpl (getZone (), 1);$ m$ Q; ~0 o' ~" L' `
modelSchedule.at$createAction (0, modelActions);
9 v1 V! X- A+ T
5 b, m* h6 D% r( M5 j+ c: g0 D0 H return this;
$ R, D) c- n& W } |