HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: S! X* P k/ U+ ^% s" L( I
* i5 q( M w4 T& v3 {6 m( l2 u1 R public Object buildActions () {3 V8 |8 {7 C* `; Z5 T9 L2 n
super.buildActions();: r( ], G3 A3 F4 k- J y$ G- T
3 E& w5 V2 p9 }9 C0 b% L" h
// Create the list of simulation actions. We put these in9 h+ F6 l2 e6 u0 l/ \: p
// an action group, because we want these actions to be8 O. W2 y: a! V4 e9 R3 E; Y
// executed in a specific order, but these steps should9 y5 e" Q+ L5 B% B* f& l
// take no (simulated) time. The M(foo) means "The message
: D% w8 E: a+ P- T' [: K // called <foo>". You can send a message To a particular
! V) y) O& ]) a // object, or ForEach object in a collection.- F. U Z7 |8 X$ j4 r$ T8 g
+ a" Q" `5 ~2 t/ m# |, Y // Note we update the heatspace in two phases: first run
' r5 q" l7 v J" F% [ // diffusion, then run "updateWorld" to actually enact the
* t+ _$ d8 B0 ^9 c0 G2 f( p // changes the heatbugs have made. The ordering here is
! O$ p/ |% ?% o/ X3 x2 o; b // significant!
: Y2 |; ^$ _4 E$ a0 b. S: l
' J6 s# ^# w2 p // Note also, that with the additional, d. z. d" U* r3 e2 F$ a
// `randomizeHeatbugUpdateOrder' Boolean flag we can3 y! n8 G k% {; A4 A5 i( I
// randomize the order in which the bugs actually run
/ V2 _4 U, {1 Y7 q( C // their step rule. This has the effect of removing any
0 U _5 F2 Y1 a O$ y4 g // systematic bias in the iteration throught the heatbug3 B B& F$ j8 V& [$ i; I
// list from timestep to timestep
! K: Y/ N, @4 r; ^
' S7 d% W T' ~+ B4 R5 M6 R5 K // By default, all `createActionForEach' modelActions have
* c Z" A y# ^; e // a default order of `Sequential', which means that the
1 H6 I+ o- m1 a8 @( z // order of iteration through the `heatbugList' will be3 v1 ^" H7 o1 q* A/ y5 l! T5 c
// identical (assuming the list order is not changed
" M% y; V) W! J* i5 V3 u // indirectly by some other process)." N6 }9 I* u* j8 [$ t
4 g8 W5 q K& r1 v
modelActions = new ActionGroupImpl (getZone ());
% P7 y1 Y1 C4 o" S5 r- p- E7 W I1 k0 `4 j% H, G
try {9 W1 D$ {7 b p6 n5 y0 Y: e6 n2 t
modelActions.createActionTo$message9 G# K' ~) W( S8 R; E- R9 V' v. w7 I
(heat, new Selector (heat.getClass (), "stepRule", false));9 t1 l% f& z! }( M
} catch (Exception e) {
: o: h4 C2 P# T6 g+ V System.err.println ("Exception stepRule: " + e.getMessage ());3 s, V1 J( ?) o, C9 ?
}
) Z1 @2 e0 e+ r% j
5 @) B) j7 H9 {# I# Z. F r2 ]% D try {
/ S# V- M% o3 F. e1 W5 }& t Heatbug proto = (Heatbug) heatbugList.get (0);7 L. X6 G& ~- t+ |" E) n* q8 Y
Selector sel =
1 ]7 u& N! {$ S' o/ l& U& c new Selector (proto.getClass (), "heatbugStep", false);
5 V, d8 ^7 I! C8 y; Z3 |2 a actionForEach =4 m3 Z! X# J; `' T
modelActions.createFActionForEachHomogeneous$call& b. _: G. c6 |
(heatbugList,5 x8 A, |, D4 ^6 o* l9 [! I
new FCallImpl (this, proto, sel,- Q) d4 X, j" h% `
new FArgumentsImpl (this, sel)));
9 J/ r: j% n3 \1 b } catch (Exception e) {
3 c: R5 A; u. }) o, s e.printStackTrace (System.err);, ~4 K- b5 O0 S% e6 k9 Y9 {
}% G) ~( f. U% g L0 l) G8 u
" S: @1 u0 f3 ]9 }0 m) L& x+ W+ h7 ~ syncUpdateOrder ();
* u, a) x$ W9 U- `( d. `0 K( R" D m, T
try {# P( k( Z ]9 G/ e0 l
modelActions.createActionTo$message & g/ X: \, p+ O- h# B) N
(heat, new Selector (heat.getClass (), "updateLattice", false));* G# ~2 e( E# W
} catch (Exception e) {+ v6 Z! |/ b( x9 |+ [7 v/ c# H7 U5 G
System.err.println("Exception updateLattice: " + e.getMessage ());
t# Z0 \) v, O( V4 q }( L$ y" q5 B3 ~+ b
( a" s& b1 [# b, g. x3 z( d5 P K- c6 z% o // Then we create a schedule that executes the+ M% [( N! a. q' _& |
// modelActions. modelActions is an ActionGroup, by itself it
( d0 {9 t1 A) X" I) ?1 c // has no notion of time. In order to have it executed in
' i' I$ x' r9 E% U9 Q; K // time, we create a Schedule that says to use the
8 J' h* [# F' z // modelActions ActionGroup at particular times. This
5 ?9 K' O( V V; M- D/ _& x9 p9 R // schedule has a repeat interval of 1, it will loop every
& J7 _1 g3 G* L" n/ E5 O# Z/ x* z // time step. The action is executed at time 0 relative to
$ E7 F3 q% Q9 T7 B5 P! t // the beginning of the loop.
~& ^; s; S( w7 A. |: x
$ C0 V1 |# d% H# U5 c1 Q5 p // This is a simple schedule, with only one action that is
8 V, E R8 [: I) y5 P) r1 g // just repeated every time. See jmousetrap for more
2 s9 F }! _# q2 [* D" _% ` // complicated schedules.3 Q8 c1 V8 ]" H4 \' I: P8 Q5 a
. u+ @$ ^% i1 |% I( m modelSchedule = new ScheduleImpl (getZone (), 1);/ [+ K! ?! l: S, U
modelSchedule.at$createAction (0, modelActions);: }1 I* S; u w7 ?; u( k0 G" y! y
1 t5 F; B& W, V7 c: y/ v1 ^ z
return this;
( b$ u7 ?' [) `) g2 N4 Z( b } |