HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
?8 s1 o) Y; I
! y c# s% H: G3 r6 }+ u* O3 ~ public Object buildActions () {( P6 Z7 z- C& w! K* G) }8 A ?
super.buildActions(); k2 I4 l' z9 k6 K2 S) j4 ?2 F, T
: _" f8 \, [4 z
// Create the list of simulation actions. We put these in
0 T8 }! V$ K* a9 d // an action group, because we want these actions to be
3 q5 ~; o5 u; V, M // executed in a specific order, but these steps should( n) C1 F3 |0 g" v9 P
// take no (simulated) time. The M(foo) means "The message
! ^1 c$ x/ y* X% ^- l$ f9 S9 g$ y( g // called <foo>". You can send a message To a particular2 m2 M% s6 B0 C d8 _! k
// object, or ForEach object in a collection.
1 ~. M1 {# E; w
/ n! `. a9 R# h" [ // Note we update the heatspace in two phases: first run
" a1 c4 Q3 ]' }7 ^: e; e9 N1 Y // diffusion, then run "updateWorld" to actually enact the
i, F0 ]9 d: `/ Y // changes the heatbugs have made. The ordering here is _: `* Z. l4 T7 S
// significant!+ \) {& p8 ], u" E, s- n Q2 H2 A
. Z1 Z0 a, B* C! y // Note also, that with the additional
. x4 U" E) S! E" N3 p t // `randomizeHeatbugUpdateOrder' Boolean flag we can
) a! u( E1 H* w" `, j% u // randomize the order in which the bugs actually run% o4 W2 Y- m4 K! O1 P1 {
// their step rule. This has the effect of removing any, {& l, q Y# X3 v; Z1 V5 H
// systematic bias in the iteration throught the heatbug
/ h; t8 q: C2 A5 @2 ^ // list from timestep to timestep9 |& y% A2 G" p% G* Y" }- s
2 r- i& L* `! d; E) d: g // By default, all `createActionForEach' modelActions have
5 p! z5 S) Q: q4 J- n0 C* G // a default order of `Sequential', which means that the
( z$ c5 ?& {' m // order of iteration through the `heatbugList' will be
+ Z2 I8 w: N1 F. d6 R. Q // identical (assuming the list order is not changed
& F+ M+ G7 U& m9 I& U2 a5 C/ s& R // indirectly by some other process).
1 L7 X6 ?5 t, }: O! K! b
# T# p1 H" t$ X# j' }& G0 @6 [ modelActions = new ActionGroupImpl (getZone ());: P2 k `3 D; g& |3 Y& y/ n0 a
. w0 }( D3 ~. D4 j w try {% v* M, n N1 ?8 Q. U
modelActions.createActionTo$message
5 z- r4 _$ D6 u (heat, new Selector (heat.getClass (), "stepRule", false));6 M1 v0 k0 J1 V6 u1 J( A
} catch (Exception e) {7 ?& M5 O9 M6 c3 N% H& D, z
System.err.println ("Exception stepRule: " + e.getMessage ());! y# |( [, X' F. _ v) o
}
$ R- q* O6 k+ j7 t: r, @' s8 m9 }! f+ z, G7 p* F
try {
% s0 i( {5 p6 d# d( ^3 m4 l" y0 N Heatbug proto = (Heatbug) heatbugList.get (0);
& A* V/ r6 ?+ i5 {9 k U) F3 ]8 G Selector sel =
$ ]* X* \2 G" T, _ new Selector (proto.getClass (), "heatbugStep", false);
8 k! h) B& O! {& N2 w actionForEach =4 o% R( R% R' G- O9 n
modelActions.createFActionForEachHomogeneous$call
6 a. S: p, x0 ^* a. y0 w( ~ (heatbugList,' H2 l: G, t Y0 D
new FCallImpl (this, proto, sel," G0 Y" ?2 b- f; U3 K4 h
new FArgumentsImpl (this, sel)));
: H2 \4 X) e, O& W& L# ?. \ } catch (Exception e) {
; k/ ]. `- P6 K& G3 e e.printStackTrace (System.err);
% k& h) p! a2 u4 Y. {4 p }
8 i7 }5 C; I: @ 6 _( a L0 Y8 l3 ^# d
syncUpdateOrder ();
$ X8 _( O- ?' ~: g5 D5 g2 _, [4 @- ?% P, F4 [! f/ I4 }1 d5 i$ W' y
try {
3 `1 X R i; O( P& c( V modelActions.createActionTo$message
$ a5 u2 r8 }' Z' | (heat, new Selector (heat.getClass (), "updateLattice", false)); R/ a, B( p. z; z/ I
} catch (Exception e) {0 p' _# V: |) \! y4 D! E, ~
System.err.println("Exception updateLattice: " + e.getMessage ());
2 T% Y- p' ^, v1 L }# W9 v- T1 [* i) a7 {. u) z) F, j
7 u# i! v+ g# e* B
// Then we create a schedule that executes the
( U' H% r& u( y. k- { // modelActions. modelActions is an ActionGroup, by itself it- }! E# y& k' A9 I$ c* S
// has no notion of time. In order to have it executed in* a* X5 g% V' u0 c) r6 p
// time, we create a Schedule that says to use the' m: X$ d# d" U9 m2 v
// modelActions ActionGroup at particular times. This% u1 x6 k: E/ z E* n
// schedule has a repeat interval of 1, it will loop every
: A7 ~4 [: f+ f* G0 R- U8 u; Z // time step. The action is executed at time 0 relative to
. L' s6 ?; L2 D6 U$ `) X4 N+ F: P. ?; D // the beginning of the loop.8 V* K5 P1 q# P% M
! l8 R/ W W7 m // This is a simple schedule, with only one action that is
- p; i9 p. x7 U6 s+ \ // just repeated every time. See jmousetrap for more
/ O. V+ u, [1 v // complicated schedules.
+ r0 ^" o/ U. l" T8 n
, K2 l* u5 A" |$ w( { modelSchedule = new ScheduleImpl (getZone (), 1);
0 `: P$ X H: h7 O modelSchedule.at$createAction (0, modelActions);
3 M' t. @% X% @* p+ @3 v : W6 X! J: P1 J2 s
return this;
: ~ H# m3 J' o2 l# j } |