HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 s7 H9 N% \. W" E& u( c& g
5 g7 X) j" s {
public Object buildActions () {; o5 t1 x9 \# \6 r, k; |% ]
super.buildActions();5 Q6 T: X( l- `
W; }* ?; w y) ?! H9 U // Create the list of simulation actions. We put these in
5 |0 n$ r/ j8 c // an action group, because we want these actions to be
7 r0 v; S2 B' X: K( B6 q% B // executed in a specific order, but these steps should
0 G! k9 |1 k5 H/ j1 i // take no (simulated) time. The M(foo) means "The message
. g4 l1 N& Y1 D5 }( O5 b( ` // called <foo>". You can send a message To a particular
5 F9 o# c5 U, D9 W // object, or ForEach object in a collection.4 a1 j; x" P0 g3 [% W
: ~8 ?0 S5 S3 `; ? p // Note we update the heatspace in two phases: first run
9 a; b7 N3 `0 w4 R // diffusion, then run "updateWorld" to actually enact the" I. w3 w9 N) [( l6 F& W$ E8 X+ f6 M
// changes the heatbugs have made. The ordering here is. T5 h8 ^0 U/ b' R7 `, N% x
// significant!
! V- [# `, S8 z2 B# h1 H4 o' \) a; F
4 b7 ?; \" k4 f7 ~, V // Note also, that with the additional, A$ \' m* N/ K5 V0 V
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ E* O6 t& |% Y$ p9 ]4 S/ y
// randomize the order in which the bugs actually run
' }1 \5 D9 E# C% C2 S" w // their step rule. This has the effect of removing any
5 j" P2 a9 G. G" G7 { // systematic bias in the iteration throught the heatbug
8 Z; B5 }) T. W# _ // list from timestep to timestep
c# _. w. K3 ]8 @; O 6 h7 L6 S B; W! P
// By default, all `createActionForEach' modelActions have
- x0 U# {+ C7 N+ e // a default order of `Sequential', which means that the
4 Z# b8 x* E% Z1 Z // order of iteration through the `heatbugList' will be
5 \5 N2 V- E1 c# R1 L' o4 M7 W // identical (assuming the list order is not changed7 J0 o. {7 W, p% n
// indirectly by some other process). ^, f2 S& ~0 I9 @9 r* C3 d
8 G: E& y1 _1 T. w
modelActions = new ActionGroupImpl (getZone ());
: b" P6 I0 A$ c% _: ^' @2 P- D6 Y2 F& g |% n' B& _7 y# j
try {) s! R) {" F4 v. k2 `7 U
modelActions.createActionTo$message
0 B( {' a* _9 [& K* C2 K6 V8 Q8 ^ (heat, new Selector (heat.getClass (), "stepRule", false));
0 y7 T/ e1 a8 G6 R* o2 e, |+ j } catch (Exception e) {
0 ?; |, x+ k8 G; G; f! u System.err.println ("Exception stepRule: " + e.getMessage ());# W0 D/ m0 O% o* p2 [) y
}. ?1 w3 U* d3 H1 F' |, z9 ~
1 |; p* R' F$ I3 c- l9 ]9 k- y" q
try {
& {$ @# n3 Q% `$ k Heatbug proto = (Heatbug) heatbugList.get (0);
2 P5 e9 l! W% G( p$ u' ~# W4 ~ Selector sel =
7 `- U: ^# _# Q! [ new Selector (proto.getClass (), "heatbugStep", false);
" j; C9 b' l& [ actionForEach =! k p# I! k$ p7 ?! ?
modelActions.createFActionForEachHomogeneous$call9 F5 p+ q# T# [) J# `0 D
(heatbugList,. l9 k- P& O! i% D
new FCallImpl (this, proto, sel,
+ W3 d! c1 Q; W1 E: i/ s new FArgumentsImpl (this, sel)));
5 q( q; k ?3 r: [" l } catch (Exception e) {8 \# ^1 X! r) Y* \
e.printStackTrace (System.err);
; m% ?2 j, E7 Q6 \2 u3 i }
$ I$ I) ~; ?: T& {1 P4 y* N6 ?; e6 W 4 _3 M- }5 w. Y9 Q& G# ]; I5 }
syncUpdateOrder ();
9 ?/ y. d6 ^" p( r6 p- [% k1 m# I5 |) A" k/ T3 q N
try {
7 ]+ r# B; v* b" I# c8 m0 l4 w modelActions.createActionTo$message 0 i. G1 X5 f4 X5 h$ h9 R
(heat, new Selector (heat.getClass (), "updateLattice", false));
' U+ K3 E% h# y- X6 F( r6 P* a, c } catch (Exception e) {
3 v0 q& O5 o, ?1 K System.err.println("Exception updateLattice: " + e.getMessage ());
2 p3 H3 P( g* \/ g }. j# k) j/ l7 L( `. O
8 O, |; s+ a# v! e
// Then we create a schedule that executes the. n2 o: T8 L4 v5 }) j7 M! |1 K2 `
// modelActions. modelActions is an ActionGroup, by itself it% r0 Y' U0 ^4 J/ g$ T
// has no notion of time. In order to have it executed in
2 k& s' p) ?* A& ^. [( F/ I // time, we create a Schedule that says to use the9 d+ ~# R7 J/ z0 q, B/ T
// modelActions ActionGroup at particular times. This) g) Y5 A0 u) w" C
// schedule has a repeat interval of 1, it will loop every
. S; Y0 e1 D, L // time step. The action is executed at time 0 relative to
* Q: Y7 K/ ?2 a7 R. i // the beginning of the loop.5 M. W; ?% a) t! }: P$ f
' [( T0 h4 r6 k" O( w# F- q# }' L
// This is a simple schedule, with only one action that is
& r6 a+ @) M" Q+ a% q( W // just repeated every time. See jmousetrap for more% Z) m" a$ a1 [* s
// complicated schedules.% A/ u& t: L7 e. o" X* ^- K0 ]
$ q4 r4 c" @- \7 |/ b& S7 [ modelSchedule = new ScheduleImpl (getZone (), 1);
1 m4 i- G; m, b% Q ` modelSchedule.at$createAction (0, modelActions);
4 P P/ v; l' k, n) p 1 E" P% {0 N; x* S# H
return this;: [% A L9 A- T- p2 [6 [4 z
} |