HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 o# B8 m& V& l
0 E) k* g+ r0 l2 h5 Y6 e public Object buildActions () {
3 t7 r4 c1 i! `. C% I super.buildActions();
c* W% {5 |( s. k( P- Y% s
4 U8 d2 k- V" y3 i4 T // Create the list of simulation actions. We put these in2 G) A' L, N# d! V3 H! @
// an action group, because we want these actions to be
- d, G) y( d; z1 O6 ? // executed in a specific order, but these steps should! d2 c+ l# {8 }; X2 j# Q4 [
// take no (simulated) time. The M(foo) means "The message7 E! z3 f/ f' Z- h; s7 p' T* V$ h
// called <foo>". You can send a message To a particular
B* }5 y n% T // object, or ForEach object in a collection.
+ \8 w9 Z+ {4 d3 [& z0 Z& m
( Z3 Y7 a0 ^( ~ `. x4 b8 ~ // Note we update the heatspace in two phases: first run0 z& q2 |, Z3 e. J: R/ j& x
// diffusion, then run "updateWorld" to actually enact the6 c/ z( J$ k5 @) { Y$ {& s
// changes the heatbugs have made. The ordering here is/ Q n: `* d) x6 `3 D8 r$ ?* F
// significant!; [5 u7 Y6 p+ F5 ?
9 l5 T# C- o- l% [ // Note also, that with the additional5 d9 C/ _3 l( G5 _" n& D
// `randomizeHeatbugUpdateOrder' Boolean flag we can
0 @ a( W* Q' r; d" R% s // randomize the order in which the bugs actually run
7 _& @8 i6 \ r( q+ G // their step rule. This has the effect of removing any4 R7 i" @2 \* E
// systematic bias in the iteration throught the heatbug: O1 E% V$ Y7 S: m Y: W- f
// list from timestep to timestep
) M0 @/ ^) R. F/ O 3 t# J0 t: m1 U( k2 r
// By default, all `createActionForEach' modelActions have; y+ I" g% B7 D( c4 X2 R" U% y
// a default order of `Sequential', which means that the
. ?+ ~, R2 ^$ `1 \) M // order of iteration through the `heatbugList' will be
8 Y5 y+ g1 h% d" |6 Y& e // identical (assuming the list order is not changed( d2 Q* ?5 h: S* _
// indirectly by some other process).* [3 i9 x! U$ l4 M, l! W. @1 f0 B
9 x- B' X% e2 l3 B4 s2 P& H3 c modelActions = new ActionGroupImpl (getZone ());' i1 m) S6 p+ S; }: y$ q# g
. _+ \" E# g$ ]$ E2 Y- @. V
try {
5 M# Z) v. P# T& f! W0 g ?$ u modelActions.createActionTo$message
3 N* X" G% a1 i: x$ H: J (heat, new Selector (heat.getClass (), "stepRule", false));
5 Q0 f9 ]& r% _5 ? } catch (Exception e) {
, M/ f! N; b7 K/ k, H System.err.println ("Exception stepRule: " + e.getMessage ());
: C$ `, L+ ^/ W6 N* f0 y' Y5 h }; G8 v1 a' b4 R5 A/ }
: J* X8 l7 h7 V& \1 h- Y0 }
try {; Z% {8 ]$ [" b: {
Heatbug proto = (Heatbug) heatbugList.get (0);# u& g. C$ c9 r# h r
Selector sel =
; E3 t7 C( d3 F' W8 F new Selector (proto.getClass (), "heatbugStep", false);
4 R7 { e, m8 Q, c actionForEach =9 c! W( P4 _' ]" X% M+ y+ R
modelActions.createFActionForEachHomogeneous$call& |+ y7 g; b ]9 n- ]2 p9 x
(heatbugList,5 _( L* B. V4 Y5 u+ ^* D
new FCallImpl (this, proto, sel,
& M/ p( ]* M( O! P) g( O/ Z new FArgumentsImpl (this, sel)));
' W/ g b" m8 `( a$ u } catch (Exception e) {: ~) I4 q f; H V8 A+ b$ x
e.printStackTrace (System.err);
/ w2 ~+ D5 R' R% [8 l u }. |% g% I7 ?" P0 S9 U6 O
5 A$ I* C& p# V5 ^! l% |
syncUpdateOrder ();
' f) I& a1 p# y6 S' h2 {, y! g
8 M: |: r W& }: g4 @ try {. d( U6 |7 l3 _1 w. D
modelActions.createActionTo$message : O% |! T0 Y7 S% I* s! H3 |
(heat, new Selector (heat.getClass (), "updateLattice", false));# a: f: n7 Y$ D, b' Q( I- `
} catch (Exception e) {/ Z$ O- G6 T6 Y6 {
System.err.println("Exception updateLattice: " + e.getMessage ());; d$ Z1 h# R# e4 c0 d/ o
}
. Y1 A% R# d, a/ @- V: W/ z1 r # c* e% _# h" f' y/ D
// Then we create a schedule that executes the
! W; W8 y" L" B: F) @* M5 _ // modelActions. modelActions is an ActionGroup, by itself it
, B8 y+ d8 X- ]7 R( Z // has no notion of time. In order to have it executed in
3 v3 W, `) z4 M) @( Q // time, we create a Schedule that says to use the
" R2 \0 z% t" A( R) E // modelActions ActionGroup at particular times. This
+ B$ Z& X; Y* M# ~ // schedule has a repeat interval of 1, it will loop every! b5 u% d2 k3 e) o. y& |+ B
// time step. The action is executed at time 0 relative to
& k7 R* v/ q( K0 G! Q ?( X6 n; B$ ^ // the beginning of the loop.3 U( o+ Z$ i- Z% y# t+ E
: o) I& J* ~0 V$ ^( O7 e( a
// This is a simple schedule, with only one action that is& Q) t- O, V" }3 v- ^# ]
// just repeated every time. See jmousetrap for more: H& a5 A; b# D+ l5 ^# Z" M6 L
// complicated schedules.) p5 U( ^+ w$ D& u: M1 [
; n! R1 }& l7 _- ?/ _
modelSchedule = new ScheduleImpl (getZone (), 1);
$ o' [8 N. @% v/ c h3 @" h modelSchedule.at$createAction (0, modelActions);
# |, f1 Q) `/ G% d% {& U
7 l' J6 |$ m9 v$ t7 |! ^ return this;. X+ h$ y$ Y1 }( v- Q( j' A
} |