HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ R+ } [4 D) m: V8 W( m- g4 a& q" m; E+ t3 k/ Y5 G; \
public Object buildActions () {
; ^# m; g4 R9 V8 I) v8 d. S super.buildActions();
8 P: }1 X d" N. w6 P+ a 1 E4 }' u; E8 w/ t0 W
// Create the list of simulation actions. We put these in! e$ t' R& z2 ^3 B" D
// an action group, because we want these actions to be" U% G# H5 Y' l5 T. O7 C/ c [5 W
// executed in a specific order, but these steps should
3 q% h3 L# W& w( C9 o // take no (simulated) time. The M(foo) means "The message
7 [/ K$ Y$ q* x. C$ p( o @8 M // called <foo>". You can send a message To a particular* l* N5 a7 d5 |- u
// object, or ForEach object in a collection.
5 `9 X7 o2 Z% }7 M
& R$ h, I0 n1 x+ |) \ // Note we update the heatspace in two phases: first run
- e% n/ d- ^3 P& U // diffusion, then run "updateWorld" to actually enact the
7 X5 x7 X7 p8 s0 s/ ] // changes the heatbugs have made. The ordering here is* x. p8 a0 y5 \& X1 l
// significant!
. {5 x$ N( r i4 F% v6 R8 Z; B
3 ~7 {$ b0 o5 F' I( v // Note also, that with the additional
4 |, ]9 \+ D/ G) R( S# @ // `randomizeHeatbugUpdateOrder' Boolean flag we can1 U5 k1 [) q. M7 E' y
// randomize the order in which the bugs actually run' e! w7 H+ }) F0 C/ X: D# b
// their step rule. This has the effect of removing any% G$ Z6 N% z" I, p, [
// systematic bias in the iteration throught the heatbug
0 v) x) s- p+ F! |2 e: S // list from timestep to timestep/ i/ S" q0 ?8 ~8 ^( T; @5 X, Q' P
6 V6 w: d$ y- ~3 N/ b( z3 U
// By default, all `createActionForEach' modelActions have
- g8 M- Z& y; L8 x' Y1 W0 j2 l // a default order of `Sequential', which means that the1 _. P" ?2 `2 z& h! V! o# K! P6 l
// order of iteration through the `heatbugList' will be D/ _* d3 n6 w9 |$ L+ B2 a
// identical (assuming the list order is not changed
6 k% O7 a8 R" N, o I* [4 b. b% @ // indirectly by some other process).
" s2 t$ A2 @7 r8 l8 A% c
1 t) U: L) i' d1 | modelActions = new ActionGroupImpl (getZone ());
: ?: B+ {& g1 O: e: c4 I# f1 E# f0 x/ b
try {" [+ B& c/ y- I) C s v
modelActions.createActionTo$message
6 A9 ]: N' j1 t; W8 g4 ?* n. Y (heat, new Selector (heat.getClass (), "stepRule", false));. }7 s: {6 d7 S) y
} catch (Exception e) {% D9 ~5 l+ t1 _' p+ Q
System.err.println ("Exception stepRule: " + e.getMessage ());. _. D' A% t) ?9 q7 x1 u7 r2 w
}
+ x, l1 p* I# b9 c0 _- C0 ^/ |. T" B7 C( T: i4 c. }
try {6 p! Y& z; O* Y; U7 [% [ y0 J
Heatbug proto = (Heatbug) heatbugList.get (0);
) l; N- u- M3 w6 [, x Selector sel = * K1 r9 C* v6 n: u
new Selector (proto.getClass (), "heatbugStep", false);' m9 b, c0 q3 {, j
actionForEach =) W/ x0 e1 H7 }. r
modelActions.createFActionForEachHomogeneous$call& [5 ?6 g6 {9 u4 u% ~; X; i
(heatbugList,7 m1 T$ o6 l' n# j- x
new FCallImpl (this, proto, sel,. ^1 u# W' j8 R. q1 c
new FArgumentsImpl (this, sel)));- L8 a' I" W- Y
} catch (Exception e) {5 K4 J4 h* W# G) Z; z: f
e.printStackTrace (System.err);1 P; A0 H1 p; r, x4 E' S
}
/ I7 o, z% h9 ^. L
! I4 ~% s6 N/ K0 g9 l1 o syncUpdateOrder ();1 B: E+ Z4 @* d v+ L2 V
, o& p" ?, d+ j3 d7 q: P" R
try {
$ U1 ~2 k: W7 O modelActions.createActionTo$message
3 S$ Q4 Y6 w/ f! W- `( d: y$ f5 D) Z (heat, new Selector (heat.getClass (), "updateLattice", false));6 ]* R2 T9 R0 K; \( d& ~
} catch (Exception e) {7 p0 O3 t3 w+ I
System.err.println("Exception updateLattice: " + e.getMessage ());* u L7 n; J+ b# ]* U3 C
}
; R& E8 _9 {" j0 g
; [/ Q- K$ J# M. |7 t9 [, g! x // Then we create a schedule that executes the
) F& }( h; f- B: P4 a) E7 Q // modelActions. modelActions is an ActionGroup, by itself it
9 _' \* |+ N7 B6 M' B // has no notion of time. In order to have it executed in
F6 {8 u: }# h J: j // time, we create a Schedule that says to use the
* e) v2 o: |: x. L // modelActions ActionGroup at particular times. This
( [' k# H, i% z) N1 f // schedule has a repeat interval of 1, it will loop every P7 t4 C! p* m5 r- P6 y2 `2 M/ u
// time step. The action is executed at time 0 relative to% _6 B9 R9 O8 Y/ x5 m' J' t) y% }; Z
// the beginning of the loop.
: W# C: z* B9 G& y. }8 r4 `
' b& d: H" Y/ p4 f // This is a simple schedule, with only one action that is
; @2 u, P# j+ O3 N: F u0 N // just repeated every time. See jmousetrap for more
6 w9 E5 w2 ?) V" S6 }$ [8 ] // complicated schedules.( Q) G/ ~/ e2 x8 Y* o, J7 Y) b
; w) v: F. z$ v: ?" q modelSchedule = new ScheduleImpl (getZone (), 1);2 z) T3 y6 f& R U- I
modelSchedule.at$createAction (0, modelActions);$ ~1 ?, P0 H. ^4 c0 f
. M G$ e9 Y. g6 A2 H2 s
return this;1 U! Z8 k3 g6 t& S! c
} |