HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% Q" I0 |4 \ r- n! r
+ N3 X: v0 L- q+ r public Object buildActions () {, _# o4 V- P3 o3 ~& [; W
super.buildActions();
" c" c% F& F- \& _7 q/ b7 d
! M4 f1 f# v( H2 V0 v' H // Create the list of simulation actions. We put these in
: _! z2 w2 U+ [ // an action group, because we want these actions to be
4 K# v4 G* z1 @6 l // executed in a specific order, but these steps should* O$ C* T) ^, T5 x: R) J7 [
// take no (simulated) time. The M(foo) means "The message
' |+ e1 Z- ?: y) f! } // called <foo>". You can send a message To a particular! i+ M q9 d; C" e! ?: e8 N# T. g8 f% N
// object, or ForEach object in a collection., v, I/ b3 Z3 U0 @3 S: O& v! g
5 s) b( X& n4 x5 M: _ // Note we update the heatspace in two phases: first run
8 N+ J% x+ L% G, f4 z // diffusion, then run "updateWorld" to actually enact the
/ \8 m) Q* X& D5 B // changes the heatbugs have made. The ordering here is
9 M& ?7 E. J, q- J& e; O // significant!
' t& X' p, I) S5 g9 N6 f
/ _7 N1 s+ {; \+ S2 i // Note also, that with the additional
: d0 v [, E9 Z5 j // `randomizeHeatbugUpdateOrder' Boolean flag we can R! J3 l% O5 _) X; |' p! I
// randomize the order in which the bugs actually run
& E2 K; w' M9 {/ @; x+ Y* q4 K$ a: D // their step rule. This has the effect of removing any
6 L8 T' F" Q# g, _ // systematic bias in the iteration throught the heatbug. v- J0 c1 V& V( f( b3 {
// list from timestep to timestep5 H* `- x9 l& Q* F& g. o4 y( x
% |% ], O1 @8 a$ H0 g0 \( v4 V
// By default, all `createActionForEach' modelActions have o8 \6 F/ p0 b# T
// a default order of `Sequential', which means that the
# o1 R5 M3 z3 D: B/ m. E& U) ? // order of iteration through the `heatbugList' will be
2 v0 q2 P- Y4 I) L! j' K3 H // identical (assuming the list order is not changed
; F0 ^/ N% l9 e5 U // indirectly by some other process).2 `% S3 ] z$ A# U
: T- e# N8 R! o9 z' Y6 ^$ [
modelActions = new ActionGroupImpl (getZone ());& J( f- H$ V. }
# _1 t* E+ S0 |8 o$ | try {
, V' ^9 w$ `# a modelActions.createActionTo$message1 ~5 v- {2 j" l, U$ P# {
(heat, new Selector (heat.getClass (), "stepRule", false));
" }, N9 ]+ k% X: A3 Z; t. R } catch (Exception e) {: ?+ j) \( A$ k, g6 R" c
System.err.println ("Exception stepRule: " + e.getMessage ());6 @- e4 O" H& G6 L( _ Y: u
}# {+ J& K& S3 Z& P; u" |8 J
' ~" E: d# X$ I! E) F9 C- T7 m try {
7 T r) o$ c& i3 x Heatbug proto = (Heatbug) heatbugList.get (0);
& r( F7 ^$ Q; `. @; M Selector sel =
" E9 b6 V, {6 x& t) M$ ` new Selector (proto.getClass (), "heatbugStep", false);
7 U$ ` {- s4 A actionForEach =4 \9 y0 e4 j0 C: b7 q) c# G/ J7 [
modelActions.createFActionForEachHomogeneous$call* D D; `3 l$ M1 L( U) o$ Z5 Y l
(heatbugList,, X0 u( o3 F! Q( z" R- B K! X" ~
new FCallImpl (this, proto, sel,6 x) j8 i* G7 ], a% x
new FArgumentsImpl (this, sel)));1 U! X% d8 M8 K+ d: P2 _
} catch (Exception e) {$ V+ I4 r5 b, q# z8 r7 R
e.printStackTrace (System.err);, r( C* t# I/ g8 h
}; u6 w$ k/ Y+ c+ [' ?1 ~) ?
( {/ }7 Y! v4 `7 c0 C
syncUpdateOrder ();6 V0 O/ V9 E2 W, c( W6 |! R
! x7 @0 T9 C6 ~, j1 X4 i& Q M$ G
try {
; L0 {: k7 g& H4 V modelActions.createActionTo$message % q! G6 J! B E
(heat, new Selector (heat.getClass (), "updateLattice", false));
# z! |$ r$ r5 L( n+ ]. o } catch (Exception e) {4 A& ^! y2 N1 K
System.err.println("Exception updateLattice: " + e.getMessage ());! i4 x) r: w" G
}* W& w; i! U* r1 Q; G, t- y
2 o3 [: [1 B, A2 R0 k( w // Then we create a schedule that executes the
1 e* f0 {: X0 v( h8 s- ^1 b( z // modelActions. modelActions is an ActionGroup, by itself it
2 }5 P: ^0 L( ^; k // has no notion of time. In order to have it executed in3 q, G0 Q3 k( h- m9 r" c
// time, we create a Schedule that says to use the
6 ~& W2 d5 g" a6 s1 _* h // modelActions ActionGroup at particular times. This; ]! p5 ^8 Q! \# P, k
// schedule has a repeat interval of 1, it will loop every
! A7 ^# y6 {1 m, a( C7 k // time step. The action is executed at time 0 relative to
+ c7 L% \+ `# J, X ], V1 ~0 ~ // the beginning of the loop.
" O8 n k% `; f0 y. S) {/ o, y' g& H6 o( L6 F0 o- e
// This is a simple schedule, with only one action that is
7 R1 E! p+ j2 j% k5 q* m! f // just repeated every time. See jmousetrap for more7 q& d* @! e4 g! t8 w
// complicated schedules.
% y* j6 U$ C9 Y' I
9 T7 w% m% N% R m* w9 j. J! K- y modelSchedule = new ScheduleImpl (getZone (), 1);
9 N% s2 }' c* C+ r; ]( a7 C! A modelSchedule.at$createAction (0, modelActions);
, c. {3 u# ^. r/ z8 K
8 P1 S$ C# P' D return this;
: c3 b+ G. C3 b: h6 q2 _ } |