HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, y5 J/ O' b w8 Y
5 A/ I( L8 A; \+ {9 Y! r6 x public Object buildActions () {
6 y) e ?; K, s. {8 L super.buildActions();
6 c) s& R: W: D" I) }0 n! f7 D & R) A# b4 S: S b; N% n! x3 X
// Create the list of simulation actions. We put these in v6 K$ m! J) n$ l' L! q/ V- w1 U
// an action group, because we want these actions to be" Z+ y F4 w: V5 Q1 b
// executed in a specific order, but these steps should
1 H- j7 e9 }2 |) H' D& d // take no (simulated) time. The M(foo) means "The message
& h) H' i p- t1 i$ m9 x // called <foo>". You can send a message To a particular
+ s8 y% l% C2 x N$ X, n // object, or ForEach object in a collection.
S# E' S) N7 |5 i / m5 D2 {& ~) g" G* R/ e8 G
// Note we update the heatspace in two phases: first run( q: V' T5 B* `1 t9 W2 Z0 q: E
// diffusion, then run "updateWorld" to actually enact the
4 X# A5 `' |0 h z3 r // changes the heatbugs have made. The ordering here is
, u, x( B' G' d; [2 {6 Y // significant!
* e: c# I; [1 A " R4 s* e( p1 B
// Note also, that with the additional3 M( j9 }. c% v
// `randomizeHeatbugUpdateOrder' Boolean flag we can
I D2 f4 ^! A7 _' N" C // randomize the order in which the bugs actually run
8 v, b! d) T8 n' y W( {+ ] // their step rule. This has the effect of removing any2 F, e/ N% U0 M+ C3 E# ?- P5 i
// systematic bias in the iteration throught the heatbug
" D( o4 l% G+ ~4 f; o0 q0 a) C+ a // list from timestep to timestep
- ]5 o8 G$ d. M& t9 I3 D
& l$ Z# {9 g2 v. D: ], ^) x4 q // By default, all `createActionForEach' modelActions have+ D/ U& Z/ G0 [3 U I P* K0 |
// a default order of `Sequential', which means that the) Y7 ?3 m6 B. x, W
// order of iteration through the `heatbugList' will be
. Y$ i) r! {, F% ^ // identical (assuming the list order is not changed/ H: V% S8 \! o) @" y: ?
// indirectly by some other process).6 Z' J- ~7 A+ V5 O: G; O5 D$ m
- U( f5 [& ?1 u! e( J; a
modelActions = new ActionGroupImpl (getZone ());
! O3 i: F" { b& h
8 u$ Y1 c7 H' V) K1 h6 ` try {
9 D a: n3 T1 Y1 n9 T; H modelActions.createActionTo$message
6 C/ V) a9 j) K (heat, new Selector (heat.getClass (), "stepRule", false));" }$ J- R! _8 P: C6 h
} catch (Exception e) {
+ P0 {" @+ [ N! p' d7 H5 A) D) h5 B* G System.err.println ("Exception stepRule: " + e.getMessage ());
- _7 c, R% n! S) a# b }. `5 C% A l' f
8 y% Z& j- ~6 i# W
try {* y; G" s/ {7 V+ w1 C! s& W, r
Heatbug proto = (Heatbug) heatbugList.get (0);: H* r* |6 E' d/ O
Selector sel =
+ ?& J9 p/ n ~* }) k new Selector (proto.getClass (), "heatbugStep", false);
, ^' b4 @& D; b actionForEach =
$ J/ w6 j' ?1 I: Y modelActions.createFActionForEachHomogeneous$call+ e) ^4 k7 |7 x; E' w9 T' @8 T# k8 n. b
(heatbugList,& R+ v" \, q$ A1 W
new FCallImpl (this, proto, sel,8 N) I) c, @" d
new FArgumentsImpl (this, sel)));% z: ?" j1 {. b. G4 P/ J
} catch (Exception e) {
' d( b8 _& u9 `- v# N1 X e.printStackTrace (System.err);5 }5 Q J& |9 x+ t; V4 N9 G! f$ ]+ S
}
4 a. e- R" U4 d ( f, \# ?# j, L& l! s7 L
syncUpdateOrder ();
9 L! u9 \2 {1 a1 ^$ A
' P4 I& O; ~0 _! Q' U( Y+ L5 _. j try {
4 x: a5 d- z$ @! T5 V( U9 Z modelActions.createActionTo$message , H$ I- ~( v# t3 I0 | P
(heat, new Selector (heat.getClass (), "updateLattice", false));
0 V; r5 j3 Y7 [7 S: { } catch (Exception e) {
5 G* l! v" q# [7 B7 _/ y System.err.println("Exception updateLattice: " + e.getMessage ());3 {9 G( t' v( T7 A( m
}
, z$ e. d, }7 c, N/ x$ R. u ' K6 e6 _* `+ V
// Then we create a schedule that executes the
, ^. S) w; {- i3 q: L // modelActions. modelActions is an ActionGroup, by itself it& ^. f6 d) I& O& _4 }. m
// has no notion of time. In order to have it executed in, `% f- }( t4 [: p$ p
// time, we create a Schedule that says to use the
- ?3 F- R9 U( E8 n" p* A8 ~, b$ u1 G: i // modelActions ActionGroup at particular times. This, F9 u9 B& u; F8 a- F4 N
// schedule has a repeat interval of 1, it will loop every: x6 w; d- Y- v. r. u% f
// time step. The action is executed at time 0 relative to% W7 r/ \" l' L) i7 I/ |" s' _- ^! _
// the beginning of the loop.2 r6 y5 Q2 p7 o- I% x
. X# [$ I; `9 y // This is a simple schedule, with only one action that is, B0 b( P# o0 p" O
// just repeated every time. See jmousetrap for more6 d0 J* G- e+ W' O) e! E/ _
// complicated schedules.
! J& `1 c" R! d) `( L3 V
2 s3 f' E' \7 H2 z' f' x modelSchedule = new ScheduleImpl (getZone (), 1);
4 T! H' f+ X$ o. ~0 E3 ~, _3 R; V modelSchedule.at$createAction (0, modelActions);
# O. y; R( t7 D+ I7 @9 y7 A + |, o1 v; ?/ a7 e
return this;
( D% p2 d% u5 ]* Z } |