HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ k+ i6 s6 B1 v! z
5 x2 ?0 Y9 i6 F& i. i public Object buildActions () {
2 z- y- \# @4 q super.buildActions();
3 x8 f: N3 y3 f2 Z. k
0 v1 o* B( K# V3 U& w! f) f. p+ D // Create the list of simulation actions. We put these in" F; M: s3 d+ V4 |7 h+ d. b6 W
// an action group, because we want these actions to be
$ X3 P$ v @3 f, R! f5 Z // executed in a specific order, but these steps should
- y U4 g% e1 X2 [; ?( j // take no (simulated) time. The M(foo) means "The message6 z2 l) D/ ]8 Q$ C, e
// called <foo>". You can send a message To a particular5 c9 {, h/ m: [8 ]& w( a+ Q7 O, @
// object, or ForEach object in a collection.0 U2 G2 S9 N2 n# @ |* c* O# E
h. N' l) A w* i5 o8 H% Y+ K, s' R
// Note we update the heatspace in two phases: first run2 m1 f, {, h( }" i' \
// diffusion, then run "updateWorld" to actually enact the
: U$ D/ B; E: O4 ~2 a9 R // changes the heatbugs have made. The ordering here is
5 E1 R) A- n3 [! y _; B // significant!
) M1 U: n( M4 l/ A+ d
* z' y7 i, Y U# C7 r- A/ U2 f // Note also, that with the additional: c" B! s0 t0 x8 ~8 j
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 {7 h( v" [( c# ?3 j
// randomize the order in which the bugs actually run
+ E% [1 E4 g$ |" V // their step rule. This has the effect of removing any
1 @. z7 R0 u' D. ~: M // systematic bias in the iteration throught the heatbug) s# \; R# Y1 C4 q1 G4 Q
// list from timestep to timestep3 Y6 J$ q* A5 H* c3 ^
* P! E, l; f8 x1 x w F- j // By default, all `createActionForEach' modelActions have% f7 i( p1 F" Q$ ?* A. z
// a default order of `Sequential', which means that the
% o+ S) i' e( z# G // order of iteration through the `heatbugList' will be
: K/ b8 P* n3 }. ~# O6 c% O8 Y) p // identical (assuming the list order is not changed
% K& ~7 y7 ~: Y* M3 G$ J8 S // indirectly by some other process).7 y+ m) y9 g* g( _" t5 L. B
: K [4 F+ o% m* f+ j
modelActions = new ActionGroupImpl (getZone ());
+ t" i( ]0 U* x+ U; B2 d! L: ^8 r+ B% H( a; ^. F9 f, V
try {
' R; c) I7 e+ D( S modelActions.createActionTo$message; ^8 T) u4 t4 L4 ?
(heat, new Selector (heat.getClass (), "stepRule", false));
) Z [, n$ j3 t# {. Y3 \ } catch (Exception e) {
! i. R8 a; M; T+ a" ^. u0 Y0 ? System.err.println ("Exception stepRule: " + e.getMessage ());
+ S& z& b9 x& V }
6 r' p/ Q2 i0 C% N1 T2 z- o, G# f; ]8 [6 S, T
try {; z& D" b) c4 |0 E+ D7 `, ]" f% p
Heatbug proto = (Heatbug) heatbugList.get (0);8 s( B, Q* j' x. M+ b9 ]
Selector sel = 4 P! U, b" `, _8 E+ k
new Selector (proto.getClass (), "heatbugStep", false);
& q( B5 l: a f& Q actionForEach =8 K1 @' f& |' b& _/ @5 k
modelActions.createFActionForEachHomogeneous$call) |9 e5 L9 h9 n4 s
(heatbugList,+ j3 M8 K0 @; t
new FCallImpl (this, proto, sel,
- g8 k# ~$ E7 i; T7 H new FArgumentsImpl (this, sel)));
8 M( S: g# Z+ V a" b! i$ A } catch (Exception e) {+ x2 }+ A6 B) ~
e.printStackTrace (System.err);* ?; M8 q7 F9 z' }
}8 R1 g: g* y: T
0 \* v& A* o4 P
syncUpdateOrder ();
2 s/ @/ k9 _: M
- t4 g- r' v* Z try { ~% j7 N8 Z% k) A, h2 G5 ~
modelActions.createActionTo$message
9 {+ @+ `( j* h7 i; L (heat, new Selector (heat.getClass (), "updateLattice", false));7 G* d2 U. g; q6 V" D. Z
} catch (Exception e) {
3 T0 A- w/ Z1 W System.err.println("Exception updateLattice: " + e.getMessage ());9 _+ K" B& J8 W
}
0 q. w8 u, O/ N v2 L* X0 s$ D! z1 {
" `9 @5 l+ y8 g // Then we create a schedule that executes the
5 ^" z! q. ~" x) g% R% M // modelActions. modelActions is an ActionGroup, by itself it5 E( x) x; D4 z5 D
// has no notion of time. In order to have it executed in
0 G0 m8 T, q! w3 n. V. N; H; B // time, we create a Schedule that says to use the5 t7 o/ z3 D4 T& f
// modelActions ActionGroup at particular times. This! j' i% g9 U( i5 p( @
// schedule has a repeat interval of 1, it will loop every
; B- R! S" i7 Y! X- M1 {% ]) A // time step. The action is executed at time 0 relative to( M5 |; }6 ?5 B
// the beginning of the loop.) e+ m( @( l9 Y, U3 u
- [; C# c6 S# V& d/ h. E
// This is a simple schedule, with only one action that is
2 p% g' V4 L5 W4 ~& E // just repeated every time. See jmousetrap for more
) Z' c) }. M# ~2 b B% C // complicated schedules." g8 g$ N$ B- S1 q6 r" Y9 Z c
4 o5 a# z1 _- K2 C
modelSchedule = new ScheduleImpl (getZone (), 1);9 `' j1 z. h( f& ^; O0 c$ U8 a$ [
modelSchedule.at$createAction (0, modelActions);
- ]* Z( O, a, p, h. {" {) O" a8 A- N7 U
4 c& q b, N) H0 }4 f& b# c3 F return this;( F( F: M: ]0 c! R0 G
} |