HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& b, `4 [0 z# E
5 h ?" J4 Z& r5 W0 E7 Z! ~( r
public Object buildActions () {2 g2 Y1 |8 J! C# z3 v6 U+ K
super.buildActions();
% T. m% x7 C0 E, }1 e; B
# z, b8 y2 P; ?5 G [ // Create the list of simulation actions. We put these in; x- B+ N2 h7 _, J7 p/ z
// an action group, because we want these actions to be
* `3 ~& g7 I; A6 k- [* G+ v( e // executed in a specific order, but these steps should D, P& u2 [1 O9 j+ Y
// take no (simulated) time. The M(foo) means "The message
2 F/ ^" n: }! p0 P T // called <foo>". You can send a message To a particular
4 o: [. q( K6 H9 j3 q5 K // object, or ForEach object in a collection.
$ G% y) B* t' O3 x# L' D; J4 [& `; ~
# }" T4 S( t U7 A0 f // Note we update the heatspace in two phases: first run% q6 |/ g0 l }2 ]5 L, `* b
// diffusion, then run "updateWorld" to actually enact the/ s) |6 U/ L1 j! [1 f% U
// changes the heatbugs have made. The ordering here is
x( u5 O7 Z8 h% Y8 c! _ // significant!% m$ Z# o i- @0 ?6 P# [
( w; f; B8 R3 ?' x5 h0 y. y( y0 }
// Note also, that with the additional
( a0 Y) W0 e$ e // `randomizeHeatbugUpdateOrder' Boolean flag we can0 Q9 ~: f, ~+ _; B4 a" l! }8 f( I
// randomize the order in which the bugs actually run
1 S) w& {, J+ \8 x1 G // their step rule. This has the effect of removing any
9 p' I8 P* W: B9 i* u // systematic bias in the iteration throught the heatbug8 u0 @/ `9 y1 h; S+ o; o
// list from timestep to timestep
5 ~: Q$ j+ l. E& s. ~, ~9 j 1 p5 Q. f. G4 W( t7 a" e5 a% |
// By default, all `createActionForEach' modelActions have! t) y! X+ x0 [# b
// a default order of `Sequential', which means that the
; W, u$ R7 J- B5 } // order of iteration through the `heatbugList' will be
2 s: s i6 x6 J: O7 G9 B5 \ // identical (assuming the list order is not changed
[+ d) d, n" @/ d& v // indirectly by some other process).
/ G6 [7 D( F( \: V, g! } ; T! D- Q7 b1 V+ T/ S: o
modelActions = new ActionGroupImpl (getZone ());
& H1 ~8 |- h7 Q( e r# L6 R; b/ N* j/ d
try {
' a9 V( u( c! r! x$ G! {" z' e! d modelActions.createActionTo$message% v( V, ]; a4 l1 }. k; l/ Y
(heat, new Selector (heat.getClass (), "stepRule", false));
. H( l, P+ F2 f2 t1 f0 u } catch (Exception e) {
5 c/ E; g# ` _, P1 o System.err.println ("Exception stepRule: " + e.getMessage ());
0 F# c" l O' r5 {% E }: r+ q4 |" e$ t# _* \: x
) H$ B, Z: J5 l+ C+ H try {
8 V+ V2 ?$ u8 H Heatbug proto = (Heatbug) heatbugList.get (0);) `) {! v/ N9 G$ B" E
Selector sel =
x, e# L3 W! H* [ new Selector (proto.getClass (), "heatbugStep", false);
, F1 C1 n8 A' T) T actionForEach =
+ ]/ t# u# F+ Y( H0 F modelActions.createFActionForEachHomogeneous$call) e8 X7 U3 w D4 N2 s, j
(heatbugList,
, }/ j3 [4 H' g B* o new FCallImpl (this, proto, sel,
# l# Z' r( u6 h. ` ]5 i% w7 U! \ new FArgumentsImpl (this, sel)));
! L/ @! h/ C2 ?4 q# l } catch (Exception e) {
& f" u- Y& k3 }' \$ [ e.printStackTrace (System.err);/ g/ o0 a# d& P/ `( Q: Z7 Y7 M: F
}
- ]' c+ K& K1 C6 ~! p
. Q; P, t `' c' O! q2 b% [ syncUpdateOrder ();8 N+ O c; Z$ W( v% Z6 M
1 | c7 {( R# B* ~' o try {
N* I+ Q$ `6 d7 D( \; P" w9 F modelActions.createActionTo$message
8 t( g& W. ~5 Y& e. l' w4 _ (heat, new Selector (heat.getClass (), "updateLattice", false));7 i; ]* l. m+ q1 s" l; t- D7 d
} catch (Exception e) {
/ N, m8 k6 v* _' p( D9 v8 w3 n System.err.println("Exception updateLattice: " + e.getMessage ());- B+ T/ `3 g0 @- W+ |
}
$ Z, s( K N6 T, R, j3 R1 o . t- {. Q& h( X; n5 J2 s
// Then we create a schedule that executes the
* I! E0 j$ ]4 `1 j% G) i // modelActions. modelActions is an ActionGroup, by itself it
# g& M4 m2 }: } N7 A // has no notion of time. In order to have it executed in
! l1 p: J. E! p$ }0 F. U n/ _ // time, we create a Schedule that says to use the3 Z: N4 a6 H2 }3 A! p8 ~/ o
// modelActions ActionGroup at particular times. This
! Z9 y7 d- w# f& K // schedule has a repeat interval of 1, it will loop every
- Q+ J6 B' n) e( ], d- g( M6 n. I // time step. The action is executed at time 0 relative to
! y3 {" l* u: _ // the beginning of the loop.
* s, s0 a. o; R% q
( J# I# k& t. t& m // This is a simple schedule, with only one action that is: F @9 r$ k" N
// just repeated every time. See jmousetrap for more
# }6 \$ r+ l1 C: L // complicated schedules.
9 v! k. S# A# r$ ? ( G, x! j+ `0 ~* x y: |% D: m
modelSchedule = new ScheduleImpl (getZone (), 1);
+ S' {2 }* J& G% r0 G2 ]& d modelSchedule.at$createAction (0, modelActions);
4 c! d ?1 U* A- ~# G
& n# ^1 d- E0 v( g return this;( }4 X4 U( X' ?; t% |& C8 D0 m" J. e" N
} |