HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 z( d5 A" y- ^" k( R6 g- L
' ^& f2 t F% E
public Object buildActions () {
2 m1 _7 G) A' w super.buildActions();
" |( g; J- w& y+ K% u
4 h6 e# u& S, ~9 M3 L3 _ // Create the list of simulation actions. We put these in
2 q/ W W b# t9 i1 w; d1 } // an action group, because we want these actions to be
+ Q" Q* w* s% x! Y // executed in a specific order, but these steps should
1 ~9 g* m ~6 o" B5 F' @6 d9 A+ c // take no (simulated) time. The M(foo) means "The message! l0 x9 ~1 q+ p% C" l
// called <foo>". You can send a message To a particular
! e" p- B/ E, E0 l // object, or ForEach object in a collection.) P/ C4 S) s- L3 V, N1 X+ e1 ?
' U- C* G- Z9 z% s4 `8 h
// Note we update the heatspace in two phases: first run
; {% ?) u, y3 } // diffusion, then run "updateWorld" to actually enact the
7 Y3 Y4 a: b$ J4 V3 E // changes the heatbugs have made. The ordering here is: v/ G1 j0 ]& u" b
// significant!$ M# q' e( ]8 T2 \, i5 Y4 ]- V: Y
$ c H, s. o4 x2 i, k // Note also, that with the additional
& X. q8 {; \% }6 x) W; n // `randomizeHeatbugUpdateOrder' Boolean flag we can K! d3 ]8 T* V9 o4 u
// randomize the order in which the bugs actually run) X; R' Q1 s: `& S0 ^2 h' s& X0 R) W
// their step rule. This has the effect of removing any
t* d$ ?2 d6 K% @- F U( G // systematic bias in the iteration throught the heatbug
6 o- ]: a; w" W# O3 F2 l // list from timestep to timestep9 a$ b, |. T4 j6 L- l
; Z$ T+ c7 ?7 @ ^6 q$ I( B // By default, all `createActionForEach' modelActions have9 M- |2 Y, o' f9 _+ ?: z( Q) j
// a default order of `Sequential', which means that the
0 Q3 Z$ r) ?7 Q1 w // order of iteration through the `heatbugList' will be
! l4 [1 t" Z7 e( E // identical (assuming the list order is not changed
f, b! x+ B, u# ~' p // indirectly by some other process)., I. {+ i m0 J- M* U. k
; o/ l3 c! ` g0 y. u3 X modelActions = new ActionGroupImpl (getZone ());- }& r, j" h+ D% w% [1 y$ \
2 Q/ y% [& V1 A0 Y* k try {- F* w- U/ _2 ^& M- S$ R3 g- o
modelActions.createActionTo$message+ o1 h) D, T! u$ `& x
(heat, new Selector (heat.getClass (), "stepRule", false));
5 J5 E. b3 ~; ~& o/ ~" \% t$ f } catch (Exception e) {
; i- i3 O& x9 J j- \0 @ System.err.println ("Exception stepRule: " + e.getMessage ());4 y; [$ p+ O0 N4 a/ z
}$ L2 P* ?3 f6 r+ y0 b* S
# w. T' |- {. m try {
0 _2 A& w5 t2 Y" ^! @3 W Heatbug proto = (Heatbug) heatbugList.get (0);6 X7 j) E% ?" H
Selector sel =
* N* J) K; q: e4 v/ C new Selector (proto.getClass (), "heatbugStep", false);
9 B1 ]. b, i) a" _. I w actionForEach = m- J6 b$ ~ u" E0 x; n
modelActions.createFActionForEachHomogeneous$call' D+ E0 X# o+ Y. C. @
(heatbugList,
7 I" Z7 a# u4 D' V8 g new FCallImpl (this, proto, sel,% Z* v: z; z2 Y: s2 d! c
new FArgumentsImpl (this, sel)));8 u T/ s7 u0 d3 j7 L* H6 {
} catch (Exception e) {
3 R+ I% p1 ^3 `8 d& ]" E8 y e.printStackTrace (System.err);7 O/ _) [$ _4 ?; K/ Q- W( m" v6 V+ B
} {: Q) W" I3 X9 M3 @8 G2 \) T
! t& m: r/ T' r; N
syncUpdateOrder ();
3 {$ V; `3 g' n* K
7 t8 {- {9 |- _4 m( R$ n1 Q try {" \+ Q0 }* ]# e0 I1 z# f/ p! g
modelActions.createActionTo$message
& L+ Q" r/ F" H! Z (heat, new Selector (heat.getClass (), "updateLattice", false));
% U: w: |0 P5 F7 Q* m- r0 x# f } catch (Exception e) {
2 [6 ^4 H1 V) u6 h# ]3 {+ a; | System.err.println("Exception updateLattice: " + e.getMessage ());9 r5 w- R/ k! `
}7 J& P4 ?& J! S! T7 a
6 s; R4 f1 u! o0 D; m4 Q5 h1 h // Then we create a schedule that executes the
' @8 i3 \# z* {% F/ o& p$ B" U // modelActions. modelActions is an ActionGroup, by itself it8 v9 H& B8 L+ a2 @
// has no notion of time. In order to have it executed in
0 A+ a# _& a' D0 F, Q // time, we create a Schedule that says to use the
. n0 ?! y8 R5 i5 V6 |9 @ // modelActions ActionGroup at particular times. This
. y7 K9 x3 j- D- s/ y // schedule has a repeat interval of 1, it will loop every
9 R0 j# I% \3 R5 J' I // time step. The action is executed at time 0 relative to
# S" v& `/ @- E // the beginning of the loop.
' S; V: h) W! M1 g; n/ B8 N8 m2 m0 e& W
// This is a simple schedule, with only one action that is
$ J& j' S/ C1 z$ [' e% i% `6 h // just repeated every time. See jmousetrap for more
4 U- B+ C6 X3 Y' s: ~9 Q4 v4 k4 ` // complicated schedules.- l7 r' }, P* {; Y [6 F
" |+ D8 r9 x0 S modelSchedule = new ScheduleImpl (getZone (), 1);' s& G. k4 \8 I$ e. K. `
modelSchedule.at$createAction (0, modelActions);
% z6 l4 D6 Y$ Y1 J # d& J) ^- h" b0 c
return this;
4 {& }: k# \3 H8 L5 S } |