HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& o# \" y* d5 x
( _! t5 T1 z. k; V- H: z
public Object buildActions () {& d! _1 y7 k7 S8 Y a4 l
super.buildActions();
4 s; D+ @. N6 R* s
& @' x/ n" A2 R. b // Create the list of simulation actions. We put these in
- Y' N. r- w% X1 x, M) Y2 H // an action group, because we want these actions to be: ^8 S5 x2 u6 g' W3 Y
// executed in a specific order, but these steps should' V& q3 x8 w& i1 v& H _
// take no (simulated) time. The M(foo) means "The message
4 p4 ^5 e+ |+ G, d3 p" H: X2 |3 w // called <foo>". You can send a message To a particular
v& G8 M) [" n% P' a // object, or ForEach object in a collection.
+ O% q& @. o S- I, V M/ |
$ b: k, X4 @) M // Note we update the heatspace in two phases: first run
' Y% a9 F8 v# T Z( a; L // diffusion, then run "updateWorld" to actually enact the- D9 j6 G: y, U b/ t: T: G
// changes the heatbugs have made. The ordering here is# P. }. e, ?6 e/ `, p+ e6 j
// significant!
2 B% D' _# w, ]8 K( K( N; B 0 g1 P: R u1 d. |
// Note also, that with the additional
+ z. P' m' i: x) \ // `randomizeHeatbugUpdateOrder' Boolean flag we can, U- r; J. U/ b: b1 l8 c
// randomize the order in which the bugs actually run
s1 l! R! m$ c) H5 I // their step rule. This has the effect of removing any
/ E* z& t% n. N // systematic bias in the iteration throught the heatbug
- ~2 h$ o ~6 T3 [3 H3 l // list from timestep to timestep
5 p: t) B8 Z1 [- W V | * a9 Q- M# s0 {2 g" ?* C6 n
// By default, all `createActionForEach' modelActions have
- {8 s9 M) Q3 j0 q% { ^$ Z2 j4 X, H // a default order of `Sequential', which means that the% x6 `8 ? c5 H7 V
// order of iteration through the `heatbugList' will be
1 }9 x. X4 u* |$ u. T) u/ N5 O // identical (assuming the list order is not changed
- W$ q8 @- I3 r( h7 t" f ~ // indirectly by some other process).6 e' i% y9 F s8 X
! ?! h9 g3 B* S
modelActions = new ActionGroupImpl (getZone ());& I1 L0 a. c4 H9 u& Q6 h3 R
6 i, y4 h' _, H/ j- J. n; r" i try {
, w, N$ f& w5 e( c' G3 ?% W modelActions.createActionTo$message
9 U6 e5 W; M* F5 d ?8 Z (heat, new Selector (heat.getClass (), "stepRule", false));9 o* [( F6 j- L/ O9 F
} catch (Exception e) {% p) b, m; Y/ W5 e5 c8 z4 F0 H
System.err.println ("Exception stepRule: " + e.getMessage ());$ R. G& i, X. ~5 A9 y) ?
}
! j+ _' p/ g8 E- n: Z5 f5 v7 I( @# N# x$ C* M- j4 d4 {
try {2 X5 F6 I+ M& }% B3 q: T
Heatbug proto = (Heatbug) heatbugList.get (0);
5 p& z D7 t- ~) ~: J" ]6 o0 Z Selector sel = : l8 R; I, J) A$ B6 r
new Selector (proto.getClass (), "heatbugStep", false);# V1 N6 r1 S' q+ K$ O v/ N
actionForEach =- g9 }0 W6 y! p+ i% `; k
modelActions.createFActionForEachHomogeneous$call# J0 w8 o: m" I: ?% W5 n; u$ z
(heatbugList,. n" O9 Z7 ^# f. `$ ?" q' S
new FCallImpl (this, proto, sel,
! x; B6 W2 w2 H1 _* K( Y: S new FArgumentsImpl (this, sel)));! ]5 J: K! p7 x# r
} catch (Exception e) {
* q7 ~0 O7 v! \/ |5 _ e.printStackTrace (System.err);
: W6 h* G6 m5 k& `2 `3 I }
# L2 {' w2 p P, v3 R' [
3 J! R6 e# e2 C0 `- a) F( b" G2 }: o. o syncUpdateOrder ();
/ @, K* k6 R6 A. c3 Y* d" X& h% `7 a2 P9 l( D/ o
try {
: s# |* J( o2 n3 H modelActions.createActionTo$message
2 \/ U" [7 c& g( u/ x (heat, new Selector (heat.getClass (), "updateLattice", false));
4 t$ D% x1 k6 B$ G) ?- W+ r } catch (Exception e) {
) u- ^1 J/ {% a) c Q System.err.println("Exception updateLattice: " + e.getMessage ());4 i7 y' h& r7 p' E- W1 C
}
5 e0 T, O( Z2 m( f * l* e$ E8 W* I6 L: R
// Then we create a schedule that executes the) _/ N" ~( b- \7 @$ h
// modelActions. modelActions is an ActionGroup, by itself it
; A- [* ~7 B; Z0 Q# d8 j // has no notion of time. In order to have it executed in
" }- M7 [1 f' D1 y y/ J // time, we create a Schedule that says to use the
3 O7 i+ _! o2 U! Y4 Z; \1 b9 j+ n: k // modelActions ActionGroup at particular times. This/ X9 o( }' j( h
// schedule has a repeat interval of 1, it will loop every
; c% T- m9 g; c4 b# a // time step. The action is executed at time 0 relative to; K W7 L! V+ L8 u3 d8 E( F
// the beginning of the loop.
* i: I/ a1 i/ J
" f# e x2 @, k1 E! f // This is a simple schedule, with only one action that is# Z( A4 N6 G' ]$ H% `9 k
// just repeated every time. See jmousetrap for more
, q, Z( T, @, F2 Y, D/ x! K // complicated schedules.. W) f9 m0 n) Y% {7 v; h
3 `4 K/ p! }2 o6 {1 D/ o, Q modelSchedule = new ScheduleImpl (getZone (), 1);8 \5 Q8 n y1 R% \4 Y5 d( o
modelSchedule.at$createAction (0, modelActions);6 q/ L) f% l" y! @
: \- V9 s" [8 [% B% s% ~
return this;
2 k& O1 K1 P# d, z! T } |