HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 L; h0 ?' h% d8 Y
* ~4 |1 e3 o7 a# B' f) J& K% k+ i public Object buildActions () {
6 J6 h5 v# ?8 B0 _, V6 [& l super.buildActions();1 ~* ]. y" N8 ~$ J7 C! k0 R
) i4 Y8 g; s' l9 [. A // Create the list of simulation actions. We put these in
, G. X T7 W5 l3 a7 K/ N // an action group, because we want these actions to be: g. ]) c3 @. R9 f2 A
// executed in a specific order, but these steps should
: K6 E! q# A7 e. h9 `; }+ ^ // take no (simulated) time. The M(foo) means "The message
* Z- t; ^0 C: y, t: q, X: j0 v // called <foo>". You can send a message To a particular- k; r8 H; \) e0 O) _
// object, or ForEach object in a collection.8 p0 z9 @/ f# R' K x
0 q1 h( h' X7 h% Y5 r: } // Note we update the heatspace in two phases: first run5 W' o0 s$ T( z5 ~) m
// diffusion, then run "updateWorld" to actually enact the
5 p" V% i; r, J% y" O. w // changes the heatbugs have made. The ordering here is i* A# X6 A5 {- d7 Q' }
// significant!
* ?. c. n1 [. J , V: Q3 E& B* H9 v/ a8 s: V
// Note also, that with the additional2 t* F) k7 E- F5 h( q: C+ R, [" \
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 B A9 z# G4 }5 {6 i! \& v% ?. m
// randomize the order in which the bugs actually run
. E- e- `* }6 b \/ n$ t // their step rule. This has the effect of removing any5 c* {- w& [$ B/ b7 }( B
// systematic bias in the iteration throught the heatbug' y" {" u# {& O
// list from timestep to timestep
! T, i0 ]' c9 A) [9 |0 I
7 [0 r) H' F4 f2 l5 t // By default, all `createActionForEach' modelActions have& ^1 K% Q! I, E5 W$ p
// a default order of `Sequential', which means that the
( g0 Z1 e4 @) z# S' H // order of iteration through the `heatbugList' will be
! m% e' F9 r- n$ t7 i' y3 h // identical (assuming the list order is not changed$ a, G1 M1 I# T' \, z" }
// indirectly by some other process).% w. l4 F2 S2 `5 d$ U# N6 G
, u2 r! H8 v, t7 x modelActions = new ActionGroupImpl (getZone ());% D6 k* N! k! I G( |$ @7 o
' c; Y' L5 }+ {- B1 P* V6 X: j' ? try { ?! z: S5 d8 n# B7 @
modelActions.createActionTo$message# ~1 d3 N! e- c" a+ v
(heat, new Selector (heat.getClass (), "stepRule", false));- C. a5 r' u8 U8 A& j, J8 I+ ]
} catch (Exception e) {
D R# Z. [' X1 y; i System.err.println ("Exception stepRule: " + e.getMessage ());; x6 t. H. \1 O5 Y
}! p X) ~0 s5 u* y/ [4 [
; k! ?( [6 B2 S0 B- j try {
% U) W& D$ R* R4 q# _' X Heatbug proto = (Heatbug) heatbugList.get (0);
- h Q: B; q: Y4 f) {: P" I Selector sel =
% ~4 b$ u8 I) g2 c- i6 r3 r" m! V0 D new Selector (proto.getClass (), "heatbugStep", false);
) ]! o/ z+ ^7 } actionForEach =
- |# B+ M$ D6 h! W$ K modelActions.createFActionForEachHomogeneous$call
% t4 G+ D7 T7 \6 y# ]) ?1 N9 Y (heatbugList,
5 E$ r: I2 H) r6 Z! J. J new FCallImpl (this, proto, sel,! t5 V0 w/ S3 ^- J
new FArgumentsImpl (this, sel)));
" v/ s1 F. q7 W+ S } catch (Exception e) {5 i! H$ O' @. b2 G
e.printStackTrace (System.err);
: w, [' b/ i e A+ ^) @ }
: `! Q, U9 h+ J$ A0 g 9 ]0 f- I* q8 [ @. d7 ^; K
syncUpdateOrder ();- ?& i5 T$ P: @: L" x! V
7 B% E% a2 S0 E( E( Q' d H M try {
- h H9 L" c7 u$ i7 B6 o2 O1 Z modelActions.createActionTo$message 2 [7 d9 M+ F5 b6 `9 H
(heat, new Selector (heat.getClass (), "updateLattice", false));
^8 m+ c% j9 k' K) w5 U/ H7 d } catch (Exception e) {
+ L L v! b# S) Q) l: _9 m+ B& ~ System.err.println("Exception updateLattice: " + e.getMessage ()); g6 I. G# q& U' Y( e
}
p- W7 b$ \7 L ! T) q( M* d3 m" v
// Then we create a schedule that executes the" D0 S' o0 x1 u6 c5 A4 Y
// modelActions. modelActions is an ActionGroup, by itself it
# V6 A2 o: ^: `5 Q8 P // has no notion of time. In order to have it executed in
; t7 \. b! g0 j/ w0 _ // time, we create a Schedule that says to use the
8 D5 C1 r3 z; w7 \7 t9 L9 G* L- k // modelActions ActionGroup at particular times. This+ q& p7 `5 f- W1 n3 n# t
// schedule has a repeat interval of 1, it will loop every2 @( M* X$ o% n8 R8 \
// time step. The action is executed at time 0 relative to- [, f' _ z7 c) K
// the beginning of the loop.
8 ^+ p/ X6 `! f1 x' b- H$ r) d$ S
0 A1 \6 a! O4 M // This is a simple schedule, with only one action that is
( p z- @, ]! v) {9 s7 F% M- j // just repeated every time. See jmousetrap for more0 R u9 B1 c9 e J+ Y3 c
// complicated schedules.. D' Z7 w. R; o A [
3 h6 e" L; d- R2 i. _
modelSchedule = new ScheduleImpl (getZone (), 1);
( P3 S* U4 ?5 _. R8 a; Z modelSchedule.at$createAction (0, modelActions);: s0 C% A Z; G$ I" u
; V) d) _% r" z3 E" z# v" ^
return this;8 p! }1 C7 Q1 }7 e. x3 C
} |