HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* l" p2 P: D+ t! s1 T3 o4 u! ~( C
; y; o! S' d$ g$ Z# b* m( ?/ S7 K1 U public Object buildActions () {9 Z! @/ h2 h3 V7 _% J
super.buildActions();
2 I5 D6 S X# f- I3 o. o- @ % K+ X! b2 e6 Q, m6 g( G- `, C
// Create the list of simulation actions. We put these in) _6 n6 g% |% H! C( V5 _4 J7 r
// an action group, because we want these actions to be
' Q+ D. y& M5 D, z. c5 J2 y // executed in a specific order, but these steps should! w$ d! V3 v% o* f
// take no (simulated) time. The M(foo) means "The message7 l2 k/ [" V9 m+ M
// called <foo>". You can send a message To a particular( Q, g' [/ L" Y! B6 e0 `" c- ]
// object, or ForEach object in a collection.
, y6 o9 f4 V) k% {/ i' t 4 O. d# v2 Z: V; B j# n0 u
// Note we update the heatspace in two phases: first run
# [/ U, ]9 i6 y7 \, | // diffusion, then run "updateWorld" to actually enact the
; e+ J+ x$ n8 Z // changes the heatbugs have made. The ordering here is" k2 c# m2 b* W2 W, f0 }0 K
// significant!
_* h+ J) i, ]2 f c
5 u/ H: y* n; w! L, J x/ g, g // Note also, that with the additional
8 w' h1 H7 G1 l6 M8 s // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 l% J# W' W, x9 `( E // randomize the order in which the bugs actually run/ ~1 F$ L5 l- d; G; y
// their step rule. This has the effect of removing any5 p/ K! ?; ^( t3 }& H- Z- E
// systematic bias in the iteration throught the heatbug: N3 q5 k. L; y6 H
// list from timestep to timestep, {. \# B* X3 d" ^
5 a* l1 F; z7 E- Q- ^1 ?& G$ e
// By default, all `createActionForEach' modelActions have
+ G! c* s% p9 t$ z2 g // a default order of `Sequential', which means that the! q- u# u( W: J3 o( w* J6 t7 J
// order of iteration through the `heatbugList' will be
8 l3 a/ q3 K: o7 z# V" H; ] // identical (assuming the list order is not changed
' _/ k4 z6 N$ ~, A, C2 u- i& M ] // indirectly by some other process).; M0 O+ T0 x& P$ t& `; R
$ \2 i$ }1 m$ ]8 F
modelActions = new ActionGroupImpl (getZone ());1 _2 i1 V, u. S" P% {
8 g; J, T. l% R* Q! d0 y
try {
/ R% z7 R0 {+ O modelActions.createActionTo$message
. q2 C& k1 z' L, ]$ i# O4 U- e2 V# M (heat, new Selector (heat.getClass (), "stepRule", false));2 d* R* [$ Z& s/ J( v
} catch (Exception e) {" e3 L9 k' q/ H0 y- c1 n1 m2 e
System.err.println ("Exception stepRule: " + e.getMessage ());9 y+ p! ]7 o! M5 Q3 E& d+ P
}
/ p5 T, [+ M! s4 W
8 b3 p8 S& O) m x; B6 s' b try {
. z) S6 a/ }: ]0 t5 J- @4 \ Heatbug proto = (Heatbug) heatbugList.get (0);# E$ ?, }: y% ^ l* E
Selector sel =
8 m5 T8 w0 F; K2 g+ L new Selector (proto.getClass (), "heatbugStep", false);" n: ~2 s( |' [6 U" F- c3 T
actionForEach =% G; |6 N% _* {4 t. `! |
modelActions.createFActionForEachHomogeneous$call( R! f: @* J; d3 p" K
(heatbugList,4 F) c* H/ h) ^5 ?$ a
new FCallImpl (this, proto, sel,
$ v' I9 J V4 z/ e3 j new FArgumentsImpl (this, sel)));& F: ]* D$ u+ ^) s# x
} catch (Exception e) {4 `# B) z+ Z; s$ `! e* G
e.printStackTrace (System.err);& ~! d5 _ W6 i7 b6 O
}
! k8 q4 z" y5 g# v q4 @
9 D2 U' u- v( u4 w3 j1 Q syncUpdateOrder ();
" G: f- M, { Y! v* |, g# C: l0 [: G/ W8 t! O
try {: {4 W* N' V: v5 I [0 L* |
modelActions.createActionTo$message
6 m4 Q) N9 M6 p- T (heat, new Selector (heat.getClass (), "updateLattice", false));
9 s# V, [/ s/ i; L0 H } catch (Exception e) { N( a7 w) o( V) b' [7 G1 p
System.err.println("Exception updateLattice: " + e.getMessage ());
/ H- W2 e. O9 P e( L, z5 I& v$ V }* A* v% L/ {7 v- R1 @% n
, U. Y& e5 E( h' D/ M! \8 T2 P // Then we create a schedule that executes the2 D2 |( M6 c" B% O1 a1 Q
// modelActions. modelActions is an ActionGroup, by itself it
3 N4 Y9 _2 ]: y1 B% G, ] // has no notion of time. In order to have it executed in
4 X7 j, H$ ~& k) q% j8 X // time, we create a Schedule that says to use the
7 g! J) j! l! ^* T4 x( d& _ // modelActions ActionGroup at particular times. This6 o# ^* ?+ ]$ a$ B& R
// schedule has a repeat interval of 1, it will loop every- N3 F& E R5 G# j; i# |+ P
// time step. The action is executed at time 0 relative to
) _3 i1 a( r X // the beginning of the loop.
- s" p, j/ \; j3 r( k# X8 J2 w( i8 h+ ~; e3 J8 x7 |2 k
// This is a simple schedule, with only one action that is
6 ^' |7 V$ @6 i7 U6 W // just repeated every time. See jmousetrap for more" G/ r$ Z S/ n4 ^ I
// complicated schedules.
! R# h: Q0 [$ T. R 8 |& Q! O3 O/ q- ?
modelSchedule = new ScheduleImpl (getZone (), 1);
' D. ^6 ]3 x* s0 c; O modelSchedule.at$createAction (0, modelActions);- Z4 d9 U$ {) |1 ~
. e l) u& }5 {- h6 a; B return this;
& K# }3 |0 Y' I" Q3 _$ e7 ~$ ~ } |