HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& B3 z; d2 G$ K% [" C' S9 n6 p6 [" v* w3 x: L
public Object buildActions () {
- C/ p6 {0 `7 j% I' \7 @; m super.buildActions();& L; \8 D. J0 _- Q4 T5 x
& n- a( ~0 L# E% }
// Create the list of simulation actions. We put these in
( G7 }% f- V4 y9 R+ {4 y9 z1 R# H // an action group, because we want these actions to be4 d0 t5 c! D) s+ X) O
// executed in a specific order, but these steps should
% F1 N- @" \( ~6 \' f5 ^0 p1 B // take no (simulated) time. The M(foo) means "The message( Y+ v: t# o/ {0 U
// called <foo>". You can send a message To a particular `5 w) ^5 G# |* b, ?1 `, x% o
// object, or ForEach object in a collection.' H$ i1 z# Z3 b/ j3 N! \3 ?. |
' Z5 f! A* c( c% M2 W( R3 q) X
// Note we update the heatspace in two phases: first run
! p* _. F6 ?* O# U. [ // diffusion, then run "updateWorld" to actually enact the
. U( U* ]3 L3 Z- O. a/ u9 j/ W // changes the heatbugs have made. The ordering here is! A, h) {5 o5 d. G$ ^
// significant!
+ O; w3 }8 a1 F# {$ O; T! x8 F
, A0 r: d& D% Q1 } // Note also, that with the additional
2 _) ], k' j% h1 R // `randomizeHeatbugUpdateOrder' Boolean flag we can
& j' M, C+ c1 _$ T" d8 c // randomize the order in which the bugs actually run
/ y$ o0 t+ F' K: G# o // their step rule. This has the effect of removing any
4 A I: N- i. ^9 W N- J& S$ W // systematic bias in the iteration throught the heatbug
. o# p& \0 J" R, i2 x8 G2 ? // list from timestep to timestep4 }+ l6 G U/ |
- i! J; N- Q( X
// By default, all `createActionForEach' modelActions have2 J. J/ q7 @4 B: n1 j
// a default order of `Sequential', which means that the/ t# W0 J( A! S& i$ l6 j Q6 [
// order of iteration through the `heatbugList' will be8 z, w& j+ A# `
// identical (assuming the list order is not changed
! {4 ~$ l5 `3 Q1 E- Y // indirectly by some other process).
; \: {; s: O ?
|. O/ B9 Q1 Q1 M; q1 h modelActions = new ActionGroupImpl (getZone ());
5 ] i# D5 u Y1 b8 J9 t; a1 I# N3 |5 Y$ [1 I
try {9 u9 e4 C9 O1 y6 ?) U
modelActions.createActionTo$message
; L- M& i$ M7 h (heat, new Selector (heat.getClass (), "stepRule", false));
: \/ d4 x# f: @6 V" T' h } catch (Exception e) {
7 R6 j1 R/ Y* x, w System.err.println ("Exception stepRule: " + e.getMessage ());
6 {* z6 i/ @/ A3 g4 ? }
! a& _/ }+ a" S* H% Y0 x1 \% ^' F! p& e6 @0 ?# ` M$ Y
try {
9 d7 ~/ r; P* V: B0 _ Heatbug proto = (Heatbug) heatbugList.get (0);! x7 Z' l2 T# T' e, x) I
Selector sel = & q* ^$ K& ]6 |. K' P$ `* A
new Selector (proto.getClass (), "heatbugStep", false);
. e6 o2 b6 s- Q: o Z# a& l3 j actionForEach =
% Q8 Y+ p# H: k modelActions.createFActionForEachHomogeneous$call
0 S& G9 m! C2 Q |: j, Z (heatbugList,) J' O3 ]7 r7 G2 A$ ^
new FCallImpl (this, proto, sel,1 V" l: [. { y% Y1 U! z0 a
new FArgumentsImpl (this, sel)));
* D: Y0 f T# Q) l) f9 X2 o. c } catch (Exception e) {
8 \- u; E$ v9 _; x e.printStackTrace (System.err);
$ x1 a* B6 T2 R1 A9 e+ F5 Z }
0 X2 \2 ~7 E& t9 c( P6 x
: }" G) s1 F* J. t9 c O7 P2 ~; ] syncUpdateOrder ();
' _& u5 y% `* K" Z u( t* G: s Y: B- |7 K
try {7 a5 u7 I$ q# L* D
modelActions.createActionTo$message . J. \# ^0 |/ ?, q% g
(heat, new Selector (heat.getClass (), "updateLattice", false));
- e6 t1 m: ^' g& i' l' c } catch (Exception e) {
. W- J7 X3 N0 X) O2 j. j System.err.println("Exception updateLattice: " + e.getMessage ());
' o/ t6 V6 J3 c( Q }9 j+ E4 [: K: h: R/ F$ @1 e1 u/ F+ ]
, Q+ j5 K0 ?% N! x& f // Then we create a schedule that executes the
& w8 U$ p) c% p$ g& j8 k' b // modelActions. modelActions is an ActionGroup, by itself it
2 C6 v: h& `' _ // has no notion of time. In order to have it executed in1 W3 f: ^" Q- f7 T/ C5 P
// time, we create a Schedule that says to use the% @4 ~# ^! \; _' y2 \5 Y1 n& K4 q
// modelActions ActionGroup at particular times. This1 Q- B- H5 Q3 r Q# Q7 P# _
// schedule has a repeat interval of 1, it will loop every8 v5 s0 G: F/ O" v
// time step. The action is executed at time 0 relative to
; L1 z6 m& J" F- H // the beginning of the loop.
1 ?% J5 r M1 y( k8 L( t; O7 d+ F% _, F( z" n7 c/ X5 B
// This is a simple schedule, with only one action that is7 h6 {: u! e# U" a8 T& K! r9 \# e
// just repeated every time. See jmousetrap for more& W) l' y7 x2 ]3 ?
// complicated schedules.
5 v* b0 ?& \6 K
0 Q; O* U' Q, W modelSchedule = new ScheduleImpl (getZone (), 1);
4 \, v- P, [6 Q9 t' O modelSchedule.at$createAction (0, modelActions);# s& K4 ]+ y% b( m2 x
: ~* ]2 }, M9 _+ q6 l6 [; g5 ` d return this;
5 v5 g* I" U/ L( o } |