HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 A+ C& i; U7 ^% q) {' L
* L6 Z. c; y5 V+ i2 P public Object buildActions () {
4 ^1 `' z) w7 A3 M super.buildActions();8 B: F; H, ?4 l$ `
2 D* b1 G0 Q6 m+ a: V! n // Create the list of simulation actions. We put these in
% f2 O2 i8 C. ]! I- N$ e' \ // an action group, because we want these actions to be
5 c7 h% x/ `* y( m( [, |) p5 _ // executed in a specific order, but these steps should' t# Y5 C( X& E% X- \- c! [
// take no (simulated) time. The M(foo) means "The message
( i, }2 [/ p" |) K0 M* V9 x // called <foo>". You can send a message To a particular
' h0 e5 w$ \% c7 C& m& N5 P // object, or ForEach object in a collection.! I6 l3 z6 L4 I- B3 m
! W# U r( ?& k" g( Y4 h // Note we update the heatspace in two phases: first run
" M* [4 a6 I" G // diffusion, then run "updateWorld" to actually enact the" u, U3 X) r4 i6 W# }
// changes the heatbugs have made. The ordering here is
. {- V7 y& T" i/ E2 s4 s) Q // significant!
4 o( M; \" ~, i2 T# ^
( O4 X; P/ ~9 M& R1 d. P // Note also, that with the additional
4 x9 [* A1 q1 l2 C) P // `randomizeHeatbugUpdateOrder' Boolean flag we can( v2 L" \' h7 S8 q
// randomize the order in which the bugs actually run
4 C: ` I& `2 H' P // their step rule. This has the effect of removing any
d; b& O( h& p7 q5 S; i, d6 ~" F // systematic bias in the iteration throught the heatbug
1 A( B5 a( _7 [/ [7 v // list from timestep to timestep
$ ]; _# U4 H# Y- j 3 n! y3 L4 ?$ ~# d- ^
// By default, all `createActionForEach' modelActions have
$ {& j1 _5 p3 p/ e! h // a default order of `Sequential', which means that the4 c- Q& R& j1 o) q2 f5 u; S' i; |8 m$ D
// order of iteration through the `heatbugList' will be" n1 }/ W# ~/ c0 i3 N. q
// identical (assuming the list order is not changed
( Z7 }1 Z' g; j( C' G // indirectly by some other process).
* P# f' c0 x8 M; R1 o4 W5 b " c) v, J* W' Q5 l
modelActions = new ActionGroupImpl (getZone ());
' T& H1 [$ ~( }7 h& k' c' Y8 m1 r* l5 v
try {/ H; {( F; F0 M/ \$ v* r- I
modelActions.createActionTo$message9 Y! w: ^+ i) F/ r4 s, }
(heat, new Selector (heat.getClass (), "stepRule", false));" a+ O. ^1 Z# B9 ~/ s
} catch (Exception e) {: T# T4 Z0 P$ l& K0 b4 T' t
System.err.println ("Exception stepRule: " + e.getMessage ());
/ C% g" e$ c7 c) Y) g: F, k }! j4 G0 Y' ?1 H# M/ m6 j% q; x
+ S2 I$ p }$ m4 Z5 I4 E try {& ^/ S1 V( X1 X+ a7 s5 H
Heatbug proto = (Heatbug) heatbugList.get (0);
) U# }& O: \. K- ^ Selector sel =
1 [. |, l- u6 \ new Selector (proto.getClass (), "heatbugStep", false); {; [8 ^4 Y3 e
actionForEach =; H7 M: [' s* |1 C; h3 V
modelActions.createFActionForEachHomogeneous$call
5 s9 B+ ~/ K% S7 ` (heatbugList,1 b5 \' J& R y @
new FCallImpl (this, proto, sel,, ?1 Q9 b$ F8 K
new FArgumentsImpl (this, sel)));$ o% i+ j0 ~- O0 R" o" Y0 L) l
} catch (Exception e) {
! E9 {8 R: c. G' o3 g e.printStackTrace (System.err);) v; ]- u2 ~( B1 |* m2 y- _5 ~
} Z2 w1 v' F* |0 F; J9 o- v6 I
F8 n+ A1 P9 n1 x. u4 e9 H
syncUpdateOrder ();% i; q9 A" ?% o- k, P. @( m
% K& e+ W. Q% i2 b
try {
' A- @2 L P; b/ B, l modelActions.createActionTo$message ' U, j, T/ z: d6 ?9 [6 D
(heat, new Selector (heat.getClass (), "updateLattice", false));
: `+ j8 [9 @2 }! X; j. m } catch (Exception e) {
& y' H1 W$ X' B* Y System.err.println("Exception updateLattice: " + e.getMessage ());. R% u5 H' d4 f# y, p5 o4 s2 G; B0 d
}
/ r! b" D4 G( l Y/ J) S. C3 Z # a! p* b- t5 j" G6 y# p
// Then we create a schedule that executes the6 B8 h! O4 ?$ X+ j& f `! q' E
// modelActions. modelActions is an ActionGroup, by itself it
- t5 E- ]5 [! p9 l. _+ ^$ _6 t- Y$ I // has no notion of time. In order to have it executed in1 b1 u0 e6 { v2 ]
// time, we create a Schedule that says to use the
7 f2 D. y* n5 N& y9 H; K2 S // modelActions ActionGroup at particular times. This, k0 E4 H5 ]9 b) T/ N/ W z
// schedule has a repeat interval of 1, it will loop every. d; x: M# ?/ r
// time step. The action is executed at time 0 relative to. E& w1 V4 {8 x
// the beginning of the loop.9 Q. A" _- ], s2 d
. i C6 ]5 @" l* b9 I9 i* ? // This is a simple schedule, with only one action that is
G3 i. {# ~. j/ ]6 \" U // just repeated every time. See jmousetrap for more$ R, A/ Q2 b8 i* D i! Y) x" L4 R
// complicated schedules." W/ H* G* w- L+ A9 N
7 _$ H: f. _7 t9 I modelSchedule = new ScheduleImpl (getZone (), 1);
' S1 H" W; H6 g B" D modelSchedule.at$createAction (0, modelActions);
( X' R# N R, n4 A ; k: Z& D/ t9 @1 _0 }7 b
return this;7 `5 K1 c Z; H4 T4 R) M7 \/ @
} |