HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 H- Y) a: D* k/ U1 |& \
# K" n( o7 M6 P public Object buildActions () {
# }+ P* v- y0 y super.buildActions();6 g) o3 ?' x' ~: n7 m( x f( I
- P$ x2 m3 L. K! G
// Create the list of simulation actions. We put these in2 H+ J( L- ~6 ~1 s3 q7 Z$ X
// an action group, because we want these actions to be k8 z3 s; E: G4 A! X7 k
// executed in a specific order, but these steps should
* G! Y+ c' q$ A // take no (simulated) time. The M(foo) means "The message
6 k( i1 u/ M8 j4 A // called <foo>". You can send a message To a particular7 a( K P3 S- {9 Z1 f
// object, or ForEach object in a collection.( a1 {7 e+ h% P1 Z8 X9 M$ f
4 A5 m0 n6 y! ]* `/ l; G; |* j. N // Note we update the heatspace in two phases: first run! D6 N; C' F9 r. q8 L
// diffusion, then run "updateWorld" to actually enact the1 S/ i9 k$ {4 {: e6 J
// changes the heatbugs have made. The ordering here is5 f3 h* R2 H. I) c- ^
// significant!
8 B1 ]- J) ^5 X& s7 u2 f
9 F- i& I* B, A% J( d& u4 z // Note also, that with the additional
/ Y, K. N+ p. ^* Y* e/ O // `randomizeHeatbugUpdateOrder' Boolean flag we can
" J! ~ d3 i$ X) q, x/ w // randomize the order in which the bugs actually run1 W* X; [6 \9 K1 d8 r
// their step rule. This has the effect of removing any" F5 v& y* y2 ]' O i b9 l
// systematic bias in the iteration throught the heatbug, }( b7 A3 [6 _8 z6 X
// list from timestep to timestep/ l9 z) }" C& B- a/ |* u( e
2 ]8 N9 J: a( a // By default, all `createActionForEach' modelActions have2 L% @: }+ q) `! ^
// a default order of `Sequential', which means that the1 w- z/ s/ R/ o" i
// order of iteration through the `heatbugList' will be ^6 d# x$ E1 b- ?' s7 A
// identical (assuming the list order is not changed( T" s: w# K3 n) X9 F
// indirectly by some other process).
. B0 {2 `+ K: a! H# F$ Q7 x! ?: D 1 s2 k9 Y- O7 c8 N/ w3 }
modelActions = new ActionGroupImpl (getZone ());
" [$ v' J/ X3 Y7 a
$ U- b, O# j: m4 I/ Z try {
- P) j. H: V/ [0 y# C# H5 A modelActions.createActionTo$message) I" b- P* j8 V" S! V& c3 b
(heat, new Selector (heat.getClass (), "stepRule", false));
% G9 r4 ~3 {- i1 w8 a } catch (Exception e) {
: P) }+ l6 [; U/ f System.err.println ("Exception stepRule: " + e.getMessage ());8 w w7 R2 X+ T, C% W2 A" N# }. r
}; B! z1 k* _8 g7 }3 v' c8 |+ Y2 g9 N
& f1 e- c$ F5 a; O( P" w
try {
& ~5 K9 {( u! T' c$ `" R7 [: r9 j Heatbug proto = (Heatbug) heatbugList.get (0);
8 v2 {$ j" r' M; Y+ J9 L$ M- X Selector sel = 6 r( h5 m# U" F$ g1 c
new Selector (proto.getClass (), "heatbugStep", false);
# g4 ?2 ~" n- @7 P5 G9 h; _ actionForEach =
3 ^" `2 s/ s+ w1 |. h modelActions.createFActionForEachHomogeneous$call2 m2 q+ E, G5 j- [/ n$ u+ ^
(heatbugList,
/ E8 l0 q( @. N: v0 k/ F9 Y( Q9 K new FCallImpl (this, proto, sel,
, Z. W. B2 n$ N' E5 z new FArgumentsImpl (this, sel)));
$ o5 e5 c! s, j4 }2 h, V } catch (Exception e) {3 d) p" Y4 u- f! p! a1 u
e.printStackTrace (System.err);1 Q' A# R$ ^ q
}, ~0 k3 ~/ u# p5 e% |# W
+ l, o) K4 }% S
syncUpdateOrder ();$ g$ ?$ P1 O4 ~, ^2 `4 G
+ Y" t+ h# {7 }' ?5 {/ C try {. p7 i9 Z) d! [: B/ X
modelActions.createActionTo$message
4 j7 S7 n& e, G `% q8 J4 ^! J y (heat, new Selector (heat.getClass (), "updateLattice", false));2 T- u& }1 |( i3 S1 v7 z
} catch (Exception e) {
! b" t; Z" r! ~6 J System.err.println("Exception updateLattice: " + e.getMessage ());( f9 \+ f. y ^ R! t4 D) \: t
}
: _8 u+ S) e, u1 y6 p" ~* ? : X5 \& Q0 Y+ M
// Then we create a schedule that executes the1 A) }( ~( v# ^# n* D3 X8 T
// modelActions. modelActions is an ActionGroup, by itself it
9 e( c/ A9 W/ F" A* a // has no notion of time. In order to have it executed in3 d) |6 @7 b* a/ r
// time, we create a Schedule that says to use the: }* S# v" o; R& J6 b
// modelActions ActionGroup at particular times. This; I( ]0 a& [% l' A, d2 f ~
// schedule has a repeat interval of 1, it will loop every
) D# w9 h" v5 ?, T* {2 k // time step. The action is executed at time 0 relative to4 l" @! f8 q$ W, H0 [3 `
// the beginning of the loop.
% H, T F% ~2 `' d- @' Y; [6 O8 k, }5 b
// This is a simple schedule, with only one action that is0 C' I4 u6 x8 ?3 v- k
// just repeated every time. See jmousetrap for more$ n) f+ C4 f( Q0 W N8 o
// complicated schedules.2 C: l2 d6 W+ K
4 O$ F" X- o4 Z modelSchedule = new ScheduleImpl (getZone (), 1);) N! E0 w9 y$ l1 j7 A* {" @6 X
modelSchedule.at$createAction (0, modelActions);* S6 F. X8 Q2 m2 @
! o& g0 Z& i4 h
return this;
- W0 s& i5 T$ W+ n% i } |