HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: O- G1 F1 T* d9 h! @1 V2 [
+ u4 z9 T) V+ I0 L. E; ~/ x* o
public Object buildActions () {
5 h/ l+ P$ H0 Y+ k super.buildActions();
8 n6 {% J0 S" c2 N4 {( {8 r" \
, w6 W, Q- v; G& J( |% E" y // Create the list of simulation actions. We put these in. h# {, R, Z L( e3 X8 f* |
// an action group, because we want these actions to be# d. g @# V0 N G& m
// executed in a specific order, but these steps should/ S5 m( y/ z! |7 ^- V) y
// take no (simulated) time. The M(foo) means "The message
' e" ~+ b" C+ v& u1 J2 u! V _ // called <foo>". You can send a message To a particular
. ^( U. J5 [, z" y // object, or ForEach object in a collection.3 [7 U* T% C y, D+ N( o _' Q
2 I5 \7 F* r0 J g" B/ e4 Q // Note we update the heatspace in two phases: first run8 N3 v5 N( u5 |: J
// diffusion, then run "updateWorld" to actually enact the
. T: L7 k. w9 E$ {9 ?8 c0 B // changes the heatbugs have made. The ordering here is
5 }, l6 B2 f8 I- t+ S // significant!/ r" o9 Z6 D" L' m2 D
! U9 H3 U; V0 _& C$ ] // Note also, that with the additional
; O, R) m+ s3 G: Y) Y: Q- M0 w8 ` // `randomizeHeatbugUpdateOrder' Boolean flag we can( G0 I/ K2 O8 R6 f7 y7 x
// randomize the order in which the bugs actually run
% ~8 X8 t$ |5 g" J7 U$ l" [ // their step rule. This has the effect of removing any
# E: z* V/ z0 S$ N# y // systematic bias in the iteration throught the heatbug! w8 S5 x8 d9 W8 c2 E
// list from timestep to timestep
8 f9 n& ~$ ]7 U4 Q) x " {0 R# r. S }% }8 z& _
// By default, all `createActionForEach' modelActions have. Q- z% t8 ?1 g" r$ J9 o9 v8 b0 \
// a default order of `Sequential', which means that the+ N- I( B8 L& b5 A8 F: v8 V9 m) t
// order of iteration through the `heatbugList' will be
! ^5 h9 n' _* ^* N/ k, y% @+ d" l // identical (assuming the list order is not changed
7 ~4 C3 f- y' c2 e8 p1 g2 { // indirectly by some other process).
1 ~7 L- Z" q: M : j4 d/ N e; `
modelActions = new ActionGroupImpl (getZone ());$ J! x* R( ^ s% |5 I' N, X) |
* u& e9 s+ ^4 `- t( q2 J' b9 S3 l
try {
, t3 N7 [" y( h; W8 k4 P- R modelActions.createActionTo$message% u* }: T& B# X
(heat, new Selector (heat.getClass (), "stepRule", false));
% `0 R4 i2 h x- K' t+ b( [" F } catch (Exception e) {' A# ?2 T0 ] ]- A
System.err.println ("Exception stepRule: " + e.getMessage ());
7 B! I5 Q1 S+ T2 Z# t, G8 q8 p }
, q) ^6 z: O% T( x( }! s* t' `( t9 A6 B$ J5 L0 g
try {
5 S) N4 V$ G2 Z Heatbug proto = (Heatbug) heatbugList.get (0);2 u5 l$ z; n- y( w
Selector sel =
/ H& U+ `! ?- b% C5 |$ W( W C new Selector (proto.getClass (), "heatbugStep", false);) N. `& y' w* L/ E$ h, E
actionForEach =: h8 d& }; z2 K# c
modelActions.createFActionForEachHomogeneous$call
3 J% X9 T% f' x2 K, m4 X (heatbugList,, w; l: U% p& J: L
new FCallImpl (this, proto, sel,
& p; A$ s8 o1 x! X: }' P new FArgumentsImpl (this, sel)));
+ j" x6 @% l8 y( u, e5 b i3 l } catch (Exception e) {9 c5 `3 _+ a% n6 F z
e.printStackTrace (System.err);
7 W t2 f4 N8 s2 N$ j; D% Z }
' Y, O; H& d: K' x3 B% p" d
! b: B& G l; {# z- m syncUpdateOrder ();( i& s6 V% c3 p3 ]" O; p* s9 z
& N3 k# {- c) @- L0 m. t( x
try {
, v; Z$ Q6 _6 @/ m modelActions.createActionTo$message 5 \8 b! E5 {' _$ w8 m# @
(heat, new Selector (heat.getClass (), "updateLattice", false));
+ R8 Y# G: F5 L, _2 T } catch (Exception e) {/ a5 L' S- |7 Q/ u
System.err.println("Exception updateLattice: " + e.getMessage ());
# @5 a9 q3 o7 t) q7 | }
! c) e0 l% N5 G
' H) X5 ?) `4 r( r& |" n // Then we create a schedule that executes the
- w% O; y/ W: h6 j8 {! h2 K6 E" H( Y // modelActions. modelActions is an ActionGroup, by itself it7 u6 g+ k8 M$ p) U, ]" ]% N6 P' b
// has no notion of time. In order to have it executed in
+ o }- W* i- T4 o // time, we create a Schedule that says to use the8 o) ~# M2 d* Z2 V+ ] E
// modelActions ActionGroup at particular times. This1 P7 ?' V0 F& t/ n9 g
// schedule has a repeat interval of 1, it will loop every! Y- A- {) H1 ~* @4 K( p+ J
// time step. The action is executed at time 0 relative to
5 U* A6 u* L: o# w // the beginning of the loop.
) k- t& y: f/ C. P2 h x0 `# B9 [0 `# ^
// This is a simple schedule, with only one action that is
# X# i% a) ?5 D) z/ d/ H! ^+ Q // just repeated every time. See jmousetrap for more D; Y9 y& J" b3 B% ?9 ]8 a
// complicated schedules.1 k/ {- }# ^0 ^+ v9 N2 i" r9 X
1 u/ z: R& p* Y; P& Y modelSchedule = new ScheduleImpl (getZone (), 1);
4 X& y: i5 o* {1 b4 Y modelSchedule.at$createAction (0, modelActions);) \7 g1 y3 a7 j2 R
: D3 _2 o2 w$ M- [4 { B* i5 X! g" x return this;
+ i/ i: U3 s k- l, f) x y } |