HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! T, H6 @& C s+ D6 I1 t, W5 `( b# \5 r6 d( x* s0 E
public Object buildActions () {
/ a: d2 A; k2 p super.buildActions();" y% r1 ^/ C4 m+ f8 n/ r
9 [0 j# @2 x7 V+ w, h* ^ // Create the list of simulation actions. We put these in
1 h9 P6 t+ r* i. U( n6 S- Q // an action group, because we want these actions to be+ _( S2 L. c. u4 [7 i, s: k
// executed in a specific order, but these steps should2 _4 }+ V8 i6 _" b/ X$ f [; q
// take no (simulated) time. The M(foo) means "The message
: S, d$ s+ g- T# p& J( L // called <foo>". You can send a message To a particular
5 W* I6 [( j+ w+ h" E0 ~- W // object, or ForEach object in a collection.6 x7 L7 C+ ]1 h
' L: `1 o( x/ _, E4 I
// Note we update the heatspace in two phases: first run
2 A/ i* u8 E6 G3 N // diffusion, then run "updateWorld" to actually enact the
4 k3 \' v! U/ n9 N$ D5 Y // changes the heatbugs have made. The ordering here is
2 I( c. ?( V* T/ ?& r, Q# b // significant!
# X( U0 _2 `; M9 x9 `2 A1 U& S8 e& a
* R( I* f' P; R9 O // Note also, that with the additional
( b3 Z" G$ j& q% [ // `randomizeHeatbugUpdateOrder' Boolean flag we can9 A( u7 I: u/ e5 B% G
// randomize the order in which the bugs actually run
5 y0 d2 E9 J+ \# @- Y // their step rule. This has the effect of removing any: b |- M# E m, y. z$ i, L, C
// systematic bias in the iteration throught the heatbug
+ S2 H5 k9 W( l# a; u // list from timestep to timestep
5 T$ w& c1 z7 w
& t4 l7 G# g+ Y7 o1 [, }6 W // By default, all `createActionForEach' modelActions have
7 z& x2 e5 j% `4 j // a default order of `Sequential', which means that the
$ V( o ?' A! r2 E/ X // order of iteration through the `heatbugList' will be' N0 K* D" `: @6 P3 f8 D
// identical (assuming the list order is not changed
9 j( F* z( `7 L! m. \0 M // indirectly by some other process).
$ ~' X/ Q- f/ D& ^ v L# } # Z0 j8 T% X4 A
modelActions = new ActionGroupImpl (getZone ());/ Z: \( E- Y# Q F# @( U o/ |: ]% ?
. t1 M. k9 N: w* I" i* f
try {
( o$ \) p K/ b7 b; u" t modelActions.createActionTo$message& p2 w4 w) I3 V5 G7 r% Q! p
(heat, new Selector (heat.getClass (), "stepRule", false));
; h8 N F' s2 ^4 T+ m* v+ X; L } catch (Exception e) {: j X+ F/ G% S2 k: q) x2 T
System.err.println ("Exception stepRule: " + e.getMessage ());5 k" J( x6 G9 z/ v3 y( @- P$ \( h) z
}
5 u/ G: w2 x A+ ^- k
( }7 }7 H; S8 u8 h$ U try {
7 T; e. g6 h0 Y' W* D Heatbug proto = (Heatbug) heatbugList.get (0);
4 E6 A9 w1 ~2 Z) y9 o) p: U Selector sel = " r, R: I: s5 K( ]6 ~# L6 p* Y \
new Selector (proto.getClass (), "heatbugStep", false);
' v0 T/ l, B. Z1 @; P8 {( y actionForEach =
( \" n1 ^ r4 s$ ~0 G modelActions.createFActionForEachHomogeneous$call5 h A" o$ f& U3 F7 n" {
(heatbugList,
( C0 L( r2 s' m. a n( ?$ Y% F! m* U0 W new FCallImpl (this, proto, sel,; s5 i$ q# s: V
new FArgumentsImpl (this, sel)));
, f$ d0 E3 ~% Q' t } catch (Exception e) {4 r4 m1 t% c" T9 n7 S0 W W
e.printStackTrace (System.err); j! f+ l$ V* A; P
}0 F- Q7 v d% W( ?0 r9 `
7 {" v6 K+ R, `2 Z& G' t* v2 E syncUpdateOrder ();& [: H3 d2 r! K7 W; K- S$ v
! F! b1 w: g: G" [ q1 ` try {1 a: j$ K K5 x$ [: M5 `9 e: B6 V) F
modelActions.createActionTo$message $ O1 e( \) W+ J- p0 ?! [
(heat, new Selector (heat.getClass (), "updateLattice", false));$ B( f* G D! s% D+ {
} catch (Exception e) {! S d$ E1 l# m2 X, F* n6 _
System.err.println("Exception updateLattice: " + e.getMessage ());
/ q* J: Z' d7 c, ^$ C: z6 D; K* G }
, `+ C! W0 a9 K& _ S/ n2 E1 f- V 6 A: B: g$ J0 ~9 T- V6 @
// Then we create a schedule that executes the
% }8 T: g& D. m, I( D# } // modelActions. modelActions is an ActionGroup, by itself it
8 H6 i5 K. G! d3 }1 |# \# w // has no notion of time. In order to have it executed in
4 D" ~; E- S& h7 e7 T3 H // time, we create a Schedule that says to use the
7 ~ |% b+ s) L2 Q' Y) O! l, ~3 [ // modelActions ActionGroup at particular times. This
) A7 t+ u, H! o+ { // schedule has a repeat interval of 1, it will loop every) f& l6 E' o L3 P1 [' y
// time step. The action is executed at time 0 relative to
2 B3 v- z) b& E+ I- B // the beginning of the loop.' X/ O+ M8 ?7 m. ^3 C5 i7 Q# a
+ I, Z- G# N) r1 T& o) j1 ^ // This is a simple schedule, with only one action that is2 f6 j2 m7 j. c
// just repeated every time. See jmousetrap for more
/ U& \5 S4 b4 A; k5 ` // complicated schedules.7 A. X$ ?$ B3 c3 H" L+ L* v
}5 J, I* M. ]1 v4 c
modelSchedule = new ScheduleImpl (getZone (), 1);4 B+ V/ n/ b4 |* P5 r4 T
modelSchedule.at$createAction (0, modelActions);" ]5 e) k8 T" }
5 i. ]3 f* E" q) t2 n: d8 ]( D return this;4 Q6 Y! A1 V. y! ?
} |