HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 m; j7 S6 t b9 _* w2 ]; R
$ [! H/ Y; i: a% ~* M) U
public Object buildActions () {
; A0 _! `9 X5 D super.buildActions();
3 Z/ D9 Z6 {9 h ~* | $ C, K: j& a8 [4 Z3 o- q( U4 m
// Create the list of simulation actions. We put these in
1 J) D, R) G6 r& a1 N // an action group, because we want these actions to be
% T4 ~! a; }) y$ g! X# l // executed in a specific order, but these steps should8 {) V# J& }' p
// take no (simulated) time. The M(foo) means "The message
% f* _, E* w, | i& z // called <foo>". You can send a message To a particular7 }0 ?, O- |0 M+ e
// object, or ForEach object in a collection., w' G, O' ^, a1 b8 l
( u: s4 |# [& W" y: }# N // Note we update the heatspace in two phases: first run
. E8 @6 C! e- b; k/ B% J1 R // diffusion, then run "updateWorld" to actually enact the
4 e: S4 R; c! f9 L! D // changes the heatbugs have made. The ordering here is
6 `& ?: y2 k2 c# K2 M& V* I // significant!
/ v+ S7 w7 M _# S 8 z) _' j4 c$ A( O/ c
// Note also, that with the additional
7 h% t0 n8 m; N0 b4 ~. J // `randomizeHeatbugUpdateOrder' Boolean flag we can; A$ r/ D/ b+ z' H! i& S0 O
// randomize the order in which the bugs actually run
* ^ d- {! {% d8 @ // their step rule. This has the effect of removing any
+ @5 s5 r; z/ s+ u6 ]. x0 b) z6 e // systematic bias in the iteration throught the heatbug
( X- ~ a* E& b/ N# J" O) @! d // list from timestep to timestep
9 c$ E, i- x% m0 a4 X
3 e* `* H, T. [5 n; o3 l/ L // By default, all `createActionForEach' modelActions have( }' A0 L/ G, R, t0 A: r
// a default order of `Sequential', which means that the2 \+ }# N; N8 Z/ f2 ^
// order of iteration through the `heatbugList' will be& E. o0 i* [+ b2 ]! V3 \4 z
// identical (assuming the list order is not changed
# F5 O2 D, P* h+ ?( N // indirectly by some other process).$ D8 L& j; ]+ V b- K) R Y$ z e2 ~
4 x. r) R9 z3 N modelActions = new ActionGroupImpl (getZone ());* Y0 Z1 e" F& Y b4 N& N% N4 H
. x9 Y. @ q* D' z5 G try {# @) v* P( W7 @$ G; i
modelActions.createActionTo$message
! E N0 { u3 s. B* T" c0 h" R (heat, new Selector (heat.getClass (), "stepRule", false));
9 U' M, j! j/ r* [8 L$ | } catch (Exception e) {/ s% H. `, i) c. B0 L" @* ~
System.err.println ("Exception stepRule: " + e.getMessage ());
* ~( u, }' S0 X) L6 j }3 ~( ^# _/ v! x; e" P( y! c
" D( F% b& F/ P$ B, g* J" I$ m try {3 s' t) v5 S& a- @7 y
Heatbug proto = (Heatbug) heatbugList.get (0);
+ |" i! n5 c, h5 N$ m" ^4 V& T+ ` Selector sel =
! g5 {9 J7 [) W, H$ q9 u% d6 k new Selector (proto.getClass (), "heatbugStep", false);6 U( k. S& @: [' N
actionForEach =
1 i' y2 g9 B0 Y; _% M modelActions.createFActionForEachHomogeneous$call' k K4 J8 C/ e/ q* a& j' N
(heatbugList,
' W/ f2 Z( u/ Z* O: j0 I. e+ u new FCallImpl (this, proto, sel,6 i5 h: B) q8 w, C* e
new FArgumentsImpl (this, sel)));3 F$ Y- e2 l! S1 a9 v D) [
} catch (Exception e) {; h# H' l7 \% U9 l7 T6 k
e.printStackTrace (System.err);
5 F1 P4 S' }4 B3 N: N3 P }, L5 n7 B( m& B" [
; q3 U5 ]9 D* o+ d syncUpdateOrder ();4 J& I: u: i+ o8 j* z
; X: \ d- C9 _% n/ Z& Q8 T try {2 Y% B2 A0 E( j. O* o$ k
modelActions.createActionTo$message
5 E: N) n5 Z/ Y2 e/ x (heat, new Selector (heat.getClass (), "updateLattice", false));
4 B9 g/ y$ u. a" [8 B4 ~ } catch (Exception e) {& A' H2 K! l# x, u
System.err.println("Exception updateLattice: " + e.getMessage ());+ d9 C: E; W& X( Q8 W# ]! l
}
' ~+ O8 x }" _5 P: K9 C6 L $ o. m/ E1 Z" v+ g% E) h! f0 Q
// Then we create a schedule that executes the9 Y! L/ f/ _* S# P: E4 T( H, O
// modelActions. modelActions is an ActionGroup, by itself it
/ r/ S( o9 W7 {8 k, Z+ [4 [6 l // has no notion of time. In order to have it executed in6 X8 U2 R& A# |' o+ W+ ~
// time, we create a Schedule that says to use the
* u; C0 c$ E- ~" D5 a0 ? // modelActions ActionGroup at particular times. This
; l# O( q5 j' w3 B3 |* J // schedule has a repeat interval of 1, it will loop every
% |) ]- N# W' x // time step. The action is executed at time 0 relative to: i; Z# Z' [! w( z! W
// the beginning of the loop.0 r9 ^* l3 V7 _5 P. N7 ~0 t
' Z, w8 z. k4 J" L7 y // This is a simple schedule, with only one action that is2 { ?8 }8 u8 ~0 u* G
// just repeated every time. See jmousetrap for more! N0 i! s) \ `) X4 Q }
// complicated schedules. T& O) k% U1 `9 [- }
, {( j7 @3 P0 n/ L
modelSchedule = new ScheduleImpl (getZone (), 1);! m' c) @9 _" y& ]5 s1 I: r$ f. q$ R2 v) ^
modelSchedule.at$createAction (0, modelActions);1 `& C1 ~ h* e5 Q9 ?5 }& ^
& X0 z7 k( q5 V- E
return this; K. F; z; ?0 l6 a1 v% `0 Z! C. R
} |