HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 E" C; K* Q3 u2 y0 i$ {
) [/ q+ \9 u& {( V2 x( z% [ public Object buildActions () {1 E5 G2 l4 d6 x
super.buildActions();8 ^+ ]! L: |( [
2 O+ A/ x5 i/ z, S0 u
// Create the list of simulation actions. We put these in/ Q/ s0 P, E# d3 z
// an action group, because we want these actions to be; b# W' [1 `" e; l
// executed in a specific order, but these steps should
, m9 y. }! ^7 U6 T // take no (simulated) time. The M(foo) means "The message
: N5 w1 P) f' Q$ l // called <foo>". You can send a message To a particular/ q) Q! v' i* f8 W% P1 b) X
// object, or ForEach object in a collection.& R; |+ }9 @ z J
' w) r' F- W# b // Note we update the heatspace in two phases: first run
3 u9 b/ @3 _. X9 i0 x/ ] // diffusion, then run "updateWorld" to actually enact the7 r( I# m4 i2 R
// changes the heatbugs have made. The ordering here is
+ K4 ?& j) \1 ?* m: t2 B" S4 G // significant!( [) y6 h5 o! W/ C2 Y. \
8 S: X% o' O7 ]. B4 Y5 X9 i // Note also, that with the additional2 O- O/ q) ~, O1 H& b; |
// `randomizeHeatbugUpdateOrder' Boolean flag we can
w( U! [# C/ H4 }- Q0 d // randomize the order in which the bugs actually run
5 r2 U+ Q. T& O/ x0 v // their step rule. This has the effect of removing any5 E9 v0 F$ A0 H: X# V% ?
// systematic bias in the iteration throught the heatbug' o9 b2 `! {! L$ O
// list from timestep to timestep
7 M; c$ e8 D5 l; @% `
/ L! U* R$ b, l7 Y$ K2 g // By default, all `createActionForEach' modelActions have
8 _* d) O6 R( W9 m! ^" K // a default order of `Sequential', which means that the
3 S# [6 t3 U. k! o% G // order of iteration through the `heatbugList' will be$ P5 ]8 y8 O/ L5 C2 @2 |6 X
// identical (assuming the list order is not changed) L8 L+ E2 L3 ^, v
// indirectly by some other process).
! G+ P0 Y) n5 V, H ( {4 H, v8 u* G: \& f
modelActions = new ActionGroupImpl (getZone ());
/ W" F( c- C) ^5 k4 R
! A0 o2 [1 b ^/ B; M4 R try {
; B; B+ q) o4 n! E modelActions.createActionTo$message
/ I( v& y `! E. U( u( Q (heat, new Selector (heat.getClass (), "stepRule", false));. R/ X' Q; q" m5 d
} catch (Exception e) {6 S) D9 x2 Q6 a/ C2 X# i' k
System.err.println ("Exception stepRule: " + e.getMessage ());
. X0 L$ D6 H9 ]) u9 F }* A2 S8 ]; q9 m: Q" f; J, [/ {: T
9 Q: n2 U$ C+ \3 q; {8 K6 R7 z2 x try {
1 S. @7 q. W2 H- H Heatbug proto = (Heatbug) heatbugList.get (0);; n5 U. o# a3 p# G7 d# p2 z5 z
Selector sel =
5 y$ H1 W$ J# n# s new Selector (proto.getClass (), "heatbugStep", false);
3 g" v8 C3 t; W: W, I actionForEach =
# E0 Y. u& I) A( Q X7 _9 G modelActions.createFActionForEachHomogeneous$call# q& S& m7 B2 h! K( ]
(heatbugList, ?+ j" z& y, }
new FCallImpl (this, proto, sel,
6 t" Q+ b+ T% s' @/ m new FArgumentsImpl (this, sel)));) @2 w( @3 V: [/ O; G' `
} catch (Exception e) {
0 e- `1 G; p( O4 F e.printStackTrace (System.err);
8 I$ ?9 V9 X) p# [: I0 g }+ Z* j" A5 e) v
( t$ e. t' A _- a: T. K3 w i
syncUpdateOrder ();3 A! L6 m) T' n0 B$ S0 N
; H9 l) b/ H: P2 U: \8 Z try {" t; ~4 X- H2 `0 J3 M
modelActions.createActionTo$message 7 X2 u9 I: c! l- {3 B
(heat, new Selector (heat.getClass (), "updateLattice", false)); ?1 d% E) I/ D8 v2 c2 n+ [* _
} catch (Exception e) {: n& j' ~3 m+ P; S
System.err.println("Exception updateLattice: " + e.getMessage ());! J% C5 r4 n8 a, L+ E- ]
}
% n0 d g* @, o0 G1 v0 B 1 I6 P% ^. W* K7 P- o* H
// Then we create a schedule that executes the
& d, Z) ]9 U8 V: r // modelActions. modelActions is an ActionGroup, by itself it
, Q* m Q8 u3 Q! J- L) E1 Z // has no notion of time. In order to have it executed in
0 U* q" j1 ^6 H/ \" v. A2 V; f' V // time, we create a Schedule that says to use the
7 @2 b& T& E* s // modelActions ActionGroup at particular times. This+ p* Z7 X* S- [- d/ w
// schedule has a repeat interval of 1, it will loop every
5 \/ O) o/ H, j) V // time step. The action is executed at time 0 relative to" i0 Z" |# A2 f5 S Z# v
// the beginning of the loop.
$ d4 x( u2 @1 a5 V& L$ ^) { ]$ ~
// This is a simple schedule, with only one action that is
4 ` t& P% O, q // just repeated every time. See jmousetrap for more3 W- z5 w! O. r- t- \8 f
// complicated schedules.1 U3 l- |* p7 a; F0 Z$ W+ i. e, A
# o+ T4 U. b+ w& R0 Z% p modelSchedule = new ScheduleImpl (getZone (), 1);4 A: W7 [" `! H! K; T+ h$ k
modelSchedule.at$createAction (0, modelActions);6 d4 m3 `: z& k
& u* y/ y2 J8 s; F5 {: w' m- } return this;4 P4 }4 n6 S# _7 D
} |