HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 g4 a! R* V9 J B% C, @8 }( N9 D% o) m6 F# k% N2 s& a0 Q
public Object buildActions () {
, G: A2 F; ~2 H* |$ F3 i super.buildActions();
: c2 v! D; {0 d3 r
3 ~6 T8 \7 _0 I3 W // Create the list of simulation actions. We put these in
) B) d8 u8 q/ C // an action group, because we want these actions to be
* `( p' T1 L, Y# ?( @1 U# D/ p // executed in a specific order, but these steps should; ?* `; N$ T' P6 Y% C2 q
// take no (simulated) time. The M(foo) means "The message
9 M- [" `/ @6 m* N k) Q // called <foo>". You can send a message To a particular" ~: k0 l# S5 u8 J" r' J, V& j/ _
// object, or ForEach object in a collection. D/ d( R$ C. T# g( w- Y
' l8 O; z4 t+ v7 p' {2 o3 { // Note we update the heatspace in two phases: first run- ?2 N5 q* i8 j& L
// diffusion, then run "updateWorld" to actually enact the
' z- H) S/ I# H5 Z // changes the heatbugs have made. The ordering here is. A4 p2 M8 t& V; \/ V5 o2 f7 q* g! @
// significant!
' ^5 q& C; ]: ~. u2 L7 f
* x7 X" ~1 u! q. v* C/ d$ L3 ] // Note also, that with the additional
& q5 {1 C- U: W9 l! v0 `; ]4 Y% J // `randomizeHeatbugUpdateOrder' Boolean flag we can
8 B. ~+ `; U/ x9 [/ ? // randomize the order in which the bugs actually run
1 l# x& ~0 M! Y L! v% M/ a4 w+ O c // their step rule. This has the effect of removing any
% h: o- n( b& K) Y // systematic bias in the iteration throught the heatbug7 I$ w* y! C, b
// list from timestep to timestep8 s k* L; ]/ E! f5 J' Q2 ?" a
/ f. V$ I M2 L l+ k+ e1 q
// By default, all `createActionForEach' modelActions have
4 _5 r' K7 s- q* ~- ?7 D // a default order of `Sequential', which means that the2 R. \( Z' m! i; k, D
// order of iteration through the `heatbugList' will be3 }) s- C6 I0 h. Y) j+ V& G
// identical (assuming the list order is not changed. P! u& }0 U7 L9 \( a( ]
// indirectly by some other process).
5 d/ @, e3 `; S$ _ - h3 D7 H1 ~# k# c( P3 B
modelActions = new ActionGroupImpl (getZone ());7 U0 ~/ Z, F$ g6 U
, n# F# Z3 A# [7 q$ k9 J try {" o* U& ^8 f; Q9 w0 l) U
modelActions.createActionTo$message
% Z; R' L& j9 e, M' o. `- x8 X+ F (heat, new Selector (heat.getClass (), "stepRule", false));
$ |, k: W! u r+ r- h } catch (Exception e) {
' K T+ V4 J& z) y System.err.println ("Exception stepRule: " + e.getMessage ());! h# D& o- m; J' O& t- k
}) R3 Z/ d2 p9 R
* E2 U- E- ?8 o
try {
- m5 B5 O; f4 ]6 ]$ R& Y Heatbug proto = (Heatbug) heatbugList.get (0);
6 T: d' D3 N. B% P: ]3 `) ^ Selector sel =
& s% Q- N2 L; V new Selector (proto.getClass (), "heatbugStep", false);
+ T6 i. Q6 P! Q. v( l6 V* f actionForEach =, ^- g: y L |$ i% c* o
modelActions.createFActionForEachHomogeneous$call
" ?) D$ O5 ?1 D9 v (heatbugList,
+ x! M$ D* k! e; c new FCallImpl (this, proto, sel,( p2 O. R1 C' Q
new FArgumentsImpl (this, sel)));
+ \8 t/ A- K6 C8 ] } catch (Exception e) {' L L2 f" D& d2 o( h
e.printStackTrace (System.err);1 c, e% q& ?8 P4 `/ @
}! n& f( X j1 U1 S
* u2 @+ \8 x, i( r syncUpdateOrder ();6 B/ a: s* I( Q6 p1 z% F& g/ z
. c# U4 F/ D# M try {
* o9 p; ~0 z: M! f* N modelActions.createActionTo$message
1 _9 N" `% P+ [% ]- a (heat, new Selector (heat.getClass (), "updateLattice", false));* G5 g) t% w2 q# K
} catch (Exception e) {* o2 }. y' v" I ~2 A! y Y$ Y
System.err.println("Exception updateLattice: " + e.getMessage ());2 N0 m9 g5 a/ i) g+ z8 S
}
0 T& Z7 h- M! B
4 D# X& B- b. d0 c0 m // Then we create a schedule that executes the- s7 A, l9 F8 E- h
// modelActions. modelActions is an ActionGroup, by itself it, u' P. |0 E8 |" C: J, M1 t# w
// has no notion of time. In order to have it executed in
2 w7 n3 @0 V, v7 s$ }( }0 O8 E1 l // time, we create a Schedule that says to use the
2 q! H, j3 f% Z$ | // modelActions ActionGroup at particular times. This
' r/ f9 B0 t/ L1 L$ [$ u2 U# j // schedule has a repeat interval of 1, it will loop every
* o, X5 t+ c# v // time step. The action is executed at time 0 relative to: i; B6 q+ k' G( t- \
// the beginning of the loop.
5 t! N% U+ i! y: H' l- P
9 K; u, m+ t- X- [, S // This is a simple schedule, with only one action that is
+ [. ^5 C9 w) w5 d% c // just repeated every time. See jmousetrap for more
2 a5 W0 w8 F+ f' @; X8 } // complicated schedules.
7 ^0 R$ {2 h ^ X: m0 a) X# n 9 | U+ F+ F# i
modelSchedule = new ScheduleImpl (getZone (), 1);; i. O) _" K5 v) v @3 P" h$ }# Q
modelSchedule.at$createAction (0, modelActions);* f& A) r- W, w2 y
u1 w9 Q0 G' I+ o0 Z; j/ w! k* r
return this;
' Q# f$ o, O) d- o3 U! A- j1 [ } |