HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 H1 | E( ?) h: ]+ t" {( k% O. ~7 l2 t( ?# J9 I# b! W% w( g9 d
public Object buildActions () {
( O" X( b/ J) L5 }0 @) | super.buildActions();
1 z6 Q& S$ g9 Z3 F2 C$ `5 ?/ z3 W 0 i* j2 f3 C: `0 @' M6 Q
// Create the list of simulation actions. We put these in9 M, l& a S. \. i8 ^! }3 O
// an action group, because we want these actions to be4 c% x t9 D5 i- J8 F
// executed in a specific order, but these steps should, O4 A; P- m, a& C9 {
// take no (simulated) time. The M(foo) means "The message+ V. U9 d, k! N) \0 K
// called <foo>". You can send a message To a particular
7 L a0 t e1 S! m // object, or ForEach object in a collection.
+ i) t( Y7 {5 I7 t
4 G3 g" }8 [* p // Note we update the heatspace in two phases: first run9 V2 e M1 R9 }6 ]+ {: l
// diffusion, then run "updateWorld" to actually enact the" H( Y3 r& |% L
// changes the heatbugs have made. The ordering here is5 o; ^& i/ X, B8 ]% u3 o4 I" o
// significant!4 {2 a' [) y) v; I5 j
6 ^0 I* v4 t) m2 O' v
// Note also, that with the additional7 \8 O' e. D& I0 y0 d
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; l2 M6 N$ Q' e7 U1 ^ // randomize the order in which the bugs actually run5 `. m7 {9 t- [
// their step rule. This has the effect of removing any
1 u& S) U4 G# a- T3 y6 [( M. e8 Y# X // systematic bias in the iteration throught the heatbug
6 A5 X$ [5 p& C6 d# ]4 t // list from timestep to timestep
* m% _2 V5 ~$ M) n0 Q2 e5 D 1 ?" _2 M8 S+ i+ Z/ A, r8 r) o
// By default, all `createActionForEach' modelActions have
' O! L& p" Y* u // a default order of `Sequential', which means that the5 G Z9 ^2 V- }) E! d# W0 s! [4 L
// order of iteration through the `heatbugList' will be8 U" x; K* D. ?) d5 Z
// identical (assuming the list order is not changed
2 R, a9 p8 y0 ^+ E* \; z6 l // indirectly by some other process).! p# B4 U" `: w8 M. L" D
+ b* p3 a1 w. ?1 p/ M modelActions = new ActionGroupImpl (getZone ());) P9 C7 X. S- l
N# i ], j& r u
try {
! y# q N/ b: W, q5 \0 T( L! r modelActions.createActionTo$message
& O2 U7 G+ M$ W" U (heat, new Selector (heat.getClass (), "stepRule", false));
+ f2 l0 w, j5 u! y. h* C$ X5 M: A } catch (Exception e) {& @$ \1 ]4 @- R0 e: i6 \( C
System.err.println ("Exception stepRule: " + e.getMessage ());: L0 ^ Y& V! y3 z
}8 m8 F! i; C6 \9 q! e) |% L
0 f- d& P: j. { H1 x4 P5 I1 _- T
try {
$ j# P' n* S$ k' ^ Heatbug proto = (Heatbug) heatbugList.get (0);$ j9 @2 f) t! |" |( u! [( E# q4 F: ?
Selector sel =
* v: D# G _' v; C new Selector (proto.getClass (), "heatbugStep", false);
% Y) F3 Q3 b& x6 Q actionForEach =
% m4 k$ g* o$ r- @ modelActions.createFActionForEachHomogeneous$call% I8 i0 y G3 Q; M' E- d) q/ U1 B5 G2 R
(heatbugList,& E% h0 e" U4 P5 S- g+ d
new FCallImpl (this, proto, sel,
$ T+ h$ P* j! |& E; e/ y new FArgumentsImpl (this, sel)));
( s6 e3 B5 ^0 s } catch (Exception e) {9 l m8 r6 Z8 E3 B8 f4 X2 }& X
e.printStackTrace (System.err);
8 w0 ?, O' d6 ^0 [- N }
9 }+ d! B, c& Q6 \" v7 A
9 O7 b% S( C& X8 w$ @7 I ?! l syncUpdateOrder ();
" H6 U2 b( B, F1 B' o* P1 W- T1 Q4 K: D
try {
/ `( N: w& N. b* `9 @7 T1 D modelActions.createActionTo$message
/ E, }4 H# U( N8 m3 s: c z! b (heat, new Selector (heat.getClass (), "updateLattice", false));
: J# Z& Z8 K4 y, y$ W } catch (Exception e) {
6 e2 h9 v! g+ B& T/ ]% u* `9 `4 T' W System.err.println("Exception updateLattice: " + e.getMessage ());6 R- O; }7 ^+ R6 x* M
}% {9 z; X5 L, E; x+ V, B
. {2 V$ y3 S4 v3 e" Y
// Then we create a schedule that executes the# ~8 |" `" u1 @0 ~( ?
// modelActions. modelActions is an ActionGroup, by itself it, ~# O% n* K6 q! z. N* I
// has no notion of time. In order to have it executed in
8 P0 Z, ^2 d. p* V6 q* U) ] // time, we create a Schedule that says to use the
- o3 g2 n+ B2 T8 R" M) n, Q0 E // modelActions ActionGroup at particular times. This2 n% Q: w4 m7 H% ]6 y" f+ A. H. ^
// schedule has a repeat interval of 1, it will loop every
( k2 ~1 k8 e# V2 ]2 X // time step. The action is executed at time 0 relative to
0 B7 v4 J: @+ W8 e+ s1 B( K2 T: M% Z P // the beginning of the loop.
/ i# ^% V6 B8 G+ M6 D; {! w% G: E$ R" O* A
// This is a simple schedule, with only one action that is
" q* M# a8 m7 q# ?4 |( p // just repeated every time. See jmousetrap for more4 Z6 }$ l! {& ?
// complicated schedules.
5 {' L& s( T8 O( R, u 0 {9 \& R. J3 P; K
modelSchedule = new ScheduleImpl (getZone (), 1);' T' i1 v) D" ~4 B! w1 g7 s) R
modelSchedule.at$createAction (0, modelActions);
3 N) e7 O, E t
% A# P) u7 E* ~$ E8 N$ I return this;
! _0 ^) H8 F: G/ H) U, a3 | } |