HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" M! M! k8 I. y; O( D
" G6 X( L% o2 u
public Object buildActions () {6 w {" g; A( l4 G) l) \+ [
super.buildActions();
# ]% D+ ]" V) s9 c" _( L - J. i/ k9 d5 {$ }5 f
// Create the list of simulation actions. We put these in/ N. L( f- B+ V
// an action group, because we want these actions to be
) Y* z# s. ~+ i: t2 C+ b // executed in a specific order, but these steps should
3 r6 h4 C# a/ d4 t$ }0 S" l7 j8 k // take no (simulated) time. The M(foo) means "The message
/ S) \) ^8 U1 n: J3 z/ E( e. } // called <foo>". You can send a message To a particular
- }1 r7 u4 E% e9 w/ X7 Q7 }0 J // object, or ForEach object in a collection.
$ p* a& i0 E, L X: P7 K
( P4 t6 e3 A* |5 z // Note we update the heatspace in two phases: first run1 {( h, d; h; n
// diffusion, then run "updateWorld" to actually enact the: C" T7 M# x. H M$ Z3 N6 p& @1 d
// changes the heatbugs have made. The ordering here is
9 H/ X0 {5 N# j" f8 n // significant!0 t- s! d! c; [' w; \/ _5 F* M
9 u4 d& i" M4 z
// Note also, that with the additional
: u4 V4 d) R8 |4 y3 T! E& f // `randomizeHeatbugUpdateOrder' Boolean flag we can
* A. o" [+ e+ O% Q // randomize the order in which the bugs actually run3 ^ ~/ b/ P$ z1 S3 ]; c' J
// their step rule. This has the effect of removing any! \' A8 I$ y) j, z5 o3 T7 v
// systematic bias in the iteration throught the heatbug; i7 ]$ T5 `% @% p' X% M+ I& b
// list from timestep to timestep
$ y" W; A$ z8 o3 q; x T
3 c1 r9 a( v6 R% }& | // By default, all `createActionForEach' modelActions have& ]$ @6 o$ }) }$ ?
// a default order of `Sequential', which means that the
; J/ s, u1 D/ I c4 @ // order of iteration through the `heatbugList' will be- p5 D. x+ F6 q7 U8 P& c9 K
// identical (assuming the list order is not changed
, X- x% n- x5 P$ J" T' ^) t // indirectly by some other process).8 ]; ~" {0 j/ W
% e7 g: o {+ r G9 B, t4 k modelActions = new ActionGroupImpl (getZone ());
! `4 d5 s. y5 ~0 L! X
) [0 B# ]( L2 g9 m0 ~: z! ^; F try {
% c. g2 G T% j! [4 [' S modelActions.createActionTo$message
! p) W& E( O. l0 |0 A) j (heat, new Selector (heat.getClass (), "stepRule", false));
: E6 f. o4 q b: w } catch (Exception e) {8 K" B% q! b- i* v( O' N
System.err.println ("Exception stepRule: " + e.getMessage ());
! L8 X! e: @4 A' h$ b; u }
9 T) F, o' o$ N0 G1 j" w9 I' h6 p% W
try {
! Z9 R' M* v( l" O+ n Heatbug proto = (Heatbug) heatbugList.get (0);# ]4 D+ n3 G; U8 k
Selector sel =
3 v/ h1 g) K; D( ` I9 t( M3 x new Selector (proto.getClass (), "heatbugStep", false);- {4 `0 }5 g) g& k* D9 `
actionForEach =
9 Y" S2 k" n9 }, [6 y modelActions.createFActionForEachHomogeneous$call
$ t7 z8 R$ Q* @3 \ s (heatbugList,5 S: X: F# w, Y7 I9 M3 G
new FCallImpl (this, proto, sel,
2 O2 g6 K: g, [" i0 Q; B+ D' D new FArgumentsImpl (this, sel)));$ [* v6 a. e6 l6 _$ w5 v
} catch (Exception e) {" W, J0 d# k# E. l/ n
e.printStackTrace (System.err);
! z$ X. j- k: {6 e4 t5 m# \ }" @+ O0 b' b. U$ v: I
! }/ _- \, E0 z2 L) u/ F syncUpdateOrder ();9 Q5 q+ ~! Q9 B/ F! y! a! c
8 f7 c& _/ o, Y" N! F2 | try {
2 Y. o9 p1 d# o; `# y% i7 u/ y modelActions.createActionTo$message / x! F+ Y N! @! N
(heat, new Selector (heat.getClass (), "updateLattice", false));
7 c- D0 J+ I1 H+ P } catch (Exception e) {
! T8 T' ~, j# B) [9 }4 _4 u System.err.println("Exception updateLattice: " + e.getMessage ()); C1 V% |4 R0 n7 U4 z& `/ t
}
% q: P8 q0 f/ c1 V+ b
3 E, e, O1 c3 K, w // Then we create a schedule that executes the) g2 N4 s& k6 c/ ~
// modelActions. modelActions is an ActionGroup, by itself it
3 b; S' g" H8 A+ ?$ { // has no notion of time. In order to have it executed in" W- L" c7 G3 }5 Y; ]( t
// time, we create a Schedule that says to use the
# Q$ }# ^3 j. s // modelActions ActionGroup at particular times. This
* ^# ~ m) R; Y" p // schedule has a repeat interval of 1, it will loop every
( c5 i. e$ s- @% g& S6 H // time step. The action is executed at time 0 relative to1 S, T G; I# ]; `- I% t
// the beginning of the loop., w- w% [/ F2 i6 I6 m6 x
/ C& T% Q" O' r# Q // This is a simple schedule, with only one action that is; y) C, j N- | e& m$ d/ C; {! M
// just repeated every time. See jmousetrap for more7 ~3 }6 U$ x% {4 `* ~
// complicated schedules.' `! b' J& q# A8 x
: N2 ~/ C# l& L
modelSchedule = new ScheduleImpl (getZone (), 1);7 O$ d; n) Y) v" g( X& Z# R9 O
modelSchedule.at$createAction (0, modelActions); ^4 @, O+ l1 T0 v9 s2 L9 L
( x5 z" G, I/ V5 a8 n return this;- J" Z0 f+ k5 g6 y
} |