HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& K; @5 t: y$ T$ l, u+ `
4 h7 \7 H" k% K3 n
public Object buildActions () {+ W: p$ s$ m0 o# w
super.buildActions();
1 V9 p9 c/ P5 @# ~ G" S. g1 G3 V+ l) m
// Create the list of simulation actions. We put these in
& B+ `0 U# t5 d" |1 e$ b# C // an action group, because we want these actions to be
5 @ I" S, U' B+ O8 A // executed in a specific order, but these steps should: D7 F( o7 O4 Z0 m B4 ]% o
// take no (simulated) time. The M(foo) means "The message0 I& t. d! ^4 X
// called <foo>". You can send a message To a particular, _% D" w7 d& `
// object, or ForEach object in a collection.
7 q7 |4 m) z& I
' l" ~1 p. o( r* N& m/ v; c // Note we update the heatspace in two phases: first run
. c6 b8 n) O. ~6 z // diffusion, then run "updateWorld" to actually enact the) N1 W' {+ x: v
// changes the heatbugs have made. The ordering here is# d7 ?# { d$ }# O% X0 P
// significant!" r3 r+ i0 w$ X# u$ O
+ l: }8 q( d; p8 i/ l // Note also, that with the additional
( W) [& k# A* [2 g // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 L1 S0 p4 C$ @8 l( I) u% f3 Q; ? // randomize the order in which the bugs actually run# H1 _8 R8 K! C, g
// their step rule. This has the effect of removing any
" [, e# A6 f' E2 J" _# d" Q! m // systematic bias in the iteration throught the heatbug
0 h& S( q7 C; L. }: x' c // list from timestep to timestep2 E4 o B$ t( g8 _, b# r/ W! I
; `% H; P9 C- _0 x
// By default, all `createActionForEach' modelActions have: A) o# j% A; s" v0 I7 r% g* @3 U
// a default order of `Sequential', which means that the
$ M. k z1 D c7 T0 d // order of iteration through the `heatbugList' will be% s, M5 S/ F/ f7 g) l& y) [
// identical (assuming the list order is not changed/ U. q0 p. ?/ Y# V; T' ?( C
// indirectly by some other process).
0 a7 y& H+ k9 J 4 p3 S5 @! m0 A5 m6 E2 o
modelActions = new ActionGroupImpl (getZone ());& d+ u4 G0 i; v
9 r( e I( c- `1 I& g try {% ~- M, [$ @- M: j! M5 k8 |
modelActions.createActionTo$message+ p! u; m! h$ s
(heat, new Selector (heat.getClass (), "stepRule", false));
1 W7 o) D: H; s1 P6 s9 K/ @' q } catch (Exception e) {0 N- [. p B: Z" ]2 P% t/ R
System.err.println ("Exception stepRule: " + e.getMessage ());
' ]' H% H3 Q8 Z, s( W }$ x( Q1 E: ]7 W
# l$ q' L/ Q! u! p3 [$ Y try {
/ d4 `( Z4 \% _2 |& \9 m1 i Heatbug proto = (Heatbug) heatbugList.get (0);
( v4 m( j9 O2 {2 x$ x* P Selector sel =
- [. N: e0 c4 ]% j; D# J# L8 L$ T new Selector (proto.getClass (), "heatbugStep", false);
- l% r9 U- k+ U4 R actionForEach =# Q E: D. y/ }* H8 a5 T
modelActions.createFActionForEachHomogeneous$call: `% `4 I! }1 k
(heatbugList,
" k2 J1 D P) Y: r( K0 l new FCallImpl (this, proto, sel,1 r, ?; i3 k7 ?) m+ U
new FArgumentsImpl (this, sel)));
8 P& X( v% \* Y- ^- f, W } catch (Exception e) {$ I# G& O8 u) X* s# W
e.printStackTrace (System.err);: W5 y" h u" r
}
8 ~; J& \2 _2 p/ S1 t& \ 7 x# S' y: }4 l# @; D
syncUpdateOrder ();
. ^0 I5 e: i- Y, r- t! i( H( p; I) g; j" l6 x
try {6 u5 v/ y" F# D) g% n
modelActions.createActionTo$message
) |3 u9 V+ ]' B7 Z (heat, new Selector (heat.getClass (), "updateLattice", false));7 k& r& B2 s& f
} catch (Exception e) {" ^4 `8 d2 Z& r' a3 Q# t9 n
System.err.println("Exception updateLattice: " + e.getMessage ());
R1 Q* x" A* B; g, j# Z, l# p+ ? } x! A) f* m2 ~( `) F# p2 X" Z
% `# T8 m7 J% U" h/ }
// Then we create a schedule that executes the% P/ F$ i/ b2 H% j" r5 p/ Z
// modelActions. modelActions is an ActionGroup, by itself it- V M+ `0 ]/ Y- k E0 n2 Z' t
// has no notion of time. In order to have it executed in: u7 n# I5 }4 W# a; I6 i( u
// time, we create a Schedule that says to use the
. z. x6 J3 J; e' L8 u/ x // modelActions ActionGroup at particular times. This
8 k2 j* o+ G5 C K8 o. G" p$ H6 M // schedule has a repeat interval of 1, it will loop every
4 t) k% v( D! c, r( S7 w4 e! A // time step. The action is executed at time 0 relative to( d5 @8 Z' x$ Q
// the beginning of the loop.
7 @! U3 W2 ~- `: G: F& a% G$ s. Z5 J
% Y2 ^' c$ ]! u/ t8 A) a7 q // This is a simple schedule, with only one action that is, T& S. a* p5 ?) y
// just repeated every time. See jmousetrap for more3 [- Q3 C8 Z8 z+ L: r
// complicated schedules.+ R5 v# ^' m' U" `6 b- O1 G& c M
# ^/ Z0 g. [0 @6 g: b* o2 s
modelSchedule = new ScheduleImpl (getZone (), 1);
) ?& h% S: X' q7 D# A- \6 q7 ^; T modelSchedule.at$createAction (0, modelActions);
! x0 x( n. H1 l* R# R4 Y
7 Y$ j* ?0 ^1 A+ ~: I3 ] return this;
/ \. j ^* Y8 @( ]3 R" n } |