HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. i5 p+ o' ]+ ^8 I" g& [
: P% Z4 X; N' L$ Y# d1 m
public Object buildActions () {
/ g9 Z( q: p" U" a' h super.buildActions();
# Z: R6 H3 B6 S2 L. @
, A% Z+ _" d- v // Create the list of simulation actions. We put these in; N$ a/ U+ e* t! X
// an action group, because we want these actions to be
$ `+ u' b7 t5 ^5 g4 m/ D6 o! t // executed in a specific order, but these steps should1 s! X0 D; L+ _, ]& y
// take no (simulated) time. The M(foo) means "The message
0 f! K& V1 H3 N2 `( J& `$ N // called <foo>". You can send a message To a particular
0 N* m' E% v1 |, q+ T // object, or ForEach object in a collection.3 Z8 B# b R3 `: z, x, e' x
, E* I% R6 Z4 {8 I. U
// Note we update the heatspace in two phases: first run! O& W2 O* ?; V4 g. l# i
// diffusion, then run "updateWorld" to actually enact the
6 }% [! M% n! A // changes the heatbugs have made. The ordering here is9 E& ~7 U8 ~: w5 f% B
// significant!
: A0 [& i" u% C' [! K: W1 i
& U( M/ {6 L; n // Note also, that with the additional
; v; ^* j1 F+ n# O) y# q/ c- m // `randomizeHeatbugUpdateOrder' Boolean flag we can( Y! w/ ]. ]4 w }+ L! ^* N
// randomize the order in which the bugs actually run
2 H7 E1 \' Z& b9 v" I3 R+ I // their step rule. This has the effect of removing any% g. u$ E# o1 b9 U# x
// systematic bias in the iteration throught the heatbug
. _2 }8 e _0 o* o0 A // list from timestep to timestep
& `$ l L8 _! a( |
& y, L: N3 L `. ] // By default, all `createActionForEach' modelActions have
/ \5 O: L& X8 t+ u // a default order of `Sequential', which means that the7 e* e/ D/ C% o; g6 S/ c2 _, `
// order of iteration through the `heatbugList' will be' p$ T+ k& K, J" E- H8 X, x
// identical (assuming the list order is not changed
; ]3 u/ T% y7 a& z5 p' H // indirectly by some other process).
( O6 K% w) u0 g T, Q# V' C8 V , ^1 g7 J g' m7 W- B4 I6 B
modelActions = new ActionGroupImpl (getZone ());
4 x( L0 ]" h1 G, k. D
2 ^. N X' m) [' o' V try {
9 i! ^( ?: [. @' O' \ modelActions.createActionTo$message
( s A8 n0 X6 h$ b (heat, new Selector (heat.getClass (), "stepRule", false));) R3 f5 b1 n( U2 c. d' P' O, Y& w
} catch (Exception e) {& s( g0 y" p2 [* n
System.err.println ("Exception stepRule: " + e.getMessage ());5 g' y( }2 |; e j `- D
}4 P! W7 z) ^6 l! k
' r" G' a5 i/ @& W$ u$ d+ y* o try {3 ~0 R2 w& A- t" ~* Y
Heatbug proto = (Heatbug) heatbugList.get (0);
# x( w* Z% a6 c% n! `! r Selector sel =
, d: \+ g- q: }2 c* s# `, U new Selector (proto.getClass (), "heatbugStep", false);
# D: x4 A X; N. g2 x, _3 `1 r, E* r4 A actionForEach =
/ J$ V: i8 O! A5 _: u! P7 {/ ` modelActions.createFActionForEachHomogeneous$call
: w4 p: y$ v2 z0 m; w7 a (heatbugList,5 f" B# x' Z! W- U5 H( Z
new FCallImpl (this, proto, sel,5 N0 g% d9 T1 m
new FArgumentsImpl (this, sel)));
( G" S, `/ `* |- }# V } catch (Exception e) {
& d0 N; d, ` N; t {, B- ^) N e.printStackTrace (System.err);0 I, I* ^1 v+ h. {2 }7 S$ _3 Q" m
}
1 D5 @6 [ H7 u( b
! r' U8 C8 |6 W7 y. k% F# l syncUpdateOrder ();7 Y& [% H$ G6 N/ B1 R& u
0 A: V( o% I, b5 h try {
2 l; |% g7 \2 v) n8 p! K) e8 G modelActions.createActionTo$message " A8 f# ]0 c# Y8 `; _3 B3 a
(heat, new Selector (heat.getClass (), "updateLattice", false));
. _( ~5 {( T$ G' M" J6 P7 j! ` } catch (Exception e) {
+ I% B1 b& H$ ?% S0 Y System.err.println("Exception updateLattice: " + e.getMessage ());
0 s* s+ i' q) d r* y! D* b4 B }
! ~* ]/ h/ D# Q! k- a! z9 a( W A; [' E 5 O" {# G- C7 s: y+ `- h7 T8 f
// Then we create a schedule that executes the
4 {6 L8 u4 l3 y3 ]/ P5 @' i* h // modelActions. modelActions is an ActionGroup, by itself it
/ [* n m1 J' r7 V // has no notion of time. In order to have it executed in; U; T) E4 Q m4 M( D6 Q
// time, we create a Schedule that says to use the
- c, }) v; d; N \4 z // modelActions ActionGroup at particular times. This
# z2 b6 Z; O- U; A0 c // schedule has a repeat interval of 1, it will loop every# ?/ q* V5 G4 c% Q. O3 E. H
// time step. The action is executed at time 0 relative to0 X, k5 a# [8 Q# h' Z+ d
// the beginning of the loop.3 m; p( A9 C- z
: _) c3 P6 E' M. T! @& l/ w K$ F
// This is a simple schedule, with only one action that is
6 ~! o2 N, L' D3 Z* ` p/ H. S+ o // just repeated every time. See jmousetrap for more
- z, w5 M9 A7 }' f2 [ // complicated schedules.
. P2 U: z' D) P
1 B5 f2 a2 i( l* q; d1 K" `: _ modelSchedule = new ScheduleImpl (getZone (), 1);3 a0 m4 W( t9 T" c( @
modelSchedule.at$createAction (0, modelActions);( m* a# Y) |# F( t1 N: k& l
; e' v; w6 _4 z3 g3 a0 s- p
return this;
8 H; U1 r$ \8 X% d) l } |