HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: [: Z, j2 ]* U. R V, f
9 V4 \* X' X( | public Object buildActions () {5 q3 F( g5 }! R- I/ |
super.buildActions();
& r3 [' W5 m1 W- S# c# { / h( v% M" }. K. I4 [ \
// Create the list of simulation actions. We put these in0 a& ^9 v! y1 ?- J, S3 w
// an action group, because we want these actions to be
7 @7 s+ N7 i* c // executed in a specific order, but these steps should( w$ M9 O; ?; s6 Q5 [$ ?
// take no (simulated) time. The M(foo) means "The message
* L2 m) Y9 G4 |5 G // called <foo>". You can send a message To a particular
+ b* o2 n* U- a- J // object, or ForEach object in a collection.
+ v) q7 m; E* u' E/ ^ , p! z, x9 _; N8 r! o
// Note we update the heatspace in two phases: first run
6 t' m% Y& g k9 T+ c* d9 O+ c // diffusion, then run "updateWorld" to actually enact the T+ q7 N* |# g) a+ R! `
// changes the heatbugs have made. The ordering here is: ^! O Y4 d% j) P8 {' z
// significant!3 R* ^, b- j% q- X1 T1 E V
% h) Y& C: x E7 y9 n
// Note also, that with the additional: ?( _2 Z# H/ G* s- g. ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 X0 X* r, g4 i! O7 o# \0 v
// randomize the order in which the bugs actually run" w ^7 A: J" W3 x, \* Z% a
// their step rule. This has the effect of removing any
& j$ A/ [* q+ Q" w8 h8 D0 X // systematic bias in the iteration throught the heatbug) A! a$ U- m% Q& @
// list from timestep to timestep
0 m }4 F* G2 ~! t / m' p5 K0 p3 R1 @
// By default, all `createActionForEach' modelActions have
, p8 b) T0 j; e* N; U- x // a default order of `Sequential', which means that the
+ L; r Q, ^/ k' g! x7 Q // order of iteration through the `heatbugList' will be. _1 A7 r: r( {1 Q/ J
// identical (assuming the list order is not changed
# J; h. e. W- O6 `7 i // indirectly by some other process).
" Z- {8 I& z, \
: y/ J0 \0 _( b! H0 C0 k modelActions = new ActionGroupImpl (getZone ());
( X% b6 G7 K! L; i" q, a, n
; |% N+ B6 t' |, B% g; q0 C try {, c4 ~1 [7 e* \- Z# |
modelActions.createActionTo$message
1 w4 s9 y7 U4 W. Y (heat, new Selector (heat.getClass (), "stepRule", false));0 a- G3 X9 ]8 F# p8 L
} catch (Exception e) {
1 K" D, q0 q- e' h" I System.err.println ("Exception stepRule: " + e.getMessage ());
- c& g* M6 t1 B Y8 t, g! X }9 Z4 V0 G) r7 f% h( e% v
1 o0 s$ R" c& ^2 b; c6 x try {8 O1 d2 E9 I$ J
Heatbug proto = (Heatbug) heatbugList.get (0);7 a4 |" K. I7 W5 S* N6 r; u# r' F' L
Selector sel =
! o9 v% s0 {! r; F* O2 T8 w new Selector (proto.getClass (), "heatbugStep", false);
+ v" t) j( }$ B, M- }* \ actionForEach =0 t9 V* w8 Y; n Q/ o; ^; a. ?+ V
modelActions.createFActionForEachHomogeneous$call9 \2 F& |& A. o8 i/ N
(heatbugList,: }7 z* ^& x6 c0 t% Q+ Y
new FCallImpl (this, proto, sel,
$ i4 y. A# U. I9 T: } new FArgumentsImpl (this, sel)));
8 J. l7 L- c" A; |# ~ Q, N6 S } catch (Exception e) {
' `; E$ w2 Z5 X* d. q* v8 R e.printStackTrace (System.err);
8 |. a1 C' e, T0 J! _ }- P0 d- i, S2 d9 `9 E
7 A- ~) e- @: c9 i
syncUpdateOrder ();
* N$ K# Q" g5 D1 n0 I$ w" P& |$ ^" ]7 o6 V
try {, ?# r$ z3 v; @4 W) V
modelActions.createActionTo$message . W4 K+ g- R; k
(heat, new Selector (heat.getClass (), "updateLattice", false));
( ^) c2 w% f* D5 W' Q8 H0 Q } catch (Exception e) {# E- G8 I& \* _4 ^7 J
System.err.println("Exception updateLattice: " + e.getMessage ()); n" Y) R5 _2 X! ]
}' o# X" A! D$ x# b7 ]+ @* l
! b4 M/ y9 I1 ^8 }' `
// Then we create a schedule that executes the2 M. k& H4 B+ H2 @
// modelActions. modelActions is an ActionGroup, by itself it
' X6 i) R: K! B$ Y( Z! n7 _ // has no notion of time. In order to have it executed in) q# Q7 y% F& d
// time, we create a Schedule that says to use the
" |9 z- \# O. t6 v // modelActions ActionGroup at particular times. This
4 G# S3 ]( I2 q$ V // schedule has a repeat interval of 1, it will loop every: J0 W0 Z) R3 w+ L, Z
// time step. The action is executed at time 0 relative to
, _8 [' P$ s7 x& ? // the beginning of the loop.# r5 H- [4 x& n. I) b: i
: H8 P! x( g, \/ g. U // This is a simple schedule, with only one action that is
0 R0 I4 q7 L/ G p+ K) @( [1 Q // just repeated every time. See jmousetrap for more
- b1 i; `$ Q2 Q8 d7 Z1 p n. o1 }5 I2 v // complicated schedules.4 v$ ?$ x+ R+ h6 I [: }
5 Q) w* F! u: E# `& R. b
modelSchedule = new ScheduleImpl (getZone (), 1);
7 E9 g" m7 R& s/ R, p5 U3 k% F$ I modelSchedule.at$createAction (0, modelActions);
$ O: v7 t$ x* w- C9 ?6 |2 h 9 b! x6 x) d3 }3 z3 g2 V& p' k
return this;7 i& Q) x7 J" c$ ~
} |