HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% T/ @. Q& }+ d2 I
+ r" {9 V3 q- ^# k# [2 D. e) d public Object buildActions () { q; R, P) Z3 S7 }( F
super.buildActions();$ y7 N) X2 s6 x( g, N
( ? F" x6 s" m$ C // Create the list of simulation actions. We put these in, _2 E: w+ V9 q1 ]/ m* e9 G
// an action group, because we want these actions to be
2 ], e/ L o1 o4 i+ U2 [0 U // executed in a specific order, but these steps should+ R7 ~; @" R6 Z# U8 q& i1 a
// take no (simulated) time. The M(foo) means "The message# Z% W9 F" Y6 i/ I9 ? Y" l: j
// called <foo>". You can send a message To a particular! X% Z \/ F( i/ J. v L, |& _- P
// object, or ForEach object in a collection.* Q4 C! o1 ^: v! j' n: p
. e0 ]* l: U3 _) \ // Note we update the heatspace in two phases: first run
3 M; B% A5 v) V+ c // diffusion, then run "updateWorld" to actually enact the/ {7 q8 T' Y' Q) H" m- Q
// changes the heatbugs have made. The ordering here is$ {7 I4 `3 i6 U8 J9 K
// significant!. U1 v8 s, C" E' Q$ ^
, A7 N. {6 S/ h0 \9 \
// Note also, that with the additional
' _( k' W3 r( d' U8 b9 }' d // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 c# o7 w o- ~ // randomize the order in which the bugs actually run4 h7 [/ y, f$ c# Q6 Y! H- E5 q
// their step rule. This has the effect of removing any
& @+ ~ g0 m: u7 q // systematic bias in the iteration throught the heatbug
) ~+ F! C G: I // list from timestep to timestep
# R W: z) P1 u7 @
: W# T/ d; D- {; l // By default, all `createActionForEach' modelActions have; p3 V( e7 k& S$ x' ]8 P8 e9 X. \ D
// a default order of `Sequential', which means that the
; D' N7 Z) o( ?& x+ g // order of iteration through the `heatbugList' will be3 H/ ^1 g+ y" @* m) P* x
// identical (assuming the list order is not changed7 r L- Q- t2 o& Q8 V' h2 H1 R
// indirectly by some other process).
0 @- h& N* Z, E- N9 @5 ?/ z1 O
0 f0 Q) X, X& d3 b% S modelActions = new ActionGroupImpl (getZone ());
4 x; M/ U8 [1 C% H
; g! ~4 d7 s' R6 {( x. d try {! O# A3 n7 f: }" K
modelActions.createActionTo$message8 k% Z6 o! u& L9 Z$ X
(heat, new Selector (heat.getClass (), "stepRule", false));! O, G, ~# f* D/ a/ a
} catch (Exception e) {
- g2 R! M; U1 E( s1 ~( }% Q" } System.err.println ("Exception stepRule: " + e.getMessage ());
- j& `0 ]% \" m" I }
3 R9 B& Y; ]; z' o; s7 s3 d: A" L$ x
try {
' G* V% N# I- J! ^ y; Q8 ^ Heatbug proto = (Heatbug) heatbugList.get (0);8 g- j4 P$ p, k
Selector sel = 9 S7 {& g- T5 E, r* t
new Selector (proto.getClass (), "heatbugStep", false);
; Q: T$ Y/ V) y; W; N8 z- y; p$ D actionForEach =5 t7 ^+ f& }% j" T# u# g# l5 L
modelActions.createFActionForEachHomogeneous$call- G4 m# b9 Y0 Y% {1 k8 Q0 d! g
(heatbugList,
8 M- Q$ V. w2 o new FCallImpl (this, proto, sel,
1 j- d+ L; z4 |) ]+ q new FArgumentsImpl (this, sel)));
$ Z: `) e% U4 r7 I. H } catch (Exception e) {0 G9 ?7 k+ `" i
e.printStackTrace (System.err);3 t; M2 }) U; D2 L
}! i6 m/ ~: x6 E7 }$ Z
+ N% }2 \1 @, W syncUpdateOrder ();
& Y) P3 B+ [& q2 P3 E& K' ]% A: Y
% ~3 c% m; f; j K try {! S6 U: B; A% N P+ Y) c x! w
modelActions.createActionTo$message . P. R& n5 d+ {4 b5 u* O
(heat, new Selector (heat.getClass (), "updateLattice", false)); _. m& _# b+ r& H6 ^+ R- _
} catch (Exception e) {9 _1 h/ y7 U9 p1 g2 y
System.err.println("Exception updateLattice: " + e.getMessage ());
# l: F4 H; o; M9 o8 R$ e! M2 ~ }
7 Y9 X/ @; W# k$ W) W) J % u; z" t8 ~# i9 T
// Then we create a schedule that executes the K0 n: i+ J1 W. |6 e& |
// modelActions. modelActions is an ActionGroup, by itself it; X; V6 R. i) N) a8 E
// has no notion of time. In order to have it executed in
& {7 T& o6 B0 O // time, we create a Schedule that says to use the
p0 ]- }" o( t% K7 o/ t! H8 [; K0 r* ? // modelActions ActionGroup at particular times. This
8 c* s5 j0 d4 T5 z // schedule has a repeat interval of 1, it will loop every! P6 Z C& D* P R
// time step. The action is executed at time 0 relative to% M w/ j1 j2 P' h
// the beginning of the loop.' X, l( a" G A' P$ |4 V; r8 \! I: y: u, C
2 }( j! O/ N, ]" r' V
// This is a simple schedule, with only one action that is) a* c: P- r( {! ?/ [" K
// just repeated every time. See jmousetrap for more
$ p7 G$ V. c; ^. t4 x // complicated schedules." \$ e: V! C4 L* b/ o# a; u
7 |% G$ h. Y. C. v3 N- N5 @
modelSchedule = new ScheduleImpl (getZone (), 1);1 ?0 B, L9 Q) ^3 E
modelSchedule.at$createAction (0, modelActions);3 p3 _1 C; I7 `
9 C* ~1 _9 S6 J1 W0 k. N return this;- W# B/ B0 w" A/ n: P- d
} |