HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* O0 u7 e. c$ ]; F$ }& C
6 J6 M1 y! N% X% g
public Object buildActions () {
- y) M# v1 I2 D+ H) p6 } super.buildActions();
, {( F: t$ Q x* }4 B$ ?/ h - ^) V2 Z* _" R( W- S- p8 R
// Create the list of simulation actions. We put these in, d$ M3 m8 t. K
// an action group, because we want these actions to be, e J X. M$ n, o; p+ Y2 G
// executed in a specific order, but these steps should
. E# @9 L( R; T- s, r: m7 ? // take no (simulated) time. The M(foo) means "The message) @; A) ?2 y3 z8 I0 P f
// called <foo>". You can send a message To a particular, L6 Y# v2 Y n7 ]
// object, or ForEach object in a collection." ^% W/ M/ h4 h4 v
, r5 n; y6 l. e5 S2 o; n, [( A
// Note we update the heatspace in two phases: first run: `! R5 U6 y( N' `+ k8 ]
// diffusion, then run "updateWorld" to actually enact the
7 e- C% Q9 U* ]0 A; ? // changes the heatbugs have made. The ordering here is
- ?! B h* u' k' w // significant!
0 k. I3 S. U& b 2 A/ T+ D/ i1 K6 ]! Q
// Note also, that with the additional
' q8 a" ]5 b. L/ v: k9 a // `randomizeHeatbugUpdateOrder' Boolean flag we can! {( [1 y, R9 |2 W
// randomize the order in which the bugs actually run% r- n$ g0 U3 j% c! k) y( H) U
// their step rule. This has the effect of removing any
# b/ M g7 T" Z; L: i // systematic bias in the iteration throught the heatbug- g% N% s. ]) H4 m, |3 b. m
// list from timestep to timestep
- R* I5 k- j/ i- q 4 f' q! ^4 U8 q+ p1 c
// By default, all `createActionForEach' modelActions have: }9 r7 b/ M9 x/ Y8 O: S
// a default order of `Sequential', which means that the8 E& P5 s* w. S( ?2 c$ J
// order of iteration through the `heatbugList' will be) ]$ S& V% F3 Z
// identical (assuming the list order is not changed
, B2 H& V; ^, M. P // indirectly by some other process).
# f V/ o' [ [- ]# E/ D G4 w/ C8 N: S
modelActions = new ActionGroupImpl (getZone ());! ]" L% N( \4 P' B
. v9 E# f$ w) r6 `/ s( ~ try {
2 Q8 V t. [" o7 N modelActions.createActionTo$message2 E% L& t2 W! a4 d
(heat, new Selector (heat.getClass (), "stepRule", false));6 e7 T% y- G, \7 }
} catch (Exception e) {0 D0 X% Q7 f6 }
System.err.println ("Exception stepRule: " + e.getMessage ());! _! K+ s4 v- h
}
1 R, p3 F, `* y0 q% g8 @% q( Y: I/ w- \/ d
try {" q6 I) B& p* u# [6 R' M g
Heatbug proto = (Heatbug) heatbugList.get (0);
- U, C; h( @+ z% [0 [* O Selector sel =
, Y5 X5 `7 p/ y, y3 I new Selector (proto.getClass (), "heatbugStep", false);
8 S& |0 o$ ]9 f |& q actionForEach =: |# y% Q! x. h+ K8 ~. {# Q8 L" `) L
modelActions.createFActionForEachHomogeneous$call
& b- L! h9 b$ L- @7 k4 ^ (heatbugList,- I6 s6 Y- c( c; J4 ?
new FCallImpl (this, proto, sel,9 H: g% Q) n" r+ d! |) u
new FArgumentsImpl (this, sel)));% _. P9 T; L: s! a6 w! q
} catch (Exception e) {
0 d! b" f `$ V6 u8 P e.printStackTrace (System.err);
! W0 ?. K1 W: p0 ]) _$ r* ? Y' a b }
9 f& e9 O, o% X; L" _1 X / ^1 e, V, M4 ^; k
syncUpdateOrder ();( S. y. s6 \- c. E
% [0 i' S% ~1 ?& M/ G/ }7 G: Q1 y try {# h: W- A' Q( D; [. W
modelActions.createActionTo$message
+ q! R" O9 X* U' \ (heat, new Selector (heat.getClass (), "updateLattice", false));; U$ J+ ^9 Y, F: t* X
} catch (Exception e) {( U; A+ M$ \2 C! H. f
System.err.println("Exception updateLattice: " + e.getMessage ());' u# E3 ~1 q p$ U" t- K1 y! Z
}
3 x- @ J1 U1 n2 j7 H ' V9 v6 h$ o7 |9 @- _9 Y
// Then we create a schedule that executes the& E& f& a) S! G6 U1 G, z/ {. U
// modelActions. modelActions is an ActionGroup, by itself it- F/ N' g, Z6 D% }! O
// has no notion of time. In order to have it executed in) B+ e+ Z( F2 D' w% W/ {
// time, we create a Schedule that says to use the9 P2 P6 Z# V. Y8 K. J) R3 c9 n
// modelActions ActionGroup at particular times. This# J, d- x% t0 K; K
// schedule has a repeat interval of 1, it will loop every& \: [. x% r/ w6 O$ ^' ?
// time step. The action is executed at time 0 relative to
- I( D' g d9 N$ a8 o1 K // the beginning of the loop.
. Q. {0 ^6 A( p; [0 R( W8 y
# i1 I5 h" z6 j // This is a simple schedule, with only one action that is
5 l$ `3 X8 y' g7 p // just repeated every time. See jmousetrap for more8 y" C+ d" L: S
// complicated schedules.6 }4 v0 N5 r* _: N# R3 x3 t
9 w( H& x- b1 u% [- c
modelSchedule = new ScheduleImpl (getZone (), 1);
' C `: C* q& l9 J modelSchedule.at$createAction (0, modelActions);( y- j- n8 V& l* A3 ?; W
, |& T0 y: o& C/ K% ] return this;
. U' t0 q3 `. R! f } |