HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, e4 t1 w& `* a5 I: F8 D) ]6 @
! e1 S7 ]6 F! d6 o) l$ R j- g" k public Object buildActions () {, ^) K6 u4 U& y9 D) o3 D+ `
super.buildActions();
7 M) C4 k7 y& W/ o4 Y, R
0 B( |7 l+ u0 N5 k* s, o: Z5 i // Create the list of simulation actions. We put these in" o. j! f1 p) F/ [+ Z; a* j
// an action group, because we want these actions to be6 G# I. [! K4 G# F4 s
// executed in a specific order, but these steps should
) M/ N" ?1 ^% s // take no (simulated) time. The M(foo) means "The message
9 ^+ _$ u; K( N9 S* e // called <foo>". You can send a message To a particular4 ]8 n4 P, X0 {
// object, or ForEach object in a collection.8 B, w& a1 e% |/ G
/ h: ]5 D6 K1 w3 g! S // Note we update the heatspace in two phases: first run t4 B/ l& j' v2 `5 o
// diffusion, then run "updateWorld" to actually enact the0 r) I6 V2 E% r& D1 A
// changes the heatbugs have made. The ordering here is
% t, |, J; [ j // significant!
; ]+ V; P' c+ M' f
8 T4 a: G; c& Q, l1 S8 f // Note also, that with the additional o5 X! {8 w( N* `6 l1 a+ [3 P
// `randomizeHeatbugUpdateOrder' Boolean flag we can& [* x$ @9 f4 v6 g, X4 B ?: S, |
// randomize the order in which the bugs actually run0 v% {! S! W6 f1 o# m2 z( d
// their step rule. This has the effect of removing any* X- M5 R) P& T) Q* t$ ?) x$ O8 \& i
// systematic bias in the iteration throught the heatbug: o( Q- J& I& i5 }7 s/ ]: s
// list from timestep to timestep
7 T$ b" i$ F3 f3 X! o, ]% O 7 k7 c! [, k( V7 E! Z2 |3 G* m3 z
// By default, all `createActionForEach' modelActions have
7 d3 \0 o$ _: V+ B5 t // a default order of `Sequential', which means that the
" u# s. k8 C- g // order of iteration through the `heatbugList' will be4 G1 v7 \6 D# t
// identical (assuming the list order is not changed. \ c1 r3 |5 A# d
// indirectly by some other process).
: s9 D- ^. v7 q* h/ T+ n1 ` 0 e0 E1 p& [; v" R) H
modelActions = new ActionGroupImpl (getZone ());
. j5 {1 |6 `3 p* Y" ~" D1 F
% p# @! Y! ~$ g+ |) j try {
; S7 @5 t4 x0 a6 ?! z# a. g modelActions.createActionTo$message
# r ^& n& v3 [9 C (heat, new Selector (heat.getClass (), "stepRule", false));
0 [( g" {; c5 f" x8 ? } catch (Exception e) {# r$ m0 { e+ F0 ?. l6 z$ O( h' j
System.err.println ("Exception stepRule: " + e.getMessage ());* }9 g H, b8 Z* V
}# u% I3 C* A' g
$ L! t+ ~/ Q. |- g try {
' j$ a# H+ [" f+ y Heatbug proto = (Heatbug) heatbugList.get (0);
+ {4 z: p; o- ~! J% |) q Selector sel = 7 Y& _ f: c$ d3 m2 f/ p
new Selector (proto.getClass (), "heatbugStep", false);
' c9 z( ? }7 n actionForEach =
7 [% C4 B4 l! p2 ~! V modelActions.createFActionForEachHomogeneous$call8 R5 l) d2 t/ j+ R! H
(heatbugList,) B1 G( C' } R1 P# i; r, Y9 ^
new FCallImpl (this, proto, sel,& y, w+ G1 q2 l3 V' T+ \
new FArgumentsImpl (this, sel)));
5 {6 [/ n+ V6 C2 { } catch (Exception e) {
# o6 N6 p/ G' M. H; p* ^ e.printStackTrace (System.err);
+ s* H7 m$ T4 K4 J }/ C7 r' P$ N" Y8 _% z' Y4 z
% i z2 ]+ }* U4 | syncUpdateOrder ();4 L. ~1 R6 j. l& U1 U3 S8 n
8 l4 c' i. q4 d5 T% ?
try {, k) l! Z4 `5 E5 E
modelActions.createActionTo$message 9 ~! {& h% j0 X% g
(heat, new Selector (heat.getClass (), "updateLattice", false));! Q- A* m- x2 R, \
} catch (Exception e) {
. B7 S5 p/ T5 x! W System.err.println("Exception updateLattice: " + e.getMessage ());1 I& v2 J: {3 c* v S& R& I1 q, _
}, M. E S% y$ `6 |/ N7 d9 |- ?
: d' e5 A) U, p
// Then we create a schedule that executes the& }# f( m7 x6 _) I! e& T
// modelActions. modelActions is an ActionGroup, by itself it
/ A/ y5 r2 X( B, e @ // has no notion of time. In order to have it executed in
c0 T' P3 b7 } // time, we create a Schedule that says to use the% l7 N9 W/ c5 A+ B/ v
// modelActions ActionGroup at particular times. This6 s1 G) F w. @( ]! e; L& C) I5 C
// schedule has a repeat interval of 1, it will loop every7 x: B d' e. R+ F
// time step. The action is executed at time 0 relative to
, A# B/ J( V3 d: F7 s) C( C7 ? // the beginning of the loop.& K$ ]) w* M% Z& c" `. \
! l( w5 f- Z- S) M4 h // This is a simple schedule, with only one action that is
: c& w( _# m9 s) a U. n+ }" B1 Z // just repeated every time. See jmousetrap for more! y5 j- Z9 S6 d3 ]: P7 ~; I6 _
// complicated schedules.1 T4 {3 u2 c0 g3 ^7 H0 V8 M9 ~
4 W5 N* u6 C L( U( W3 _: M modelSchedule = new ScheduleImpl (getZone (), 1);
& G; K6 I) Y# g9 j' Q+ G9 o modelSchedule.at$createAction (0, modelActions);, Y' S5 L) k1 P! A$ B
u9 }$ z: V c
return this;( \$ ~9 P0 ]/ k( C& n
} |