HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. W* I$ |5 w5 X7 R$ u1 X# b1 ~6 G9 H
: T% B/ M" r* Y. n8 S8 [* K! |' v2 r
public Object buildActions () {
% c& R0 b3 I- e5 w2 { super.buildActions();" B4 R ~6 V7 J! I. | W- d/ G
1 G' S( }% m9 f8 y3 C0 i
// Create the list of simulation actions. We put these in
+ C% `7 Y9 @$ a. N& D4 } // an action group, because we want these actions to be! B1 u7 I/ \. b, A( F3 K
// executed in a specific order, but these steps should
! h# |# T* C" T7 b0 @, P; W% |9 i // take no (simulated) time. The M(foo) means "The message
+ u b; F. @5 O9 i+ s7 X. d# P: T // called <foo>". You can send a message To a particular2 Q7 n$ v* a1 r. Q& A) r8 O% b! u5 {
// object, or ForEach object in a collection.) e j: Y0 b: V) s2 V+ `
# A' s; Y2 e# ~' Q# S' P; W // Note we update the heatspace in two phases: first run/ o) l1 s6 L9 v4 B: L1 g
// diffusion, then run "updateWorld" to actually enact the
2 Y, D# t6 k+ E* R9 K. M // changes the heatbugs have made. The ordering here is
- h' `7 B" N. }9 z$ B // significant!& ~2 |& _8 i9 v; X6 |
( e9 |* r% l t; P, Z
// Note also, that with the additional6 e1 S0 S* P# M; J) W6 X
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 u4 m4 A! w- _6 ~0 j" n // randomize the order in which the bugs actually run. k5 B% b- g" I1 o' Z
// their step rule. This has the effect of removing any# M' ~9 Q/ N" q G* [
// systematic bias in the iteration throught the heatbug
1 j. h+ ~' |& h" s) d // list from timestep to timestep' e8 f$ ~1 G1 r3 t& ]( Q
* J7 |! V% k% g% d. Q# z$ f) q // By default, all `createActionForEach' modelActions have5 s1 E: {- G; w: B9 { N3 @# C1 t
// a default order of `Sequential', which means that the, ^" l; t2 U A* h
// order of iteration through the `heatbugList' will be
" Z9 L. l7 L# d3 J // identical (assuming the list order is not changed6 h5 F Q; i$ i
// indirectly by some other process).
; V6 w; z& e1 a2 P% H( P
! s) W+ O+ ^ p9 R4 l+ } modelActions = new ActionGroupImpl (getZone ());; }6 [) @: F. ~0 h) b- [7 q: Y
1 ^, I( r, |( Q8 c" m9 ?6 s' i try {
2 z/ N8 p3 ]3 O9 D5 b4 W) M) z$ u# W modelActions.createActionTo$message
. E. y& R8 l5 N- Q (heat, new Selector (heat.getClass (), "stepRule", false));
! v1 Y: I& H- I5 @0 ? } catch (Exception e) {
& F1 R. \/ m. R* Q6 }% C System.err.println ("Exception stepRule: " + e.getMessage ());; O! @& x4 O3 O0 ?# h
}
7 D; X* l9 F+ F9 Y
, S; j1 f, y- h" }% @ try {% Y; w3 J$ j" I/ G
Heatbug proto = (Heatbug) heatbugList.get (0);
/ L; w! Z2 O8 @( ?/ r* t7 v Selector sel = 1 L, |" s$ L/ I/ ^4 L# F
new Selector (proto.getClass (), "heatbugStep", false);
* d5 k, Z' M% w# O1 n actionForEach =
& f7 r$ u4 y! T9 r modelActions.createFActionForEachHomogeneous$call
( i: K) r8 y# n4 ?$ K9 y (heatbugList,: U, f, T$ u3 X9 C
new FCallImpl (this, proto, sel,: A! n# I( P9 Z
new FArgumentsImpl (this, sel)));
9 i6 v e* |4 r. V8 W* M9 |; [ } catch (Exception e) {
' J$ }" [5 {1 b, F" |6 I e.printStackTrace (System.err);
( Q6 P: i$ q7 I8 E# L }$ U8 c5 k5 {( A( y
0 z" R# Y) _7 K0 Y/ n syncUpdateOrder ();
# k, R1 x+ q7 r% R- q7 N: R" Y, c% ?5 t
try {
" F6 `/ U! U0 P: ~! E modelActions.createActionTo$message ; K( q; M2 e# u/ v
(heat, new Selector (heat.getClass (), "updateLattice", false));
3 V4 n" r% ]* X } catch (Exception e) {
& D8 V( ]9 a7 I System.err.println("Exception updateLattice: " + e.getMessage ());
3 R4 | e# u- s( \) ~' V4 E }
& A, z: c* s! x) \
; c. {& e. n4 ~9 w# i- O( x // Then we create a schedule that executes the
' {# S; c/ S# t& W4 D // modelActions. modelActions is an ActionGroup, by itself it
2 _" }$ _4 j1 N. Z% V" F E1 D // has no notion of time. In order to have it executed in
" l b- k$ R* t // time, we create a Schedule that says to use the
! _5 o: s$ y' `0 E2 K5 l // modelActions ActionGroup at particular times. This; K1 y# c! G4 L$ l
// schedule has a repeat interval of 1, it will loop every7 k8 ?& g/ O; H8 ~
// time step. The action is executed at time 0 relative to
* u7 w ?& r. N; Z) P: r( W* U // the beginning of the loop.
7 M5 v6 ]" v5 z& V! D ^3 J. B7 k- ^0 D. p0 B" t4 z' V
// This is a simple schedule, with only one action that is. v2 e, ]; f) s# a2 |. N
// just repeated every time. See jmousetrap for more
9 t, ~3 j/ m/ K/ N4 O4 m, l // complicated schedules.
6 k+ r, Q2 W7 w' @0 ~
& B; f! ]6 ?+ A/ m% J# d% R modelSchedule = new ScheduleImpl (getZone (), 1);
7 B3 o$ x$ n, ]9 c+ { modelSchedule.at$createAction (0, modelActions);
, c+ I$ L7 p# d/ J+ h$ E9 U% Q+ I & I$ o; v* c0 N! o
return this;
: R4 A/ C' c( c" q" J' Q } |