HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; \6 s8 I9 t7 U" M/ [- o) F4 J8 O
" Q; } N, F/ l% ^2 a0 n$ {+ }/ ?
public Object buildActions () {( D6 B& M* s" f) ]
super.buildActions();+ h2 g" E9 u6 L0 T, D" `
6 V' \* U5 S7 t$ G6 p6 W
// Create the list of simulation actions. We put these in
; K! p$ b8 u F* h5 }# M; } // an action group, because we want these actions to be
" n2 b! r0 R" z$ a // executed in a specific order, but these steps should8 o5 R7 Q* Q: L5 Y
// take no (simulated) time. The M(foo) means "The message
5 ~$ i# O( V( H: D // called <foo>". You can send a message To a particular5 g8 W8 g2 M7 j
// object, or ForEach object in a collection.
2 p" ^2 F) l+ y. T- H
- Q5 T" `& F' t( n6 M& l( M // Note we update the heatspace in two phases: first run
& ]. ^5 \/ h6 L: Z8 _, {" o // diffusion, then run "updateWorld" to actually enact the
* x0 o* Z# X. w // changes the heatbugs have made. The ordering here is
4 I7 t3 D* h1 }! @; v# z/ ~ // significant!' C g! W8 I8 a6 c) c+ l2 w
* i0 o. S9 o6 y# [2 o* V \ // Note also, that with the additional$ \, S( i6 l! z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ w* c1 a. H9 v6 M // randomize the order in which the bugs actually run
3 y0 a" x2 O1 S7 e( Z6 V% h( v1 k // their step rule. This has the effect of removing any
, H' m+ Z8 O6 f6 \9 N6 j // systematic bias in the iteration throught the heatbug
" p6 \8 X# L- B // list from timestep to timestep: Z0 x5 w% e0 [$ S' j
/ ^6 v+ @5 w. l7 }6 q4 P. N1 B5 e // By default, all `createActionForEach' modelActions have* Z8 A9 H/ e1 W; U9 J3 `8 W' G* V# ?
// a default order of `Sequential', which means that the" K/ q& E v( f7 D/ S& \7 J
// order of iteration through the `heatbugList' will be
. o& P1 [- e4 F9 o: T // identical (assuming the list order is not changed
1 r' ]* Q7 {* Z7 V1 w* _, N+ d) G* a // indirectly by some other process).
, U$ H4 Z( c7 s q7 f
9 P, |' O$ ^! q# Q" X modelActions = new ActionGroupImpl (getZone ());. f" ^+ V! l7 |# l) v, B
% q+ C' H" c3 c! u: h try {
. n2 n1 s0 U O6 V5 y+ |9 H modelActions.createActionTo$message B% z; U& d; s8 L1 E: D$ v
(heat, new Selector (heat.getClass (), "stepRule", false));4 E: b ]' `% v* K9 ~
} catch (Exception e) {$ w) h+ r Q1 }$ W
System.err.println ("Exception stepRule: " + e.getMessage ());5 P+ ^) v/ F" G' i+ B
}
" Z5 b8 t/ A0 y5 i* b& f0 s9 R% s' D- h5 w, {. g3 B$ Y6 F
try {
4 ^9 V( r6 h0 E( L/ Y# L Heatbug proto = (Heatbug) heatbugList.get (0);: Q* L2 p6 {( i+ o3 ?
Selector sel = 2 |! L6 w/ k3 E" H. j) ]
new Selector (proto.getClass (), "heatbugStep", false);
( j7 D( p3 ]+ p) F( F actionForEach =
, z9 I8 F6 k9 Q' x/ x: I7 Y' | modelActions.createFActionForEachHomogeneous$call; s/ J" J' J* {' o2 l3 r
(heatbugList,
0 T+ y1 C( u3 {, ^+ g4 z' w new FCallImpl (this, proto, sel,
( V% q, a J7 W. c- {5 l5 V; [ new FArgumentsImpl (this, sel)));! ]9 I# I6 B; [' q* G, l+ u
} catch (Exception e) {( Y8 q; ?0 i: p; y. [
e.printStackTrace (System.err);0 w% ?% d, j( i) m# O1 |
}: e: K5 w: ?2 D( H8 J* z/ E
% ]$ ]4 U5 a& d. ^5 { syncUpdateOrder ();" | Y) I6 B2 m0 d
. a/ J/ d/ ?: i try {! K% C8 B) e; i1 `
modelActions.createActionTo$message 6 o/ S' U$ g9 s& {# c( }0 B+ T
(heat, new Selector (heat.getClass (), "updateLattice", false));
% y# \! J& \( E( |6 A } catch (Exception e) {; w+ Q7 O2 _6 Y; I0 {3 O; R. Y
System.err.println("Exception updateLattice: " + e.getMessage ());; U$ X6 j. F7 R
}( m3 T5 y2 M5 ? m5 H
" v# E5 ] ?4 ]+ }: D3 F6 w
// Then we create a schedule that executes the
m& L; h" j1 C4 e1 A+ t // modelActions. modelActions is an ActionGroup, by itself it
2 f4 J, \$ r6 \2 V+ q // has no notion of time. In order to have it executed in1 d5 Y% }/ L- g$ Y' S8 s
// time, we create a Schedule that says to use the- B _( j1 X( g/ @3 j- E: D' O
// modelActions ActionGroup at particular times. This
' x- u4 [/ B! B4 q n* ? // schedule has a repeat interval of 1, it will loop every
5 O J$ O3 M( Q( Q( [# e, M // time step. The action is executed at time 0 relative to
' V+ j: u3 S/ ~: j // the beginning of the loop.6 r" ^% G' T+ u( ?% | A
0 Z2 m# o9 e) ?" W- s+ z1 t // This is a simple schedule, with only one action that is+ Q% l) g- [: ?
// just repeated every time. See jmousetrap for more
( R$ b# u8 w: n; U+ U# j // complicated schedules.) ~! ]5 q/ Z& w0 D
* s) g v3 W( r
modelSchedule = new ScheduleImpl (getZone (), 1);5 e; \. o0 G% l2 C
modelSchedule.at$createAction (0, modelActions);
$ }; }1 I6 U3 X+ r* m
8 K8 e/ J( C, N; t return this;
k1 |% j4 z% w! a7 Z } |