HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# y+ i5 n" K! Z
4 c" ^0 D) T: k4 U/ g4 V) ~# w' b
public Object buildActions () {
& P* D" v' P) k7 S: [) z super.buildActions();
) g% K) R; C' Z# P. D ( G& k/ V1 ~ {7 t, N1 L
// Create the list of simulation actions. We put these in) o/ V* F8 p4 Z0 a
// an action group, because we want these actions to be- G/ f( m+ u% A3 x
// executed in a specific order, but these steps should
4 I7 }# p: v W* I, m // take no (simulated) time. The M(foo) means "The message
\6 u* ?5 w6 P7 I- h* X // called <foo>". You can send a message To a particular" r2 U6 j; \! E n. _3 C! \
// object, or ForEach object in a collection.
# |4 @# N5 s$ j5 Z 9 F9 p; V3 f; R5 `2 P) ^7 G
// Note we update the heatspace in two phases: first run
6 v* i, w: ^4 k+ \5 C& x // diffusion, then run "updateWorld" to actually enact the& }2 E( J' p) _/ v. Q2 a( J
// changes the heatbugs have made. The ordering here is. ~8 z! d4 i8 Y7 a
// significant!
% P7 g& r$ A9 [& T w! K
! W, z; X% F9 n, |" V // Note also, that with the additional
' E) d$ d% {/ z5 K% ^4 r // `randomizeHeatbugUpdateOrder' Boolean flag we can8 M8 k" E: d: U2 c! t+ q( r( T
// randomize the order in which the bugs actually run6 j, U: Q$ D9 u7 d
// their step rule. This has the effect of removing any! P" \9 D' J$ ?
// systematic bias in the iteration throught the heatbug9 n/ \- l" B) x. S q
// list from timestep to timestep' {( d; ]( Q3 m" M) r I$ a: B
! x* w; H% ]! f0 j // By default, all `createActionForEach' modelActions have
# Y3 a# ?3 ~# `1 i // a default order of `Sequential', which means that the
8 V& }* {; |! P" p2 Z // order of iteration through the `heatbugList' will be
: O L9 h; q3 \; b# q; g // identical (assuming the list order is not changed5 Z* P0 Z; { v# D
// indirectly by some other process).( p% {8 z9 C& c3 ]* X
3 \! N( x2 @$ i6 f
modelActions = new ActionGroupImpl (getZone ());* n0 m& C, C2 X) o
8 X. L% o! d2 N' T }" U" i try {
{2 q1 T4 S, O% r+ O modelActions.createActionTo$message; [7 L9 P+ l: l" [- A3 \4 K, d
(heat, new Selector (heat.getClass (), "stepRule", false));8 D. H2 ]! E* {) I0 n0 O
} catch (Exception e) {
% }$ r. {% z: e) s5 U System.err.println ("Exception stepRule: " + e.getMessage ());" V- G, h0 V1 o- |. t
}
6 A2 h3 r+ g. K- F8 g5 A$ ?
, @; ]/ R# a4 c4 \ try {
( p2 p( z. V7 g4 W$ P Heatbug proto = (Heatbug) heatbugList.get (0);
6 z7 `& V* y; v7 k" d Selector sel =
6 q; Q$ f6 N) J$ g new Selector (proto.getClass (), "heatbugStep", false);
, y/ W+ T% w. O, r2 a( B/ {# v actionForEach =* }0 Y/ U+ q0 Q: A( s2 N$ l
modelActions.createFActionForEachHomogeneous$call# g4 [" q" f+ k8 A+ ~
(heatbugList,
f/ t3 b+ V1 g+ w( ^) h2 L new FCallImpl (this, proto, sel,6 R' W$ a$ C7 p( h" T" O: W
new FArgumentsImpl (this, sel)));
9 U G( S# \1 S6 g } catch (Exception e) {
+ y# F$ t* E9 ^( x/ s e.printStackTrace (System.err);+ M6 `7 W" y# l' W8 m9 P6 ^8 v
}. c* A: s* Y/ Y: c* c7 r; y; Y
, K" U# R) g2 Y5 S9 r2 [2 o3 N/ _
syncUpdateOrder ();
: N- K' U3 Q, t7 _
8 d/ Z% I* q) q8 z: w7 Y' y/ z try {8 E) D: B* o0 [' z3 q, Z
modelActions.createActionTo$message / b( j0 N" v" e7 X3 _7 s
(heat, new Selector (heat.getClass (), "updateLattice", false));
% K/ e* V( j0 _& e' ?" [5 N" i } catch (Exception e) {/ ?6 {% K4 E. a: |, h6 Q
System.err.println("Exception updateLattice: " + e.getMessage ());% w& K- l9 H! e, {/ c3 j
}0 ]" u K' O! v, X6 X* f
9 ]0 w6 I; N* }0 {8 [
// Then we create a schedule that executes the
$ l- h. U, }1 M$ ~, j" `+ [1 f // modelActions. modelActions is an ActionGroup, by itself it
% p( @1 ~# M: s4 X7 u // has no notion of time. In order to have it executed in
9 q8 ]! t: t' Q3 A* o( S* I/ z' T // time, we create a Schedule that says to use the7 V j9 C5 f& f) w
// modelActions ActionGroup at particular times. This
" e5 h" W' t2 g v! }5 I5 h // schedule has a repeat interval of 1, it will loop every
' ~. A% h [$ h) \7 X7 L // time step. The action is executed at time 0 relative to' Q) W8 t. |6 r+ R- l4 E
// the beginning of the loop.
4 x/ \3 G# v/ C( k" y& }8 z
# m$ z$ T; `8 v& f) {2 _2 [ // This is a simple schedule, with only one action that is4 z g$ T: C8 }7 M# \) I$ S
// just repeated every time. See jmousetrap for more
, }1 D" a9 }/ Z8 x% D8 ~ // complicated schedules.
, B% }, e" B" s( H8 x& M( f
3 m( b$ s/ g; k" w! t; l modelSchedule = new ScheduleImpl (getZone (), 1);$ i' K; b( c- n: s! ~& i
modelSchedule.at$createAction (0, modelActions);
) \6 j5 L5 [2 b! \. a
6 C b0 z) Z' X5 S' a0 t return this;5 ?+ g, u$ @) q
} |