HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" f5 I H4 t# K8 Y$ B( u
& p( [8 j6 H/ b
public Object buildActions () {
2 v( z1 d6 U' d0 s* j. j% L: ] super.buildActions();
2 |* o: o' z" |- q7 h # l8 n& q5 a& E
// Create the list of simulation actions. We put these in
6 a/ G; X* g' Z. y$ H // an action group, because we want these actions to be
* k: Z4 J# B s- T# @, d+ ?" h/ Z // executed in a specific order, but these steps should/ _4 e( I) ?) i) ^* Y9 v. N
// take no (simulated) time. The M(foo) means "The message
0 S- n6 J V, s/ | // called <foo>". You can send a message To a particular' {$ U0 o8 s2 N9 H' q+ i
// object, or ForEach object in a collection.
Q6 m" |2 J, Q, T) F 5 U& E5 V1 g0 G! b
// Note we update the heatspace in two phases: first run
( A( c3 y/ v4 U" \ // diffusion, then run "updateWorld" to actually enact the
7 R0 J" I P/ e9 h, r- z8 F5 O, I' e! C // changes the heatbugs have made. The ordering here is
0 _; g* h9 T$ ?/ z, p; @ f // significant!" l( b' E8 o1 c0 |' U7 v, H3 E z
0 `+ x' k) M6 m @ // Note also, that with the additional
6 k- e3 x5 S1 i1 x4 c5 i // `randomizeHeatbugUpdateOrder' Boolean flag we can
# u; Y5 [& I: k$ N& [: U0 J: B // randomize the order in which the bugs actually run, Z% p# s5 j# I9 J7 i+ z" n
// their step rule. This has the effect of removing any
- X; Y# s# ]+ A: | V // systematic bias in the iteration throught the heatbug' u5 f( ?1 ^/ k$ K0 W' r
// list from timestep to timestep# p! v9 V5 y- D7 ^% K) O
6 Q s) x6 V" ~6 X3 Y // By default, all `createActionForEach' modelActions have
3 h+ v3 z5 S/ Z: O) W2 Y' T // a default order of `Sequential', which means that the
4 k' i" l: p- a. ^- r! J0 [ // order of iteration through the `heatbugList' will be+ R8 y# b7 W' [1 a7 N
// identical (assuming the list order is not changed
* P. G2 G" G7 f. T9 F, p1 B // indirectly by some other process).6 x- k: ?7 ?! @; y2 [
& f. ~. v6 M# }- |
modelActions = new ActionGroupImpl (getZone ());
) q2 ]- i: f4 s, n1 F5 a$ k- j; `0 m
try {
$ f% k X5 R9 q modelActions.createActionTo$message8 N( F& g4 F; k5 p; K0 z# g
(heat, new Selector (heat.getClass (), "stepRule", false));9 Z K/ t' Q# ?6 f: W Q4 P
} catch (Exception e) {" k* a3 X- Q" W9 o# X M
System.err.println ("Exception stepRule: " + e.getMessage ());: q7 b" F" n; N* \; D
}
2 k: G* b; H% p; Q1 _9 i9 d$ X) U' @" a7 w- c: J& S4 }. U
try {, ~& F! T0 E& J, Q! t8 X' L
Heatbug proto = (Heatbug) heatbugList.get (0);
3 ~& I9 Y f8 y! p1 t8 N Selector sel = 5 `/ N# a$ |6 h# t
new Selector (proto.getClass (), "heatbugStep", false);9 F' H9 A% p! J6 n
actionForEach =; ?' p3 t. y5 Y% _ @) j
modelActions.createFActionForEachHomogeneous$call
! E3 @+ f) {& j+ c) y7 K3 x (heatbugList,( g0 ~8 z& g$ X* z. d$ M2 |) t
new FCallImpl (this, proto, sel,: `# f* i$ ?( V
new FArgumentsImpl (this, sel)));0 @9 Z( L+ ~ e
} catch (Exception e) {
' s4 z3 a& k' L* m, {# B e.printStackTrace (System.err);
4 y( R' y0 l/ l, N }& k% j+ @0 }6 ^9 W* l
# J4 b- U: |7 p) u: Z/ t' E
syncUpdateOrder ();
* `- R) P! l0 g: F
$ ~. K1 V6 ~# O- b. j) _ try {9 r% j& o* D$ A+ W7 Y3 G2 G
modelActions.createActionTo$message 0 c" ]1 y/ I( f# E2 f
(heat, new Selector (heat.getClass (), "updateLattice", false));7 ?: U: |3 e; I8 |# M
} catch (Exception e) {7 V- O1 k: E/ _, f$ U2 L
System.err.println("Exception updateLattice: " + e.getMessage ());3 m+ E) S* m7 E% {5 M
}) ]1 S+ j0 x5 [4 ]" u$ v
+ b7 m I) J7 d0 |+ K
// Then we create a schedule that executes the
9 \. @+ z9 j" I! h6 f+ x // modelActions. modelActions is an ActionGroup, by itself it5 i( w; u7 g2 p
// has no notion of time. In order to have it executed in* P5 w }/ x+ X6 r5 a! ?! g
// time, we create a Schedule that says to use the
/ a/ a0 c3 h6 L: ^5 z7 J // modelActions ActionGroup at particular times. This
2 d( i/ L Y2 i* k // schedule has a repeat interval of 1, it will loop every
6 Y% B7 q: C9 x // time step. The action is executed at time 0 relative to( N1 A* i0 B1 F) Y
// the beginning of the loop.
8 g' H1 A' J7 e% s1 s
- G1 C& B8 J* [ // This is a simple schedule, with only one action that is
% k; k9 T" x' x. Z // just repeated every time. See jmousetrap for more: M) J2 @! B2 {
// complicated schedules., y" o; m) j# h4 b
( I6 X9 }$ G" g) w" l
modelSchedule = new ScheduleImpl (getZone (), 1);
% C" x% E0 U a! U8 f: p, e$ z modelSchedule.at$createAction (0, modelActions);
6 t. u2 e( ~9 f+ a$ }
( c4 ~' j( A+ G return this;
9 T+ d/ W8 V! x7 `1 H0 h } |