HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 f5 X( ?) j- {3 A# s$ x
0 o- h2 q D1 w: ]4 I" M! s5 G6 j
public Object buildActions () {
9 {& ]; G* c% `2 m) W super.buildActions();9 L0 J0 \ K2 V6 t! o0 \
- w, T0 @% |. N7 Q& P // Create the list of simulation actions. We put these in
' I$ r* V! _& K- o3 `( A // an action group, because we want these actions to be
2 g5 C8 {" L( X' @: e // executed in a specific order, but these steps should
8 i! s8 R. T8 q0 \ // take no (simulated) time. The M(foo) means "The message( @! V; m" D( e+ {% {, {1 i7 `+ o
// called <foo>". You can send a message To a particular/ D$ C$ v0 b; p5 h) f+ u6 e6 i4 Z
// object, or ForEach object in a collection.
3 G3 i& }' d; ]3 S( W1 h " N5 w C8 q4 |2 j1 U( l" v: {
// Note we update the heatspace in two phases: first run3 v/ q+ D4 F& r
// diffusion, then run "updateWorld" to actually enact the
3 s9 G3 H0 P- g5 I2 A2 o // changes the heatbugs have made. The ordering here is
( ~* |6 g* K! B& ? // significant!" }7 s9 @* ]. s2 Q4 e* j8 Q
: c; c: j6 C) n" N$ c4 `- z* E$ _
// Note also, that with the additional9 n$ J2 C8 j: @
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ {: W. B6 H* ?$ }
// randomize the order in which the bugs actually run' C( o( G$ y q; }
// their step rule. This has the effect of removing any; J+ Q: U5 R& K4 F, g7 \
// systematic bias in the iteration throught the heatbug
! Y( s% f: {: H, B0 I6 _3 C" i // list from timestep to timestep+ q6 ~3 x; _3 z- }6 `2 d8 B3 k9 Q
( Y! d% h& a) V" I4 y9 [+ z, {9 o // By default, all `createActionForEach' modelActions have! k, H+ r* o5 r# {+ h0 K5 H7 @
// a default order of `Sequential', which means that the
: `$ j2 `. X# k2 U // order of iteration through the `heatbugList' will be+ r0 M- @% F2 W ~
// identical (assuming the list order is not changed
, K& ^0 q, |- b // indirectly by some other process).
% ?9 }3 y9 D ?9 K5 P/ s! G
' k6 c! S' F! Y modelActions = new ActionGroupImpl (getZone ());
4 f7 i( ^- E b
' Q* r$ w/ {! N/ D3 v% ? try {5 C4 x! a4 h$ q! W8 s
modelActions.createActionTo$message
9 v0 W3 U* N1 V# ?9 q( [2 r. L* [& R (heat, new Selector (heat.getClass (), "stepRule", false));
; M# e) H' D& Z- E- ~; f% K } catch (Exception e) {4 q a0 I d" [- a
System.err.println ("Exception stepRule: " + e.getMessage ());3 S# D% e* G$ p0 ~; m9 e- M" R
}$ a' s0 y: Y) \' e0 W: N
4 t+ O% \# j& _& ^ C; T+ p5 o
try {0 X. ^ [( u9 y1 [
Heatbug proto = (Heatbug) heatbugList.get (0);
$ _3 k( m8 w7 X/ W/ T2 K Selector sel =
3 s4 l5 E9 I8 k% O new Selector (proto.getClass (), "heatbugStep", false);/ s$ `; |( R2 M
actionForEach =
1 e+ r6 V3 D2 V. X4 Z& ` modelActions.createFActionForEachHomogeneous$call& z5 U7 T# f" [) K: d" p( t5 }
(heatbugList,0 H2 {. _4 {* r# E: y" k: y% W
new FCallImpl (this, proto, sel,
3 v7 @' @" A" w% V new FArgumentsImpl (this, sel)));
# ?* J8 w9 ~; A) Y8 y& C } catch (Exception e) {! I, w. M# W. a, x+ E
e.printStackTrace (System.err);
1 O! t( }+ W* U) T }
) T. O; \7 q# p( J
( A$ q) N8 u1 K# {% u' `+ R syncUpdateOrder ();
' h; W7 c7 o2 U+ [# D% h5 d
( ~" k. Q$ R" Q1 ` ?" d* |& H try {
& f9 K Z; h8 l' ~! P modelActions.createActionTo$message
% x5 h$ n$ D T (heat, new Selector (heat.getClass (), "updateLattice", false));
l9 R6 D5 z8 h# |0 L% F2 M } catch (Exception e) {
' B2 y9 t! `: }% A System.err.println("Exception updateLattice: " + e.getMessage ());6 p& g) S! m& X1 n* w
}
+ z: ~+ T- \- r8 q, Y3 ^
' |$ J/ D5 | [0 T; W: \ // Then we create a schedule that executes the
8 G! a/ Y# s- H% F" ~( x // modelActions. modelActions is an ActionGroup, by itself it
1 Z9 {' c, z6 G |# r // has no notion of time. In order to have it executed in
. H6 O! e# k0 ^; j% T: M, L // time, we create a Schedule that says to use the# U( I( ?) S4 \, o0 T. p( @/ c
// modelActions ActionGroup at particular times. This8 S6 [5 w( G* v6 i+ ]( E) u9 G; R
// schedule has a repeat interval of 1, it will loop every
0 E& `) l& Z* D // time step. The action is executed at time 0 relative to/ p/ C% V1 `( I4 U
// the beginning of the loop.# C+ v" F0 ?$ t, F. k$ n Q0 Z2 J( t
5 x+ T& B! L3 G, C // This is a simple schedule, with only one action that is& }0 M4 r( A) o' J! {
// just repeated every time. See jmousetrap for more
6 w% {" v7 A' O% [4 l$ }7 R // complicated schedules.
! R( ?' T, I# p! e7 k4 {5 A
8 d% h* x7 f. v" Y modelSchedule = new ScheduleImpl (getZone (), 1);
% V, o: l- t1 b/ [8 z modelSchedule.at$createAction (0, modelActions);
5 O6 A# B5 g* s" E1 [2 p( q o" S: R" f5 D+ T7 n* [
return this;$ {3 _9 o! p7 r) o0 q
} |