HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) D* \! O& V$ L6 D% ?# F9 a( q+ Y$ Q$ A3 B7 ~! f6 }
public Object buildActions () {
' d: z2 M) p# M0 c super.buildActions();
+ K/ M) q1 d: @6 Z 8 Z% _1 `. M- j' r l7 x1 |
// Create the list of simulation actions. We put these in) L/ F4 K9 J1 i) e5 f
// an action group, because we want these actions to be; r' v2 @) c6 H0 c7 `/ U
// executed in a specific order, but these steps should
# n3 i) b2 J9 \7 l) ] // take no (simulated) time. The M(foo) means "The message
% i' f0 i: `; z$ Z9 {( b4 S: A, _1 s // called <foo>". You can send a message To a particular! q( m2 `+ B6 W' T8 ?
// object, or ForEach object in a collection., A. l9 K% ] U9 j3 O9 O0 }5 F
9 B+ V' l! }- p // Note we update the heatspace in two phases: first run# F/ l2 W- A/ j7 V; J% ~4 b
// diffusion, then run "updateWorld" to actually enact the
$ F6 m5 H: G1 ` // changes the heatbugs have made. The ordering here is7 k' G0 m) p* s2 Z: q3 T$ T# l5 \
// significant!
; e- M; N% J! D4 t
! P: a0 H1 |, M // Note also, that with the additional% N7 W- K( n1 @/ U9 g
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ I. b, l/ w+ L8 M; {
// randomize the order in which the bugs actually run5 ?4 _/ a* e- @/ M4 x; ?$ l3 K
// their step rule. This has the effect of removing any
' A5 l: V1 _! G! g X. B // systematic bias in the iteration throught the heatbug
8 O0 R9 f: q3 P% v( G5 W4 Z // list from timestep to timestep
3 o% H/ {) Z" a' W 5 X) f! i ]" p& r. L
// By default, all `createActionForEach' modelActions have
- W" m j3 w0 @0 B9 f2 W4 ` // a default order of `Sequential', which means that the: q7 l' c3 j6 g# N
// order of iteration through the `heatbugList' will be% T+ {6 N/ X8 I7 D# q. o; N
// identical (assuming the list order is not changed
5 S: ~) g# Y7 H" C. k6 _ \ // indirectly by some other process).
9 c1 P2 G( U9 H+ w8 ^
1 ^$ B, {& O% w8 k- e% l# c ? modelActions = new ActionGroupImpl (getZone ());
5 i7 a* j( J* v& h! n
: k9 e+ z2 ~% B, m! y try {
, \! ?* Z; G7 e2 ` modelActions.createActionTo$message# W/ m, s+ J O, e
(heat, new Selector (heat.getClass (), "stepRule", false));
/ [' G# R& g5 ?; K h1 Z+ q } catch (Exception e) {% H9 l6 O4 o+ ?& e8 y8 E
System.err.println ("Exception stepRule: " + e.getMessage ());- l! W& [: m) ?/ m5 s
}
+ @6 u2 Z) v% \, V0 m
1 J) G/ u* Y3 S9 j( G& b7 c5 j try {( i' Q* L7 H6 n) C3 w, f
Heatbug proto = (Heatbug) heatbugList.get (0);) o- p: {$ k9 N: p, p2 q7 k
Selector sel =
0 K/ o4 y: b, U1 Q: C new Selector (proto.getClass (), "heatbugStep", false);" U0 w1 t8 x- N6 J
actionForEach =
/ |; M( t# h2 A3 b, [7 o modelActions.createFActionForEachHomogeneous$call% Q$ y/ g6 {5 C' M
(heatbugList,; Q k/ G, q' g( ~
new FCallImpl (this, proto, sel,
4 f) z6 |# z/ p- e" K new FArgumentsImpl (this, sel)));
, }# v% c+ d. B _" e } catch (Exception e) {# [* B5 D `& C2 Y1 M! ~
e.printStackTrace (System.err);
, H v* u3 c6 s/ I3 f1 i8 L3 f5 O$ F }, I& Y9 {0 w- \+ ?! N; P6 L
8 M6 j$ p0 A+ j* O& S syncUpdateOrder ();
: O& [+ @2 q% p) o+ m! W9 p6 q( z
try {% L; S& u' F2 g
modelActions.createActionTo$message
+ [# k* ]7 a1 A! d6 q$ @ (heat, new Selector (heat.getClass (), "updateLattice", false));
. E4 {( t* M" G7 ]3 ?" W' x0 j0 h } catch (Exception e) {: ]3 z! e7 c! z _7 k& _
System.err.println("Exception updateLattice: " + e.getMessage ());
) P6 [7 x r8 U6 h5 A& x" [' p }2 t/ U' F; L- m2 H& e, P6 d6 Q7 {* j; ^
" N6 ^. @8 W% \7 O
// Then we create a schedule that executes the$ a9 _2 I6 i9 \- p x0 H
// modelActions. modelActions is an ActionGroup, by itself it* M; M0 o2 @ n% j' s/ K
// has no notion of time. In order to have it executed in
" L5 }: T" _7 L. \- a // time, we create a Schedule that says to use the
2 j% l0 O/ G9 X# n3 R+ B, S2 Q // modelActions ActionGroup at particular times. This: B0 m! s# C, O: m% z+ W9 s4 @
// schedule has a repeat interval of 1, it will loop every( W* M( n4 b: A* C- k
// time step. The action is executed at time 0 relative to( `- b& u5 a9 u$ R) {4 F
// the beginning of the loop.
4 {; Z1 Y7 x5 T! \9 V. i$ R( K
# A4 K$ C9 R0 o# s+ W" t/ o7 ~4 r // This is a simple schedule, with only one action that is6 n% c( H# U; H: q
// just repeated every time. See jmousetrap for more
" v( a6 o/ P. L3 { // complicated schedules.
/ H, h6 N7 ^: u: ^ . a7 w& R' |1 B% Q/ U
modelSchedule = new ScheduleImpl (getZone (), 1);9 O* ~5 O1 ]0 B0 x/ c4 f- l. c
modelSchedule.at$createAction (0, modelActions);
+ y2 S) g" c, K- ] H% A
4 }/ g, ]: ]0 r1 Q- H: B& ]5 D return this;+ G4 P- H' `5 H! `
} |