HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) G. S1 M# m# D/ R2 `& m- O, N/ u) ]8 f( }7 M
public Object buildActions () {
5 C( M j P) W6 p g9 i- i super.buildActions();+ j: g8 f( W% G B
% l6 E& S4 Z7 y" T4 O
// Create the list of simulation actions. We put these in" \( c8 c* y; x* J
// an action group, because we want these actions to be+ W m4 q" L" {" T8 [5 ]; v$ M' l: U
// executed in a specific order, but these steps should% ]& F1 C2 M9 z: v$ S$ Z
// take no (simulated) time. The M(foo) means "The message
h9 O. ?5 F; G: `/ h( c Z // called <foo>". You can send a message To a particular; r% n- X" c |& s/ E& f
// object, or ForEach object in a collection.- o8 l1 R' s0 m/ F" e
% \, B" h; {! o& r N // Note we update the heatspace in two phases: first run2 p% r/ c8 D" ?/ S
// diffusion, then run "updateWorld" to actually enact the( |* U4 } Z0 Q+ m7 u) L7 E5 B0 ]
// changes the heatbugs have made. The ordering here is- M4 U3 B; U6 ]5 c* k% _
// significant!* i2 S# `# W' ^
( O% u) \& K$ }8 J! T/ q; O" j // Note also, that with the additional+ o* G x1 h9 P. z! \. }
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ ^+ |' ]3 h& Q' W: V // randomize the order in which the bugs actually run( ?# C1 `, r+ h8 }
// their step rule. This has the effect of removing any
: m, A+ Z. D: ~, Y' A' g6 c( K // systematic bias in the iteration throught the heatbug
2 X* Y% U( R. U& L( p0 e8 t' Q3 E4 V+ r // list from timestep to timestep
! | L4 q) A% g3 ` 5 W2 N( V5 A w
// By default, all `createActionForEach' modelActions have
4 J* C& B! }/ A- H // a default order of `Sequential', which means that the' W; D" q; }4 p% _8 o& Z; `% D* X( d
// order of iteration through the `heatbugList' will be* m5 ]! r9 e: R
// identical (assuming the list order is not changed
* G- {$ W9 D7 n0 m! h( P+ Z // indirectly by some other process).% g4 H1 a" H9 ~( U& G
: I/ F; ^6 m% X, n7 A; Z modelActions = new ActionGroupImpl (getZone ());
+ s! g. E) i, x, r' D
8 R: |6 M5 _! A try {
* B# D% Y- h! k9 q! z: T modelActions.createActionTo$message
0 z: C8 l! ?# N3 }, X (heat, new Selector (heat.getClass (), "stepRule", false));# F3 w. D0 K1 g8 m3 I* c1 A3 \
} catch (Exception e) {. O- e; y; |% u- x6 O8 r
System.err.println ("Exception stepRule: " + e.getMessage ());2 e2 @" V. m6 Y) x+ W& ?
}0 Q7 d8 [8 \1 H
' c8 v* U x. ^/ v2 ~. K try {
( d' E- F( P0 A, K2 ^: g Heatbug proto = (Heatbug) heatbugList.get (0);
; F$ r* H1 I1 W' n% o( l Selector sel = 0 c$ f+ p# H* }2 U# ^7 ]$ N
new Selector (proto.getClass (), "heatbugStep", false);
, @, B$ C- s% y- h" { actionForEach =7 s" ]/ D( \, E8 n
modelActions.createFActionForEachHomogeneous$call
( W5 ~9 U% l4 C0 O/ p2 ~' g) ~ (heatbugList,* F# N8 U' Y! N; f1 C Q
new FCallImpl (this, proto, sel,$ q+ U5 e0 I3 A6 t
new FArgumentsImpl (this, sel)));
0 ^3 L0 `6 d `5 B" M K7 E* T" e } catch (Exception e) {6 D$ k/ J2 C0 E* z
e.printStackTrace (System.err);
% H) B, \# _! p3 n* W8 v# P, I }* ~( K# D% P) W* L/ f- G: m
9 m; l9 Z' x: P8 v" I+ T9 X3 N5 k( R l
syncUpdateOrder ();2 T- [$ F5 l! k$ b4 c/ q
0 S6 @. l* ?1 r3 O" M try {
% F4 I# }0 G/ h" H( c0 t modelActions.createActionTo$message 1 H+ `+ L' P2 ?
(heat, new Selector (heat.getClass (), "updateLattice", false));6 L* D& E' b3 v
} catch (Exception e) {
# M& L" y6 P% B$ h. B System.err.println("Exception updateLattice: " + e.getMessage ());( w4 v/ h7 Z7 {# {; J5 L
}
0 k8 w# t/ v2 t5 x9 Y& v; |
) i; p2 m8 a8 V0 j( \( E // Then we create a schedule that executes the6 g: u" Q8 G) ?+ T! x* ]
// modelActions. modelActions is an ActionGroup, by itself it- v# q0 R. ^ q5 [ @! R Q/ g( p1 D' `
// has no notion of time. In order to have it executed in6 ^: s; g: G& f- |6 L3 K
// time, we create a Schedule that says to use the$ D$ b6 E! ^9 \& t- `. x
// modelActions ActionGroup at particular times. This
/ e% u0 @1 u( r' E2 n s // schedule has a repeat interval of 1, it will loop every* ? P6 A- _ N8 i- ^
// time step. The action is executed at time 0 relative to9 ]/ \8 g- M0 r) M0 e/ q
// the beginning of the loop.
% ]( E4 f6 \! k; z
" _$ Z6 w/ O6 w // This is a simple schedule, with only one action that is
; b2 h/ Z, ^+ z2 F // just repeated every time. See jmousetrap for more
: {( ]- @) u% p$ e* I& \ // complicated schedules.% j j( ~; b( w# J1 V g
2 @, F# I9 F6 ~" p2 K5 S7 i; Y modelSchedule = new ScheduleImpl (getZone (), 1);
1 e6 U# B2 W! F/ i' i modelSchedule.at$createAction (0, modelActions);) Z% ~0 G7 B/ a5 G; `/ p+ s& F8 D' Q
3 L. w* R& n, A* @/ ~- b
return this;
$ o% Z0 A3 D; j } |