HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 m* n" P: V! Y1 A& A' A* E5 R
6 p3 w( T; v' }. a, W0 A/ R
public Object buildActions () {
' r/ e1 E5 Q1 e5 i9 x super.buildActions();
6 J' V, y k- Z6 D. l & s# H+ q- _ j- D
// Create the list of simulation actions. We put these in& o8 i; I% A; F/ Y; s
// an action group, because we want these actions to be
2 J( o% b; n, Z( Q0 W // executed in a specific order, but these steps should
" k! C0 J2 U; C1 Z- g8 W // take no (simulated) time. The M(foo) means "The message
% v2 F1 L0 c+ W: l2 p9 M* V // called <foo>". You can send a message To a particular/ S# v* Y, |" p- \$ K
// object, or ForEach object in a collection.1 o3 S0 D! j! F* P3 _
# L0 y) |% p' t v* f& C2 e" a // Note we update the heatspace in two phases: first run! R6 Y9 N) t o
// diffusion, then run "updateWorld" to actually enact the
$ B" o8 c$ Q* W // changes the heatbugs have made. The ordering here is
R; s. a+ U& R$ [; v6 P // significant!
7 t1 r+ L! N0 p 9 O# P$ Q1 j2 d: O) h- v' R! b
// Note also, that with the additional
3 \9 O5 ?+ I9 |1 X // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 O- g; A- W9 _; E // randomize the order in which the bugs actually run2 Q& E: g7 z( W# k0 g8 L6 B. l) T
// their step rule. This has the effect of removing any
1 j* [7 F$ p+ \) a7 T! ~ // systematic bias in the iteration throught the heatbug! Z2 S/ N+ o" Q, T' c+ v) a9 G7 E
// list from timestep to timestep7 d# P% V9 L* d: @) }9 e' X% N
( V$ ^& X9 i" [' l8 Z- b // By default, all `createActionForEach' modelActions have/ ^+ K+ I( P9 w/ y4 _
// a default order of `Sequential', which means that the
8 z" `, g- a3 ?5 Z // order of iteration through the `heatbugList' will be! t. C, g8 \- s+ J& d7 d3 \
// identical (assuming the list order is not changed4 U) N& W. O7 v
// indirectly by some other process).
- d o& Q; [- ~+ X! ^ & Z& b: e8 P; X d9 u
modelActions = new ActionGroupImpl (getZone ());
% u2 [9 y# y) o5 Z) [6 a' f
9 u" D" {3 g$ o3 ^2 j9 A$ Q try {- T" _1 s/ H; V3 r( Z
modelActions.createActionTo$message0 \1 F5 X. N4 D3 W' V+ v7 Z: ]: y0 k
(heat, new Selector (heat.getClass (), "stepRule", false));+ q3 b* h) u% {
} catch (Exception e) {
5 Q8 v- h% M$ p5 `0 |# c2 N! { System.err.println ("Exception stepRule: " + e.getMessage ()); x% I! I" v2 D. A
}) l: E! @. K! X/ q% r/ {7 E
: Z, I! Z. u# i& C- e try {( X( Z$ j& K9 L, t! l) w
Heatbug proto = (Heatbug) heatbugList.get (0);
/ S& H1 n; x6 M0 F* _. c9 J Selector sel = 7 ]. l, A/ r5 N4 j; x1 g
new Selector (proto.getClass (), "heatbugStep", false);
' o! M- }- ] ~. `4 t actionForEach =
+ K8 D+ C2 i& {2 M) v9 } modelActions.createFActionForEachHomogeneous$call. ]$ u/ M' w5 e4 t! ^5 ~6 _/ z
(heatbugList,
" O. y: H6 R1 A8 _- \7 T new FCallImpl (this, proto, sel,, i) C6 Z C3 e( J3 L
new FArgumentsImpl (this, sel)));
- m) y0 Q: ]) ]' K' d" j* ? } catch (Exception e) {
& R; ?" n2 v G4 j$ x- j5 p# U e.printStackTrace (System.err);3 N5 {: n9 Y! Q7 y& |# l
}
2 U$ w; O' M- d5 l1 z8 z , D R! k1 t+ F! Y I
syncUpdateOrder ();
8 R: o7 |# v& o; [* E. B
R3 n& I3 D: n& I try {: T) W5 j2 f/ m; _( h# C+ C( X( [
modelActions.createActionTo$message . y8 j5 W; S0 x
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 }& z) I0 Y7 k2 A } catch (Exception e) {
1 a2 y: x) ]) n System.err.println("Exception updateLattice: " + e.getMessage ());: [ u4 c: b5 p% A1 B2 U4 y; p
}
: k6 W9 @; ^, @# C2 h
9 j& K; C! l5 Y; d& J // Then we create a schedule that executes the$ F1 O0 x7 f# s7 x
// modelActions. modelActions is an ActionGroup, by itself it" }1 }* G& C0 o; V/ u5 H8 j: p0 `
// has no notion of time. In order to have it executed in
7 e) h7 K, J9 o/ z // time, we create a Schedule that says to use the
/ V: p1 s" V' g/ ] // modelActions ActionGroup at particular times. This
5 Y- G9 J; [$ I# R+ p3 g& g# o // schedule has a repeat interval of 1, it will loop every! M/ U8 A% y' t2 e4 G+ b6 ]6 h( |
// time step. The action is executed at time 0 relative to# V: n' @* x' Y) G7 z6 O X8 K
// the beginning of the loop.
2 ^* z o3 \7 f
" B# d7 ?! o6 F" l9 A+ [0 s // This is a simple schedule, with only one action that is5 j; \9 u; d. p0 O9 z$ H
// just repeated every time. See jmousetrap for more
" @; B/ |. C! A% Q$ R0 i2 m // complicated schedules.6 L% g6 `1 h) k/ a" R
& u4 I6 B& m( n) K2 {
modelSchedule = new ScheduleImpl (getZone (), 1);
C& V6 F) A" e modelSchedule.at$createAction (0, modelActions);
' g7 a( D8 v3 [) Q( K+ Q * E4 [8 C: \7 |3 p" ]5 c+ Y
return this;
2 N4 R8 \! S2 N& Y } |