HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# L }8 o! _, ]
3 j; A! W7 W+ ?+ b
public Object buildActions () {
9 e6 v3 V2 T0 S, v- t e; T super.buildActions();9 i3 i5 W& k. W# A
+ R$ j7 w8 }0 U S // Create the list of simulation actions. We put these in
- ?0 I$ D* x7 q6 l- ~# A // an action group, because we want these actions to be% d1 a. [7 b$ B2 e0 @+ [) Q2 P9 T$ [
// executed in a specific order, but these steps should% l' w# _+ ^( x8 e1 I4 [0 B
// take no (simulated) time. The M(foo) means "The message( B+ w6 M& [9 g
// called <foo>". You can send a message To a particular
8 q# j2 ~0 `! h% N% S* o- n$ M // object, or ForEach object in a collection.
( }1 S- L9 `0 `. K, C 1 L- t) z0 Z# j( ^( u- B
// Note we update the heatspace in two phases: first run
3 ]1 _* a8 \' l5 ] // diffusion, then run "updateWorld" to actually enact the
9 ]$ O) [2 J# U/ M6 A4 e% l5 w+ y // changes the heatbugs have made. The ordering here is
W0 p2 s8 @+ D- E. h- } // significant!; m& \7 P" g2 u8 G* u
, C. C6 N5 h$ D+ V0 W
// Note also, that with the additional" P# V! \1 }. E2 X; ^# w9 d
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; [4 I: |) m9 a7 r' F // randomize the order in which the bugs actually run9 \6 I, Z8 A: V* W/ [) [
// their step rule. This has the effect of removing any# r9 g3 P+ a3 ]9 O d
// systematic bias in the iteration throught the heatbug
% s) K) I( ?( }% u7 g- _5 l // list from timestep to timestep( P+ H! {5 z/ Q* y7 _; }
- t/ _5 r. \+ a; x. ^
// By default, all `createActionForEach' modelActions have6 v# O* H& y' t, ^
// a default order of `Sequential', which means that the
+ N2 x0 ^, X% I // order of iteration through the `heatbugList' will be( r q9 D1 x. |: T( \ A
// identical (assuming the list order is not changed8 h7 Q# k+ S' e" {5 v: N8 s
// indirectly by some other process).
3 r) `, ~9 F5 W 3 f* F' l' l% k( x
modelActions = new ActionGroupImpl (getZone ());4 E: C. U8 | F; u2 l
- }6 w$ Z0 {/ }5 S. R
try {
) K( e! |7 f7 p modelActions.createActionTo$message7 E+ G) Y/ y2 C+ Q
(heat, new Selector (heat.getClass (), "stepRule", false));8 u' D2 C2 ^0 Q" U$ [$ }( o+ A
} catch (Exception e) {/ Z/ H2 N! x) w
System.err.println ("Exception stepRule: " + e.getMessage ());5 O& k& {' A; m4 E4 z+ U) O
}
' X0 ?7 ~: Y5 l9 e9 a Y4 P$ k, W' W; f# o5 Q+ D. o; w
try {
' K& g+ q# b* A# n4 v Heatbug proto = (Heatbug) heatbugList.get (0);
1 K# t7 Q+ Y! \ _ Selector sel = ) L9 @% G1 s5 t/ i: e
new Selector (proto.getClass (), "heatbugStep", false);7 D- Y! M; _- q+ N4 e
actionForEach =
: S% q7 u1 T3 U) P# j" N modelActions.createFActionForEachHomogeneous$call
5 [$ M, i9 j; Y! N0 r (heatbugList, k. f' R2 ~9 d; y7 z+ S" \
new FCallImpl (this, proto, sel,
( t; d: D R. M6 k' O$ R new FArgumentsImpl (this, sel))); U7 Y& U2 L# N/ \1 J( t6 V
} catch (Exception e) {$ Q( S8 ?3 {( ?$ d
e.printStackTrace (System.err);
, ^4 @* {+ v, `" @* ` }
+ K) D6 e* H" M& K' I p7 o8 ~
: W. K& N$ l1 K4 `2 k2 L0 t9 C syncUpdateOrder ();
5 v; M5 d; H0 p3 J/ G: |% C; R8 [9 o, A/ W0 B$ t
try {
4 _1 N. B; a+ `6 v modelActions.createActionTo$message
+ h7 O: C% l, X( a( T (heat, new Selector (heat.getClass (), "updateLattice", false));
) e! _8 C! b( p9 L } catch (Exception e) {2 Y5 C/ C: p6 `+ }1 F
System.err.println("Exception updateLattice: " + e.getMessage ());2 |0 u, h' @1 O+ w4 ~7 w9 n; c
}
2 k! L0 `. Q& K" {; G/ G& m 8 n c. y$ ^# ^6 I: Y
// Then we create a schedule that executes the
7 J6 y5 T. V; I( b // modelActions. modelActions is an ActionGroup, by itself it
* h. H9 i2 Y% D* N) p // has no notion of time. In order to have it executed in
- |; a, D, L; D9 I // time, we create a Schedule that says to use the. q6 T' F2 w* d) {" Z
// modelActions ActionGroup at particular times. This
1 L7 G- |2 B+ J/ f" ?* t // schedule has a repeat interval of 1, it will loop every. }) f0 G, E( a& w9 \
// time step. The action is executed at time 0 relative to% j. X" I* U; j4 Q3 s
// the beginning of the loop.
6 R2 v; ` V u) }1 [# N, |+ g3 k1 F0 p1 ?9 W; I
// This is a simple schedule, with only one action that is9 m' n( ?! X1 ?' N# N$ q+ E
// just repeated every time. See jmousetrap for more
$ r/ g/ Y# ]! T$ c; i2 J: T // complicated schedules.
( {* x( p) w$ R8 P
3 a# b! j- n* f: z0 k6 M modelSchedule = new ScheduleImpl (getZone (), 1); @ K: w8 q8 Q( o7 H
modelSchedule.at$createAction (0, modelActions);3 y1 H# K3 A. P
& X$ O2 p; B* j7 }5 [: v1 g return this;. y9 Y& d9 c6 ~9 c6 Z, C
} |