HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& K. b, F* E: E2 c0 R7 K6 g
& Y1 W3 s0 o! w8 r- f1 ~! J9 I. N- q6 L public Object buildActions () {& x5 `! g- c. D) Q
super.buildActions();2 K: a' O4 P9 |2 I& \3 f
8 e3 j+ M% x9 b& P0 O8 I
// Create the list of simulation actions. We put these in0 }, x& h' t1 w% [
// an action group, because we want these actions to be
- B/ X- R- d" }, B* Y3 \ // executed in a specific order, but these steps should ^$ Q& S/ h! h; t: Z, _1 ]8 N6 S0 v
// take no (simulated) time. The M(foo) means "The message
1 \* }! q' _2 Y4 H3 B6 v // called <foo>". You can send a message To a particular
- l+ p5 W1 x* B& _' E // object, or ForEach object in a collection.' J( e" z0 k! I: O+ z* W- K N* [0 ]1 L
9 E& ^& k/ H/ F# O9 _/ `0 i8 ?
// Note we update the heatspace in two phases: first run9 v7 C5 u1 P1 v( w( S
// diffusion, then run "updateWorld" to actually enact the
8 \3 _; v1 n; P! [( r/ Y // changes the heatbugs have made. The ordering here is3 ^* q# K; ^9 Z0 \' d5 G4 f
// significant!
t5 O3 ?8 s3 Q* p 0 w/ _/ N* u. H8 X
// Note also, that with the additional# f+ c$ s+ _' Y+ ]) T$ Y
// `randomizeHeatbugUpdateOrder' Boolean flag we can
5 C+ w) A. g3 u; B; Z1 D4 p& [2 r // randomize the order in which the bugs actually run
9 k4 ?" @4 x0 @1 d) p // their step rule. This has the effect of removing any
8 f3 S$ O/ M B4 ?7 f // systematic bias in the iteration throught the heatbug$ d ^ l% I6 z: }) O
// list from timestep to timestep) Z1 A, d/ @( j9 q" ~
2 ?" j4 h' I, h // By default, all `createActionForEach' modelActions have: H6 N( ~- O* `( i1 L4 R6 S' \
// a default order of `Sequential', which means that the
5 G5 E% ~6 b) I // order of iteration through the `heatbugList' will be F+ G( r, K$ c
// identical (assuming the list order is not changed
8 A# ~: o' |1 S( d! R& e# e. g1 X // indirectly by some other process).
3 Q- Q- P; s( n9 M
$ R, y) D* @" r) Q( O4 c/ C modelActions = new ActionGroupImpl (getZone ());
5 N. c$ ` o# h3 O' {
; n8 U+ J* a' |; J' J Y try {
9 `5 a$ b7 X( n# M: p7 e6 t) q modelActions.createActionTo$message
9 I1 K* U4 c4 h+ u (heat, new Selector (heat.getClass (), "stepRule", false));' k8 b' F4 H! C" k7 h. K8 B
} catch (Exception e) {- o% Q; v/ L( _, L6 Y8 u
System.err.println ("Exception stepRule: " + e.getMessage ());5 q5 X" a- B# Z& v, T9 P6 E
}
; ~" M# n* x+ M# j2 {( M" [, r$ @) Q( l4 V) ?
try {0 ?: t/ y- {: q
Heatbug proto = (Heatbug) heatbugList.get (0);% \( A$ H y5 _; E* {" r
Selector sel = 3 Q7 `1 T( n e ?" X( }
new Selector (proto.getClass (), "heatbugStep", false);# }" I; @& X! C6 b8 L
actionForEach =
% I g, o( g+ H1 Z7 ?- y modelActions.createFActionForEachHomogeneous$call3 J( ` v$ \, K8 _8 z0 C" \# K
(heatbugList,
$ f u: R* K2 \/ ~ k) w8 R) A" w7 [ new FCallImpl (this, proto, sel,% Q. u5 G# L% n) k- R& H7 W* n
new FArgumentsImpl (this, sel)));
; o% n$ J2 I$ B% q" X$ R- ^ } catch (Exception e) {* y" Q1 `, W3 O' U
e.printStackTrace (System.err);# v" t! d1 ?% g
}
; v, m0 w% u! k' w
, {( H h+ l; S syncUpdateOrder ();' B% P0 `4 V* I9 c0 @% p
+ ^8 p( T/ t/ t" T4 P4 l& M try {
6 H1 t8 |. m( I/ Y modelActions.createActionTo$message : A, W1 Y" Z; g
(heat, new Selector (heat.getClass (), "updateLattice", false)); P; p$ x; T- p# j2 g: B/ }
} catch (Exception e) {3 \3 k* l4 n5 C4 k4 j
System.err.println("Exception updateLattice: " + e.getMessage ());. U' C6 R* g2 Q2 H ~6 v2 m, p- G
}2 O3 V- e0 F1 l7 w, `- E `1 [8 h
" c+ Z8 }3 x1 _, M4 @ // Then we create a schedule that executes the
$ R* d) \6 O# d8 ^ // modelActions. modelActions is an ActionGroup, by itself it
8 |6 E! D1 S2 y F2 Z9 R // has no notion of time. In order to have it executed in
- Y' L/ j$ N y9 d1 m, ?- P# z // time, we create a Schedule that says to use the
$ P' I; y- c3 G9 l& v# w // modelActions ActionGroup at particular times. This
$ x$ `# X( H3 i; ]6 C2 P // schedule has a repeat interval of 1, it will loop every5 C" u* ?6 J1 l7 R$ h& I
// time step. The action is executed at time 0 relative to
" ?5 [. K% _2 v# V# Y // the beginning of the loop.( P$ ^ w- R' u: E
% p8 F4 d' G! m
// This is a simple schedule, with only one action that is
7 {7 K6 H* q/ g0 Z% Q( V5 ]" e // just repeated every time. See jmousetrap for more q! r! H7 T: e7 N
// complicated schedules.' {7 x2 L" f/ S& U3 m- M
6 b% Q3 R' v7 t1 T5 t' a
modelSchedule = new ScheduleImpl (getZone (), 1);2 b1 G8 d3 s" S% i. B* \
modelSchedule.at$createAction (0, modelActions);1 [8 h1 g3 i) {) S# I7 r( a
* ?; I4 P/ w1 P6 w return this;
/ l2 \2 K$ J, b } |