HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% Z9 E; @( O4 ^3 U7 T
" h# [2 ~1 B* Q% \) }
public Object buildActions () {
7 b( C) f9 E: u% ^. f' n super.buildActions();
2 Z- j" j( g( W. N: H- a% | 0 h/ p3 [3 `8 l' ~( z
// Create the list of simulation actions. We put these in0 B& z& x- H1 H! {& Y8 H7 q/ X
// an action group, because we want these actions to be% G7 H/ x2 C- Z$ m. A
// executed in a specific order, but these steps should4 N6 |& Y- v* t3 Z
// take no (simulated) time. The M(foo) means "The message( h8 j9 s! n1 s2 l0 w! I4 h
// called <foo>". You can send a message To a particular
3 I7 U. s: R J4 o5 [9 b. X // object, or ForEach object in a collection.
" Y% u% m1 \, N7 u1 U
% f8 \% \7 }6 q/ d+ c // Note we update the heatspace in two phases: first run0 {! t( f. m9 X/ C2 q" ?; K z
// diffusion, then run "updateWorld" to actually enact the
: k# {. D+ w* A( q% J) U/ [- ~ // changes the heatbugs have made. The ordering here is
% u% z( R- Y5 m: b; a // significant!7 g. d1 R7 D. B- R9 N
' `8 C9 m/ o# t g+ `& |( h0 A0 k
// Note also, that with the additional
6 Y0 v: d0 U3 G- I {) U9 @7 J // `randomizeHeatbugUpdateOrder' Boolean flag we can1 N+ @+ d0 n9 |
// randomize the order in which the bugs actually run
8 b7 M7 k$ E8 v" | // their step rule. This has the effect of removing any7 v+ ]/ h3 {. F% A9 d
// systematic bias in the iteration throught the heatbug
- o% g( @- r' |9 r `' ? // list from timestep to timestep& A% f( b, K* J
: N+ Q+ }0 s9 Z2 l // By default, all `createActionForEach' modelActions have: g, ^1 e4 w* h0 l: f$ ~9 G4 g
// a default order of `Sequential', which means that the
) n: A& B( ~) F+ {9 f% y0 o // order of iteration through the `heatbugList' will be
* m2 Q* d) p* R; m, P) i // identical (assuming the list order is not changed( v( Q& z0 I. v$ Y: B9 z% C. |7 N
// indirectly by some other process).
/ s2 a* z. N: S) M$ F
5 |8 J! g, j/ b& F! k( h" Z; f modelActions = new ActionGroupImpl (getZone ());
3 g( Q4 G- g' C' o- R
- ~5 `) b1 w% [; q+ c4 K try {
' S G: `8 p6 L4 {: H modelActions.createActionTo$message5 d) A; f3 g ?5 {, g/ ?
(heat, new Selector (heat.getClass (), "stepRule", false));
! {/ l6 y3 |0 B, T9 J+ G. {" E } catch (Exception e) {
" q% f) E& t; m! b8 x System.err.println ("Exception stepRule: " + e.getMessage ());- M& |: R+ L4 o; d' K# R
}
4 |4 X9 B0 v ^' d6 Z6 N5 A9 |# n6 [! z8 o8 |2 M
try {! D# P: f& B' | c v/ c
Heatbug proto = (Heatbug) heatbugList.get (0);
2 X( w2 Y" N2 Z8 C' n& V1 o- E7 | Selector sel = + ?* n9 T9 |9 f1 l
new Selector (proto.getClass (), "heatbugStep", false);8 Z b! F8 ]+ o$ r
actionForEach =+ p+ j2 ]1 B4 J+ S2 p( U
modelActions.createFActionForEachHomogeneous$call9 }3 A% K$ a# Y
(heatbugList,
/ [- V9 S# D- A) ^ new FCallImpl (this, proto, sel,0 [3 D/ [/ T( s- J
new FArgumentsImpl (this, sel)));- L* ]4 { s8 [- {5 p5 t& [
} catch (Exception e) {6 M% c/ f% Z; ]
e.printStackTrace (System.err);
8 K: {# l# J) {7 ]2 R& ? }0 B) @6 d7 e% @9 a1 _& _
! W( b# o8 d" m" J4 H) ~ syncUpdateOrder ();9 j* }5 _! C% j8 @1 r, A6 [* J: O
8 C9 Q3 D0 K8 g2 W( b& r4 M w
try {
( A7 o) @) n$ } T. U modelActions.createActionTo$message : a7 U3 x7 q7 _/ F7 t
(heat, new Selector (heat.getClass (), "updateLattice", false));, z5 @9 D* u6 s) s+ ?8 \
} catch (Exception e) {
9 [! Q4 ~, g, ]) w; ]/ K: Y System.err.println("Exception updateLattice: " + e.getMessage ());7 D* D Y+ o; e `0 ^8 ~
}
& t( j( B5 J! Z( Y 7 s- @- g7 q' i$ ]. a2 \+ o
// Then we create a schedule that executes the9 j4 X) q) d- i: A( W( I( n8 ?
// modelActions. modelActions is an ActionGroup, by itself it! [$ d& ]- K! z* M" {1 k
// has no notion of time. In order to have it executed in4 I* ~* K% a7 N }. Z. `$ D
// time, we create a Schedule that says to use the1 e' f) D: ]/ M
// modelActions ActionGroup at particular times. This
A: x& O- A1 j, I: F // schedule has a repeat interval of 1, it will loop every
$ |8 t8 O* V$ r8 Y // time step. The action is executed at time 0 relative to7 O" c9 ?4 O& s% ]. S6 `
// the beginning of the loop. y m5 F8 ?* P9 u0 p
2 C! W7 c* D0 i( p. _# A
// This is a simple schedule, with only one action that is
) U }) X' D* J B8 k // just repeated every time. See jmousetrap for more, D9 S7 k9 z- `+ {
// complicated schedules.
' a s5 y+ x3 b 4 \' O' _8 G8 E# f6 h1 G6 l2 ^ `
modelSchedule = new ScheduleImpl (getZone (), 1);% w2 |! u2 S- \& z
modelSchedule.at$createAction (0, modelActions);
; }1 q2 c+ F0 k# r2 U5 o5 M
6 ~4 J3 M( [- `; } v return this;
8 R% U! w! v+ J; E( x! X } |