HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 U* W- d- c) j7 J( w0 P
, H3 ~. e. w4 p# l* ^8 k public Object buildActions () {
2 f* J" p! }9 D' U5 n super.buildActions();- A8 W. r1 F' D/ o$ R$ L
6 b" d% t3 L0 Z# R$ w) `$ t, b" X // Create the list of simulation actions. We put these in
. V. x8 W% i# b# H // an action group, because we want these actions to be
1 j, Z' B7 [% @' m# U3 O7 Y0 Y // executed in a specific order, but these steps should
, m0 N3 {! E' S; l7 { // take no (simulated) time. The M(foo) means "The message
7 X( d* {: s& x1 Q0 m5 j2 X // called <foo>". You can send a message To a particular8 S; c( G2 I' S5 |* ^) }
// object, or ForEach object in a collection.9 o9 b& Y& d- ?/ j4 }
?2 R; O$ W F% U, O9 y0 H* [ // Note we update the heatspace in two phases: first run+ H% O5 }6 p7 _0 |! [6 Z, y- m
// diffusion, then run "updateWorld" to actually enact the
' m E6 R& l& n3 r; E5 J // changes the heatbugs have made. The ordering here is* S+ f7 ~ V) p$ L. F7 h& s; n$ I( g! Q
// significant!
1 h- g: V. b0 _2 @& _! X ; H6 ` U/ @& F/ ~' g! Q/ o4 l5 X) b$ J
// Note also, that with the additional
. t$ ^' n8 u, D" _4 r: H // `randomizeHeatbugUpdateOrder' Boolean flag we can
; ~, I( }- A- A // randomize the order in which the bugs actually run
* r6 Q- Z$ _% T% h- _) \" J$ w // their step rule. This has the effect of removing any
& i! V, h0 Y9 h' h) Z& c9 k // systematic bias in the iteration throught the heatbug3 y, W" z5 k7 u% K D* T
// list from timestep to timestep$ {9 X7 v9 R o3 |9 K# |$ r
1 X; w2 a1 G. U% Y
// By default, all `createActionForEach' modelActions have
4 \1 o [/ ]) C7 |7 \ // a default order of `Sequential', which means that the
- ~3 u% ~, X, a // order of iteration through the `heatbugList' will be
$ f. z$ C! Q6 u9 Q( U // identical (assuming the list order is not changed
; e! K* T5 J' S& e // indirectly by some other process).7 e* s% b# b+ o% K( W: J6 W9 Q6 T+ L
4 T1 `+ W4 h* R& n
modelActions = new ActionGroupImpl (getZone ());
6 J) F( z- f1 T7 C* v! y# l1 ~& J& }0 h, O4 t& J$ M, r% C; O
try {
* H6 C% t6 V' B' l modelActions.createActionTo$message
7 T& ]) i' ~( N) `4 f8 `! ]+ D (heat, new Selector (heat.getClass (), "stepRule", false));. I$ R8 u Q) ^7 q" D& B3 x
} catch (Exception e) {& r5 e" b( f" ^$ |( }4 J
System.err.println ("Exception stepRule: " + e.getMessage ());7 J1 F2 Q- m% u5 I% u
}
! X* n2 o+ S5 y5 D5 E( w6 I3 X; T% H% R
try {' ?7 O3 o7 q5 [% ], m5 T
Heatbug proto = (Heatbug) heatbugList.get (0);
0 | }& T4 P1 W: V' \7 M Selector sel =
# X% B) a% H+ z+ p( q3 z new Selector (proto.getClass (), "heatbugStep", false);
- o. I$ w" D, D( S) u. l) B actionForEach =
7 [/ K9 n3 f" W# _8 v7 {/ o3 | modelActions.createFActionForEachHomogeneous$call
. L1 X% c' ]% ]0 |2 O (heatbugList,
: j, C9 o" A+ q, |- H7 ~ new FCallImpl (this, proto, sel,3 s L- | w4 h% b' J
new FArgumentsImpl (this, sel)));
, Q2 {4 E5 B/ i8 k) ? } catch (Exception e) {
) z3 q9 J0 m" K' S e.printStackTrace (System.err);
2 ?& m, k) k/ S; x$ n) s% i }
+ V7 E3 y2 E& D+ o3 `4 C
- D7 B7 o$ z! y5 m& s8 x) S% ?3 B syncUpdateOrder ();
1 }, {8 x4 S9 v# s m& \. c
- I" Y: O( x7 x) H/ ^ try {
: R4 c" [7 w" N# K1 O6 u9 K5 j7 w6 n6 G modelActions.createActionTo$message
9 P/ {. j* N; R+ ]: E( z (heat, new Selector (heat.getClass (), "updateLattice", false));# E' T. k3 }" J: t8 ~8 R: V) D8 ~
} catch (Exception e) {
# b5 x. t* ?" U6 g4 e# O, Q System.err.println("Exception updateLattice: " + e.getMessage ());
" i' \: c- }1 J+ @2 E! K% [ I }
5 }. U) k4 @: T1 H8 |& f9 b 6 B! W* p, Y& ` s& t
// Then we create a schedule that executes the9 D& E7 z7 _1 K
// modelActions. modelActions is an ActionGroup, by itself it* {8 t4 ], ?: @, G
// has no notion of time. In order to have it executed in7 } w" Y/ p3 d9 k
// time, we create a Schedule that says to use the
: `0 ~5 ?4 N- H) G% K& N // modelActions ActionGroup at particular times. This" Q6 ~. g* W, \7 ~: {3 X
// schedule has a repeat interval of 1, it will loop every( B4 E0 f2 c% }6 ~, ]; p
// time step. The action is executed at time 0 relative to7 Z2 P7 A% a1 K0 ?" J* ~
// the beginning of the loop.
! V5 l: E3 U7 ?9 {/ P* g3 b( ]. b" }1 z7 \- J" `( \4 _
// This is a simple schedule, with only one action that is
1 q0 w2 e s) Y3 c2 s& W6 K! P // just repeated every time. See jmousetrap for more
: s& n# v3 e/ `! K& d A3 a0 g) S // complicated schedules.
! k* s5 H9 O' n
; e, J- L8 j* ^9 ?# F modelSchedule = new ScheduleImpl (getZone (), 1);
- t3 Q* {/ a& N n5 O modelSchedule.at$createAction (0, modelActions);# g3 N8 L- |# a
( m* H W8 ~: g: _# x return this;3 j5 I2 p1 ]; E b& b: B
} |