HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# n, I) n& ]5 F. s
4 h$ L% t: P1 r. ~! A& D# p, Y: } public Object buildActions () {. H( z$ h+ w, @2 t8 k# a& g
super.buildActions();
+ m. |: _8 K* G; V8 S, M; E 2 m9 `! v! e8 e* I( d( }
// Create the list of simulation actions. We put these in
h3 Z- C9 Y: ~6 G7 ^8 H9 I" J: ~ // an action group, because we want these actions to be8 M; r& a# m% H& M M+ k. P( O
// executed in a specific order, but these steps should E) `; D* O% E% Y
// take no (simulated) time. The M(foo) means "The message' v: a- O5 a! }1 }7 D
// called <foo>". You can send a message To a particular
: ?" _! Z1 D% Q7 \. Q' [/ D2 c! k0 } // object, or ForEach object in a collection.4 }, h# g% P/ {$ Y/ C
) R7 n9 f, q; c: P7 x
// Note we update the heatspace in two phases: first run
* T( j+ G7 ^% W" V8 G // diffusion, then run "updateWorld" to actually enact the% H- B0 }4 | {1 X# f8 C' x
// changes the heatbugs have made. The ordering here is
$ }1 c. j h8 V- l2 ?& r9 |6 ^, F // significant!( R& ^. O2 ~8 B" N4 E/ Q% O! O- ]5 O
7 R% }2 V1 ?4 f3 V- O0 S
// Note also, that with the additional6 h6 B/ T) Y" W8 ^, O
// `randomizeHeatbugUpdateOrder' Boolean flag we can
7 Y- K/ Q% g k; k // randomize the order in which the bugs actually run
: ^4 b5 Q, n2 ?5 t' D // their step rule. This has the effect of removing any+ h# S5 ]+ n$ y; ] N
// systematic bias in the iteration throught the heatbug( M3 B* H {4 |2 t" F
// list from timestep to timestep! B4 q( P5 |7 ^9 q- L4 ^
4 Y5 J+ ?7 b7 d0 d0 g2 X$ C9 X& y- Y
// By default, all `createActionForEach' modelActions have3 h) l' z2 @, c3 h. S
// a default order of `Sequential', which means that the2 h+ t/ B$ U4 i; e
// order of iteration through the `heatbugList' will be
2 ^* K! _% \9 Y1 X4 D9 X, C // identical (assuming the list order is not changed$ R8 L/ [4 f% Y7 I% i; t& ~7 a
// indirectly by some other process).
+ `2 Z3 l, v; s; D2 _0 T1 o2 k $ r' @# M3 A1 r+ }; C3 b8 @3 }7 z0 ]
modelActions = new ActionGroupImpl (getZone ());
# ?; ^* `; b1 H, j
* l3 a0 n$ y7 K- B1 g. d% z/ R try {
1 U* U# C0 ]4 X9 p% ]- W ^ modelActions.createActionTo$message
: r/ e8 U" i6 } (heat, new Selector (heat.getClass (), "stepRule", false));+ Q( E3 ^ w+ P: a9 f, p: Q
} catch (Exception e) {# ~- l9 L% N; f2 R% c! J; Q, }( @
System.err.println ("Exception stepRule: " + e.getMessage ());
! ~5 k i3 d$ v }
5 v9 L' k6 v9 G/ T/ j
1 f0 F0 [9 N' }, b try {% H% g5 G6 `; l5 c3 S
Heatbug proto = (Heatbug) heatbugList.get (0);
3 G+ e# ?- O, @: O* e Selector sel =
, I9 L+ J" B# z new Selector (proto.getClass (), "heatbugStep", false);
\8 h4 c7 h* i% b8 I actionForEach =
/ L9 Q! M2 g6 L modelActions.createFActionForEachHomogeneous$call
. a+ s; \. {& O$ h8 U1 y (heatbugList,
- x3 E; Z7 {6 b2 F, R* W8 m new FCallImpl (this, proto, sel,% \4 R P q$ M! Q
new FArgumentsImpl (this, sel)));
2 e, y& C% Z: x) M4 k } catch (Exception e) {2 A2 A$ _1 M% P0 j0 R# o
e.printStackTrace (System.err);
' }) c) [' `; J7 V2 B4 I0 w, ?/ U }$ ?3 ^& a6 g" H: b; `* J1 a
7 _' k: x) Y N! j
syncUpdateOrder ();
' Z, S* l0 T( J% \ u$ Z$ P/ Y' T& m
5 U% m* }4 V9 }1 C try {
3 D5 F* M4 t3 N% Y. a4 \ modelActions.createActionTo$message / C2 ?3 d0 h; v) P
(heat, new Selector (heat.getClass (), "updateLattice", false));
' S- K3 w7 O! a" h- X } catch (Exception e) {( |) Z5 R( W& }& L6 u2 i3 N
System.err.println("Exception updateLattice: " + e.getMessage ());1 \; D+ c: e5 n* u# ?$ r4 v
}
" R7 y6 q1 P3 `; a* k0 m - e) }6 P$ ~! U9 h. |
// Then we create a schedule that executes the
' H8 e& g& v, a // modelActions. modelActions is an ActionGroup, by itself it0 f6 q) c9 @8 Z0 Z* t; Z
// has no notion of time. In order to have it executed in$ d& }( c$ U& v7 E" \1 m
// time, we create a Schedule that says to use the
7 D6 O- ]; B6 \+ q. E // modelActions ActionGroup at particular times. This
4 b. a) N: Q9 b( p) ~ // schedule has a repeat interval of 1, it will loop every
' ]8 r% J- ]) ]" v. j // time step. The action is executed at time 0 relative to! x) c# m8 E, l) `
// the beginning of the loop.
3 t$ Q9 U8 Y; K8 L" w& y; N7 E
: s6 U' {8 B {, m% {% y# p9 f // This is a simple schedule, with only one action that is- D. k* D$ d% k5 s! h' e. a
// just repeated every time. See jmousetrap for more, {! z+ }& Q+ H6 N" u
// complicated schedules.
* ~0 [. Q& ]9 {* q
& l z' y" w5 }+ [5 |+ S4 v5 }# B modelSchedule = new ScheduleImpl (getZone (), 1);
9 U) {& |& q; D' K0 {0 K$ p$ |6 R modelSchedule.at$createAction (0, modelActions);' Y) Y: b% `. U( r
! g3 W' u3 g4 r; J' E, _$ t
return this;
1 U! q- N x2 a6 O } |