HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# T4 W& r ~" z! l" p9 B. B5 ^2 H1 y7 A5 i* O
public Object buildActions () {
2 u/ `; P/ m3 z7 x- R super.buildActions();' |9 L' o+ e3 f( n4 f6 i0 g
0 r8 X/ a: _( a
// Create the list of simulation actions. We put these in# A1 N/ j/ e' L0 X8 U
// an action group, because we want these actions to be: y5 ], {' T& @& u; \$ B" c
// executed in a specific order, but these steps should' q" V% c9 D3 H- x V
// take no (simulated) time. The M(foo) means "The message2 _; U7 Z E. m) y5 i: H7 a
// called <foo>". You can send a message To a particular6 T* l/ F2 A" {) m) i z% K6 ]
// object, or ForEach object in a collection." Q/ F5 h2 p/ P3 ]" V5 V" ? g
* W3 I3 i4 f; p' y4 c4 e3 P // Note we update the heatspace in two phases: first run0 f, g( V$ w5 {6 y5 V" u6 W/ z
// diffusion, then run "updateWorld" to actually enact the. O! [+ Z/ B& G
// changes the heatbugs have made. The ordering here is, B, W1 ?5 ~9 j% |
// significant!* Y! e3 r1 U; z5 s9 {; p
! E; b+ }0 s% P
// Note also, that with the additional
% S( C' L; a, X# e6 D4 ?9 z // `randomizeHeatbugUpdateOrder' Boolean flag we can# l& x( O) j9 y7 q$ c
// randomize the order in which the bugs actually run/ R9 D5 K- c+ I/ X
// their step rule. This has the effect of removing any1 R9 q. M4 z5 p& h) q
// systematic bias in the iteration throught the heatbug7 R! _$ l) j5 M* R% d
// list from timestep to timestep
- [' A3 \* }2 K 7 w/ B* J- U) y. C
// By default, all `createActionForEach' modelActions have8 z( L$ |1 p( e2 l0 C; R( B. B
// a default order of `Sequential', which means that the
& L- T0 _" t4 P$ {8 p5 K4 }* W2 a // order of iteration through the `heatbugList' will be
( A4 U0 \3 f! v' K! I4 w // identical (assuming the list order is not changed5 n/ @2 j3 {' f/ s7 E3 K
// indirectly by some other process).+ s/ f$ y0 X: H6 R
/ c+ g. _9 \4 Z- E8 x p1 ?$ |
modelActions = new ActionGroupImpl (getZone ());
) \! k6 e: u/ Q, p6 W* w
* x: a* ?8 ?# H; K1 i# C try {1 C: B( m# t. e% q0 q* `3 w
modelActions.createActionTo$message
- H3 m v I2 q9 N: R; H) e& w5 d (heat, new Selector (heat.getClass (), "stepRule", false));- Y7 E# {3 x- N& f3 v/ N: E. b
} catch (Exception e) {
* |, J) Y: N# |7 Z& k System.err.println ("Exception stepRule: " + e.getMessage ());2 n4 p! `1 `0 a
}$ Z/ P. v, R# U8 \: ^ n
/ R5 W8 S: A9 }5 D$ a, P k
try {
* w% p& m( J& ^/ j. T3 {! S Heatbug proto = (Heatbug) heatbugList.get (0);8 E1 M4 _9 E Z) R9 E: h' e
Selector sel = * x, j% y1 S& s+ ^
new Selector (proto.getClass (), "heatbugStep", false);, W @0 u" K2 v
actionForEach =! @; \) ?* H. F
modelActions.createFActionForEachHomogeneous$call
- n- e0 M# i E) P (heatbugList,
6 _7 E% t. f. g new FCallImpl (this, proto, sel,
: x1 J* J* R s1 X new FArgumentsImpl (this, sel)));
: N3 v0 L4 D7 W; C } catch (Exception e) {+ o" g/ ^( ?% M+ f d
e.printStackTrace (System.err);. _; d: B, J; y: `0 f
}
2 G6 b7 `0 K3 B" g ) T3 M1 Y5 M* Q$ J: }7 g$ i
syncUpdateOrder ();& Y, G! G1 c5 [& K: s8 k+ }
5 _9 C7 u/ t1 k- G
try {
" Q/ K* m. u9 { modelActions.createActionTo$message $ D3 J( \( m; F0 j- |
(heat, new Selector (heat.getClass (), "updateLattice", false));2 }% r' }2 d' e2 a- B
} catch (Exception e) {
( D$ ?3 `5 K& h4 Y" s+ i" u+ w0 Y System.err.println("Exception updateLattice: " + e.getMessage ());, E, Q( [, e0 Q' G5 V
}
* J2 k5 a1 W. k! t& _/ m 0 s7 D) c. k1 B
// Then we create a schedule that executes the
4 F/ ]. f) m3 z$ ]3 ^2 G& ] // modelActions. modelActions is an ActionGroup, by itself it) |- U8 e# T+ |- q& j4 B& [
// has no notion of time. In order to have it executed in- `' `' u3 [) O, L6 x% r
// time, we create a Schedule that says to use the
* Q. _3 f; k9 F3 m) t) {, ^ // modelActions ActionGroup at particular times. This M C4 m0 i4 j e/ b2 Y
// schedule has a repeat interval of 1, it will loop every
; b) A# g6 W' e( g, m" F! [3 Y2 x // time step. The action is executed at time 0 relative to( H7 s# {# Q( t4 A
// the beginning of the loop.3 s3 o" D9 Z' @2 P3 u
3 I3 A$ O8 K0 M
// This is a simple schedule, with only one action that is
7 P6 m+ w) L# x" B) u" W // just repeated every time. See jmousetrap for more. `8 J7 S; e# L$ R5 ^: F
// complicated schedules.
+ O8 Z1 i( _- i f7 x
; J0 @! A2 D$ B/ C7 u# F5 D modelSchedule = new ScheduleImpl (getZone (), 1);
' N$ n0 P% _0 X modelSchedule.at$createAction (0, modelActions);! R" L' ]2 B2 \' [9 ~1 y* n6 P
( I+ k7 w* P! O# W return this;
8 \2 i f) f, Y9 u, v6 `1 Y7 K8 H1 z1 Y } |