HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! b" D% Z$ V1 p0 g
; M, m9 {/ e8 _
public Object buildActions () {
. }) m$ {2 }* K/ R' \ super.buildActions();7 |7 Q& |) c t# n( Z
. V/ c9 T1 a1 N. x0 n+ j
// Create the list of simulation actions. We put these in5 h" ?7 ?6 p( C1 R( a$ U9 ?
// an action group, because we want these actions to be1 v0 n' u& ~* [3 Y
// executed in a specific order, but these steps should9 Z9 m3 G5 h# N% J8 p9 U
// take no (simulated) time. The M(foo) means "The message
8 T/ \+ l6 S7 J& Z g8 z) | // called <foo>". You can send a message To a particular
# X4 a/ m7 R7 [. z w3 Y // object, or ForEach object in a collection.
7 @; |. W# R9 N( Y# E
' G# O T& o5 y5 d0 v, I // Note we update the heatspace in two phases: first run5 W3 Z; b, X& h8 x
// diffusion, then run "updateWorld" to actually enact the
0 U9 ]& |) t; k8 t // changes the heatbugs have made. The ordering here is
6 R9 K( s; M. d1 V7 y6 o0 I // significant!
2 n& S1 Q4 x7 G; c) m* i' f
& N; T: ]8 p* o( I+ L( c2 N // Note also, that with the additional
0 B5 V$ @1 h* I3 H; \* ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 D% D% v A. @% ~8 a3 Q // randomize the order in which the bugs actually run
7 x8 r; u1 Q' ]2 `! ^1 u5 D // their step rule. This has the effect of removing any5 B0 v {$ b) w8 q; e- B
// systematic bias in the iteration throught the heatbug* y0 O9 Q/ [- W/ l9 [
// list from timestep to timestep
4 c O" e* Q2 L' V j
( u6 p# C' [" ]5 q. W4 ?! F2 P% N // By default, all `createActionForEach' modelActions have G5 V/ P2 W/ H" _0 D
// a default order of `Sequential', which means that the2 r+ f- R: { Q! C- f( T
// order of iteration through the `heatbugList' will be9 {; f( m: u* I& g1 v2 Y2 G6 ^
// identical (assuming the list order is not changed
+ A4 A& m( e. I+ }8 d // indirectly by some other process).$ z8 H& |- [# \5 C& `! }3 J3 {
' |4 F% x1 I! ]& U. t- X' j8 V
modelActions = new ActionGroupImpl (getZone ());
2 Q: u c1 o, S* w' J# J) x) d ?/ j8 N& D
try {. O; Q; |5 u! z7 Q7 O' r" U0 c
modelActions.createActionTo$message
5 E( L" {9 H( J0 o# H (heat, new Selector (heat.getClass (), "stepRule", false));
! J, u9 F& u8 A$ E5 } } catch (Exception e) {
6 R+ t6 X4 X5 a System.err.println ("Exception stepRule: " + e.getMessage ());& A" m9 {( @7 C3 k+ D$ H
}/ A. M; L% y2 I$ R4 K
V, Y1 z+ b# B: x0 u; v- C3 J' R try {3 G/ X& M/ n. W1 {: e% q
Heatbug proto = (Heatbug) heatbugList.get (0);% p8 b d# U: W5 C3 P, g
Selector sel =
) \ h5 S4 T% F7 S, u* P* _ new Selector (proto.getClass (), "heatbugStep", false);
% h* [$ r3 K6 g, u; N- ~* k actionForEach =
4 K) M% f4 Y# w9 _9 W modelActions.createFActionForEachHomogeneous$call
5 z/ U) _- S/ q3 O (heatbugList,
! `% T$ C7 _, I& x4 E; A. G" Y2 X new FCallImpl (this, proto, sel,
: U1 ?4 N% P K new FArgumentsImpl (this, sel)));
4 c9 q5 O, c% c. v8 }" P O } catch (Exception e) {" s, H2 U( b! K3 X6 U+ V
e.printStackTrace (System.err);9 ]: F: G2 a( ^- s- u V0 y
}3 X% Q. q7 r* |. W, D8 U
0 [* m$ I7 ]+ I6 ^6 Z' b, J syncUpdateOrder ();
- a0 }& I0 ^7 K/ n, e8 u" I) X
% m% O5 l4 b2 p/ S try {
; V+ `$ ?$ T4 u, j0 Q modelActions.createActionTo$message % c, C( Z5 j2 b# i0 G" L, f
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 K0 L; i1 H! Y$ u! b* W8 v: N } catch (Exception e) {
, v! p" U: `, t* P, ]$ s+ _: K! W System.err.println("Exception updateLattice: " + e.getMessage ());5 A1 [; P9 w+ j
}2 s" z2 _2 \* T# S5 y
# E5 g N: t' O6 x h
// Then we create a schedule that executes the
2 D' X) q( z/ }$ l3 Y // modelActions. modelActions is an ActionGroup, by itself it+ \6 H' _/ |" u. B: [
// has no notion of time. In order to have it executed in
* e& d; M0 ~6 d7 r) \$ m) Y // time, we create a Schedule that says to use the/ H8 o5 x* s+ D$ ]* }2 {
// modelActions ActionGroup at particular times. This4 Y1 j; T; T, G& u0 k. t
// schedule has a repeat interval of 1, it will loop every
4 @# r( J# z! A6 w9 ^ // time step. The action is executed at time 0 relative to
# T& w( e/ p7 @1 `1 `+ U: E% r8 b9 a // the beginning of the loop.
c8 Y+ X6 W9 _- @! N! ^8 O* b' S" `4 O
// This is a simple schedule, with only one action that is) M* r- e0 t2 ^+ L9 p
// just repeated every time. See jmousetrap for more1 ? K3 j1 H9 V
// complicated schedules./ x2 U5 E6 Z( J6 Y
! _% S7 ?& J# p3 j: N) z4 j; ^ modelSchedule = new ScheduleImpl (getZone (), 1);
! c# U/ I& M% b" W9 c modelSchedule.at$createAction (0, modelActions);
* o& ]- N9 R8 o
) ^1 n: R& u- T5 ` return this;
3 V# {3 [' o. x } |