HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ w; A. s1 J. U* B7 U) L
1 {, c: C, B, D) | public Object buildActions () {
6 `9 i9 P7 q! E# J super.buildActions();
2 d. N! Z2 X$ i) u$ g3 S 6 N5 h" B0 q6 A* [9 X5 O
// Create the list of simulation actions. We put these in6 c% e3 s3 N/ @! ~% _
// an action group, because we want these actions to be
/ o, q4 P0 R. ]& K- p1 s8 s // executed in a specific order, but these steps should
6 G3 [2 j# A& c- x! z // take no (simulated) time. The M(foo) means "The message
8 J, W L z9 a9 [& l- o // called <foo>". You can send a message To a particular6 J+ C" j' E4 Q
// object, or ForEach object in a collection.
& `0 X \- @3 y$ p$ ^) n: U( ]3 G/ f o" U. x! c+ q9 s. d8 j
// Note we update the heatspace in two phases: first run
5 `0 w5 ]2 ^# h% [; [7 Y C // diffusion, then run "updateWorld" to actually enact the- L* \$ s/ l6 \
// changes the heatbugs have made. The ordering here is% F+ a9 _, Z6 x! Q h- j
// significant!# _: R: y( V5 p2 G6 ^" [
! ~* h: |. H9 k/ w0 f
// Note also, that with the additional, n9 m, r& `* p+ w& c$ y3 l: l
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 ]" e+ ]6 V( l# W& m# s% P // randomize the order in which the bugs actually run2 X; G- \6 Q0 l
// their step rule. This has the effect of removing any. Y6 F0 t( X$ C M$ g. K
// systematic bias in the iteration throught the heatbug
+ n! ?6 ?- k ^0 W! I // list from timestep to timestep
A+ ^$ N* t& e! {- _ 7 f' A) X' o0 T+ x* S- J ^
// By default, all `createActionForEach' modelActions have
i( [6 |+ M8 |) g4 J // a default order of `Sequential', which means that the
; x3 N# g$ F* @' p- a j5 m // order of iteration through the `heatbugList' will be
u5 z1 f" ~) u. v2 O/ C, B // identical (assuming the list order is not changed
$ u J7 Z$ U5 x& G: t9 Q+ u // indirectly by some other process). g( Z" s+ U: [/ s) s
% z a L: a& S( y( h) R. z x
modelActions = new ActionGroupImpl (getZone ());
$ O1 f! D! E5 }* S# w* r8 k4 s8 X
# n+ U$ d1 z7 v9 v1 j. |1 D try {+ B! z- H7 G7 F* l7 M6 @
modelActions.createActionTo$message
6 E' q R# S2 v% u7 { (heat, new Selector (heat.getClass (), "stepRule", false));
. ^, u! w3 b* T3 R- T } catch (Exception e) {; @2 P) w9 x' K/ t. ?
System.err.println ("Exception stepRule: " + e.getMessage ());
7 K7 A+ P' H2 Z S# o e" z }
: E3 W) }+ t0 c# T7 S8 y4 ]& ]! B0 T) } c+ P
try {8 W) R$ ~' L! J$ S
Heatbug proto = (Heatbug) heatbugList.get (0);
6 v t' x: s& y8 h Selector sel = n; g9 L3 v) r; K9 Z* E* o/ X
new Selector (proto.getClass (), "heatbugStep", false);
4 f( s& s4 `/ o' |6 Q actionForEach =
+ @5 m2 k$ L1 ^8 f/ o modelActions.createFActionForEachHomogeneous$call
; |# n8 S) r# P5 v! J- n (heatbugList,
- n# d3 D6 S( v3 |! r. d) v! o: w new FCallImpl (this, proto, sel,
5 ?# a) O7 j& u6 h& f; } new FArgumentsImpl (this, sel)));: V; v- C0 ]6 g0 ^; |2 O. ^
} catch (Exception e) {3 l2 W1 Z6 J0 {
e.printStackTrace (System.err);" B9 `2 }/ ^! P4 l/ H/ R* H
}
3 Q1 ]+ q6 c. b! h/ u
" U8 j9 F: O8 p5 g$ e% }# U! K syncUpdateOrder ();" W0 a' ?& b% E f5 R
+ E) ~/ h5 ?, b try {
- ^- p: y, O; q modelActions.createActionTo$message ) E$ w9 w8 d% b n3 T' \+ y4 Q. F
(heat, new Selector (heat.getClass (), "updateLattice", false));
% L Z7 @- J& U7 K } catch (Exception e) {) o7 [& P3 q6 d: o# ^
System.err.println("Exception updateLattice: " + e.getMessage ());! V& V4 G- c# ^ Z, K' w: o, M
}8 l9 _% r% Y# S# | ]1 w0 G0 C
# r) B! e4 }; a5 m( g* N% d9 K8 K // Then we create a schedule that executes the
: w$ n7 y7 ]5 D. ]6 H- S // modelActions. modelActions is an ActionGroup, by itself it2 I5 k5 m7 y4 p
// has no notion of time. In order to have it executed in# q; W9 `, u7 o$ k0 p, ?
// time, we create a Schedule that says to use the( \: B& e' T) S1 E" _+ s% A
// modelActions ActionGroup at particular times. This9 E* a* A- I0 q& S, Y
// schedule has a repeat interval of 1, it will loop every
6 @! D' z! ^; |5 s3 s // time step. The action is executed at time 0 relative to
7 r$ r- h6 _* j( }2 M+ y) ]: y // the beginning of the loop.
% f3 E" w7 o) x9 g: S y4 m
. S! O0 U4 Y9 I5 h0 a+ f9 a* O // This is a simple schedule, with only one action that is
# a) A2 e p$ k1 u7 D // just repeated every time. See jmousetrap for more1 Y' x! w: f, c) A ^ @+ Q7 b: @5 I/ k
// complicated schedules.$ U3 ?+ o: |2 B7 F
6 ?" t8 t" r' |) Q' P9 m modelSchedule = new ScheduleImpl (getZone (), 1);3 I2 f4 g6 e7 ]
modelSchedule.at$createAction (0, modelActions);
/ J% {6 f3 U: g0 { }4 F8 ]1 P( T) G 1 u8 q/ U1 _5 T7 U f7 T' ?
return this;
$ j8 W. F# p! b" { b% v3 j } |