HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 E. U5 G! \8 \" q d Q& @6 z" }
8 T3 o$ [0 E7 z3 t' i' ~
public Object buildActions () {
2 t! P: Y9 M6 K( D" M super.buildActions();' w! f D. c& m( M% h) @! H' \
0 _2 n! N. |$ E4 V // Create the list of simulation actions. We put these in; D1 _9 h/ N( ~; S/ Z2 P# h' c
// an action group, because we want these actions to be5 i1 f- J D9 ^0 [
// executed in a specific order, but these steps should' ^ }% V/ `( G' i
// take no (simulated) time. The M(foo) means "The message- G* w4 _% J2 d
// called <foo>". You can send a message To a particular+ U5 t! m) n2 O0 Q" Y
// object, or ForEach object in a collection.
5 @# }, ~* I; G
* V ^% d* z- [+ Z // Note we update the heatspace in two phases: first run: b+ o- Q# e. t, ~
// diffusion, then run "updateWorld" to actually enact the) N0 V3 Z9 b! Y* r
// changes the heatbugs have made. The ordering here is
4 I$ C9 Y- o0 S6 T, n // significant!
+ O+ ^7 `. {: ]$ F# h, B S
( q( B" ?. T% T: M8 t" x5 z // Note also, that with the additional/ X! h8 [( [+ ]6 N+ l
// `randomizeHeatbugUpdateOrder' Boolean flag we can; W' S: ^- e: C7 L2 `
// randomize the order in which the bugs actually run6 s7 [! m& g" Z2 J( a
// their step rule. This has the effect of removing any
0 v q! L3 |9 h- s/ q' e // systematic bias in the iteration throught the heatbug
+ t/ p) j7 q$ r" ^0 y R( a // list from timestep to timestep! Q- g9 h+ D( ~5 c7 u$ Q" q$ ?
6 b# M0 ~: k! |+ b% T3 L$ @( }& m
// By default, all `createActionForEach' modelActions have
, H* c u: w5 h4 [+ b // a default order of `Sequential', which means that the
1 w! b4 o0 R7 f, _4 Y) S0 H // order of iteration through the `heatbugList' will be p* A; |. c E+ c7 @& n
// identical (assuming the list order is not changed
* H! B0 J5 T# G+ h // indirectly by some other process).6 t- M/ ?5 d9 C. [, ~4 B' O. ~
' F. T) C7 S5 Z8 E% w H modelActions = new ActionGroupImpl (getZone ());
& ~6 I. J- \; Z g" ?
5 k& W9 r4 M5 S x, r9 {8 t1 c try {
! r- I2 J9 r' o3 T# z8 _ modelActions.createActionTo$message# H8 S6 \9 p' b1 ]. O/ f
(heat, new Selector (heat.getClass (), "stepRule", false));
* t% M7 N4 y$ ^3 O+ Y1 Z; J; H } catch (Exception e) {+ F; x2 m# K# I/ I z
System.err.println ("Exception stepRule: " + e.getMessage ());
y1 H% L% z y) N8 @1 f5 m$ ~ }
& h! ?( I" `, C, B8 c7 T1 j. _: M; |
try {
9 A1 w* l' {8 q5 A/ ^# m7 q% i: e Heatbug proto = (Heatbug) heatbugList.get (0);
( @: A' M& ]( }) ^ Selector sel =
9 V( e- _( V% ]- k% G new Selector (proto.getClass (), "heatbugStep", false);" ?0 F7 J, l6 r+ g/ e( b
actionForEach =
" _7 e) o. ^, y8 p3 g) k+ i modelActions.createFActionForEachHomogeneous$call& `# x3 y3 J: J8 c* T2 w2 B
(heatbugList,0 t( u9 y- b8 f$ \7 i. C5 E
new FCallImpl (this, proto, sel,
1 r2 f. t+ g! s new FArgumentsImpl (this, sel)));$ L% a. h( |6 T# R- U/ Z$ ^
} catch (Exception e) {
: N0 C: t# S* @0 X2 P3 f e.printStackTrace (System.err);
0 q5 a- a, Y7 H% U }) {$ f/ y! w8 j& F; ~ s, u
2 ]" M" `- W6 o! q' b& H
syncUpdateOrder ();. j3 N# g3 j& C& d) j |# q$ |- \* `. J
9 x) e3 T( X' Y& k6 C) k/ z3 y3 v
try {
6 u3 K* d5 `% N1 V5 `; \* {" k7 z# @ modelActions.createActionTo$message 8 h* N7 a: L0 ~: G8 B
(heat, new Selector (heat.getClass (), "updateLattice", false));5 g: @9 @' @2 d: t
} catch (Exception e) {
* S/ \2 J# C( q, Z$ M System.err.println("Exception updateLattice: " + e.getMessage ());! g8 d9 J c' x
}
1 @% ?$ @. \ y5 ^. T) n- Y
! Z1 a1 u" Y4 r- d // Then we create a schedule that executes the2 V1 K* m; c4 K' ^; _ L0 \! A
// modelActions. modelActions is an ActionGroup, by itself it% w( w- v( g$ u3 V6 E7 y) g4 e
// has no notion of time. In order to have it executed in7 n4 h: T3 {3 L7 C" M; c! _, V
// time, we create a Schedule that says to use the+ W' b5 v1 `+ O: P/ E% P0 E
// modelActions ActionGroup at particular times. This
) q. }" v1 u4 x0 {8 x. h4 @+ o0 K // schedule has a repeat interval of 1, it will loop every
" `* J. |# I' G- ^) f // time step. The action is executed at time 0 relative to9 y; b$ } J$ V$ a h6 J9 [
// the beginning of the loop.' F6 i# y6 G# c; l; n4 }+ z
( ?' l7 P: L' y& F
// This is a simple schedule, with only one action that is
. `( e( q+ I0 [& m {3 ] // just repeated every time. See jmousetrap for more
' S& E; q- R, s, [$ O# t1 p4 o4 c // complicated schedules.5 S5 e( Y$ d; i, a) }, D. h
0 K6 w4 u0 n" o. |$ h modelSchedule = new ScheduleImpl (getZone (), 1);
+ L0 l, }+ Z7 x$ x% Z" E; Q modelSchedule.at$createAction (0, modelActions);
0 x5 ?8 C2 ~& T7 L* ]. a5 x2 a ' c- _- B5 ?; J
return this;& i. c! `/ P$ I- y) l2 z
} |