HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) m4 g. |* Q/ T3 K0 |% K* ?( \9 g5 k9 w! O" Q4 B! O* S1 B
public Object buildActions () {+ w7 h! I* N# j6 [
super.buildActions();# O% M1 j ~$ D2 v8 `& B
2 |9 e2 f( w2 c // Create the list of simulation actions. We put these in4 t2 {' \1 C2 L
// an action group, because we want these actions to be& g9 S% e/ f# K
// executed in a specific order, but these steps should% B4 E8 A8 z4 P% Z0 R: ~
// take no (simulated) time. The M(foo) means "The message- K. p2 }, t: j$ P( m9 I
// called <foo>". You can send a message To a particular
* J3 R! T3 X5 C0 S, h // object, or ForEach object in a collection.
2 R" Y' `/ G: E! i! L$ B6 t; G, j
' G2 I' u3 X7 Z9 e+ S // Note we update the heatspace in two phases: first run
2 L. F. [& f5 N: \ // diffusion, then run "updateWorld" to actually enact the
/ l4 G# B- g6 M7 T // changes the heatbugs have made. The ordering here is
& q# W. t# ^) B& }. J2 M3 A0 ] // significant!
" V; K- m. h6 ~8 r b( M! U % D2 ?- B2 f4 S, f
// Note also, that with the additional
% F/ b$ \& Q# S* |( p; b; j // `randomizeHeatbugUpdateOrder' Boolean flag we can
0 ~, p4 ]. M7 \; m // randomize the order in which the bugs actually run
4 Z8 F* [9 ]6 a8 R, x( e // their step rule. This has the effect of removing any. _$ \6 `9 K8 J8 x7 b
// systematic bias in the iteration throught the heatbug
7 o" |5 g" O) t" R // list from timestep to timestep
7 e C2 q% x$ M# `) [6 S , x. P9 j8 J) g+ @: }
// By default, all `createActionForEach' modelActions have# J: Z) K6 ?% G- x* E4 |
// a default order of `Sequential', which means that the
5 j4 i8 O% M, k$ M' D0 r" f9 X" K // order of iteration through the `heatbugList' will be9 h. C B8 i9 U4 `
// identical (assuming the list order is not changed! g# J2 q/ q3 E4 i
// indirectly by some other process).% w, R$ V+ f) `! L
- p" W( y+ X. ?! l) [: H, M9 l
modelActions = new ActionGroupImpl (getZone ());; R) d4 c' \% b8 @( A& T* s
# c( x) g3 q% ?$ H) Q7 L try {2 [5 q1 a* I) ?8 ?1 B5 C Z
modelActions.createActionTo$message+ `9 J X2 {/ f! @( ?) J
(heat, new Selector (heat.getClass (), "stepRule", false));) D. k0 |& A2 N
} catch (Exception e) {. s D; L7 f9 ]4 Y, i" d
System.err.println ("Exception stepRule: " + e.getMessage ());
+ w% ~3 @/ x! j- L; _ }
4 d5 o- b' _+ \
/ J& w) c& x4 f' n* h try {
+ r! x6 Y2 d; A) Q4 { Heatbug proto = (Heatbug) heatbugList.get (0);
0 d$ x. c# j+ o, e Selector sel = : @- n* b3 c3 m0 P! d
new Selector (proto.getClass (), "heatbugStep", false);
1 }7 c, t2 n1 S6 X$ X actionForEach =# n; t9 ^) p4 T' S! z3 J; V ~
modelActions.createFActionForEachHomogeneous$call
+ h5 n4 I1 u! X1 k: E- e: n4 g (heatbugList,/ N( f: c! h+ x0 _3 \0 U" j
new FCallImpl (this, proto, sel,
% h: M7 Q9 ` i* a. s7 v% r, u new FArgumentsImpl (this, sel)));7 F8 ^1 r3 ]0 y% g& f3 I8 ]5 l
} catch (Exception e) {
9 V* ]+ N: m7 j I e.printStackTrace (System.err);+ ?" o7 [3 l8 w/ Q- K9 h
}; T4 G6 t; `+ u/ b) B
+ S( }! N- f$ X% S5 { syncUpdateOrder ();
6 w; X# k9 D9 [# t3 A9 U2 n- t! u0 \+ H7 r5 k) w: K4 H
try {
' Y( i7 F- r: Q2 q$ f0 ] modelActions.createActionTo$message / k3 H ?8 S* T# E5 [! I7 Q
(heat, new Selector (heat.getClass (), "updateLattice", false));, I- A2 x9 P* f
} catch (Exception e) {& ?0 N4 S, Y( I# y- q3 `. V( c+ o
System.err.println("Exception updateLattice: " + e.getMessage ());! S% H8 j6 i) w1 b
}- ]! m9 [: E% g2 M9 K4 ?
3 q/ v6 v8 c, C) N! b1 H // Then we create a schedule that executes the
" F4 Y7 q; e/ ~" n# E6 D // modelActions. modelActions is an ActionGroup, by itself it( @6 L* M5 X; e2 C0 P
// has no notion of time. In order to have it executed in
# _! L! ?. a E% W( p // time, we create a Schedule that says to use the1 R5 Z) I( Q% V! g4 E4 O: V
// modelActions ActionGroup at particular times. This: a2 S) T0 t% T( o: j2 M& u
// schedule has a repeat interval of 1, it will loop every& P7 a9 P8 n @, a) S5 u
// time step. The action is executed at time 0 relative to, z. ?3 G; v+ P; M
// the beginning of the loop.
2 f. _$ c8 ]; i/ c. ~
4 \' @# d! ~" e7 } // This is a simple schedule, with only one action that is) e. v6 Y; u1 Y9 @" J! J: I, ]7 ^
// just repeated every time. See jmousetrap for more
4 B$ `6 p6 H4 I, c0 G // complicated schedules.4 Z3 F0 R8 Q; U% D% v0 w6 I" P# e
/ ?! Y1 U- X: R+ Q3 o& o& `; Q modelSchedule = new ScheduleImpl (getZone (), 1);) p) H; B/ R$ I9 x- U
modelSchedule.at$createAction (0, modelActions);
6 S$ C g; L, s& D( o 1 X8 ^# D/ C) K; P" V+ N
return this;
" Z. n9 [) n0 j7 u+ ] } |