HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! [5 g: j# w0 Z% f- M/ o6 m1 Q5 d# Q- `, Z- {, [9 E& @: |; q# I
public Object buildActions () {5 J* V; _9 @" f; c6 A. C
super.buildActions();6 _. ?/ E" z5 n# h
3 x/ J+ f) b% v }9 G: ^ // Create the list of simulation actions. We put these in
/ y8 b7 i0 w- \" V // an action group, because we want these actions to be
1 L! N& [ S; X // executed in a specific order, but these steps should
) D8 n# E* u, q. u // take no (simulated) time. The M(foo) means "The message3 b& E1 L; r" J' G3 Q2 e
// called <foo>". You can send a message To a particular( V* ~* \! x+ W: a3 @) ~3 G* c0 I
// object, or ForEach object in a collection.
9 E2 B t" A8 R
- M/ W& @2 C0 _" d" p. J* X // Note we update the heatspace in two phases: first run
$ |* o- e$ m; U: I( I4 O // diffusion, then run "updateWorld" to actually enact the
! j' r! s% x8 P1 A& T' e) A( [ // changes the heatbugs have made. The ordering here is
+ y3 h( N. u2 V- K8 u // significant!0 M+ y W# X0 X
( X* R9 V0 k# o/ U& k) d( `# k // Note also, that with the additional/ W' G# s2 C* l# E8 Q
// `randomizeHeatbugUpdateOrder' Boolean flag we can: F, v% j7 c$ j- {& {, Q% |
// randomize the order in which the bugs actually run
5 f+ Y7 d- \1 \/ W2 w // their step rule. This has the effect of removing any, Q+ r2 L; G; O" Z8 ~% Y
// systematic bias in the iteration throught the heatbug' X1 K% X5 z/ w$ z/ f
// list from timestep to timestep
0 R! r% s+ P$ H4 N3 ^9 a 8 Z0 e% f. r4 y$ n3 Z; A$ T! Q
// By default, all `createActionForEach' modelActions have: D8 K8 B$ W, P3 y
// a default order of `Sequential', which means that the3 d: P; n+ U( e% V# U) e5 M- A: f
// order of iteration through the `heatbugList' will be0 s M' ?7 G# w! O# N v8 c
// identical (assuming the list order is not changed; R% C. W! a- B& K' |( m- a; M
// indirectly by some other process).; w, ?/ |; ?2 E) \7 y
9 w4 `9 O/ z" O9 e
modelActions = new ActionGroupImpl (getZone ());9 d3 \1 I* x' a7 w; s2 ~
2 k3 O; ]5 \+ J: a7 K4 n try { |2 `5 I% ~2 o8 k- _8 S
modelActions.createActionTo$message
; E. b" L) v! X1 n0 b4 q- L (heat, new Selector (heat.getClass (), "stepRule", false));! I$ A: |7 m3 M2 J5 f
} catch (Exception e) {
; @2 ]. b4 G; I System.err.println ("Exception stepRule: " + e.getMessage ());
/ m" u5 l% ^' G/ o }
4 n6 Q2 a: I' ~+ b6 z' b
* B0 F8 e9 E' c* U+ w try {2 X8 X w; h0 |& n
Heatbug proto = (Heatbug) heatbugList.get (0);
( H6 n3 X7 {7 `8 z5 x Selector sel = 3 K& Q1 p" W8 p& @
new Selector (proto.getClass (), "heatbugStep", false);& T9 v [% f! J6 n
actionForEach =6 \0 Z3 V5 k1 I. P% d/ A8 b
modelActions.createFActionForEachHomogeneous$call; q: V4 Y, D" M) a9 d
(heatbugList,6 E1 a( s) X+ f9 N; X9 L5 k+ H
new FCallImpl (this, proto, sel,
! N& t" W8 s/ k1 l. v0 _ new FArgumentsImpl (this, sel)));
' v2 d+ i& S* W) T } catch (Exception e) {6 e* J& ?; G( k3 E
e.printStackTrace (System.err);
, j) Y5 e; k5 V; ^8 l' l }
) g- n' [/ l' n$ Q' d
: F1 ~3 j% ?; z. a syncUpdateOrder ();: C) `2 U& M4 J6 ?
1 S% A" {' [- a, _! D. B4 |
try {
0 |$ V8 u; g8 O$ Y3 q% X modelActions.createActionTo$message
+ k5 c: _; h( h: ` (heat, new Selector (heat.getClass (), "updateLattice", false));
) h4 W, R6 {3 x8 B3 L9 {' o# ] } catch (Exception e) {! @2 b4 c, A2 z/ P/ y7 _
System.err.println("Exception updateLattice: " + e.getMessage ());2 y( Q7 C2 Z5 s* z1 E, k1 V
}& B( L/ T% M3 T3 S- [; o+ {' @
3 k) b. _7 J6 g( i* X; `) ?8 ~/ R // Then we create a schedule that executes the
& F; P" u6 \8 N9 n$ W U7 U // modelActions. modelActions is an ActionGroup, by itself it
% [! X: e0 H; o // has no notion of time. In order to have it executed in* g- ?$ I5 p, X; z/ g
// time, we create a Schedule that says to use the
- `3 {$ l; [: _ // modelActions ActionGroup at particular times. This
, ~7 L% O+ N$ _ L+ C( g // schedule has a repeat interval of 1, it will loop every
% x) v7 q8 L: e/ m, A7 F // time step. The action is executed at time 0 relative to
0 u; l" F: ]) q4 b // the beginning of the loop.
" U6 p& t6 K0 N) ?. ?; V" Y/ r- E4 A0 f. |# u Q
// This is a simple schedule, with only one action that is) b# e2 _' l K& N& P" Q* }
// just repeated every time. See jmousetrap for more
& V* [% f" W2 o' g' Q // complicated schedules.4 q8 I) t# R7 s
2 D4 T k3 |- Z9 h8 f modelSchedule = new ScheduleImpl (getZone (), 1);, f2 g7 N; l! H: A6 i' R' K; s
modelSchedule.at$createAction (0, modelActions);
0 f+ |6 m+ w) z5 \) r# s 3 H7 B+ Z, Y: F
return this;; d% B v" L1 e
} |