HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: R2 |8 ~# h1 q
# v" p; `6 w3 n* ?2 i8 L" } public Object buildActions () {
" n6 V9 t& ^2 Q super.buildActions();( [8 N2 @$ s$ N) @$ N4 v. P
/ ]) L/ Y; C* c- B# V; ]( T
// Create the list of simulation actions. We put these in
+ b& E' j& H2 ]- D0 F: f2 c // an action group, because we want these actions to be' I& T, W. K3 @- F! W/ Y
// executed in a specific order, but these steps should# d. _) J2 @& k% U1 ?7 ]) q
// take no (simulated) time. The M(foo) means "The message- ~' R7 l7 |1 Y4 j
// called <foo>". You can send a message To a particular
E; V6 J# A- K3 k" ~. C // object, or ForEach object in a collection.
. x: a; ~1 @6 L! G " U C! R0 S2 ?) T
// Note we update the heatspace in two phases: first run
* _/ l$ a1 v( z2 F. m // diffusion, then run "updateWorld" to actually enact the& H# ~- |* T, ?8 O# R
// changes the heatbugs have made. The ordering here is! f/ K' w* B+ P( Y! I
// significant!5 d0 \4 K7 P3 b2 _7 ? \9 x
1 G; O- J4 k0 C7 I
// Note also, that with the additional: i1 C. C0 F3 t& F' U" Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 V: n. b1 k6 ?" E' ], F
// randomize the order in which the bugs actually run
/ d# @6 c8 l6 Y- i. l# B // their step rule. This has the effect of removing any
& O9 `0 v1 l& S7 ~$ k. B9 y // systematic bias in the iteration throught the heatbug ^0 \5 \& {7 Z1 J6 v
// list from timestep to timestep
3 @. F/ o Z* r! c* y" M: X 5 A0 K, |; q. ~( R
// By default, all `createActionForEach' modelActions have
9 D, m# J+ p9 S' H // a default order of `Sequential', which means that the+ _/ \$ b; O+ q
// order of iteration through the `heatbugList' will be& w* c$ N. i5 f
// identical (assuming the list order is not changed3 {) B5 T& J B y- |- w2 a) S
// indirectly by some other process).
' x, d* |9 y+ P$ S) p / @, R, b' P7 t5 P
modelActions = new ActionGroupImpl (getZone ());
7 n- M8 ^: Y/ k" p; \7 W) i5 n, T8 |% [* h7 R7 X& V" V
try {
, d+ ?5 {& |% M7 ~/ b; ?: ^! n5 ` modelActions.createActionTo$message* M& l0 v5 X/ y! g4 M7 f+ G
(heat, new Selector (heat.getClass (), "stepRule", false));: l9 p. @6 H+ Y& i) l. |. \/ T
} catch (Exception e) {
9 A- d3 }: B% ~ c System.err.println ("Exception stepRule: " + e.getMessage ());' m% V+ [( _$ F( p' I/ g
}/ ]8 S8 I" r" ]# B
3 w9 V" T( S* n! t- C. w try {
4 i2 F8 o) W* Q {2 `, T0 ?; M Heatbug proto = (Heatbug) heatbugList.get (0);0 J; ~0 u( e7 k& i* F1 O
Selector sel = ( x7 ? N. h5 G: K/ E* B
new Selector (proto.getClass (), "heatbugStep", false);
# K( W9 E5 `+ w- `2 H actionForEach =8 H2 H, [& m7 d" z' M8 I, `/ s" J- a
modelActions.createFActionForEachHomogeneous$call
1 B# O1 q0 f. e (heatbugList,
& i2 m: P" w1 d2 ^% }7 ]) E new FCallImpl (this, proto, sel,
X, ^) G! O6 d new FArgumentsImpl (this, sel)));
/ \, B B. a5 | } catch (Exception e) {1 W3 L+ ^. q) N
e.printStackTrace (System.err);1 G! C& s- V# [6 G. W9 {+ S
}% L2 B, L; T+ x
% T3 q/ R3 b# V, V) j* ^ syncUpdateOrder ();, z& u( T, e) X3 n% v' n
7 z5 ~* F% o5 ?, P& x6 h, ?. B; c try {. U, L, u2 m. d% V' j N& q
modelActions.createActionTo$message
. u8 v9 s _3 s. v' m+ Q) h0 s (heat, new Selector (heat.getClass (), "updateLattice", false));5 b/ t+ M( |+ P: a
} catch (Exception e) {$ w5 k- E; G& A/ Z
System.err.println("Exception updateLattice: " + e.getMessage ());
9 C$ i- w( g6 R- d9 { S }" N6 d+ Y. X( ], k: j# _
3 I6 v3 \. H6 S; K, y // Then we create a schedule that executes the
' a" [" G; f2 Q/ G/ w; N // modelActions. modelActions is an ActionGroup, by itself it9 m, l& d3 |2 d: ]
// has no notion of time. In order to have it executed in
0 [ D% y( b$ g // time, we create a Schedule that says to use the! G, u, _4 }3 r l& b" B0 F Z
// modelActions ActionGroup at particular times. This
/ ~1 z7 R. [/ t0 @! W // schedule has a repeat interval of 1, it will loop every
; ?7 a3 g0 B9 I1 [- n // time step. The action is executed at time 0 relative to
' `* g) i5 @" t- W' X- J // the beginning of the loop.
% _0 M) ]. g6 p2 i9 u
* \) g# t! O3 X, _; C- t // This is a simple schedule, with only one action that is3 `$ w. e+ B% W" t, G! i+ F
// just repeated every time. See jmousetrap for more
0 @- M* H) E0 d3 D3 h1 y8 b" W- X( T/ d // complicated schedules.
. F) l8 n( V8 d& E# t% H$ R/ J
: X1 L; M2 E$ Q# u5 g$ K# \$ Q8 M modelSchedule = new ScheduleImpl (getZone (), 1);
( z+ L; q% }- S) A2 s2 ]4 \$ @ modelSchedule.at$createAction (0, modelActions);
6 I( R% x. b; F6 w ^5 d P H/ w / }5 i) S* O( n+ a0 d; g: Z( D
return this;2 [4 V- ^8 ]" r ~1 h! U+ [1 Q
} |