HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, b$ j( u" P/ _( d4 E
! ]) r0 ]4 y4 M+ N+ X
public Object buildActions () {
: ?% u Y! n9 s |2 g4 Q super.buildActions();1 }9 X' @7 F1 y* V& D! C
; T/ a( L$ |2 ~
// Create the list of simulation actions. We put these in ?0 j% w3 R& r! x
// an action group, because we want these actions to be, u* p1 m, K* U
// executed in a specific order, but these steps should
' ?9 x' Q# F: ? J // take no (simulated) time. The M(foo) means "The message
5 `5 C' Z+ g) n! _& Y! |- x D7 W6 w // called <foo>". You can send a message To a particular
5 l7 g) [, p4 {9 Z2 o) }# y. p* r // object, or ForEach object in a collection.
( k( v' z: g1 p4 M5 h . T. F- M' u/ b9 n
// Note we update the heatspace in two phases: first run# R7 w7 t* D' y, E) d/ A: Y0 p
// diffusion, then run "updateWorld" to actually enact the
2 V6 I4 t: ^: M y // changes the heatbugs have made. The ordering here is8 j3 w. M' a7 Q5 l3 {
// significant!
D# L9 D6 X( U! V) Q- Y! a
, o; W4 S; q" d: ^# r. ?5 v# H // Note also, that with the additional
" D' S8 b) w! C8 L& h: H, Y // `randomizeHeatbugUpdateOrder' Boolean flag we can
, j; h; I- F! u% m // randomize the order in which the bugs actually run
R# N: g4 ~ {5 K* @8 t // their step rule. This has the effect of removing any8 t/ ~/ }3 n6 j; q$ {
// systematic bias in the iteration throught the heatbug5 }' i' |- s- a! f) l! c1 {+ m+ V
// list from timestep to timestep
6 ~# t2 ?: F* ~8 W# @) R3 r : ?& G4 i' q, b- z
// By default, all `createActionForEach' modelActions have
y, f: S6 r# \7 { `: V/ Q // a default order of `Sequential', which means that the
# X2 X! H0 H) d# X0 ` // order of iteration through the `heatbugList' will be& |! [1 Q3 L0 I/ l. ^- Z& Z
// identical (assuming the list order is not changed
0 i. W# l- I1 X. g1 [( Q' B+ f. M9 b // indirectly by some other process).% @9 o0 s! C7 f( V6 R
) D: F( T# l, V6 y! j modelActions = new ActionGroupImpl (getZone ());7 }$ T& V1 t: F: r
2 H8 J' {* ~, C- K9 f* t* l: A& }
try {
6 G% D- ~' D8 F% ^; W+ }' D modelActions.createActionTo$message
0 M; }7 K7 s% a (heat, new Selector (heat.getClass (), "stepRule", false));
5 g: I% S& P" } } catch (Exception e) {
1 N6 E9 K) u/ f7 W/ d System.err.println ("Exception stepRule: " + e.getMessage ());! w0 P y8 a1 S0 T
}
3 J# ?7 S$ Y/ m: L8 y$ ?
- n2 `2 P6 J2 a9 N) \+ H9 m try {8 R$ m1 J a+ i' I
Heatbug proto = (Heatbug) heatbugList.get (0);
: k* S! F4 J* |* j; i) l8 f) D! r Selector sel = , E4 {( I( }+ m* C% m) R
new Selector (proto.getClass (), "heatbugStep", false);
! [* k x( V# I/ z0 Q: [, p& G actionForEach =
5 ~1 S" _9 M& s7 v# N$ v: E; s: ~ modelActions.createFActionForEachHomogeneous$call
/ I7 U3 L* ?. c; m6 T$ _( x (heatbugList,
0 {3 m& o. Q8 {1 X4 u5 V new FCallImpl (this, proto, sel,
5 D; B& ?+ s. R9 Y- R new FArgumentsImpl (this, sel)));) ^8 r6 R# J5 B4 K, T! Q
} catch (Exception e) { y+ y# C. [4 h' Y5 Y. y
e.printStackTrace (System.err);* D4 u7 _$ X" T, T/ G
}
% n: c* V) M/ b& d+ g8 D& { 2 z# U2 s. K8 q# D0 |4 C
syncUpdateOrder ();% ~! V$ \1 ?0 @1 F' k
" o" Q$ ]9 T _0 }
try {/ F; r% I# Z; [+ ?
modelActions.createActionTo$message
+ o% m* n. M, \! k) x (heat, new Selector (heat.getClass (), "updateLattice", false));
$ T( \& m: w* W } catch (Exception e) {+ Z/ G7 ~* v$ t# T# ]3 T
System.err.println("Exception updateLattice: " + e.getMessage ());
) T# q" ]& C- i4 j) |2 q2 m! S4 n }
+ I1 m5 _* H7 B3 o2 t+ Y* c, m Y% Z
* ^4 H: ~( S2 w+ F // Then we create a schedule that executes the
& `8 Z1 N0 _! t# l // modelActions. modelActions is an ActionGroup, by itself it
; c) A* l) Q8 `4 K // has no notion of time. In order to have it executed in1 B! H! p$ [ V- R q7 k3 r( y
// time, we create a Schedule that says to use the
2 X( C2 H' B+ ^, z // modelActions ActionGroup at particular times. This/ q0 p) e/ F5 r3 y j. e3 h1 f
// schedule has a repeat interval of 1, it will loop every. t, B: A* p+ h5 e. c
// time step. The action is executed at time 0 relative to
- P# V+ F8 n! y8 S. a8 U5 t // the beginning of the loop.
* F. e' K3 s" f8 A5 Q% }0 |5 Q5 m/ W& ]( q' R) {
// This is a simple schedule, with only one action that is+ V3 ?% V, ?) {! I; z1 ]
// just repeated every time. See jmousetrap for more
1 s% r. h- k5 B% S8 G/ {! e B7 w // complicated schedules.% Z, A- D3 S: k& m4 f
$ ^1 h2 m5 q4 i
modelSchedule = new ScheduleImpl (getZone (), 1);
# ~* f+ Y+ B+ G% e! ^0 F modelSchedule.at$createAction (0, modelActions);) r4 O( |) Y% M0 N O! h: I$ Y6 ^
( v3 s/ d6 Y. x- W: a n1 t& W* `
return this;: F& T+ F1 _" ?% g
} |