HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! Q! r; m6 n' G( O- a: n' p% W! ]; _4 }) j
public Object buildActions () {7 _& R+ E# p& i9 `6 }( }0 }
super.buildActions();
" V) M" G$ v' K8 V1 W1 |( n+ [ 4 ]; ? d2 C7 ]' a; D: S
// Create the list of simulation actions. We put these in7 F9 f* F( R9 a
// an action group, because we want these actions to be @ A+ _5 x2 z
// executed in a specific order, but these steps should4 } V& `7 L8 }* l* l
// take no (simulated) time. The M(foo) means "The message0 H" K6 z2 V- ?4 I9 H9 h2 \6 j
// called <foo>". You can send a message To a particular
# x9 t- K) v) I% j7 C% { // object, or ForEach object in a collection.
, G8 a B( p$ k
: q& N9 p) j# k3 Y3 N0 `# c8 c // Note we update the heatspace in two phases: first run
7 Z; ]* v# q, g5 N // diffusion, then run "updateWorld" to actually enact the. n1 i S/ i* K0 Q- j" i) E
// changes the heatbugs have made. The ordering here is
% ^+ K6 t8 i# o, ]4 f0 s // significant!2 c) V4 k! @+ N- L
, z% U* B$ [6 k+ n // Note also, that with the additional
1 F& M$ K _5 ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ c( c5 k# G: u9 b9 [4 C# `) m // randomize the order in which the bugs actually run
& z/ t; j! {% \ m8 M // their step rule. This has the effect of removing any
$ X3 S' q0 o) p0 n% A& s8 O2 Z // systematic bias in the iteration throught the heatbug
& E2 j$ y2 J2 z+ f# a // list from timestep to timestep. c1 J% v+ K& V4 f( E: \; T
5 M8 o1 C+ |* R8 Q. {0 K6 P
// By default, all `createActionForEach' modelActions have: U$ K, }. Z" `) w/ t& a& h5 H
// a default order of `Sequential', which means that the) G7 v5 @7 N, R
// order of iteration through the `heatbugList' will be" ? O- }1 d! k4 Y4 L! p
// identical (assuming the list order is not changed
) S& \! v9 s+ y. p* L // indirectly by some other process).
' H" @8 u. g7 k( Y
% q8 z' I |! p' s modelActions = new ActionGroupImpl (getZone ());) a0 W c" o+ C
A% y; r `* n$ \$ k9 d try {
1 o8 K) o; T2 G( b2 \ Q+ V, h modelActions.createActionTo$message4 _2 A9 i( T9 k, H" F
(heat, new Selector (heat.getClass (), "stepRule", false));+ S1 b' p2 O# S9 p5 m9 _$ G+ u7 U5 ^! s
} catch (Exception e) {6 S& R- M8 H# r) T
System.err.println ("Exception stepRule: " + e.getMessage ());
, _( A, ^2 v- l9 A+ B% i3 y# P$ }. ` }$ y' L7 I* f- r J9 l% E
. l. }: T: q+ p3 {# ^
try {( E& ]0 H+ g& T- ~6 L
Heatbug proto = (Heatbug) heatbugList.get (0);
. r. S ?( ^( M: a6 j/ o Selector sel =
7 s% a1 z2 i- Q* S new Selector (proto.getClass (), "heatbugStep", false);
- w' M$ N: p: s1 j6 i* K actionForEach =
% B/ @$ @8 y. N" l' ` modelActions.createFActionForEachHomogeneous$call( l( |3 N/ q. X; ~
(heatbugList,2 R' B) ?7 [; R! w5 _/ G& v8 P
new FCallImpl (this, proto, sel,
# z! q3 B' D" o5 L$ ? new FArgumentsImpl (this, sel))); O8 [* c) X3 R# [: x4 d
} catch (Exception e) {$ V8 @! O. M# G, I& X- L p
e.printStackTrace (System.err);$ `6 m5 B2 Y3 B/ H7 l6 M
}/ p: V7 W% _1 O) B9 c/ }$ ]
2 d0 R6 ?/ b& a6 ?! h
syncUpdateOrder ();* T7 Y0 n" w# n8 u: ]! j& `
- ?% q, k5 ^( x3 y try {" \3 {0 n; D1 _- ^7 D# W7 z
modelActions.createActionTo$message 9 d5 l# J; W: g L" k$ j* p4 ^
(heat, new Selector (heat.getClass (), "updateLattice", false));
) r* _/ f+ }2 h9 |5 U. x* G X } catch (Exception e) {
/ s8 s5 q: A% W* [5 r( t8 G System.err.println("Exception updateLattice: " + e.getMessage ());
5 \. G j' s0 t& `% Z }
1 w$ U) u* g3 F# V' N 3 v' B5 P0 S; ?1 U- t: x3 t @5 z$ A; y
// Then we create a schedule that executes the/ Q5 X( Y6 b& m# z; O' h6 ^+ k
// modelActions. modelActions is an ActionGroup, by itself it
- T; L* r4 a; I! P1 P2 R // has no notion of time. In order to have it executed in2 J3 R2 x9 W }, A$ Y8 L
// time, we create a Schedule that says to use the
) Z/ \5 E' z1 U7 e% n // modelActions ActionGroup at particular times. This
4 |& h. f; Q9 o( s) @ // schedule has a repeat interval of 1, it will loop every
- ~" S; _! d4 n // time step. The action is executed at time 0 relative to9 R" l3 ^& B# \: h; u& y
// the beginning of the loop.( r6 q9 i: ]; d3 Q7 A
6 Z+ Y; i# A8 S // This is a simple schedule, with only one action that is
F8 `3 C. x% r' t // just repeated every time. See jmousetrap for more" _4 v( m% S: z/ |+ h9 f
// complicated schedules. f* l& k6 k6 s3 n6 S9 U7 l
; n& p+ ^+ p; a, X- G modelSchedule = new ScheduleImpl (getZone (), 1);
( r( k4 ~& O1 l' ~5 i H1 F modelSchedule.at$createAction (0, modelActions);
" F, X& k% t& O1 b/ K# E
6 h# X: g. R- x2 t0 h3 X9 ~ return this;
" J0 p9 F l! Z. I; m# _( k% p) o } |