HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" P2 C6 F/ V9 {# L" w2 V# v; G
+ A5 h2 b( d9 m/ d+ b" A& t
public Object buildActions () {
$ C! K5 P# ~4 M' z! d+ O: K6 v super.buildActions();: h. s. t5 s# Y. o1 F
( R# O& Z2 F% P/ C
// Create the list of simulation actions. We put these in0 |0 o# l6 n2 K2 K5 @1 l3 {
// an action group, because we want these actions to be9 D$ |4 Y! x [& k8 j$ [% v* t4 B% k
// executed in a specific order, but these steps should: Z) U9 o# N( J% M+ D' M2 {
// take no (simulated) time. The M(foo) means "The message
+ d% b% U% i! V' [$ L // called <foo>". You can send a message To a particular( m( p% V2 W' q9 q
// object, or ForEach object in a collection.. j$ k6 R; d( w( ?/ v0 Q; V: K
0 [0 T5 `+ h: g7 s/ q) C
// Note we update the heatspace in two phases: first run
6 k8 l. Q" q. ?- h // diffusion, then run "updateWorld" to actually enact the3 [ Y2 a: @( }+ b
// changes the heatbugs have made. The ordering here is
- g: L; k8 g5 z. |+ T0 q: g // significant! T! [$ g* i) e _
( L5 E: Z$ I# f) V% Z0 M6 G // Note also, that with the additional
" Q8 D9 C* D1 T // `randomizeHeatbugUpdateOrder' Boolean flag we can
1 [% R; G+ d' z# i% D [ // randomize the order in which the bugs actually run
2 e4 Q9 w+ f- J/ K2 e* W" |5 } // their step rule. This has the effect of removing any4 f1 Y) ^- ?+ ?* g) `* _& Y
// systematic bias in the iteration throught the heatbug4 b: x- z( V3 C5 v+ Q& H
// list from timestep to timestep' }& V$ p& K1 k5 s5 n
! |/ R% ~4 |% d // By default, all `createActionForEach' modelActions have
5 _; j) g5 G6 a: v // a default order of `Sequential', which means that the, v7 ^0 U' o7 H J) i( C6 P0 q
// order of iteration through the `heatbugList' will be
6 o+ j8 ]" w2 ~0 z) Z o4 E- {- d" E // identical (assuming the list order is not changed
6 p/ c/ q; D. z) Q0 D" A" M // indirectly by some other process).
1 q6 m* a! f0 Z( p+ t$ y' f
0 ?* F6 g1 E+ g4 P Y modelActions = new ActionGroupImpl (getZone ()); C# W! p: N; [8 o
4 A- h+ H, v% }# ]+ W8 z8 q9 r# \8 u# S try {9 |+ S. V0 T0 P1 k
modelActions.createActionTo$message
& B! x5 Y/ D' ~- A$ Q/ S$ v: c (heat, new Selector (heat.getClass (), "stepRule", false));
" N' p# i6 d' g. L; \: r0 t3 [7 ` } catch (Exception e) {
3 r9 O4 S5 s7 S# W& S System.err.println ("Exception stepRule: " + e.getMessage ());( {2 U8 [$ h* R( [8 D3 e
}5 ?8 G7 P0 T+ @* @3 R, r# C1 b
/ R( w( w Z& J f( Y try {
7 h* m& Z' j( O Heatbug proto = (Heatbug) heatbugList.get (0);" D+ @! {( e8 w4 J2 {+ t8 d& w
Selector sel =
2 T1 W6 e& C k* u+ d; } new Selector (proto.getClass (), "heatbugStep", false);! b( G7 e2 D4 S5 Q8 V
actionForEach =
8 O: o3 _4 e3 G7 H. \ modelActions.createFActionForEachHomogeneous$call$ S* a+ y! @! z: y# v9 T
(heatbugList,1 v T6 p/ S6 b* |$ V2 F& C
new FCallImpl (this, proto, sel,
5 V! w5 _, o5 C4 b+ O& ^: ^ new FArgumentsImpl (this, sel)));$ H j: V# Z$ P6 W D; D0 j4 m3 Z
} catch (Exception e) {
# O z% Y& v) t3 ]) p( q e.printStackTrace (System.err);2 _6 H9 w/ S/ h& m+ E4 U
}. Q/ j; N: Z8 a5 O! M* W5 g8 P8 c
8 m% z; G a+ A- M' X
syncUpdateOrder ();4 c$ P0 k0 `/ s1 e% A0 ]3 c
! l- X& z, V& o: D t
try {
" U4 j2 U- e3 F' M# h modelActions.createActionTo$message . p! a& d9 W" Q( |9 C
(heat, new Selector (heat.getClass (), "updateLattice", false));9 B1 }9 g" I6 P1 E `% B
} catch (Exception e) {
, i1 Y$ s7 X1 C( m' p* Q4 g System.err.println("Exception updateLattice: " + e.getMessage ());( Y: Y! Q% b4 s2 I4 w0 a7 i+ d
}
: r/ c9 u5 w1 A7 l 5 n- T7 a: R' K2 F
// Then we create a schedule that executes the
& ~+ @- h4 R( O- O1 ]% N" V6 u4 x // modelActions. modelActions is an ActionGroup, by itself it
+ }. a" o3 I: p4 S* O) S( r // has no notion of time. In order to have it executed in! B4 u/ ^. i* L
// time, we create a Schedule that says to use the
& L3 U' A; j. U& m, v( T$ Y9 k/ F& i! E // modelActions ActionGroup at particular times. This, q* U' y" H/ Q: D9 r! T$ s. m* H
// schedule has a repeat interval of 1, it will loop every
9 ?. @8 [7 ~9 w" Q1 K) o) k // time step. The action is executed at time 0 relative to
- Z, @5 o% e! b1 z/ z: j5 N+ U2 s // the beginning of the loop.
: ]" S" V h0 E! t' U
; b2 k6 z% A' F7 J // This is a simple schedule, with only one action that is9 T g" q9 N9 J
// just repeated every time. See jmousetrap for more
$ ~! N1 n3 q- P8 Z: t // complicated schedules.
( f0 r y. ^+ m" U
& u- d. n; j3 W8 F# J( k! x modelSchedule = new ScheduleImpl (getZone (), 1);
! Q- \) o) H# m& x8 z P modelSchedule.at$createAction (0, modelActions);' S0 k+ R* ]/ ?$ s6 x( n
8 Q+ z! L0 q( o# j3 u7 I return this;" M9 _6 V+ y5 V1 B5 ?/ K* D: z; K1 q6 U
} |