HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 ?5 P; a8 N1 X# {' X
3 L& U6 `. B+ Q0 C0 }7 x* r
public Object buildActions () {
5 l+ l$ `+ j8 t% K; N3 [/ R super.buildActions();8 I" D& i8 k4 _2 o6 R }, e# Z
`3 d7 s5 p% `0 S \
// Create the list of simulation actions. We put these in
; s# p# r. p1 d. x& q$ Y5 l // an action group, because we want these actions to be
- r* o4 l* b% z // executed in a specific order, but these steps should; i/ \3 y- ^( t' L! t; r0 c
// take no (simulated) time. The M(foo) means "The message
# G4 m+ F+ z: |, r // called <foo>". You can send a message To a particular- i" U5 w2 c* i: d2 V4 }9 ?
// object, or ForEach object in a collection.
9 Y4 ~% ^- W+ {1 w, ^ 0 J+ g/ V: ]3 L+ [/ f
// Note we update the heatspace in two phases: first run3 J; s6 D9 z. s! u: [. C6 z
// diffusion, then run "updateWorld" to actually enact the( [5 K$ O6 H; ]6 c" g, @0 d' y0 t
// changes the heatbugs have made. The ordering here is
0 k; j. V0 j& O6 k# b // significant!: C u8 C: y+ s/ M& k9 f, R7 E
2 _8 o F+ A w ] // Note also, that with the additional
3 T% l" F: |# i$ L ?5 ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can) ^4 D5 B3 u- @6 K
// randomize the order in which the bugs actually run
. u1 b# C5 i: M& w( \# \) D$ K // their step rule. This has the effect of removing any0 }( x+ Y& g, C8 l; Y6 }$ G; J
// systematic bias in the iteration throught the heatbug
9 l/ ]8 f# U0 g& {- L8 D // list from timestep to timestep
q5 L- U/ c# i. t6 F 1 L6 k( L$ x$ o7 S' C: @5 r% K
// By default, all `createActionForEach' modelActions have
: c3 K% _, b4 f. K. I // a default order of `Sequential', which means that the
& V3 ]" Q/ T P$ u5 P // order of iteration through the `heatbugList' will be
0 ]& y c2 y+ X$ |/ k4 g2 y // identical (assuming the list order is not changed8 P8 v* N4 U: ?9 D
// indirectly by some other process).
5 L3 ^ }/ }1 S5 f# z( e' M
+ Q9 T; ]) J3 A$ C; }" s modelActions = new ActionGroupImpl (getZone ());1 y7 q% v f/ H/ K: ]5 c$ I) m
- M ]0 l5 r; g( ^
try {. N- ]" A8 `& m# I# K
modelActions.createActionTo$message( w& O/ z5 M3 o$ x! \+ {- N
(heat, new Selector (heat.getClass (), "stepRule", false));# |1 k+ D N2 H8 c" J
} catch (Exception e) {* Z9 X0 O1 K" T0 J' g: s6 r
System.err.println ("Exception stepRule: " + e.getMessage ());4 i3 A7 S, Q+ i0 Y1 J, \1 m
}
3 {8 o: B9 R1 U' r1 k/ @) k$ V/ i
try {) z% W$ j. r. F! q% P+ g T
Heatbug proto = (Heatbug) heatbugList.get (0);
' Y C5 [ V. x Selector sel =
& ~- c, O# O% w7 `+ v new Selector (proto.getClass (), "heatbugStep", false);8 M& q, ?2 f" I2 G! y" z
actionForEach =5 m: a' y* g1 J1 p: g( h
modelActions.createFActionForEachHomogeneous$call
6 G3 E3 D: i6 l (heatbugList,
0 {8 G- j% g: ~ new FCallImpl (this, proto, sel,) F$ F: }" I% o& {0 p0 k# q; ]
new FArgumentsImpl (this, sel)));
7 G# [( A& v F } catch (Exception e) {
7 ^# ~/ z& B4 R- A# p e.printStackTrace (System.err);& J1 A: h- g5 V; g
}2 Z, R+ T" J" u6 \5 ^; J% l Z) g, O
5 z# |% O. Y7 L a syncUpdateOrder ();2 A, S+ R% a( w3 ~: \8 _, X
W+ E, }/ O6 S5 D w try {0 u# z1 \3 W: e7 ?1 c' T
modelActions.createActionTo$message / f$ r3 s8 w2 U
(heat, new Selector (heat.getClass (), "updateLattice", false));( H* S+ T8 O& d! K7 c* t
} catch (Exception e) {
6 h% o2 i+ ~/ r& G+ z( G+ [ System.err.println("Exception updateLattice: " + e.getMessage ());, W/ \4 Z V, R4 Q0 l, f
}
; `2 l5 h) F0 N. d* j1 z& f
8 X5 Y$ `, w, t, @, C // Then we create a schedule that executes the# P$ U2 m( x, m; w9 k: i1 D
// modelActions. modelActions is an ActionGroup, by itself it
r5 i. Z' X# L9 X2 P5 O. }* R( r // has no notion of time. In order to have it executed in- W$ j2 f& e9 R8 R% t5 g$ K
// time, we create a Schedule that says to use the
& J2 f6 N8 C9 [# W3 |3 ~ // modelActions ActionGroup at particular times. This0 o, ?0 _3 v" f; V5 J$ K
// schedule has a repeat interval of 1, it will loop every
/ b# R9 N" V3 c4 ~ // time step. The action is executed at time 0 relative to
4 v0 S+ L8 W! j7 R& c // the beginning of the loop.4 \) _! O o& `6 H4 {7 s+ W& C
0 R* ?4 k; L4 T // This is a simple schedule, with only one action that is
* K6 m- H/ d; e4 q8 z) |, P: g // just repeated every time. See jmousetrap for more1 S. T- I5 @' S& O+ M+ | l7 |. M: p
// complicated schedules.
: v# l6 i( W1 w6 L1 Q) l4 h& q3 ~" | ( P2 k9 e4 |; q6 p$ _: ~5 V# P ?
modelSchedule = new ScheduleImpl (getZone (), 1);7 V3 Y- p" e9 w7 p" K2 o( q: u
modelSchedule.at$createAction (0, modelActions);
' a; K: d- S* P
% U' ~6 m; x, K' w" Q$ f return this;; H5 B c* i2 I, c: i
} |