HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ R0 v" ~" V5 ?- P+ ^, r' |) Q
% ?- D6 g3 k& h9 I public Object buildActions () {$ S) o7 s3 q% ^3 J$ _2 S- t6 { |9 [" g
super.buildActions();7 k D8 w5 h7 y$ J
; D" b1 H+ \; [( K
// Create the list of simulation actions. We put these in0 |" U( U0 _/ z8 ~- O0 \( {9 l+ M
// an action group, because we want these actions to be
" [# e" Z; S) t+ L // executed in a specific order, but these steps should
r0 q/ H- Z8 j6 \9 X: b // take no (simulated) time. The M(foo) means "The message
6 v* g+ Q4 \( b' [ // called <foo>". You can send a message To a particular. C' p* |) s; l4 K5 f
// object, or ForEach object in a collection.4 A( p3 a4 `! P, n, K7 c x
3 _" J, ^8 F* v+ W6 H5 R
// Note we update the heatspace in two phases: first run
& l8 y& D `1 Q5 B8 N; ? // diffusion, then run "updateWorld" to actually enact the
4 d) B# Y9 m. _( c& U8 G // changes the heatbugs have made. The ordering here is
& R( [9 x; ?& y) k* M- w! ] // significant!2 |% ]# ^/ x& V: }: T, ]7 ~
7 D' V0 O3 {0 t/ b4 o9 } // Note also, that with the additional
4 e$ J+ H& E) M" ~ // `randomizeHeatbugUpdateOrder' Boolean flag we can1 x- \9 S; J4 n0 q1 Z1 v) L" ^, @
// randomize the order in which the bugs actually run
6 a$ l4 a2 H" A // their step rule. This has the effect of removing any
2 A; Q. d! `9 W# I4 z // systematic bias in the iteration throught the heatbug5 y6 y4 t' U0 U" O* o$ f( s
// list from timestep to timestep
/ f2 T/ G+ n0 M2 R ; }; y8 c1 B8 D' g; q, g6 Q0 N, ~
// By default, all `createActionForEach' modelActions have% M4 D( H! h- v9 A1 r& B" R
// a default order of `Sequential', which means that the8 Y1 x( [. r5 y% a& m
// order of iteration through the `heatbugList' will be& ?) u3 x6 W* p) _) k: H. k
// identical (assuming the list order is not changed
! d* E- q! E3 Q% J, c // indirectly by some other process).
7 j8 V3 C: I5 Z3 d4 l8 U3 ]( [
- T6 o4 u) \8 Q3 W' A x; n# d modelActions = new ActionGroupImpl (getZone ());
: c* Z Q7 n3 s* G w. F+ A5 U: ~2 {4 ]4 O; K
try {
/ a% X) y$ q7 p' l* ]& O modelActions.createActionTo$message; J) |% j7 K, L
(heat, new Selector (heat.getClass (), "stepRule", false));6 d I) ^- t# U8 e
} catch (Exception e) {0 j5 e9 ]( f1 G! s
System.err.println ("Exception stepRule: " + e.getMessage ());
5 y# @- D. T& B/ {" N6 k }
4 L) I5 f' M I3 ~
9 l3 P' c8 r4 M' ]2 l7 c try {0 ]1 x: \& Z5 ]' F; z
Heatbug proto = (Heatbug) heatbugList.get (0);
5 T* H3 x& p3 A0 L" B2 |& K$ T Selector sel = + u0 Q7 r6 f1 l$ u
new Selector (proto.getClass (), "heatbugStep", false);8 s5 Y4 [2 W8 w3 ~- R" k' q
actionForEach =) U' V' N$ k! T P, J# D: O
modelActions.createFActionForEachHomogeneous$call2 y/ H0 f8 ~5 v z$ o
(heatbugList,
2 h# C3 v1 U8 E$ D- ^( _' q new FCallImpl (this, proto, sel,
% V. G( O, p7 R0 P9 @+ O. n new FArgumentsImpl (this, sel)));5 A. V, y% A% G- O; W P2 R
} catch (Exception e) {6 ?+ r9 k$ Z5 V2 X
e.printStackTrace (System.err);% R8 M' P& f9 D! Y! F
}: z6 b+ v- Z5 X% m1 F% ~% W6 j
: k) V( |2 m! X9 m2 i4 Q( L syncUpdateOrder ();
/ H k8 ^ ]/ E+ f
# C/ }$ b9 O0 u9 r. a6 B) n try {- o7 |+ B3 R% r2 A( u
modelActions.createActionTo$message
+ a) c, j1 t8 }) ^ (heat, new Selector (heat.getClass (), "updateLattice", false));1 |$ J9 _, j* g; ?. t+ ]# z' B; ?
} catch (Exception e) {) t& T5 {( S1 I4 r0 e( U+ J: f t2 K
System.err.println("Exception updateLattice: " + e.getMessage ());- c4 J" C7 Z9 o5 p$ B% \9 w% A9 @. Y) c
}
7 k. ^7 s0 l, s) N# A- @- ~( I
. q1 k+ Q: {" ?8 d7 l // Then we create a schedule that executes the
~1 n/ }. Z) ^; D8 ~ // modelActions. modelActions is an ActionGroup, by itself it
% T h% s/ [. _0 { // has no notion of time. In order to have it executed in3 Y) t, \5 V3 m5 |1 Q; D
// time, we create a Schedule that says to use the+ c: a+ ?1 {0 `, C& R7 v! b, t) i
// modelActions ActionGroup at particular times. This5 e! _) r. u0 R" j( I. {+ b, }' k
// schedule has a repeat interval of 1, it will loop every
% ] l) z" _4 t6 k! g% p // time step. The action is executed at time 0 relative to' A# v4 ]/ K8 N6 S
// the beginning of the loop.9 s; v) ?7 M% n- N
7 ?3 G) M4 }( b# V* | // This is a simple schedule, with only one action that is! Q; p3 B8 J) Z$ y/ V
// just repeated every time. See jmousetrap for more
' ?. j6 c' f, L2 a, a t // complicated schedules.3 X t7 G; ]3 M
' q6 r- j. c+ f( x& \4 H
modelSchedule = new ScheduleImpl (getZone (), 1);4 m, k0 x: ]: R4 u" a
modelSchedule.at$createAction (0, modelActions); {9 f5 ]' s/ s3 y
! q* }3 w7 o. p4 B3 j1 x return this;
: b8 e& ~+ l/ i1 [( E) R4 Z } |