HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ n7 u' ~, d/ w, |2 x+ a y; l
% h; n( Q6 v5 R: { public Object buildActions () {' t, O9 [5 _. T
super.buildActions();2 k8 Q5 Z' P, Y0 z+ N
$ b' \) f% M) V' e
// Create the list of simulation actions. We put these in* S# c! n& I+ J: C2 x
// an action group, because we want these actions to be# `, z/ U. u. h' T' t. O9 m
// executed in a specific order, but these steps should
6 x/ y- O' r( l // take no (simulated) time. The M(foo) means "The message
+ ]: L! c. q3 O( { // called <foo>". You can send a message To a particular) }* ^! n' Q4 E/ o; W8 {, }
// object, or ForEach object in a collection.
: U$ g" c8 r6 ^+ T4 I ^" M ( j- I( t; P8 X) D2 h
// Note we update the heatspace in two phases: first run
8 f1 K; n, p8 \, g) { // diffusion, then run "updateWorld" to actually enact the
6 t7 ?! z# g, I' O8 H, I B // changes the heatbugs have made. The ordering here is
" W8 I: d" y. Q+ { V9 e) @# w // significant!6 ]/ B) W5 {' ^$ I0 g5 i3 N
: D& K8 ?. r4 C' Y& i
// Note also, that with the additional
/ Q# l. p! [6 l( B4 {, c // `randomizeHeatbugUpdateOrder' Boolean flag we can5 p/ O5 @# c: d8 A
// randomize the order in which the bugs actually run
) X" u _5 \' B- \ // their step rule. This has the effect of removing any- S: d) a- L- p
// systematic bias in the iteration throught the heatbug+ b! S- l; u/ o& F1 L" p
// list from timestep to timestep& ~& p! {+ Z% I3 r( L, D, ^6 Z9 f" {) U( q
# y% `4 T* _! X
// By default, all `createActionForEach' modelActions have" R5 Q9 p7 a! U4 J) k$ S
// a default order of `Sequential', which means that the
+ I) W0 B: }/ f2 q$ [2 s // order of iteration through the `heatbugList' will be- d7 I. I" @7 H8 K
// identical (assuming the list order is not changed9 X0 k, M6 ~- s9 Q
// indirectly by some other process).
" K1 e" W% _1 z" U) A' h8 k
% U7 }9 i: d+ S* N modelActions = new ActionGroupImpl (getZone ());, R* x3 _3 Z3 e5 g, D* K" Q% K8 {
% l0 r1 \2 ~3 V" G3 R try {& r+ q0 L$ ^2 M! D2 a
modelActions.createActionTo$message' h# n: W" y4 g, |( y( `6 T
(heat, new Selector (heat.getClass (), "stepRule", false));
6 z! c$ h9 C/ V, C/ k5 x! Y } catch (Exception e) {- F7 _5 K# x8 V8 g' Z8 u
System.err.println ("Exception stepRule: " + e.getMessage ());
4 J4 r- p" N7 q' X: }, g }$ f2 ]9 ~8 u$ ?- S" B4 e
" m: g( T' S" ? k/ g1 {
try {
; t- {1 \5 u$ ~$ t* a Heatbug proto = (Heatbug) heatbugList.get (0);% L0 k J8 |5 e2 t4 d
Selector sel = 1 U- u: e. m2 i. ^
new Selector (proto.getClass (), "heatbugStep", false);
! f5 v* O0 H4 k- V2 N% i actionForEach =, e# K `% U& h' \
modelActions.createFActionForEachHomogeneous$call
7 a# I; f( b& ]# T6 d (heatbugList,1 t+ ~, T$ F2 x& {
new FCallImpl (this, proto, sel,0 \- X+ q' ` `8 v+ ~
new FArgumentsImpl (this, sel)));3 X# t1 H. l9 V" B* H, |3 }
} catch (Exception e) {3 O4 x; y; t3 m( q& O
e.printStackTrace (System.err);
, d3 [8 _3 e1 w0 I& \8 _. ^7 H }
; m! z) m7 }" _$ X& j - l4 T- w( I: Y2 v* c
syncUpdateOrder ();
6 A/ x9 d" V, M3 W" I' O
" \' W1 {' l+ F. {1 _" P try {
- A; J; A, l' o2 j) z- v modelActions.createActionTo$message
* Q: ]- B+ P1 E7 Z8 a1 j+ H (heat, new Selector (heat.getClass (), "updateLattice", false));
* s( h7 p, o3 M& r: K9 l Q } catch (Exception e) {2 F" z' n( B! S" K; J7 v& n. R% |# T# p
System.err.println("Exception updateLattice: " + e.getMessage ());
9 ~. j6 n$ L; i M5 v* c }
8 z3 R. \/ L* G/ g n
5 K9 L8 f$ Q. t; T // Then we create a schedule that executes the( Z: R, P' x+ P' E# f: ?+ L; b' i
// modelActions. modelActions is an ActionGroup, by itself it% T, h$ T! h; T& `/ f
// has no notion of time. In order to have it executed in& ~6 W; B! f! v3 g: L& a
// time, we create a Schedule that says to use the
8 ~+ Q' ~; E, J M. k // modelActions ActionGroup at particular times. This) h( Z# L9 b2 d' C6 K5 G p9 q
// schedule has a repeat interval of 1, it will loop every
, m7 z `/ k' U // time step. The action is executed at time 0 relative to
/ M7 c$ i- P' r# k // the beginning of the loop.- V8 Q& J' t9 K( n$ {" q' C- o( U
9 R, e6 B5 a0 o$ B
// This is a simple schedule, with only one action that is
3 C h) q4 V4 }6 g, ?4 o2 K' } // just repeated every time. See jmousetrap for more6 q. U6 `2 g/ s- v; l
// complicated schedules.. g8 l/ F. R; B
8 Z1 s8 y6 A9 Y- k. I& s
modelSchedule = new ScheduleImpl (getZone (), 1);
* v" r8 \; s3 l1 W; Q! U8 K& O modelSchedule.at$createAction (0, modelActions);
* I% r' s# ^1 N) o/ O ' S" [0 o" ], @1 E
return this;, X( p. d( ~& r" K4 \
} |