HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' ^ }- B0 ]- U/ }) j) v! E( s& `
! B$ q3 c$ j. L% H$ p4 i public Object buildActions () {) v5 f- L9 V6 }+ T+ d4 e' [
super.buildActions();
: R$ a6 g) _4 ~2 V% G1 r
/ G& @; ~- j* z% k. | // Create the list of simulation actions. We put these in
9 ?* P. B# F2 g" V( X // an action group, because we want these actions to be# ]; F- G1 }* D) D
// executed in a specific order, but these steps should1 _( U$ ?0 Z7 o7 t- s
// take no (simulated) time. The M(foo) means "The message
7 W4 L/ n, G" V. T N" B* B M // called <foo>". You can send a message To a particular8 r+ r- {1 O; Z# w( F( ]' T
// object, or ForEach object in a collection.* b8 }/ u2 W( h3 m0 k
8 r9 l _( `4 q- s y
// Note we update the heatspace in two phases: first run
; }# }4 Y) E" ?2 ^3 l( f0 M8 L // diffusion, then run "updateWorld" to actually enact the
# ]1 y: F8 A) i2 K // changes the heatbugs have made. The ordering here is
$ J& f# @6 ~* W // significant!
1 Y9 O; }' a: b, u% d % r2 p8 w. s6 x9 \/ k4 v
// Note also, that with the additional
2 E1 g e# o' B5 w // `randomizeHeatbugUpdateOrder' Boolean flag we can1 x0 D$ }( r6 t
// randomize the order in which the bugs actually run
- o- B8 ~1 R0 w( \7 s; r* v // their step rule. This has the effect of removing any4 b) q t5 o) x
// systematic bias in the iteration throught the heatbug6 b$ `0 Z {* @
// list from timestep to timestep- E) h5 e X1 K
2 k0 H7 {2 q& X/ G# N' m
// By default, all `createActionForEach' modelActions have
3 a! e0 W: b* T' d- ` p // a default order of `Sequential', which means that the; ]9 [: c9 h; ~9 K3 `: q
// order of iteration through the `heatbugList' will be3 ~# X% B/ U9 ~
// identical (assuming the list order is not changed
W5 q9 a K/ |2 ^. K // indirectly by some other process).% C9 }' t7 T" B# o& `: C* ^( d
% P: W$ y/ ^% Q
modelActions = new ActionGroupImpl (getZone ());
. ~+ i5 t3 [# y: w9 h6 w% ]1 U% T. u% m% p. E% A9 t: P
try {, S1 S6 O0 g9 `9 B( i
modelActions.createActionTo$message
2 y5 h$ w4 H$ I! r* H z (heat, new Selector (heat.getClass (), "stepRule", false));8 [- H& m; Z5 _2 h6 d
} catch (Exception e) {
2 H9 N* P/ v% K System.err.println ("Exception stepRule: " + e.getMessage ());
6 {! l0 D, a" O: Q6 c. W# H/ n }& w, G4 L6 i1 |
: I6 c- H) E! s* s4 J try {
3 t& P* k+ `6 h4 ? u* c& T/ x9 u Heatbug proto = (Heatbug) heatbugList.get (0);* [+ }2 W( d8 h: J0 J! [% H
Selector sel =
9 s, e- N/ o* F; F" r& X* K new Selector (proto.getClass (), "heatbugStep", false);
3 o6 y' G I" M+ y2 G: R actionForEach =$ C5 R8 U* u! {
modelActions.createFActionForEachHomogeneous$call7 e3 z8 j5 e+ x J s3 n
(heatbugList,+ `3 u& h: H3 u
new FCallImpl (this, proto, sel,: @) t8 x1 g9 l4 d% G
new FArgumentsImpl (this, sel)));
! x- } H# e& F+ u8 v } catch (Exception e) {- S9 v- B/ d# R# G) y. |6 o3 \/ x5 h
e.printStackTrace (System.err);
4 u. K3 H' [# y. t" F0 w }% S$ h9 x3 q5 c
l$ c; i" q# {7 _( ^/ S. ?) ~
syncUpdateOrder ();
0 F! f1 G3 u7 u& w. e6 B) u, x3 ?( I0 y5 ?& }
try {$ Y# i1 C4 H7 e
modelActions.createActionTo$message
3 {: w& z# j! k' [ (heat, new Selector (heat.getClass (), "updateLattice", false));
! q, s" w8 m% }* v1 z } catch (Exception e) {
$ ]8 j1 r k6 ^8 |, Y4 b6 g System.err.println("Exception updateLattice: " + e.getMessage ());
$ a4 P1 V; n, n6 V% } }; w) B5 l7 s$ }+ X; n( a
7 I) R. C0 S# ^
// Then we create a schedule that executes the& {* U# B$ H9 w" B! J" b- l
// modelActions. modelActions is an ActionGroup, by itself it
* g: ^- X( C7 a5 a3 Z // has no notion of time. In order to have it executed in* }7 k$ [* o) T1 L0 @9 A# R
// time, we create a Schedule that says to use the
1 | `7 F& D1 c9 S6 q+ b/ ^6 a. @ // modelActions ActionGroup at particular times. This6 f4 [$ }4 E5 m6 x: J% v
// schedule has a repeat interval of 1, it will loop every9 P7 w7 v ]- B& f& I
// time step. The action is executed at time 0 relative to
# o. r, ~ F- u8 g // the beginning of the loop.7 O9 }- w3 S+ N8 R+ k5 b
4 [: Z; }/ }1 @+ i+ K
// This is a simple schedule, with only one action that is
! K$ k9 n8 S/ X- B* C& E- @/ c // just repeated every time. See jmousetrap for more
3 Z* @+ g) R6 _; I6 D9 ^ // complicated schedules.
0 N) U/ S$ u: u t
3 E4 S) l: C, @ modelSchedule = new ScheduleImpl (getZone (), 1);
8 i& I% R: Y- t modelSchedule.at$createAction (0, modelActions);. y4 Z# e8 m$ }6 ^
9 ~, K; m `4 u* b- `# I+ V return this;3 ?: t+ _! f4 j+ x0 d6 O7 a
} |