HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" f) F8 g0 w+ u' Y; m
( r* {$ t: G) U ^! h7 F5 \ public Object buildActions () {
5 ~& c% r# j* X* X! E$ V( w" _ super.buildActions();/ E6 }; G" a8 |9 Q( |# K0 M
* n$ l) D2 l" v5 O- f. o& s
// Create the list of simulation actions. We put these in$ F3 ]# n+ x, @+ `
// an action group, because we want these actions to be5 S' N6 M! p* z. \8 D* \
// executed in a specific order, but these steps should& k5 s# w/ T8 @+ v& o
// take no (simulated) time. The M(foo) means "The message
" s6 D8 y2 c' G1 u. R8 A // called <foo>". You can send a message To a particular3 W+ V' [: Q' H. ~6 E3 N# h
// object, or ForEach object in a collection. N2 |5 |, t- v1 e
7 a/ Y* u, ]! _4 n6 ?6 B @2 \ // Note we update the heatspace in two phases: first run
. @& J% S- r2 q7 [% O! C // diffusion, then run "updateWorld" to actually enact the& `& O s6 U: p! R- o3 t
// changes the heatbugs have made. The ordering here is$ e' [- F6 x4 U* {
// significant!
4 T" ~9 s* a2 u$ d 3 C/ n( p% S2 t; b
// Note also, that with the additional
4 q' [4 H# ^# N; p // `randomizeHeatbugUpdateOrder' Boolean flag we can/ N8 r2 C) p' ?; ?. l& G
// randomize the order in which the bugs actually run1 f* t Z9 K5 B
// their step rule. This has the effect of removing any G$ b1 \) T) W3 {
// systematic bias in the iteration throught the heatbug
+ O% [8 Y: G# y4 T( o$ N% R // list from timestep to timestep
. s6 x3 J- z/ y2 S& l/ s 4 u( K% t* b- }6 Q
// By default, all `createActionForEach' modelActions have
; k) Q9 o7 i& o9 U8 { // a default order of `Sequential', which means that the- q! M$ \2 J( O, i$ T9 R# c# h( U
// order of iteration through the `heatbugList' will be) s3 Z5 u& v5 g$ }, b* Q* F
// identical (assuming the list order is not changed
/ V! p" H& W. D; U# L3 v* l( }% \! [ // indirectly by some other process).
9 m( l/ P/ k- G+ q; z4 P+ P 7 j# T4 l6 g3 E, P
modelActions = new ActionGroupImpl (getZone ());
/ V$ {5 N, k O4 C9 k( Q9 U& e* K5 d4 y2 d1 }5 J! Y
try {, K+ G" G3 k$ N8 Q* T. t
modelActions.createActionTo$message
& b0 w7 E+ i; b c! l+ A# @" q3 _ (heat, new Selector (heat.getClass (), "stepRule", false));! k6 O; o Y' r) m
} catch (Exception e) {
1 F C' A$ A# y/ R! m System.err.println ("Exception stepRule: " + e.getMessage ());$ h+ ^5 J3 ^# Z% ], Z
}
- Q8 r" u, e# B& b' d2 C/ a& g1 H+ q3 v) l$ z
try {
- j7 ] B8 k3 p2 b" q/ M Heatbug proto = (Heatbug) heatbugList.get (0);, P: ~: B4 S* l
Selector sel = & U' a- B" P' h" R8 N
new Selector (proto.getClass (), "heatbugStep", false);
6 h& P! b5 G, Y! b actionForEach =. o9 r3 Z! S" @8 \% @' g
modelActions.createFActionForEachHomogeneous$call0 g& }5 p: I8 ], s$ b/ Q
(heatbugList,% {3 b5 M' n7 U- @7 e7 j( F J6 ` z
new FCallImpl (this, proto, sel,
( i: }) }: ^2 r+ d8 {% `3 C& Q new FArgumentsImpl (this, sel)));
^5 ~0 l& [' ]7 {& q; | } catch (Exception e) {5 Y! n. ~, f" z) \
e.printStackTrace (System.err);# S: \1 |! z6 ~0 h& g) H
}
( F5 x ^+ M* o4 s, B2 j+ t ; `1 Y* T/ B [- q
syncUpdateOrder ();2 C; Q. g' [8 Y; n b3 u
, x- a2 c! ~2 e try {: N: e& X" c. ?' ~
modelActions.createActionTo$message
) Q+ t, U, {2 D$ P0 k% a# L (heat, new Selector (heat.getClass (), "updateLattice", false));+ l9 F' }/ Q, A* f( V
} catch (Exception e) {
5 L' Z, K- z7 _, V- B. E System.err.println("Exception updateLattice: " + e.getMessage ());
; b" p# _/ V& a8 g+ P/ B }
1 D9 f, D- G; O$ @ 6 \4 y* v; E( j. T, N# p* X
// Then we create a schedule that executes the
a' m2 O$ h7 X- A // modelActions. modelActions is an ActionGroup, by itself it2 S( {+ W( `! B5 s
// has no notion of time. In order to have it executed in. W, R- l& {' [9 [
// time, we create a Schedule that says to use the
* {# F1 H' m& R/ p/ ` // modelActions ActionGroup at particular times. This
7 Z6 d8 ?; e; j) f/ Y1 ` // schedule has a repeat interval of 1, it will loop every8 P8 a6 [. w+ G3 I }3 w
// time step. The action is executed at time 0 relative to" W1 Y8 W7 ^; r5 u
// the beginning of the loop.
/ l! k" E" Q/ b* e q+ Q
7 R8 w! R% x* _( Z( h& ` // This is a simple schedule, with only one action that is" f+ E3 |' ~% s- \, D& Y2 U$ u
// just repeated every time. See jmousetrap for more+ V/ u/ e1 O2 ]! b6 X$ c, C1 @
// complicated schedules.
. m8 Z* Y- y3 V: B
& W! F6 f, q8 I( p3 L+ y9 d modelSchedule = new ScheduleImpl (getZone (), 1);
1 g8 i, H: ?' `; M modelSchedule.at$createAction (0, modelActions);+ `0 Z; r: d/ U1 y" V5 }$ j
% @, f5 X* s/ |/ I1 M( B6 {
return this;$ T2 @4 f+ |6 d( y; D/ u0 f
} |