HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) ~: d2 w2 ~% n8 G& n; m
: q: o# M% v; V9 h
public Object buildActions () {
/ K) N6 |, ]* t$ L5 B0 _ super.buildActions();
4 `8 s0 B9 g8 I) _( a3 r% {9 T 6 y& c5 a5 ? j7 [) a/ V9 t
// Create the list of simulation actions. We put these in, Q) e7 Z1 U) w: ^) a* Q# a3 `! b- ]7 O
// an action group, because we want these actions to be
& T* r0 ^* r, K- }$ ] // executed in a specific order, but these steps should' H3 `2 S5 Y! M6 S. X4 S& @, _" B
// take no (simulated) time. The M(foo) means "The message
- F6 D1 j7 f: _ // called <foo>". You can send a message To a particular
0 [% x! }8 X$ B6 k# m; L // object, or ForEach object in a collection.* L- M7 a/ O( {) |& h- @7 N
$ n1 }! ~/ L6 F // Note we update the heatspace in two phases: first run
% M+ A, ~3 p1 u8 P3 z i // diffusion, then run "updateWorld" to actually enact the! u: x' O- l9 @! R5 i, m0 E B
// changes the heatbugs have made. The ordering here is
- ]) r6 e3 Q9 g( [# r" `" k // significant!- w1 V. T, z/ h/ ?* z
0 E% }" Y* q5 o: m. R" a/ a
// Note also, that with the additional+ z; L: h; l% s! v
// `randomizeHeatbugUpdateOrder' Boolean flag we can) i- R2 v+ ?% j: Q
// randomize the order in which the bugs actually run
5 z; C; \0 Y7 y: R4 v // their step rule. This has the effect of removing any
' u! I" ]% {7 B o* a // systematic bias in the iteration throught the heatbug2 @! l- A4 i: {: L |; W9 v P
// list from timestep to timestep
C" B- C! E* }/ B9 k+ V ( N+ S& g8 s6 d# X9 a
// By default, all `createActionForEach' modelActions have
b0 V% Z& m4 R4 P8 f$ S // a default order of `Sequential', which means that the3 k* s4 }8 p& S. Q( x/ p
// order of iteration through the `heatbugList' will be i, }) h9 q9 P' b$ ^) }' c
// identical (assuming the list order is not changed
: |, {7 i8 r" h7 j- t7 ?! e // indirectly by some other process).
1 A' O0 n: h7 P: ?7 g2 Z
" K2 d. x4 f9 {& G I) O0 g modelActions = new ActionGroupImpl (getZone ()); _0 P$ w" H6 \. v# k5 `
- w. g8 |4 S4 h; i. @ try {
' l( L8 F8 A/ ?+ B+ r/ ^ modelActions.createActionTo$message8 k3 r! N" _% n
(heat, new Selector (heat.getClass (), "stepRule", false));% Q; O" `, P1 ^# `. w( V
} catch (Exception e) {
- l" |6 i5 ~; P System.err.println ("Exception stepRule: " + e.getMessage ());
, h6 I C# r5 ]( O5 z8 h }
! C8 V1 u) Q- l7 a, L& C1 e. D& r* J7 t+ P+ g4 Y$ L6 K- p
try {% p' K$ k( _4 t
Heatbug proto = (Heatbug) heatbugList.get (0);
# u3 e. `% \- ?! K3 N Selector sel =
7 | _; K9 g" y3 L) s) L+ a new Selector (proto.getClass (), "heatbugStep", false);
9 v5 c' u: d, V) a actionForEach =
6 ]. w6 v# l: s( q modelActions.createFActionForEachHomogeneous$call5 d6 N: t) ?& a; g( K/ m* l
(heatbugList,
5 t7 K# H( h$ [4 M. j. N0 z new FCallImpl (this, proto, sel,4 F1 d( A# H/ Z! b$ c/ j
new FArgumentsImpl (this, sel)));2 o6 F1 G" |3 d+ l- u' A
} catch (Exception e) {; o) }; Y5 T2 f" o* Q: [# j& ?8 b
e.printStackTrace (System.err);0 A. n' n7 E# s
}6 I" ~: }8 P( e1 h
- h* C( I6 t9 h! d
syncUpdateOrder ();
3 [! ?& s9 l' r* p! u; b; R) c# a
try {' f3 j: p- S! Z* D# y6 q9 b0 y I
modelActions.createActionTo$message % w5 a3 x. q+ K8 ?$ B
(heat, new Selector (heat.getClass (), "updateLattice", false));1 d; [! m& \2 Y6 D4 x
} catch (Exception e) {
& N% y4 ~5 B8 \6 `! b System.err.println("Exception updateLattice: " + e.getMessage ());
$ }4 k; Z/ `2 n" {& W. G }
' Z. K) L: B- i0 L$ m. U+ }- i0 } 1 w3 K$ f+ ^( H4 h! w7 e
// Then we create a schedule that executes the
, N( ^- O$ y* T! L+ W& a // modelActions. modelActions is an ActionGroup, by itself it: C7 |2 z, s) c8 U
// has no notion of time. In order to have it executed in
; h' q6 e$ T& O7 U* G- v/ C // time, we create a Schedule that says to use the
/ k" S3 e& X7 ~: ~( j$ W+ N, O // modelActions ActionGroup at particular times. This
( y* B3 t/ I% `* y6 ?8 o // schedule has a repeat interval of 1, it will loop every8 }! s" k: ~5 t
// time step. The action is executed at time 0 relative to
2 O. m T2 P, L* [4 ^) C C1 X // the beginning of the loop.
/ C/ g1 `" G' K0 V. R8 y0 F# B5 w+ {/ t$ E5 k* Z
// This is a simple schedule, with only one action that is
* e+ ?! G, J4 E9 _. R" ]1 \0 G // just repeated every time. See jmousetrap for more
i5 J3 s+ R# l/ T& [; s" X# u // complicated schedules.
: m- Y# w, U, ^. l2 S
' I2 y$ P4 W! o modelSchedule = new ScheduleImpl (getZone (), 1);
% k \) }7 I% m; N; P modelSchedule.at$createAction (0, modelActions);
% \; `$ N9 F* B! \ 8 F) M% ?! p5 k8 q
return this;
+ S+ P* j" }4 ~! N+ w } |