HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" ~6 T, g7 s6 w) ~6 R$ E
' S$ ], q) l) A public Object buildActions () {4 N8 R- X5 v6 ]$ L
super.buildActions();
5 c2 O, H5 s& E2 B- }5 a$ o' W5 s q5 p9 Q' e/ G6 x
// Create the list of simulation actions. We put these in- ]! {) B& H& p# Z K2 g
// an action group, because we want these actions to be' z) Y2 k2 ~, b! w, X
// executed in a specific order, but these steps should
$ h9 I- Z; [$ y6 ^! c# l) b( J- l4 X // take no (simulated) time. The M(foo) means "The message
% G& ]0 J% G/ o/ l, R, e // called <foo>". You can send a message To a particular, o1 D7 F* c, M9 t+ l% D5 Y
// object, or ForEach object in a collection.
& ?1 M! Y1 F% e ' O6 s) k, i2 T- l. W$ p2 M* n
// Note we update the heatspace in two phases: first run
8 f8 I; f% a) v# B3 L // diffusion, then run "updateWorld" to actually enact the
$ a- c6 J. M* e( t1 |9 g1 o // changes the heatbugs have made. The ordering here is8 n, }" P/ {7 N& W9 S
// significant!0 |( t3 d/ r/ S- Y9 s0 B3 V9 i
0 T0 g$ v) Z) J& p$ x' C9 k5 l // Note also, that with the additional& x$ g1 B$ s0 c& k: f! c- ^1 p7 B! [
// `randomizeHeatbugUpdateOrder' Boolean flag we can; \) I" N( w+ ^" d
// randomize the order in which the bugs actually run% m. Y. _# |) F* k
// their step rule. This has the effect of removing any9 c3 |4 {' y9 D" {* d& T
// systematic bias in the iteration throught the heatbug
7 a/ Q! m, I% x5 E8 M6 { // list from timestep to timestep. Y$ U- T+ p+ a
" b# D0 u' f4 g // By default, all `createActionForEach' modelActions have
, f+ ~+ B5 ~0 H' ?7 r2 R$ V+ w( {1 t // a default order of `Sequential', which means that the( I9 [1 u7 @& @2 R" g
// order of iteration through the `heatbugList' will be
" ~1 j3 b/ F: C: m# d' C // identical (assuming the list order is not changed
+ B5 K9 o/ y0 R S$ `2 @ // indirectly by some other process).
& }2 \5 c. Q' J( O0 l& ^ 0 K1 s+ @5 I4 X( R8 z0 v" @
modelActions = new ActionGroupImpl (getZone ());
- g' @0 q/ i9 u' Y: A! W8 ]& X" s# p( _& ?( x r! F0 J
try {: ]( h4 v* G$ ]/ v' p& o
modelActions.createActionTo$message
* C+ {& K8 M7 F A! o& K (heat, new Selector (heat.getClass (), "stepRule", false));" s2 o: F3 j% l/ C
} catch (Exception e) {; r3 T( [7 _9 U+ i3 R
System.err.println ("Exception stepRule: " + e.getMessage ());6 a$ [" w- h \8 }( e5 W1 N, S
}
7 A2 @2 R; M! a" Y
# c, h) J# Z( I. h( i# B* u; E4 t try {
/ F7 Q( _; N4 V! q4 M& C6 t Heatbug proto = (Heatbug) heatbugList.get (0);
7 ?/ i( ?" F0 V$ y) Q Selector sel = , n/ \4 ] H& [1 P7 j! j2 g% f
new Selector (proto.getClass (), "heatbugStep", false);$ C/ E% q1 A3 \* k; u2 S F+ T7 @
actionForEach =# p- K; n D$ x0 ]6 P$ V$ w
modelActions.createFActionForEachHomogeneous$call
6 t, E5 N& d; `' p/ Z4 g: t! o4 F (heatbugList,$ a" Q0 _2 U' Z
new FCallImpl (this, proto, sel,0 Y( u( ~* ^5 J1 m9 f( e
new FArgumentsImpl (this, sel)));8 j( k, W5 N1 }1 R
} catch (Exception e) {; t$ \) ^2 ^& H2 R( v% O+ S9 G6 x W
e.printStackTrace (System.err);; s5 v+ u+ t% ?, U$ L9 d9 J& r+ h
}
, e) v8 W7 Q3 Y! L, p
5 R& q' I6 F$ W6 f1 m q syncUpdateOrder ();
& a) W k1 v/ b9 j6 k @+ a' n. ]6 \4 ?; r7 r
try {
- C K7 l" |, k7 C% a; F( Z modelActions.createActionTo$message
# `2 { }4 c8 H( k5 {2 V* i (heat, new Selector (heat.getClass (), "updateLattice", false));
- g' i" Z1 H c3 W% j$ Y+ M } catch (Exception e) {9 s, J ^3 m9 R' q
System.err.println("Exception updateLattice: " + e.getMessage ());
3 G% |) ^) C/ q3 S- ?' _; T( I }
# i. D1 V6 y8 p* T* a6 h5 ]1 I
* \ v. }0 u$ A( z) i% l // Then we create a schedule that executes the
! Y, D0 @3 Y1 R( U: d8 a // modelActions. modelActions is an ActionGroup, by itself it1 S; r6 }* F( k# h5 z4 _; ~5 h0 k
// has no notion of time. In order to have it executed in
0 v2 U7 ]3 y* h0 J& u2 ^# E // time, we create a Schedule that says to use the: m- F: K( w6 V3 A4 G( N* u, w" N: T
// modelActions ActionGroup at particular times. This8 b4 r4 s# q* x5 v! r. h9 ]1 \) G7 J, n
// schedule has a repeat interval of 1, it will loop every; c# E/ ?- C$ \! J! c
// time step. The action is executed at time 0 relative to9 Z+ @+ @) ^ `9 I& m
// the beginning of the loop.
. P" I, a( K" W* u$ s6 q$ f2 i1 J! ~, }
// This is a simple schedule, with only one action that is
$ v2 L8 a# m3 _/ \6 t // just repeated every time. See jmousetrap for more# f# U# j; n% x) h3 o
// complicated schedules.4 T8 K, p3 l; e V
$ X* q1 Q6 D% e& o modelSchedule = new ScheduleImpl (getZone (), 1);# e* w [! g" z U. d
modelSchedule.at$createAction (0, modelActions);
' W) g( T0 d0 ?1 H7 i6 ?! P
' g7 S& {5 W, E+ ~: Z5 M b return this;6 T) m) K ~. T |
} |