HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ w1 H4 ?1 x! a2 t) o- F6 a
- @4 h5 Y: |6 t# c public Object buildActions () {
- R; }# B# n6 Y u+ ]. K2 S2 B super.buildActions();
" k. L; a3 b, F 7 I7 b9 V7 }9 K8 f f
// Create the list of simulation actions. We put these in7 t/ ]$ {$ ?0 Z2 p) B' F
// an action group, because we want these actions to be
9 e9 k% a( b2 E/ U // executed in a specific order, but these steps should5 i, j# f' ], C+ |. q* W
// take no (simulated) time. The M(foo) means "The message2 s# U0 c& Z4 i* U ]! W; }
// called <foo>". You can send a message To a particular
[( |. V! y8 f' c0 z // object, or ForEach object in a collection.$ k {/ X8 |8 W4 l( N% @
0 S, {& ~4 X% C% I' [ // Note we update the heatspace in two phases: first run$ G H, m( o8 W @( k) P
// diffusion, then run "updateWorld" to actually enact the
M7 E1 U+ ]$ ?/ |" p // changes the heatbugs have made. The ordering here is9 t4 d1 \) I# ?' D* e
// significant!/ n4 b4 X% I% W; k
2 P2 [+ E+ o% t# F8 ?. h5 h
// Note also, that with the additional
& {4 \* n8 ]7 k! C) Z) l0 W5 N! X // `randomizeHeatbugUpdateOrder' Boolean flag we can
* c3 }( H5 m% d // randomize the order in which the bugs actually run' y6 ^7 @8 r! ^) P: b q
// their step rule. This has the effect of removing any
1 a4 M6 B5 X) D9 ^ // systematic bias in the iteration throught the heatbug
4 \) [2 p1 u# @: ], o) r6 R2 z // list from timestep to timestep& G# e& w# o0 p6 Z' w1 M1 d
9 Z3 o T: S) ~1 G7 K // By default, all `createActionForEach' modelActions have
! V$ W8 w, o/ {9 R( X3 B // a default order of `Sequential', which means that the
) o0 I2 O8 Y# m1 t/ _5 ^" T // order of iteration through the `heatbugList' will be/ M' D x H, C; _0 ~7 L, ]
// identical (assuming the list order is not changed6 ^+ H9 o- A2 _4 B. O( ^% }
// indirectly by some other process).' F7 P0 N1 d- i4 u4 b
6 T L; ?; Z6 ^
modelActions = new ActionGroupImpl (getZone ());0 I( l n8 Y/ F7 s8 @+ d# K+ B
2 ]2 T% K) _* \2 z, f1 { try {3 J- B8 G& o5 p4 a& P: M2 X
modelActions.createActionTo$message
2 o# q$ O& h5 q% {& d; e1 e, S (heat, new Selector (heat.getClass (), "stepRule", false));! r+ b+ ^" ?- `! K* e
} catch (Exception e) {! i. n( C$ E- a Y6 z
System.err.println ("Exception stepRule: " + e.getMessage ());
7 z7 T( J, }' q: f/ H; G }
( A1 w: o4 v. d: |: ?0 U8 \# h: i h
try {- r3 s: j4 J* k$ o8 k
Heatbug proto = (Heatbug) heatbugList.get (0);( z! J l( j5 C6 ]0 y# E8 C
Selector sel = ' ?9 h! Y. C. _$ Q+ ?
new Selector (proto.getClass (), "heatbugStep", false);- e# a. _5 a4 Z9 C! Q) x
actionForEach =
5 C9 y* D) s) n2 _4 x4 {, s- S modelActions.createFActionForEachHomogeneous$call3 L/ p5 n1 o( f+ H* F- v
(heatbugList,$ E$ Q4 K% u& f; |* v
new FCallImpl (this, proto, sel,
+ R6 R7 X' ]# {/ ^& d. T: S new FArgumentsImpl (this, sel)));
" D+ L% u& u# Y } catch (Exception e) {
& X W# \, \) Q9 `% p' H e.printStackTrace (System.err);0 k) [& J- d1 I( L
} O5 l; M. q _5 e' P
" V3 Q& j: v( h syncUpdateOrder ();
+ @" F. X) ]* T1 Y% `0 k3 S/ C: M$ Y1 h# {2 v: ]" l" ], a
try {
) K; N- N& l; V: E! J modelActions.createActionTo$message 8 ]* D* `0 L0 a# D/ A! O
(heat, new Selector (heat.getClass (), "updateLattice", false));. o5 a6 N9 v2 ^7 e: s
} catch (Exception e) {
$ S! o$ \; P! u* C# ] System.err.println("Exception updateLattice: " + e.getMessage ());
! f1 q* o% s0 ]" { Y* |: |# \4 \ }
. z/ r: X# p1 d5 N" j % \$ w# ]0 m+ ~! ^7 }0 I
// Then we create a schedule that executes the4 c* S* Y! {/ ?5 P" Q
// modelActions. modelActions is an ActionGroup, by itself it
& y3 A) o& b U1 B* ? // has no notion of time. In order to have it executed in3 n* K5 `5 j) s, v4 {
// time, we create a Schedule that says to use the
: g* _; g, J# R% M/ L // modelActions ActionGroup at particular times. This, O6 }. A& p# O; R; O4 a% O
// schedule has a repeat interval of 1, it will loop every+ n) p$ k Z/ J4 ?9 q/ N$ M
// time step. The action is executed at time 0 relative to H I) P7 I- J' x' m
// the beginning of the loop.. Y, Y8 _1 d2 P7 @8 h, s! h# `. a
! m6 p: y; }/ [" \9 v* X
// This is a simple schedule, with only one action that is
1 H0 R9 {/ }5 _2 i* Y+ N1 R, j: K // just repeated every time. See jmousetrap for more
9 m; m: ?8 I6 D, n // complicated schedules.
6 \" o' D0 u/ W. C. C7 W% t6 r . ^4 c! {( e0 l6 N
modelSchedule = new ScheduleImpl (getZone (), 1);7 x5 y8 W2 y) p" u3 y1 m
modelSchedule.at$createAction (0, modelActions);
1 B' i& s" h [0 Z, d$ a 9 C3 k L2 j6 P; l
return this;
6 j1 v; F' g; T3 P/ Z } |