HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
G* x# c3 n" M& y+ W
; f. O3 }" f. y+ {$ k) W public Object buildActions () {" R! Z- L3 L4 g. p* s" K3 W4 J+ B" D
super.buildActions();+ m# o: s) a' X" [3 [% L) h! L
# S2 R# A' K' Z- P5 J
// Create the list of simulation actions. We put these in
3 S' ^ M6 Z/ w) K$ L1 o" p // an action group, because we want these actions to be
2 _4 V# M2 W ]! e // executed in a specific order, but these steps should# I5 y+ F5 V# _+ J: ^' ]" A5 D
// take no (simulated) time. The M(foo) means "The message
( C) E! J l2 L // called <foo>". You can send a message To a particular
! m, V# ]1 _3 V& P // object, or ForEach object in a collection.
2 c9 W* k) p3 r! t: h* k6 w% U
2 x8 A! x- i$ y& s* q6 L/ R // Note we update the heatspace in two phases: first run: u" @8 h1 g) Z, ? I" `2 _% S; N
// diffusion, then run "updateWorld" to actually enact the) }. q! ]* ^- s3 d1 E' q, W
// changes the heatbugs have made. The ordering here is
9 v3 a' k# g9 X6 P( p // significant!3 b8 U$ E! K* q0 f6 M
' `' x$ Y7 c+ N4 P0 K
// Note also, that with the additional5 Q4 L8 m7 a0 p, L: G' y
// `randomizeHeatbugUpdateOrder' Boolean flag we can4 m Y; ]1 ~3 I- O0 Z4 i( W% r
// randomize the order in which the bugs actually run* ]. }) B1 J- y+ O% n# Q5 w
// their step rule. This has the effect of removing any, E" I7 G. l1 K
// systematic bias in the iteration throught the heatbug- w% M5 h- M0 P' \1 f9 }# V
// list from timestep to timestep
5 k, ~% i" c0 o% a
) p W5 s1 {7 l N; Q/ k* u. D // By default, all `createActionForEach' modelActions have8 C- b+ c7 S; J( _2 Z c9 n3 R! Y
// a default order of `Sequential', which means that the6 n$ q+ s/ D3 d! X" m
// order of iteration through the `heatbugList' will be
: E2 g; b4 I6 @6 @ // identical (assuming the list order is not changed
+ d* `- Z8 k& C2 q // indirectly by some other process).
* q7 j; m: x6 C* e+ a ' J7 H! q1 x" n6 |4 O/ \2 g% Z
modelActions = new ActionGroupImpl (getZone ());
! |% W0 T" {. L7 U( u4 `
" i% o2 }6 F1 H. P try {9 W+ D* m' d* z# ]' a
modelActions.createActionTo$message2 x3 j1 j+ F4 x& b9 Q8 M( G, k. M
(heat, new Selector (heat.getClass (), "stepRule", false));
4 r/ U0 C3 E# A5 s) A: s- @ } catch (Exception e) {
9 F" X+ P, h$ P6 A System.err.println ("Exception stepRule: " + e.getMessage ());/ q" \! o0 u& T$ j: ~& q" }+ r
}6 Q- l1 D' `( J
' D* L# ?* ~: w0 E) Y! F try {2 P: ~ Y0 ~3 Y( u' H2 @; b
Heatbug proto = (Heatbug) heatbugList.get (0);9 R7 i0 j! F5 L# L1 p7 y
Selector sel =
0 l* t5 L8 D9 q8 A, ?, B new Selector (proto.getClass (), "heatbugStep", false);6 r. N4 K; O4 o- e. r* M
actionForEach =
# E9 a) p! S0 k3 X9 g9 K' o modelActions.createFActionForEachHomogeneous$call# P2 z0 P' S) X" D& k! K1 a. H, b3 T
(heatbugList,
7 p9 G* W: p! U0 L2 B) l# Z Y new FCallImpl (this, proto, sel,8 P* U2 Z5 W5 W7 l+ u; N: ?- D/ h
new FArgumentsImpl (this, sel)));
* v/ X; ?+ v/ Z! W } catch (Exception e) {$ Y6 |. z6 p# |# q) p0 _* u6 v
e.printStackTrace (System.err);
+ q0 g0 H$ p. x6 m$ H }/ K9 e* p7 p% D* m7 ~
7 ]3 t v& t( j3 N3 Y6 t; c7 l syncUpdateOrder ();
1 k8 y D4 s) a2 m2 S4 B0 S" s' S& M {, {2 T: @, Q# o
try {
- H G' n" V9 w( u modelActions.createActionTo$message ' i3 ], k$ d+ {$ }6 r) [ Y4 S
(heat, new Selector (heat.getClass (), "updateLattice", false));) k- a# l6 ]6 u& u7 H" z( [3 L8 U
} catch (Exception e) { X7 l8 c" a$ H4 Z5 k8 z7 s1 l- o
System.err.println("Exception updateLattice: " + e.getMessage ());8 C0 S! t* S1 {1 b& X2 g
}7 A, M8 X1 t% R2 ~5 G
! }( y. L. G% b7 P4 q
// Then we create a schedule that executes the
4 Q0 s, h, \0 e // modelActions. modelActions is an ActionGroup, by itself it3 D, H- ^3 A, X3 r. e" G' L' H7 O
// has no notion of time. In order to have it executed in
$ P; G' T0 V4 |, ?, U // time, we create a Schedule that says to use the8 R) _$ F; u- q; H
// modelActions ActionGroup at particular times. This& s4 E7 D, j# d+ b5 ]& a
// schedule has a repeat interval of 1, it will loop every2 f; N! L8 ?% R [
// time step. The action is executed at time 0 relative to
, O: q, F! I/ z r // the beginning of the loop.! z' v$ V: p( y/ K& g+ y* K
9 y; ?9 n' _& c& T: J9 m# p- c
// This is a simple schedule, with only one action that is
: I( k( G* e6 ?2 L" O9 k // just repeated every time. See jmousetrap for more1 B! t/ J7 N0 f, S5 n1 Q) f
// complicated schedules.' K; J/ d) v9 d, h& k
, g# n# l# x! M% M! { modelSchedule = new ScheduleImpl (getZone (), 1);
* Y3 C( h2 }4 p5 {! w modelSchedule.at$createAction (0, modelActions);
- Y7 e9 q' o# q2 K8 o
3 l% f/ D7 N1 ?( V" z& a4 i1 p return this;
9 t! n, t; v8 E& Q) k( ^ } |