HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 b2 E4 @2 Z$ t& l( B1 p) Y- Z0 I- f
public Object buildActions () {$ z8 T; }/ [- s L) Q) Y
super.buildActions();, K: e" r% h5 ~# `1 S) L
: G0 p4 M6 X s1 J1 Q$ C // Create the list of simulation actions. We put these in5 A; Z" \$ i3 |4 c% r
// an action group, because we want these actions to be9 K0 C& E$ Z9 p$ a$ l. ^! C# d
// executed in a specific order, but these steps should/ `4 a) Y5 ^7 ?8 z) E0 R" }
// take no (simulated) time. The M(foo) means "The message( E, K( a' N6 J* w8 u
// called <foo>". You can send a message To a particular
2 M" D6 E! r: C: g$ s4 I; j // object, or ForEach object in a collection.
5 n. i4 U' [% }/ S; u
8 K- O; Q N( N4 I // Note we update the heatspace in two phases: first run; e5 s( s& R6 b& o8 o
// diffusion, then run "updateWorld" to actually enact the
1 X; j$ ]# {( e2 p3 G# f% b2 _ // changes the heatbugs have made. The ordering here is
$ ^0 T8 X, l) r* k // significant!% V* d+ K; ?; K5 |1 k/ w, C# _& v+ w7 k
$ ?& L9 F7 t6 M( M# d // Note also, that with the additional& ^$ h6 @; Z% l! K4 F$ ]" k
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& C9 P. T1 f L% g0 ]+ b // randomize the order in which the bugs actually run
. ?/ n9 \0 q* D- I p1 Z // their step rule. This has the effect of removing any) m' ]* d7 T/ t9 P! `0 x9 G+ h
// systematic bias in the iteration throught the heatbug
3 r( G% M- N: P2 o // list from timestep to timestep
0 k8 t4 Y( x' k- e/ m3 R
: C. M& b, H" n( O0 E0 z5 O+ r+ r // By default, all `createActionForEach' modelActions have
9 ~6 D6 n0 `* e/ B // a default order of `Sequential', which means that the8 @0 U. i+ k* W: \( _. \# a6 l; T) x
// order of iteration through the `heatbugList' will be
8 q% N; p- p; S/ ], ]3 s7 d // identical (assuming the list order is not changed
7 K0 k$ Q' j; y7 f# K/ c // indirectly by some other process).6 \7 R0 `& }" A& a9 {
2 U" X" d) `7 D5 R2 L
modelActions = new ActionGroupImpl (getZone ());+ u+ v% ] Q6 @7 e
* K+ n9 C7 Z' h! X
try {
( @* Y( Y1 @3 _ modelActions.createActionTo$message. ?8 c4 K6 Z& y( a' `
(heat, new Selector (heat.getClass (), "stepRule", false));; b& v; z! r5 [2 l5 l* y# K; z
} catch (Exception e) {1 j* |0 a3 w( ?" N, [% s
System.err.println ("Exception stepRule: " + e.getMessage ());; M# z& x' \$ J, D( W7 P) v
}
' a3 i/ L0 y( I3 X
; w! R7 p! d z6 J$ Y# ] try {
: ^ W. c: ~+ o& d0 h0 P6 A Heatbug proto = (Heatbug) heatbugList.get (0);
. H8 p: A! g; M# Z5 a$ ? Selector sel = 6 j) Z- C+ I0 T) U7 W! k7 k6 |9 p
new Selector (proto.getClass (), "heatbugStep", false);
* z6 y! n7 l2 z, b c: `8 _ actionForEach =
6 @* h" \; L# g1 q modelActions.createFActionForEachHomogeneous$call+ n4 j& \/ n9 a* {- _
(heatbugList,7 A `$ ?) j- J& D( z. _$ X2 ?
new FCallImpl (this, proto, sel,
7 G/ N3 M7 ^, S+ j7 o new FArgumentsImpl (this, sel)));5 G8 D" l- f$ K M
} catch (Exception e) {$ g, N. H+ n7 X0 n; T( I
e.printStackTrace (System.err);( W. G; p3 S8 ~) {0 X3 ?( z( _/ @
}1 p, m+ D; v+ L+ L8 T' e5 N& j, ^0 ^
- _+ A6 F G* |2 l' z0 H syncUpdateOrder ();4 E! ?9 U5 m" t# x* i. r
+ ^" B; c8 B F* q, A
try {
+ x0 r* b& e, I( A" y* A6 W modelActions.createActionTo$message - n6 ` I6 B0 F$ A3 r4 @4 @
(heat, new Selector (heat.getClass (), "updateLattice", false));" z3 m7 u0 @3 f$ K
} catch (Exception e) {
3 ?! u# k. ~4 Z9 I8 ]" ^* S System.err.println("Exception updateLattice: " + e.getMessage ());; `- }: N f2 i' M( Z
}
4 I0 z8 ]; H, l9 q" k+ q
1 g7 [- R w* L4 n7 Q- Z // Then we create a schedule that executes the
]6 D [ ~) S" N8 i; Q( ] // modelActions. modelActions is an ActionGroup, by itself it. }& j$ R+ d! q: F" O0 D) i# q
// has no notion of time. In order to have it executed in
, \" {: ~9 ]- Q+ Y // time, we create a Schedule that says to use the
0 }8 Q: H1 x* o* c4 U# Z // modelActions ActionGroup at particular times. This
0 j7 ]" b; n* n6 W/ n // schedule has a repeat interval of 1, it will loop every
- |' V0 n1 Z$ |* d: a6 ?+ { // time step. The action is executed at time 0 relative to
5 Y. |3 p% @8 s; U2 @0 G // the beginning of the loop.
9 }# O6 V% N" i5 M; ?6 Z& \$ K g% g, R* m7 L
// This is a simple schedule, with only one action that is
& S2 x ?, K2 u! S% e! b4 R* w" @ // just repeated every time. See jmousetrap for more
# k7 C7 P! B! \* @( V // complicated schedules.
+ L; h, o5 }% P$ N0 V
3 x! I+ p8 f0 h% B! w modelSchedule = new ScheduleImpl (getZone (), 1);
3 n0 o$ l1 [9 o$ H( c modelSchedule.at$createAction (0, modelActions);0 v/ i S3 } Y0 P# O, R6 R
1 a; | _2 }3 n: c) Q8 K; ^$ V! O
return this;
+ ^3 k, M* }2 c! @4 z } |