HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
g4 T7 {. u& c, I8 w' T0 ?0 ~9 a/ v/ f
public Object buildActions () {
3 G$ n% T1 s9 p6 f, V2 D3 u/ N. H super.buildActions();, I& O; T! `) }0 O
- s) P, J: F: g, @$ s+ W1 u // Create the list of simulation actions. We put these in2 J6 n& T2 r8 b1 ]8 |& J
// an action group, because we want these actions to be
' M2 k- O, b9 J/ }) E // executed in a specific order, but these steps should$ f% ?" y& e$ z% n! ]$ m* Q$ G
// take no (simulated) time. The M(foo) means "The message
; o5 t' M- a7 h! Z& x" r C& t; O // called <foo>". You can send a message To a particular: `$ T9 c+ y+ d7 S/ f8 ~8 x
// object, or ForEach object in a collection.
6 Z, I+ z6 }* }: S9 i$ x% T; T
R) y. `# k; v& J, X // Note we update the heatspace in two phases: first run
# x( e4 a$ ?2 x. J( ?, _3 { // diffusion, then run "updateWorld" to actually enact the$ r/ ^7 E9 y \
// changes the heatbugs have made. The ordering here is
, b. a" Q/ K# }/ K1 w$ a // significant!
* T$ e1 }& T+ K+ R- H& R' \
3 l' x7 e% J/ l. I7 c* G; v // Note also, that with the additional- V1 G h4 w2 z H N& n, @
// `randomizeHeatbugUpdateOrder' Boolean flag we can
" u7 d* a, H4 C* ?' {3 _4 | // randomize the order in which the bugs actually run
. i6 l1 A9 o" y U% a* r$ ~ // their step rule. This has the effect of removing any
4 ^: [) h0 n' |/ e" t: i // systematic bias in the iteration throught the heatbug
# l5 z+ N5 a+ J2 E1 B // list from timestep to timestep
' h9 ?$ Z) W! v% {8 d8 z, w- o
+ C' ~" }# w2 z2 t5 x: K9 w. V // By default, all `createActionForEach' modelActions have
" `. h- D" D" u1 b& m! \ // a default order of `Sequential', which means that the, B* a7 Q4 q% S' p+ r2 P8 X
// order of iteration through the `heatbugList' will be
4 P* h$ b. E5 [" i+ _ // identical (assuming the list order is not changed) r5 [% J8 S9 p' j o3 U2 a
// indirectly by some other process).! R Y5 K r5 |: H4 { L
9 L+ q7 z8 W9 i3 ]% ~: z. Y, L( Q modelActions = new ActionGroupImpl (getZone ());; m* ~4 v$ B& d$ Y7 o7 _
3 n) L4 f# K% k4 X8 B" b
try {" C; H& m& _% E+ f& p/ F& B# N
modelActions.createActionTo$message7 ?$ P6 i8 d( i0 a( ?1 a" X9 r
(heat, new Selector (heat.getClass (), "stepRule", false));
- E6 a% Q5 o/ m) _ } catch (Exception e) { v) B1 {, t$ ?% d4 E* P. \
System.err.println ("Exception stepRule: " + e.getMessage ());+ d% r1 X4 f" j
}0 Y4 v; @3 ^$ I" k: P9 C. L
( W* ~. u6 C; R% S
try {
4 i( N6 j1 k) c) c. b' P Heatbug proto = (Heatbug) heatbugList.get (0);! Y& e# x# A# B* ~
Selector sel = : s6 Q: U0 A: ~1 U
new Selector (proto.getClass (), "heatbugStep", false);$ |$ O3 R( Y X1 H: G+ r% e4 m
actionForEach =9 l8 \+ O6 D N* W/ U) j+ Q
modelActions.createFActionForEachHomogeneous$call4 ^4 ?% A* L8 z6 K& e4 a% q
(heatbugList,/ V$ `+ m8 v% Z( E( k
new FCallImpl (this, proto, sel,8 a. W+ U/ q# E- A o: {" W, x
new FArgumentsImpl (this, sel)));* v5 Q0 w _: j* F2 p
} catch (Exception e) {5 W5 b! V+ k; F8 l. g! P8 }- c
e.printStackTrace (System.err);1 c( E) s% a) r- l
}
8 J5 _ m- n# J: J( ~! [" [ # J h4 C/ g: o5 c8 |5 U& b
syncUpdateOrder ();8 ?! ?( `/ ^3 m% x4 N, x
" l' P0 P5 h8 F/ s; F" {8 I try {& B/ Y& ^: ~5 D
modelActions.createActionTo$message
) g% Z) \% c N (heat, new Selector (heat.getClass (), "updateLattice", false));# \2 Z( O5 d, i( [
} catch (Exception e) {
8 c+ @# {- y& h( U) T System.err.println("Exception updateLattice: " + e.getMessage ());$ X" G! X4 |/ M, \# d
}* s- m* }; x* J2 o3 I+ {9 b
" ?4 R& K3 f. n8 k( u( a! X
// Then we create a schedule that executes the# e* p: h9 m0 i" ~! T7 t
// modelActions. modelActions is an ActionGroup, by itself it9 p4 L$ f' U/ M# S$ X* I5 o- W
// has no notion of time. In order to have it executed in, Y; a/ `0 [8 b
// time, we create a Schedule that says to use the: k1 X- N0 x4 Q4 O
// modelActions ActionGroup at particular times. This" [ r2 V# N0 h, e/ O+ W
// schedule has a repeat interval of 1, it will loop every: h4 d/ M, K$ W- v l1 r
// time step. The action is executed at time 0 relative to# p" L: z4 q9 }
// the beginning of the loop.' a, C b; @* `2 [& N
% \7 x' l4 j* h# c( {$ y ? // This is a simple schedule, with only one action that is
/ q, f3 j) S% x9 d' P // just repeated every time. See jmousetrap for more) O/ H& _' o6 ~: K- g" Y
// complicated schedules.
# F; f9 m2 s/ m; q4 U % V1 c; Q( f# b5 r' g: ?' ]- r
modelSchedule = new ScheduleImpl (getZone (), 1);
* K1 `/ W# I2 ?, I1 r+ \8 }$ [ modelSchedule.at$createAction (0, modelActions);4 T- ]; X' f2 N+ w" b
- `1 a1 b; f+ n! @( H. K! F( _' q return this;( n9 b2 X: L5 e V8 K% B& W! Y
} |