HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ P9 F" `7 z# n; v9 M' P
. e% ^8 s6 N: S3 {& U. @ public Object buildActions () {
% t. }+ P' u! f' c# G' J# N; k super.buildActions();% p8 b: O0 M5 Z7 p
0 R+ I, ]; Z, [) O // Create the list of simulation actions. We put these in9 M% l% l" p2 W; P% q, m% G
// an action group, because we want these actions to be
+ f# Z3 j$ F# |5 P8 O, M // executed in a specific order, but these steps should
5 A! o* T' L2 `6 x% |- ? // take no (simulated) time. The M(foo) means "The message
: b) Q+ c! B4 a. x6 r+ I6 ? // called <foo>". You can send a message To a particular
! ]/ u2 l: z, b" f y# u // object, or ForEach object in a collection.& y+ o7 ^% m8 `" i0 x
7 L7 Z$ G5 v8 U. b2 l7 m* v // Note we update the heatspace in two phases: first run! ^8 s# I! \6 g9 j% C$ ?# W' h' R
// diffusion, then run "updateWorld" to actually enact the- L/ f0 v0 g: _( S' J6 ?% V( R
// changes the heatbugs have made. The ordering here is" `3 u9 } Z$ O' _* a% c
// significant!
4 f0 G! r: _: Y, K
1 G" x9 ]( O2 i/ k7 c // Note also, that with the additional9 q; y; g1 k5 ?7 R p+ m% ? V
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ E$ n" A& Q8 f // randomize the order in which the bugs actually run6 x/ D: ~! l! {5 c3 [. [8 V! p, G0 k
// their step rule. This has the effect of removing any
6 w( k& M: l& i // systematic bias in the iteration throught the heatbug
$ n/ y* Z3 J# n& q) c, d1 V, Y' O# ?! q // list from timestep to timestep
3 X! L- \/ s @" r2 K) N6 {' H
" p5 i5 E S7 n // By default, all `createActionForEach' modelActions have
7 W, E; N5 U1 Y6 ?3 x( {4 U5 L // a default order of `Sequential', which means that the2 R+ k# U$ l. d3 v3 P, R) p
// order of iteration through the `heatbugList' will be! d7 F) Q a5 w9 |9 K& w$ W
// identical (assuming the list order is not changed( N. ]# y+ \& a0 M) _
// indirectly by some other process).; g! A+ g2 ^6 O( p) V) n
4 i: e" j9 B3 c$ q1 [/ g' d6 J
modelActions = new ActionGroupImpl (getZone ());
, i& j E$ y5 ~) r" J
# D. C- r3 \: l$ n( _" I1 | try {- r0 Q0 K, { t1 R; b; _8 L
modelActions.createActionTo$message" z4 ?) ]( k0 v% h) m& N6 Y
(heat, new Selector (heat.getClass (), "stepRule", false));
. C4 }7 Q1 y0 ^' M5 d+ E } catch (Exception e) {$ P1 G. X* c# U% @
System.err.println ("Exception stepRule: " + e.getMessage ());4 S. \ K- h& D1 y, ]- @
}
& b4 Y8 B6 S6 P5 Y8 I* I
4 Z3 L( s3 k" S/ v% F1 B: d( M try {1 w, K3 W. H) b
Heatbug proto = (Heatbug) heatbugList.get (0);
" ?. a/ k- A6 O; B1 S Selector sel = ' u1 H1 \9 T' s: ^
new Selector (proto.getClass (), "heatbugStep", false);
! R8 j e/ h: S' L% U, D actionForEach =
' s' E1 u1 x2 N% ?$ I modelActions.createFActionForEachHomogeneous$call3 ]0 v: N+ G; Y' t# M) S
(heatbugList,7 z# L; `# S! W: [- _# E+ z
new FCallImpl (this, proto, sel,
! V( i5 O a, D X new FArgumentsImpl (this, sel)));
5 b8 t) ^5 v3 d7 O% r/ D9 E } catch (Exception e) {7 R$ D; g) y0 J4 P) o* a, b
e.printStackTrace (System.err);) @6 G. K' t0 n$ X( {5 W5 ]
}
/ j" K! P% u2 M' B6 _ : Q' l( U. w; h9 a, S* i
syncUpdateOrder ();
/ |3 w7 Y" J- d. a! T" q1 W' f7 v8 C7 X- U5 _
try {
) S; o! c c% Y @. H+ W modelActions.createActionTo$message % |. p$ G. R# J' p
(heat, new Selector (heat.getClass (), "updateLattice", false));
: S& V; R$ n7 F% m. y- g: M } catch (Exception e) {( J. ]$ G6 W& p3 U# z d
System.err.println("Exception updateLattice: " + e.getMessage ());) K5 k8 \7 I, q2 V4 [9 T& C, z
}4 f! H, z( g! b& I: w2 R
: S! z q `7 S4 v) y
// Then we create a schedule that executes the! Z9 E$ d* w2 V! C4 I0 G
// modelActions. modelActions is an ActionGroup, by itself it& B9 ?( B8 e2 S1 }
// has no notion of time. In order to have it executed in! V) e3 ^9 H# ^( K0 \
// time, we create a Schedule that says to use the+ r* G' n# O% b/ f5 E' y
// modelActions ActionGroup at particular times. This
& h. O1 v$ n2 o! r$ ?. [, x4 f. } // schedule has a repeat interval of 1, it will loop every7 q8 J# J4 `$ a* T
// time step. The action is executed at time 0 relative to
* ~; A3 v5 `- A B0 v/ n) y( F9 [ // the beginning of the loop.* L/ A9 z& {! R* n* U; l$ q- ?+ i
5 {: Q: d5 U, R; L. F6 p // This is a simple schedule, with only one action that is( q7 r3 j6 O }$ ^+ y
// just repeated every time. See jmousetrap for more8 x3 i" p8 [, V# E
// complicated schedules.
+ a: w. g* `" b* w6 r 3 a+ f# r( i; X
modelSchedule = new ScheduleImpl (getZone (), 1);7 b; t+ C C3 S4 F
modelSchedule.at$createAction (0, modelActions);
' x. D8 F, i4 T( ~0 h4 n 2 c0 B* D- ^7 m: ~& o$ j
return this;+ ?$ _5 X: e5 ~5 u" q; B% b
} |