HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% S% Z6 |& _5 M% ~" C) C
, Y a. g: K% b" x1 D public Object buildActions () {
$ B6 V8 n. Q5 _0 W. \ super.buildActions();
! U# n3 ]4 ^* r, M0 O
' ]9 W2 H* h4 R9 s9 E7 k6 X // Create the list of simulation actions. We put these in
! [2 V) @- z7 V- V9 @( B" U& s3 c, }: |, b // an action group, because we want these actions to be; [/ @3 |$ {0 A$ _" ~/ r$ P W& h$ `
// executed in a specific order, but these steps should2 o; E" Z6 X! J- ~
// take no (simulated) time. The M(foo) means "The message0 I5 {7 R% x5 F3 Q
// called <foo>". You can send a message To a particular
, {" T- e) K9 u8 ]7 f" [ D n // object, or ForEach object in a collection.
( ~) R7 G3 T, B- s. x# S) y, Y 8 C5 n0 }5 Z& Q$ {
// Note we update the heatspace in two phases: first run3 }" v+ J5 N( f' v# A
// diffusion, then run "updateWorld" to actually enact the8 e4 S( p( p; E$ i* |( X r% i
// changes the heatbugs have made. The ordering here is
- y' r* i& u/ S% b& `1 f // significant!1 d7 [+ o3 K4 E1 m' a6 a# F6 z
2 E" D2 I# \; M& a! { // Note also, that with the additional
7 Z! t' T3 x0 {; k% R% p // `randomizeHeatbugUpdateOrder' Boolean flag we can
: c0 B. V3 d7 ~) f: p // randomize the order in which the bugs actually run
6 a7 Y/ T: w. Y // their step rule. This has the effect of removing any6 ?! U: G5 Q- y! |9 z
// systematic bias in the iteration throught the heatbug: H: r- ]1 n# u% [: l! d& o
// list from timestep to timestep# \3 v) W( q* `# I
0 ?$ k! Q6 G& b Z, \2 X' c, l' Y8 Y // By default, all `createActionForEach' modelActions have
9 S5 M) W+ H2 w A) u // a default order of `Sequential', which means that the' J" @0 k0 ~' b1 U: n
// order of iteration through the `heatbugList' will be" ^3 p8 e+ B; r8 ?6 R9 i* |
// identical (assuming the list order is not changed% U3 x* `* K0 \3 r; |" p: t
// indirectly by some other process).
7 d$ m9 Y) `2 G3 D+ {- X/ h+ y : Z0 L% G6 z+ V* Q& b1 Q
modelActions = new ActionGroupImpl (getZone ());: ?8 ~+ Q) K0 h0 w* B" K
8 F+ Y* z0 a- n$ P# g try {
2 T. S5 V% K1 T W. @4 o, G! M: } modelActions.createActionTo$message1 ~! T7 N1 \1 n9 J6 O/ }1 [
(heat, new Selector (heat.getClass (), "stepRule", false));
K, e- s: t% n8 T6 b P } catch (Exception e) {
: X7 G) R0 F5 S& D# A1 b- q0 ^+ c System.err.println ("Exception stepRule: " + e.getMessage ());
" n# p- t4 a# }1 N9 @. X }
$ j6 g: L+ g4 t2 f& |
, U1 ~( ?$ u1 v: Q9 u% W& H) Z try {7 m: v& y% w) ^
Heatbug proto = (Heatbug) heatbugList.get (0);) A8 r% j2 ~1 h$ q# }/ k q
Selector sel =
% O m! D7 N9 M! I: A# e n5 t8 _ new Selector (proto.getClass (), "heatbugStep", false);3 x$ V- x* E7 u5 y& v
actionForEach =
: G" k+ M: X# H7 r: W( x modelActions.createFActionForEachHomogeneous$call7 O' L# k0 @4 i1 L2 k
(heatbugList,* j6 B5 { N% i- n5 T
new FCallImpl (this, proto, sel,7 |" r# }7 o$ F
new FArgumentsImpl (this, sel)));
x. b: ]7 I, ^' B$ J( k! Z } catch (Exception e) {* c6 a$ h5 z$ W# w0 \
e.printStackTrace (System.err);
s# |: b u7 p, R }$ v+ D9 j5 `# {2 \# [2 N! |$ D
1 r" k: P R, M2 d* K' Z syncUpdateOrder ();
% r$ \1 S- Q4 {+ _. K/ Q# H. W) e h z6 u5 p
try {
7 U+ @; y/ r7 v modelActions.createActionTo$message % n. C/ b$ _+ C, R" c4 i, g
(heat, new Selector (heat.getClass (), "updateLattice", false));
" v3 Y2 G X1 c) R7 D6 s } catch (Exception e) {
( l9 L$ H" X# H7 G1 o2 O System.err.println("Exception updateLattice: " + e.getMessage ());
0 P, Z( k# B6 t" k }" f: i3 t+ |2 \$ N# D* H5 Z
' k4 d; p& Y6 w: m7 _0 \& E( }
// Then we create a schedule that executes the
( E j1 I4 W" H. G6 j3 i4 C // modelActions. modelActions is an ActionGroup, by itself it
+ o ?' y' w1 L // has no notion of time. In order to have it executed in
* P- l* F" N) X: B+ [9 Y // time, we create a Schedule that says to use the
& e# ]: ~" { z, K // modelActions ActionGroup at particular times. This, H" I; z$ a* x, n3 n
// schedule has a repeat interval of 1, it will loop every
+ o" X" H, x' x6 ~9 a: U/ M9 ? // time step. The action is executed at time 0 relative to7 r$ m1 M% d: f" g; q0 ?: O
// the beginning of the loop.* s# i% z* d: Z
0 B& t- N9 {( X5 M! o! D // This is a simple schedule, with only one action that is
' @0 _2 d/ t% U4 `, G2 N3 O; b // just repeated every time. See jmousetrap for more
0 k) v' `6 E9 w1 s% c // complicated schedules.9 ^) ~) o$ e% O# f+ |7 F
7 Y( r6 o- J- y1 C* Z9 L1 J' j modelSchedule = new ScheduleImpl (getZone (), 1);; S1 X+ k3 N, Q2 s3 R' m+ V" d
modelSchedule.at$createAction (0, modelActions);
y, g o8 ?5 s
" ?* F# @+ }+ M$ _; ] return this;
" r# K' E1 ~5 V! j/ D+ [ } |