HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. P. L, J; Z3 `) W, p9 G( @+ [
( J( B7 t( [$ Z1 F3 X+ F) m; { public Object buildActions () {
5 V8 b( I1 g9 l6 z3 d9 T' |8 O3 w super.buildActions();
2 e; p [, @& B5 X3 X, g9 P
6 M) ]0 R, E; Q5 A7 r // Create the list of simulation actions. We put these in. o! X. Q8 T9 w& P( l& k. {/ @. W( n
// an action group, because we want these actions to be
$ p$ D1 f4 J; E! ], Z* |4 i7 Q9 z" O // executed in a specific order, but these steps should
U, t3 f' r/ ^4 A5 N. F! V // take no (simulated) time. The M(foo) means "The message" H+ B& m% E# z$ q- u
// called <foo>". You can send a message To a particular
) D" W3 p' q9 E" i% s // object, or ForEach object in a collection.
$ q7 d: r; k. [9 Y! h7 S 8 u1 V, K# p; t# P: ? \* q! }1 p! @
// Note we update the heatspace in two phases: first run1 `; D; v# x+ H
// diffusion, then run "updateWorld" to actually enact the
. u, h7 E, ] U // changes the heatbugs have made. The ordering here is' w( A* s1 O" E/ D
// significant!5 [0 M) c% u% f2 ^" ?% a. v
6 h2 ~% k4 |% ?2 e+ A; d* v
// Note also, that with the additional$ Q w6 S3 ^5 s! ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can
% b0 n3 w0 k* A4 J$ P0 ^ // randomize the order in which the bugs actually run
' W4 P3 D6 a5 g: C$ j7 X4 _ // their step rule. This has the effect of removing any$ Q) E3 t( e" ]3 A* |9 p
// systematic bias in the iteration throught the heatbug
4 e6 R; A& {' Y7 V6 {0 }4 x& U // list from timestep to timestep8 A, k2 _+ q. W {0 E
0 s* `: m0 M7 r& @( M // By default, all `createActionForEach' modelActions have
! g3 @. @( e. u6 }6 t( J* s // a default order of `Sequential', which means that the
* I/ D* V7 C9 b // order of iteration through the `heatbugList' will be- I$ L# C7 h! I9 e$ l
// identical (assuming the list order is not changed
; |6 m. d; p0 i- j2 ]5 w // indirectly by some other process).
+ T, q. A$ O: f! T$ F
' s* D( N4 ~9 B modelActions = new ActionGroupImpl (getZone ());
" y% u/ K) E }$ M( F9 Q
. W" f4 z6 h9 F3 O* p2 R try {
- g' R9 b4 w6 P) f! D* H modelActions.createActionTo$message- o' S: c! [7 B. w
(heat, new Selector (heat.getClass (), "stepRule", false));" E" y4 V; x: [) j1 J" C4 y% q
} catch (Exception e) {
( X1 h- R, }3 n: Y System.err.println ("Exception stepRule: " + e.getMessage ());6 }4 c9 p+ T6 B: H- R1 V' J
}6 M( B: C& s2 w# X0 _& k- h
/ u$ B i- [; Z# @: O# D4 J
try {
8 i: L0 ?: t7 x8 q f7 h* o Heatbug proto = (Heatbug) heatbugList.get (0);3 E a" P2 y3 C" N, y
Selector sel =
% ?; d' G! _2 N9 m9 { new Selector (proto.getClass (), "heatbugStep", false);
/ Z _( t1 |$ e. j1 z actionForEach =
$ o' ^0 Y2 f' X; F modelActions.createFActionForEachHomogeneous$call
- n( b0 [* L6 Q2 T O (heatbugList," ^. ~# Z1 V6 I1 }& E5 A1 C
new FCallImpl (this, proto, sel,
) a6 q. H* }7 D: j+ T# W4 Z new FArgumentsImpl (this, sel)));
b$ {6 R' C1 G8 Z7 E) `- ? } catch (Exception e) {
9 O1 C G& ?% e9 ]* g! E e.printStackTrace (System.err);7 C8 o+ s4 o* a, u* O8 O6 E! [
}
# c+ c9 r" j C* @, c; E# w2 X. `8 a* ^- `
4 ~: P" E( U9 Q6 t1 C! [- _. c syncUpdateOrder ();
- o3 p4 D1 i8 |& t% y% K# a
- B' a$ y. B7 t9 c0 G try {
8 }" O1 J) b9 B5 ]0 N modelActions.createActionTo$message 2 A2 o5 p0 l7 G& c$ j' Q
(heat, new Selector (heat.getClass (), "updateLattice", false));# }" H' f6 b$ R
} catch (Exception e) {# r M* k* t7 w+ P
System.err.println("Exception updateLattice: " + e.getMessage ());, d& q8 ^& D: N/ d" ~3 F. `' Z
}
$ s, K1 o' {. o; W$ g8 A
) l5 N# o6 B1 w4 C+ U // Then we create a schedule that executes the
6 S7 [9 ~" E0 i3 Z // modelActions. modelActions is an ActionGroup, by itself it8 p. D5 l e/ N0 T0 K- g( k
// has no notion of time. In order to have it executed in) e- Z/ m/ [! v& w. s9 ?
// time, we create a Schedule that says to use the6 N7 }* W5 S+ p; K
// modelActions ActionGroup at particular times. This
/ Y# M" d3 _4 y9 ] // schedule has a repeat interval of 1, it will loop every
" N: [3 I/ ]3 Y // time step. The action is executed at time 0 relative to7 O- o! j' u4 F7 [3 G! u- b, O! f+ n
// the beginning of the loop.; t9 G' m( o! g! c g+ [- @
a2 J% ~1 T0 h# I+ L9 H
// This is a simple schedule, with only one action that is
+ W Q G) l+ S: x // just repeated every time. See jmousetrap for more$ Q8 D! D6 Z! z* W2 c J4 r9 V
// complicated schedules.3 q$ I m0 c0 J! b6 R
7 b8 D( b2 r( U2 B& J modelSchedule = new ScheduleImpl (getZone (), 1);
# Q3 F8 m1 M3 V: S9 k/ S modelSchedule.at$createAction (0, modelActions);9 ^# Q- j6 f( Z* G
9 W& Y+ K5 a: I7 c( E return this;$ |% T; y. s+ L* ?
} |