HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 P3 Z; ] D: f; i* Z7 h/ v* Q8 M# {) l/ } u4 o1 _ l7 A% w- C
public Object buildActions () {
8 Z% i" W+ `6 Y8 k0 U0 B7 Z super.buildActions();
( ~7 S* w# Y3 I% v! q8 | $ ^5 b; F( s+ K" y* q" `
// Create the list of simulation actions. We put these in6 _: [. t' p1 _5 S) Z
// an action group, because we want these actions to be
- \5 b" W+ c5 a8 {9 z // executed in a specific order, but these steps should. G' o2 U# n4 K6 u4 t. ~, w8 g8 Y
// take no (simulated) time. The M(foo) means "The message
/ J$ d7 {) G' y" v: y4 J9 h6 U // called <foo>". You can send a message To a particular. A# N3 R" N/ e6 Q3 N* J2 J
// object, or ForEach object in a collection.
7 b$ Z; {7 f" n6 G' V
: ?: H5 D6 @! Q! T8 I // Note we update the heatspace in two phases: first run
; Z3 _6 d; N4 M: u9 n // diffusion, then run "updateWorld" to actually enact the
( K: F4 B# B; y* W // changes the heatbugs have made. The ordering here is8 Z( _* U& K: i6 [, l& D
// significant!8 c% ~5 A/ c1 C( z9 |
5 Q; f2 s( J3 _0 g, W+ s% j
// Note also, that with the additional7 b. I0 s) J2 J/ }5 G; D
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 x% ?0 h4 E: p& L) p
// randomize the order in which the bugs actually run7 ?: f h: Y8 Y) U8 W( l' t6 F" d
// their step rule. This has the effect of removing any
/ M4 v2 l( \3 G, y // systematic bias in the iteration throught the heatbug' Q: i! V# F! w7 C4 v2 j+ p, m
// list from timestep to timestep
8 _ c3 m- }' W- y * m' U2 B( L9 u" X' d$ g( n
// By default, all `createActionForEach' modelActions have
4 e% T! \! ?9 K) Y* F9 d3 C // a default order of `Sequential', which means that the
& z- f3 j* |* e- a1 j9 x' W // order of iteration through the `heatbugList' will be
9 L& O7 R+ L# U+ s( U, S9 U# S // identical (assuming the list order is not changed
F+ G- @2 g8 A. a6 ] // indirectly by some other process).9 w/ s; e' h: }' C/ G. C$ i; \
! n1 {4 y9 f" z modelActions = new ActionGroupImpl (getZone ());
G* A% G/ O& h6 p
* Z; B% V2 v6 `7 f" N1 n try { ?; k3 _- n. W y( d
modelActions.createActionTo$message7 Z! b! x1 O2 d6 O. H
(heat, new Selector (heat.getClass (), "stepRule", false));) w5 [$ U& c! [7 t+ q
} catch (Exception e) {, h! w8 e* `6 O& g0 m% f7 y
System.err.println ("Exception stepRule: " + e.getMessage ());
4 a! ?0 i' R. [+ y }3 J2 k2 `9 R5 r$ O6 q% A" p+ [2 W- ^
. V- a+ I5 K. L8 b/ h
try {
. E/ s7 S0 i* ~. g. m; }9 q% O. r( g Heatbug proto = (Heatbug) heatbugList.get (0); [% \6 a" ]5 ~. f) c
Selector sel = + d0 T( c# W6 Z0 {& f- c
new Selector (proto.getClass (), "heatbugStep", false);( ~* P5 \# ^# F! Y# _& m& B4 r% f" c
actionForEach =
8 A/ [8 f9 U' z2 `8 k modelActions.createFActionForEachHomogeneous$call3 g1 F4 T7 Y& Y& {* H" j- E9 x
(heatbugList,
. m7 ?/ o, d% O8 C6 `3 ~, @ p new FCallImpl (this, proto, sel,- U5 V6 p: i: I! h+ E
new FArgumentsImpl (this, sel)));. q3 V. b' J; x0 [; N0 z" h
} catch (Exception e) {% ]5 K! ?) C G; z3 A5 c# k3 |
e.printStackTrace (System.err);1 M1 q3 t$ [, q. ]! n3 M2 |* t
}
9 |4 {6 D: h- E9 C, x1 A% a; n 9 R7 r6 U, @" n: S& A# p4 ?
syncUpdateOrder ();
+ K3 v9 l! R% f9 k0 C+ s9 Y- t5 z$ p. T! p
try {
' r2 [3 T) G/ s modelActions.createActionTo$message 8 X" y0 t5 W( a0 d; T) J
(heat, new Selector (heat.getClass (), "updateLattice", false));( @) Q* C9 o& x3 Z0 B% l
} catch (Exception e) {
1 u) O' D7 W8 n! B! s System.err.println("Exception updateLattice: " + e.getMessage ());
6 ^ a8 }7 _5 k3 ]; N5 B }
' q0 k$ m |* y# ^; V2 h
3 _. A" Q3 }' P% ~* t // Then we create a schedule that executes the: X* {/ a& _8 v' p' y% |
// modelActions. modelActions is an ActionGroup, by itself it4 Z6 D0 x4 I, a* _/ ~; b$ l( g; c- |. [& x
// has no notion of time. In order to have it executed in) F7 l% p: S% {6 h; z& t O4 S
// time, we create a Schedule that says to use the
" ~ @' H/ ^7 I+ k) ~2 ~5 V3 ` // modelActions ActionGroup at particular times. This3 _3 a' B: k1 Q, _! I+ F* }" d
// schedule has a repeat interval of 1, it will loop every
4 S5 M0 v: M! a- c. V // time step. The action is executed at time 0 relative to
1 U. `$ e0 A4 A# |% |( ^( L$ s // the beginning of the loop.- b) j, `. v7 J8 u
7 p$ P2 T% I# r; r2 M x3 U // This is a simple schedule, with only one action that is8 G2 m k. A. Q8 ^6 j7 i Q y
// just repeated every time. See jmousetrap for more. t1 R# Y) G! ~, f+ p! h
// complicated schedules.
" R" {% |. _+ A" l0 I% Q) v
$ u0 I6 w- v+ e0 J modelSchedule = new ScheduleImpl (getZone (), 1);
: L1 j- J% q A modelSchedule.at$createAction (0, modelActions);
5 ^7 {6 `* F3 ~9 f, `1 F6 M
4 u. B2 u- }! v1 D( i/ \ return this;; I. t$ d6 r, h" w: ?7 H
} |