HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 q" N5 k }5 j" V5 G- @" V" C0 U* }- O, d" q: `1 e; w2 q
public Object buildActions () {
1 l0 _7 ^1 r$ {' f' m8 W super.buildActions();
7 Y. g1 }; }( X0 @& l 1 A' N( I% O1 d# t8 b, ^ Y
// Create the list of simulation actions. We put these in5 D! s& l( W+ d3 E- y
// an action group, because we want these actions to be) b9 b9 }0 G C* O0 z( ]
// executed in a specific order, but these steps should1 i" k+ s, q; y6 G/ o3 [2 A4 M4 }
// take no (simulated) time. The M(foo) means "The message
( s2 ^# D0 o6 H r9 U+ `) x& a // called <foo>". You can send a message To a particular4 n$ P, S, m8 U; l8 H) B
// object, or ForEach object in a collection., p; K, l6 s' n0 |
2 R# c5 M$ K$ Y+ O% p
// Note we update the heatspace in two phases: first run- [& W4 ?! _" H J
// diffusion, then run "updateWorld" to actually enact the: s, j: f( e. o
// changes the heatbugs have made. The ordering here is, |) }/ I; |3 ]5 ^6 a4 O
// significant!
0 o- `# F" h1 R7 c }2 p! x$ `6 q
" o2 ^% b- U, i/ p# \( c // Note also, that with the additional
- l9 c7 v- e J4 N // `randomizeHeatbugUpdateOrder' Boolean flag we can
# ?9 o( U3 e8 X5 U$ ~& e // randomize the order in which the bugs actually run; t$ N8 p* R- @) G& g
// their step rule. This has the effect of removing any
* B0 u6 x& t! y5 X+ D+ G4 ? // systematic bias in the iteration throught the heatbug
2 A$ y, k9 T- g // list from timestep to timestep
E/ }, w8 p" J; `: k- A 3 p* k5 |3 w* N# l J) k& `
// By default, all `createActionForEach' modelActions have
" e/ h# ^# D4 a. l! X( R; A // a default order of `Sequential', which means that the3 }! i) H0 q8 o, g7 L
// order of iteration through the `heatbugList' will be/ U" W) [5 I6 V* k, `. T
// identical (assuming the list order is not changed) `) B9 q* Y; c, M) D' k
// indirectly by some other process).
) A5 W4 ?4 t. k& G) U0 Z& e ; s" x$ y# E) l5 ?2 @2 d) J
modelActions = new ActionGroupImpl (getZone ());
1 |$ J. K! W/ g# A, f8 @5 J) E8 o
. B% W' {1 _7 _2 A$ w* d2 L try {# N, K7 d r+ y- x+ t$ ]* z+ f/ l
modelActions.createActionTo$message
- [ I- P( S" _7 G1 g (heat, new Selector (heat.getClass (), "stepRule", false));: ?3 E$ e' }* {# G$ w' v) b+ j: R
} catch (Exception e) {) g) L0 s9 @% f/ r% j7 W
System.err.println ("Exception stepRule: " + e.getMessage ());
/ J9 n1 y* L1 {+ ~ }& S3 `" E! V3 f4 V- v
6 k2 ~' |; x8 j8 r5 L
try {* e0 d2 z& D$ x' r' }" v7 H4 m
Heatbug proto = (Heatbug) heatbugList.get (0);! P* f; O$ ]: v) j
Selector sel = ^! o6 ~ m7 ]4 ]( A
new Selector (proto.getClass (), "heatbugStep", false);& I1 c3 E1 P! g& d9 Y8 J7 s
actionForEach =
$ E5 k* v: d% W4 C! K, g modelActions.createFActionForEachHomogeneous$call$ p2 D) B+ ], I9 d+ T& ^& J
(heatbugList,
9 i# o0 O* ]& E9 I: k0 k4 d( k2 u( ~ new FCallImpl (this, proto, sel,
: E7 A1 `+ B2 S, T( B new FArgumentsImpl (this, sel)));
$ h" z/ P$ h- l- h2 f } catch (Exception e) {
: ^( _6 a: q/ q e.printStackTrace (System.err);, y& |& \( ]2 N' V* A2 B2 }& u
}; c+ K8 K; E, `( l) h( ^' |
9 c, i9 a2 u& x* j* W' B! `
syncUpdateOrder ();" Y& t7 c" I' g# u; \) ^0 J
; n+ F r' \0 F: O1 @7 K
try {
: X9 O' U4 G4 p! z modelActions.createActionTo$message f% U8 u# K4 W4 R# D
(heat, new Selector (heat.getClass (), "updateLattice", false));& \; u3 \3 X+ b$ w) K
} catch (Exception e) { X' ^: _' F. S: g/ `
System.err.println("Exception updateLattice: " + e.getMessage ());
$ t# k; M+ m% M1 X- E1 S8 X9 I }& e5 V$ T% a9 L% f
" w3 j- e. Z' M* ]+ K // Then we create a schedule that executes the7 Y: h3 ^. I2 S0 D
// modelActions. modelActions is an ActionGroup, by itself it& L8 {( G5 U& S* T$ D7 y
// has no notion of time. In order to have it executed in
2 O' |' }; b6 r; b // time, we create a Schedule that says to use the
% U; x6 l X. U# c9 P9 g // modelActions ActionGroup at particular times. This7 O) O+ w1 P7 K: L4 y$ f
// schedule has a repeat interval of 1, it will loop every# M' \- s% L8 S) g3 J2 Z5 W; c
// time step. The action is executed at time 0 relative to1 p9 J$ W1 l' t9 a
// the beginning of the loop.3 u* u) n5 [, `. G$ s# J5 j1 @+ D3 j6 v
1 x/ k- ^4 @6 n: {
// This is a simple schedule, with only one action that is
7 `+ d( v0 C3 n$ U6 } I7 I // just repeated every time. See jmousetrap for more4 o: R! Q& D" ^$ i5 ~: p
// complicated schedules./ X O- M3 |9 ^/ e6 r' T N* t
% e4 _/ R: F( P modelSchedule = new ScheduleImpl (getZone (), 1);" E* U& @ Z6 {% n6 K7 b7 d
modelSchedule.at$createAction (0, modelActions);
2 m7 J+ ?# `" M N# O
: s& q: k9 m1 K- t- G5 u return this;+ r* G+ ?- @! e4 F" i
} |