HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# j+ n% Y1 Q. [1 [5 T6 _) I
! y8 s% R. }7 C2 X! G' Z. ~
public Object buildActions () {
3 U) U: X5 U0 ]8 Q: |( V0 \/ I super.buildActions();8 p$ w, M' c- ^7 Z. _ k' Q: G
' L3 [4 d. a! M. d5 t% C+ r* ] // Create the list of simulation actions. We put these in
' ~- O9 e, @9 _8 E6 [" g1 q. L/ p2 g // an action group, because we want these actions to be5 d7 k; [) I8 V4 M: A
// executed in a specific order, but these steps should
1 o1 j0 e' [' B. n7 U/ \. v" Z* _ // take no (simulated) time. The M(foo) means "The message
1 {) H9 L" ^7 J // called <foo>". You can send a message To a particular
% W) g( X. }1 S6 a b' G9 y% B0 M( n // object, or ForEach object in a collection.
% t( a6 v% v' C4 ?( T/ V' p' y8 p
: c2 _' T: N! S. P2 I9 i) d1 G! j // Note we update the heatspace in two phases: first run5 w! _$ S, J9 r7 b6 Y/ R/ z2 I
// diffusion, then run "updateWorld" to actually enact the8 ]: B/ S+ v! q4 ]1 m
// changes the heatbugs have made. The ordering here is
6 \, W- |' x2 C5 h, H0 Q' J2 b // significant!
' X- `( a" x* A7 f
% `4 H- R9 `1 o: U3 G // Note also, that with the additional
) |+ b6 S9 \ ?0 s // `randomizeHeatbugUpdateOrder' Boolean flag we can$ a; A# T! }8 S" [6 X" _; \- ^
// randomize the order in which the bugs actually run9 ~# ^ I$ E! C# j& u# H0 ?
// their step rule. This has the effect of removing any _6 ]1 c2 v% u: R" m1 J
// systematic bias in the iteration throught the heatbug
K2 H# y( Z* y) M0 { // list from timestep to timestep# @; ^ u9 Q( ?9 u& K* F" P
2 D$ i# b8 e5 L6 E1 j: } // By default, all `createActionForEach' modelActions have( K0 r7 [. ~" I4 ]! a3 |3 w
// a default order of `Sequential', which means that the
; l) M6 W; W R' p // order of iteration through the `heatbugList' will be
! x0 ~5 P7 ]# |# d // identical (assuming the list order is not changed! w0 H: _) i$ l- B% P
// indirectly by some other process).- n+ N2 @6 j4 t0 L% j- d: M* X
" Y- G) I5 v/ [* t+ W, j$ c
modelActions = new ActionGroupImpl (getZone ());
# h+ c: E; b, Y6 ]$ R; p: B. T! M; B8 F& [8 Z
try {( e8 v; K- p6 i/ b6 `# v
modelActions.createActionTo$message
5 @ Q/ u8 V4 \ (heat, new Selector (heat.getClass (), "stepRule", false));& k8 ]( m7 m+ [. [8 F0 t
} catch (Exception e) {8 l4 \# S9 j& q9 m8 I! P
System.err.println ("Exception stepRule: " + e.getMessage ());
2 J7 O7 N1 |) J4 a0 u( E$ ? }. W& ]- J0 z6 o# S! K9 x4 J# d
1 p4 F9 K$ C* U3 U# x$ B try {
6 v/ q; K: ^, X& ] Heatbug proto = (Heatbug) heatbugList.get (0);
2 p' j$ Y7 S& Q! ?3 i Selector sel =
- Q" e" | ]5 s$ v/ K new Selector (proto.getClass (), "heatbugStep", false);4 s$ P7 j$ ^: t! Q6 T; {, X5 i
actionForEach =" W Q- s" \& C; G, ]# ~
modelActions.createFActionForEachHomogeneous$call
$ l6 d; O5 }/ d7 M (heatbugList,
. N0 _5 S/ J z new FCallImpl (this, proto, sel,
+ w$ q! s0 d; U6 l/ C6 c$ K new FArgumentsImpl (this, sel)));
( S# F F/ M7 N2 p- o } catch (Exception e) {
( x2 W5 L, \7 o! Y ~ e.printStackTrace (System.err);
6 L n6 V0 t" I& F3 ]7 q5 Y' k+ B @ }7 J& N% Y7 F6 E' F
5 ^3 I Y5 ] ? j syncUpdateOrder ();
6 Z# w s" ]4 o
. _0 X ~7 ?/ X5 T try {- K; i- B$ @0 p5 G1 V* ^
modelActions.createActionTo$message 5 k' e3 _% g' ?) f; N
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 q8 h# W0 p* d! w+ \ S/ f y } catch (Exception e) {
1 I: c* i! J0 M; D System.err.println("Exception updateLattice: " + e.getMessage ());
! F! v8 }/ A+ e2 V! u$ i9 o# g/ q3 ] }8 }6 G" c R" L) j; C: Z: z5 `2 g
% D5 T7 D& e" j3 h) K5 C% _3 [8 W // Then we create a schedule that executes the
_: u7 T; }2 r // modelActions. modelActions is an ActionGroup, by itself it
$ }/ e; M! ~0 ]( U% y7 q) z // has no notion of time. In order to have it executed in
+ y" V. z5 Y- E& }9 R4 A4 g! s // time, we create a Schedule that says to use the* X+ J" e% s9 l0 U" U
// modelActions ActionGroup at particular times. This% }* E# U- m" V
// schedule has a repeat interval of 1, it will loop every
# j8 x1 u3 z% @/ ?3 g- u // time step. The action is executed at time 0 relative to/ P7 e! \9 y/ A4 r( T
// the beginning of the loop.# o* d3 O+ o# v/ x6 ? h
9 e2 S. |# w( W b
// This is a simple schedule, with only one action that is
; c8 j/ Z. `! T7 Z" H+ w. } // just repeated every time. See jmousetrap for more
& ]1 H2 L% @' V // complicated schedules.+ M& f2 M3 T5 C
( b7 p9 M0 R/ i; U. y8 I) Y modelSchedule = new ScheduleImpl (getZone (), 1);
# H" W: X% h% K. @( q0 I modelSchedule.at$createAction (0, modelActions);
1 g+ Y% R2 `% f' z5 I: d( y, B 6 b6 K- A4 j& O% Y3 G( L
return this;
( d; p) [( G' w& }6 h } |