HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; N4 W& S R3 B1 r# ^# ^( \
8 U" a" Y# ~ M3 l# M public Object buildActions () {9 n6 B/ n" v$ h# h% d9 r
super.buildActions();; p: M! C7 r+ N, c2 k
! }8 c1 K/ U8 m/ q: q
// Create the list of simulation actions. We put these in, e8 x% `9 s7 ^8 F; [& J) D
// an action group, because we want these actions to be" \8 I+ e+ J+ b* N. _
// executed in a specific order, but these steps should
7 `( k4 C8 Y8 g5 a& ] // take no (simulated) time. The M(foo) means "The message
" f6 N0 S" Q4 x8 O4 j* G3 E // called <foo>". You can send a message To a particular; E. ^1 W0 B) O* k' H" k. Z+ Q
// object, or ForEach object in a collection.( U0 C" @# b) f
: p) q3 R3 l w# q
// Note we update the heatspace in two phases: first run
! H8 B0 u2 d x; ]5 s; \( L // diffusion, then run "updateWorld" to actually enact the C* B" o9 A( Y) o4 t- H, U
// changes the heatbugs have made. The ordering here is r. |5 y7 m! O6 J4 R- K1 I
// significant!: a6 K& M+ d( Y+ ~: _
& K+ f( _$ @: `4 A z
// Note also, that with the additional
3 K5 [0 Q2 r S0 Q/ f2 K // `randomizeHeatbugUpdateOrder' Boolean flag we can
; i. C* \& C' Q: B // randomize the order in which the bugs actually run
) \! b( @! h5 a2 s6 `: f4 L% P // their step rule. This has the effect of removing any1 {2 b% G: x' n$ P) {1 K
// systematic bias in the iteration throught the heatbug
% }0 T/ H0 i. x" P7 g // list from timestep to timestep
/ v* F. E" y0 ]" Y ~ & q/ b9 W( U* W
// By default, all `createActionForEach' modelActions have
p$ X4 C0 U3 F! A9 s" S // a default order of `Sequential', which means that the3 d# t$ r" _$ K5 p
// order of iteration through the `heatbugList' will be
+ K% z. b9 _* J) z/ D // identical (assuming the list order is not changed4 Y2 ^' c& h- y6 g5 z& }$ w% g6 a1 O
// indirectly by some other process).: Y5 Q7 q3 Q8 E( ~" u9 y7 L/ S
" U5 g9 M. v$ j1 W" x( Z
modelActions = new ActionGroupImpl (getZone ());
' y! p8 _) B* `0 Z
* l0 h `6 l2 d5 t try {$ X) E9 E/ x" A0 N0 ~3 a
modelActions.createActionTo$message
& Q! |9 n3 _3 G1 z# w (heat, new Selector (heat.getClass (), "stepRule", false));' W5 _8 N9 ^+ z' c0 D8 j
} catch (Exception e) {. y- x. U% D* Y, x$ O3 T
System.err.println ("Exception stepRule: " + e.getMessage ());2 c" f/ v, k5 r/ G& `+ z8 W
}, H' Z) o( q5 f' H& @
% V% ~) g$ B! @
try {" V; f- J, t' ?% f
Heatbug proto = (Heatbug) heatbugList.get (0);3 @8 R/ n' c" E. \# {2 A* J
Selector sel =
8 }2 s0 G2 W+ D" k V2 [. s new Selector (proto.getClass (), "heatbugStep", false);
# w4 Q: c j" j2 l; c actionForEach =/ C; e8 k; u$ a. l
modelActions.createFActionForEachHomogeneous$call
2 e. v: M0 l# z- M' ~! r2 H (heatbugList,
! [* H1 n' H7 q* B% Y1 |/ s, o' } new FCallImpl (this, proto, sel,
' \ Y. u5 ?) m; }" e new FArgumentsImpl (this, sel)));1 m# w& }. [" a
} catch (Exception e) {4 e' J6 F6 _: T# C- |
e.printStackTrace (System.err);6 W: V X6 Z; K
}; f3 X$ ^! [8 p X
; ]4 a, N) O+ _. a& Q/ ~ syncUpdateOrder ();
- \1 c4 \( l( N# U0 W9 K$ G$ L. N: y+ Q2 u. M9 O6 U% B2 B" S
try {
( F8 _: V3 Y+ P1 t* L |- m) v3 y modelActions.createActionTo$message * V0 B$ h8 p3 I7 x8 q
(heat, new Selector (heat.getClass (), "updateLattice", false));( o% W/ H* F/ a' R
} catch (Exception e) {9 j7 o6 e% e* N$ X3 K! K2 j
System.err.println("Exception updateLattice: " + e.getMessage ());9 Q: i6 l# \0 ~5 y- Y6 C
}
u3 U$ T4 ?4 x) l# Z8 M * C& ?; }. ?1 y: {8 K. [! {
// Then we create a schedule that executes the# D( [5 c' U/ h" ]* n/ U
// modelActions. modelActions is an ActionGroup, by itself it2 P4 w9 f) [. {5 X6 A
// has no notion of time. In order to have it executed in
. c; @) w7 ]" ?8 i2 O H // time, we create a Schedule that says to use the
' R) f7 ~$ J5 @6 T // modelActions ActionGroup at particular times. This9 p) g q; N" \- F2 o& K, v3 i6 X( C
// schedule has a repeat interval of 1, it will loop every
; M4 r0 e/ X/ m" J' B5 _ // time step. The action is executed at time 0 relative to
+ h* I6 f" G5 x' h1 a# i5 m // the beginning of the loop.+ i% f0 d1 B+ f
h# H4 C' d3 \0 x6 _- ] // This is a simple schedule, with only one action that is! K, r2 e, u' V& `9 f8 n- H7 B O: G
// just repeated every time. See jmousetrap for more
' {+ t+ ]* y9 Q. a% X& U' ^ // complicated schedules.0 c( e ]% I% X
2 B: i: C1 D: B h modelSchedule = new ScheduleImpl (getZone (), 1);
- p! @/ y- t( j modelSchedule.at$createAction (0, modelActions);5 d0 F$ n; {8 ?7 l
* f7 U/ b: b; K/ ^6 t% h0 u+ P0 z: T
return this;2 J* y, g) f$ x( P: M
} |