HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 Z$ r9 _& @2 e1 p0 X& X1 N9 Q6 |# t$ a( {
+ ]; C' s. D+ c$ G" X. L& ] public Object buildActions () {
" `( ?7 x J: d! N3 D super.buildActions();
: G; X+ i. m/ w& {8 N3 \4 J " ^: G- u6 |: c* E# b
// Create the list of simulation actions. We put these in9 ]( C) v5 b. ~+ z& C8 Q% \
// an action group, because we want these actions to be, v7 v& k2 m4 l7 Q
// executed in a specific order, but these steps should
+ n& Q f0 e0 C& U/ k // take no (simulated) time. The M(foo) means "The message
1 y2 }( j7 n7 i, g* {; u // called <foo>". You can send a message To a particular
* v% ]2 y5 X- O% y1 U& K // object, or ForEach object in a collection.8 u2 e) S1 y! K1 L, H! r' X' u
: [. ?$ l3 ~4 }% }
// Note we update the heatspace in two phases: first run
, t0 [$ v& R( b6 ~6 {3 ^4 e // diffusion, then run "updateWorld" to actually enact the, Z: T2 D. h3 k5 U3 R
// changes the heatbugs have made. The ordering here is7 H9 g% R; G* d
// significant!( e: t! d! W2 u4 t
# } d8 f8 j% _* l& ?1 N2 K; c5 v // Note also, that with the additional. x. @, {' j- J" ~
// `randomizeHeatbugUpdateOrder' Boolean flag we can8 v" v4 e8 T: B- @3 V, z6 {0 \
// randomize the order in which the bugs actually run
8 n z, p* A& M$ C/ p, y' x" h // their step rule. This has the effect of removing any
7 J+ j7 ?+ ]$ i // systematic bias in the iteration throught the heatbug
9 v1 Z& i" _3 r" s |1 k5 N$ _# U2 u6 I F // list from timestep to timestep
2 P: a6 r& t+ j+ z6 r
" S+ n" v3 n% f D // By default, all `createActionForEach' modelActions have. G- X" a C% m- f* Q* F' c
// a default order of `Sequential', which means that the2 `& [3 {4 `, g9 _+ r7 I
// order of iteration through the `heatbugList' will be
6 S5 y4 n- \8 |# l" g/ D // identical (assuming the list order is not changed
% E4 m% {) e2 w' h! }- d) t2 f. L // indirectly by some other process).
+ h- N6 x2 H$ Y+ |' W2 H+ ~$ z
b, R6 C3 V1 S modelActions = new ActionGroupImpl (getZone ());
: y3 C9 h: I- q6 j
: Z2 R8 l6 ~7 h! }+ E' u; \ try { Q0 y* w1 g/ P/ D: @6 s6 |
modelActions.createActionTo$message( @' k- `& ^* Z/ N
(heat, new Selector (heat.getClass (), "stepRule", false));
0 [. F% G4 K/ S& e) z" g } catch (Exception e) {$ Z8 g7 M0 d) d: M. E' Y
System.err.println ("Exception stepRule: " + e.getMessage ());" l$ L& {- j% K' ?: e
}5 p- N" J% a" A) J! ?! i
/ z- W+ A! w: k: U! }5 ~ try {
% X$ C& t0 D. A2 Z Heatbug proto = (Heatbug) heatbugList.get (0);
3 B/ ]* }2 [3 Y! S* q Selector sel = . Z# d W; z3 d% T. |0 k
new Selector (proto.getClass (), "heatbugStep", false);
8 y6 m) `2 A0 j+ }7 ?8 o% o5 A actionForEach =
/ ^3 R( x- @8 y6 r- G modelActions.createFActionForEachHomogeneous$call+ v4 w/ T% p7 C
(heatbugList,
* K0 a8 z Y" z- D. l2 b$ {2 g new FCallImpl (this, proto, sel, M p' L2 g$ P. C% x3 C
new FArgumentsImpl (this, sel)));
- p/ z$ T- W# [% @" d& P } catch (Exception e) {
+ q. h1 i1 b! S9 r/ S e.printStackTrace (System.err);7 q, G* G" K. s) o
}
$ u. o5 T; v7 V T5 R: Z$ G$ h/ C8 ? 0 t5 P, T7 n: {# V1 W; b
syncUpdateOrder ();+ f: ]- G1 X" T% u
/ E1 m0 ~. ]" c2 B4 I" q) S- \$ ?: q
try {
" M& b- P( G" a2 i modelActions.createActionTo$message
/ Z$ c4 }& @! w) ~ (heat, new Selector (heat.getClass (), "updateLattice", false));
5 X$ r7 @% F) \3 H' w4 Q% [ } catch (Exception e) {
& W9 y6 v* s& N4 q4 a7 _ System.err.println("Exception updateLattice: " + e.getMessage ());
2 w, [, d# W8 C; ^ }
$ Z0 K* | c$ ]
9 u$ i$ Z2 [/ @3 s' d // Then we create a schedule that executes the5 x' g" A- b1 b. Y; X `+ [
// modelActions. modelActions is an ActionGroup, by itself it
/ c) Y6 D" T/ _) Z. T# `! `, ` // has no notion of time. In order to have it executed in
. w/ B: _2 ]2 m7 f Z // time, we create a Schedule that says to use the
7 S3 s: y' h" ]0 i2 n I+ ?" b; [ // modelActions ActionGroup at particular times. This
, U" `' ^% E. _' h // schedule has a repeat interval of 1, it will loop every
2 X% \/ {: J- `5 f# [9 J // time step. The action is executed at time 0 relative to( H3 t5 N1 a7 u; b. n. O
// the beginning of the loop.
: P5 j! I$ x& {2 S# D& B9 y
, F9 G# g% l% c. V9 M( R# x // This is a simple schedule, with only one action that is4 G% F5 Z4 ?- t6 |5 H* x+ N* m* {
// just repeated every time. See jmousetrap for more
+ q9 H0 c$ g' c+ T6 [4 V. M( r# | // complicated schedules.
/ K4 h2 z# I N & K( c0 d4 y6 z* W* |" L5 m1 G
modelSchedule = new ScheduleImpl (getZone (), 1);
* u$ a, `# ^! ^; ?2 Z" F! @ modelSchedule.at$createAction (0, modelActions);( k9 Z7 M. p3 @' t. B. z; g/ ^6 F
0 a2 E9 U3 p+ ^+ w7 G: H6 }7 I' D8 ]" w return this;
& l. W3 n) U( j: @. U# v a } |