HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ _! d& t6 y/ E
: d- V7 n# g0 E- L9 Q$ h public Object buildActions () {' F4 w; R R1 g2 a" W! q
super.buildActions();
, B5 `. R2 w: D
* L) f1 G0 d' Q: o1 j) W: ] // Create the list of simulation actions. We put these in8 z' ]7 u: R+ V
// an action group, because we want these actions to be
}( B+ v# C3 ` // executed in a specific order, but these steps should V. J$ K( g4 s+ Q9 _( ]6 @
// take no (simulated) time. The M(foo) means "The message
7 [: H8 |9 p! u) g0 O4 Z j6 L // called <foo>". You can send a message To a particular
" X5 |/ ]" F9 b8 k // object, or ForEach object in a collection.
' d# e( [) ]" |4 J }3 G
+ g z2 F5 F7 V) T // Note we update the heatspace in two phases: first run
: e7 v, C9 j4 C! ^- F // diffusion, then run "updateWorld" to actually enact the
8 D& @6 K$ }6 ?7 P5 h // changes the heatbugs have made. The ordering here is
( L! r( Y$ q S( |1 \+ r" h) r // significant!7 S7 Z( _" R- f6 z3 P ?) ^
2 x& q+ X: K# m1 l5 ]% w
// Note also, that with the additional) q' w3 M5 z! A' V6 ^! G
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ Q( A, @' q1 O! S( J' a- J; D // randomize the order in which the bugs actually run' M7 n6 }5 h) p9 g6 l% a/ ~
// their step rule. This has the effect of removing any
X* w2 ?0 @' x6 V; s- M9 [! n // systematic bias in the iteration throught the heatbug
% C/ k. M/ ~: k$ r // list from timestep to timestep
+ r, H% T0 @9 ]7 j) {4 @% r/ P
! l: e; a- A) C" _* y3 d7 h+ Q // By default, all `createActionForEach' modelActions have
6 p- e6 u" s- `( P: _' n // a default order of `Sequential', which means that the# ?6 d5 R ?- x A
// order of iteration through the `heatbugList' will be
: y- [0 f& {5 p+ Y: R // identical (assuming the list order is not changed4 q9 d1 \9 ?& N$ B
// indirectly by some other process).
! z4 ~) T; {1 o2 M0 r4 C
" ]: r0 D( t2 p* `2 _& D' s, w! S4 P modelActions = new ActionGroupImpl (getZone ());2 |1 Y9 @3 O7 x g
: g8 {3 I/ F2 n. f/ ^5 b2 ^' u9 c try {- i* \- [4 _; Z
modelActions.createActionTo$message+ D+ a5 Y) `! l
(heat, new Selector (heat.getClass (), "stepRule", false));
; o8 W4 Q/ k' T9 H } catch (Exception e) {
+ ]/ Y* j1 [& p/ I- A System.err.println ("Exception stepRule: " + e.getMessage ());% Y) T3 G" ]6 u+ {3 r- }! p- ?8 _
}
0 B! X' x" T8 F" D, Q* v2 k
9 {# O: i7 d4 Y% {* E% T try {
' f1 @, Y- A5 E% L Heatbug proto = (Heatbug) heatbugList.get (0);
6 u6 D3 K0 J" I0 T3 H5 R Selector sel = " G, l% F+ n# L# m1 E; y$ C
new Selector (proto.getClass (), "heatbugStep", false);9 J2 c. L7 s f: G x$ G) m0 k( w
actionForEach =* H; t$ y" j1 k$ b! _( o
modelActions.createFActionForEachHomogeneous$call
8 w9 I1 v' h M+ ]0 C) R (heatbugList,& m6 y& M# I3 N3 q
new FCallImpl (this, proto, sel, S' _- s: ?9 n
new FArgumentsImpl (this, sel)));
3 l& y" ?/ F/ A; N, f: X' [" i+ r } catch (Exception e) {
! u3 G# Z2 Z4 n. E3 D: B+ Q e.printStackTrace (System.err);
8 K6 }: L D6 A0 |( }& V( a }4 a5 D3 `7 {: r/ u" l8 c9 c2 L
6 ?+ P, ? p; I" N* W& M
syncUpdateOrder ();
& r0 b- Y# [) [1 d6 w. f
# p9 d( }3 o$ l/ d" E- F1 H try {2 x* X9 }9 {, U9 d
modelActions.createActionTo$message * y( ^% s7 V! h0 M
(heat, new Selector (heat.getClass (), "updateLattice", false));3 r: U7 s2 z1 s% g% @3 i) ~9 R
} catch (Exception e) {
5 V/ j* O+ X2 Z* u System.err.println("Exception updateLattice: " + e.getMessage ());
: j; l) Q* E" q0 o }
- S) A+ a1 S, E
3 J, \/ x) Q; ?% _+ O // Then we create a schedule that executes the
" \9 a. S" f! N3 d% z) } F% e // modelActions. modelActions is an ActionGroup, by itself it* s/ O' s' p7 e* y" `& Y7 ~
// has no notion of time. In order to have it executed in, S. I. k( m5 ^
// time, we create a Schedule that says to use the$ j2 w1 U7 R5 s
// modelActions ActionGroup at particular times. This
5 X" l. V) ~" j // schedule has a repeat interval of 1, it will loop every
' H( G) U' B' M8 u# w // time step. The action is executed at time 0 relative to% ^/ W% e9 m8 Y' s
// the beginning of the loop.
) H: M+ G+ A l1 e. Y) ]
8 ?0 Q9 R& g% I) f // This is a simple schedule, with only one action that is
4 r" l3 `0 l* r1 p8 B9 y1 B+ t Z // just repeated every time. See jmousetrap for more
6 S: O! z" a# T, E% H# q" s! u // complicated schedules.
! t" @" e, {8 I7 \) X ) Q+ r, Z) C# b; U
modelSchedule = new ScheduleImpl (getZone (), 1);
) `$ o. W6 H9 U8 k9 S" A modelSchedule.at$createAction (0, modelActions);1 w# |( ?9 {/ n( A4 _5 O
: B/ I3 M7 \+ {1 C return this;, o5 t, Y0 f" M8 S" W
} |