HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 x- C" Q2 m2 d& ~0 V
2 ]# W& |! V) n public Object buildActions () {* K+ y& l2 \% y# S$ g2 y7 m; Y
super.buildActions();
7 w6 c$ t0 z* j& B
/ d& T! r9 h7 { // Create the list of simulation actions. We put these in3 D5 {% g5 j2 N) n% {9 U g
// an action group, because we want these actions to be
6 T6 A. u) Y* ~! |2 s // executed in a specific order, but these steps should
( N$ L; B% `- n8 j4 }7 ~ // take no (simulated) time. The M(foo) means "The message; l* j3 S$ l, k1 j% Y
// called <foo>". You can send a message To a particular2 |" x4 w& q3 R- c5 K
// object, or ForEach object in a collection.4 ?1 D @ x% {0 @
6 l% M4 T7 N( |7 r1 v: L, ~ // Note we update the heatspace in two phases: first run
* Z0 @* P. @. \! {1 L // diffusion, then run "updateWorld" to actually enact the
, Z9 e$ s3 }/ `/ U& z3 o. f: Y // changes the heatbugs have made. The ordering here is
$ u) r! R. B0 b4 P/ b3 \" N& f8 P // significant!1 m) l5 m. |) W, Q
, U8 ]& E) C1 N9 _* |3 X
// Note also, that with the additional
4 M0 ?8 V% Q$ M1 J0 V7 j. Y // `randomizeHeatbugUpdateOrder' Boolean flag we can2 n f7 ?2 S* w6 k. Z- ]
// randomize the order in which the bugs actually run( P6 p8 h( t# s$ e% r7 P
// their step rule. This has the effect of removing any! @, M4 k" _; y, P0 u; `' N
// systematic bias in the iteration throught the heatbug& V* ^1 f& I! z; P! [
// list from timestep to timestep
) g( W" q. A/ |! m6 m; j; S* a% @
7 |/ e$ a& k2 O+ U$ u3 ]" R // By default, all `createActionForEach' modelActions have
1 W6 Z1 ]8 i5 z // a default order of `Sequential', which means that the
1 l; r1 N" I; M // order of iteration through the `heatbugList' will be
9 Q# i: r5 g9 b+ p1 X // identical (assuming the list order is not changed
* g/ ^7 r# r3 N // indirectly by some other process).. i0 X E7 r" E+ Z$ l3 V. w
0 H, S& o( G% a: W6 |
modelActions = new ActionGroupImpl (getZone ());7 I* m8 D! J: z' ^ T. d
' N! J# P% F2 ?. R) Q8 c7 A% E6 t try {
8 f; D: a) Z( M p' j8 w1 S modelActions.createActionTo$message: m" s) u1 |# `
(heat, new Selector (heat.getClass (), "stepRule", false));- U+ l8 E" _) h. F& Z- i/ V
} catch (Exception e) {
& c; [% P4 _+ s, s7 v2 m3 [ System.err.println ("Exception stepRule: " + e.getMessage ());
( L. r3 R$ M% o1 i, i }
1 t* G& }* v s4 }. X F) M8 U7 u: s& H- k; p5 o9 w3 \
try {9 {4 n' @% s3 `: {5 ]& [
Heatbug proto = (Heatbug) heatbugList.get (0);
, D2 g1 F5 K* f Selector sel = $ t j; O9 ]" E' _: {
new Selector (proto.getClass (), "heatbugStep", false);# G: g' N& ?, I& M. a1 w& M Z
actionForEach =
) W% s0 e7 {/ F0 N4 ~6 w modelActions.createFActionForEachHomogeneous$call V2 l5 E2 ~" E% T- H7 M4 v
(heatbugList,/ l( y$ J' ^1 ]4 N: A2 I
new FCallImpl (this, proto, sel,
+ `! D" j* \2 P$ L new FArgumentsImpl (this, sel)));
; y3 d6 F8 ^4 X: p3 F } catch (Exception e) {
. Y8 G2 s) a# l& ?$ G) w e.printStackTrace (System.err);
p& M2 |" Z+ G- s8 i2 e }& Q# H. l4 E+ F* t8 E1 j
1 i" k0 c: b% b* S# Q syncUpdateOrder ();
( c0 }) Z3 n" m; ^4 s+ ?2 H: ?; D! q5 j! i
try {3 C" P. i: N: v) d/ }! f
modelActions.createActionTo$message ( {6 ]5 u5 h3 r2 n$ |
(heat, new Selector (heat.getClass (), "updateLattice", false));
* ~8 w, S6 @$ ]. n( V } catch (Exception e) {- H: p, ^: K) r- \! t
System.err.println("Exception updateLattice: " + e.getMessage ());
4 a4 I% V/ e' I) x" [ }
; A1 y; F+ D$ T! J. H# H
+ k8 i- m7 r% _; M; N9 u // Then we create a schedule that executes the
. h, i# X9 H% |2 ]5 ?' s // modelActions. modelActions is an ActionGroup, by itself it( F. t6 l) m7 [
// has no notion of time. In order to have it executed in
! H# B3 L* O+ P% _4 E9 S // time, we create a Schedule that says to use the, P/ z4 ]- P0 u6 o, j4 ]9 t2 v* _3 \
// modelActions ActionGroup at particular times. This
7 x( Q A T {: O0 r // schedule has a repeat interval of 1, it will loop every
- v l& K9 T$ w2 c7 _: i1 N // time step. The action is executed at time 0 relative to0 s( v% {- A( w9 ?
// the beginning of the loop.& `, p' p/ [7 F! Z9 }, i
# Z+ z% [- A0 V7 C7 Q
// This is a simple schedule, with only one action that is
0 z9 J1 P; {# p4 Z // just repeated every time. See jmousetrap for more
9 w1 [4 O: b: a: p) U7 v // complicated schedules.5 w; G6 U7 i# O% D- M R; h z+ H
: }( k8 ~( x7 v& d5 v3 c7 x
modelSchedule = new ScheduleImpl (getZone (), 1);
# g: Q6 C% J7 ]+ b# v' A; e' B modelSchedule.at$createAction (0, modelActions);
! `3 X. O9 d3 ~ 7 Y/ \; B6 u2 {3 u, u1 D- _) l
return this;9 s( E# a* t9 e0 _- q1 m0 D
} |