HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' C2 ~7 [6 J8 N) w; G- s7 _: ^
6 W$ [& R2 p+ L/ p
public Object buildActions () {
; C: H+ C# g/ c D: }# j' s super.buildActions();
5 Y. ~* C% @1 W2 m & f$ m2 u& k0 J" N5 m4 w1 c& `! [0 \
// Create the list of simulation actions. We put these in" u/ o7 _1 J1 p& r0 r, `
// an action group, because we want these actions to be
) j6 [. c! J, W) h$ a+ L+ [ // executed in a specific order, but these steps should' S' I: i, ]2 c2 Y' j
// take no (simulated) time. The M(foo) means "The message
% Y, a; o/ J2 @3 q // called <foo>". You can send a message To a particular
% M) @$ J. A3 O // object, or ForEach object in a collection.
4 A' |) w8 E Z* N* F
, S: |6 n# v8 a# M/ z" Y# f4 j3 T // Note we update the heatspace in two phases: first run! T) n! y3 v, C: {9 T$ f
// diffusion, then run "updateWorld" to actually enact the
5 b* c' |# U' J. P r% j // changes the heatbugs have made. The ordering here is0 }. \2 j* C# p& r6 E. [
// significant!. v# F% z9 B. V- x2 z' i! a4 V
- R! V& z. \9 b j+ h+ C% r
// Note also, that with the additional" {; O W# I. H$ c( m
// `randomizeHeatbugUpdateOrder' Boolean flag we can x0 y! d" |) s
// randomize the order in which the bugs actually run. ~( e( R9 A" w* T! ]8 G' D
// their step rule. This has the effect of removing any
1 P: a% F1 B2 ~" G // systematic bias in the iteration throught the heatbug. u7 q) ?1 N j! L* s9 Y
// list from timestep to timestep7 S2 V$ E5 _- p4 d3 a8 a+ _
" O/ H6 M- m9 |- ^7 P // By default, all `createActionForEach' modelActions have
" z8 C. P" Q, O' O( ^ // a default order of `Sequential', which means that the
' n( |. A# S U c! ? // order of iteration through the `heatbugList' will be! k4 |: `; Y: ]" q5 E8 \) ~
// identical (assuming the list order is not changed( Z2 }5 e7 p. e2 t' h& X' U! }
// indirectly by some other process).
; d: ~+ d& u) b, b( X
w; D. Y+ x" c7 S- a- D1 m modelActions = new ActionGroupImpl (getZone ()); p: _3 Y/ j. ~
6 U% i. g/ T+ p A; L" }( a try {' [5 ?& v2 R" \( v3 u* ?) b7 d
modelActions.createActionTo$message
2 t& Y/ x* ~' o1 A# h6 H) z' s (heat, new Selector (heat.getClass (), "stepRule", false));( v. V6 {# n% U- h% \( x
} catch (Exception e) {- G% B5 ?% V/ t. a" h
System.err.println ("Exception stepRule: " + e.getMessage ()); b$ u/ Z, g" l. Q1 m) c3 F. v+ {) y! J
}
2 Q/ } g, H7 B
' x" u, _0 n: ^$ K try {9 R5 `; y8 M+ q3 G1 X+ E
Heatbug proto = (Heatbug) heatbugList.get (0);' E. e5 X; w v
Selector sel = ' h B8 z* k7 p: s
new Selector (proto.getClass (), "heatbugStep", false);2 T+ Y; S, t' z8 \& u! g, `" ]' o: V1 |
actionForEach =
7 }/ c$ v7 _/ d! G% s! ]" c3 o modelActions.createFActionForEachHomogeneous$call( g* d" j* S! ^* Z
(heatbugList,
/ ^( _! j1 V2 D K( c( o! i new FCallImpl (this, proto, sel,' I" z1 ~2 b4 u' p" v9 w
new FArgumentsImpl (this, sel)));" N' N& f4 ^ ~, |
} catch (Exception e) {
0 r3 @7 B) l J e.printStackTrace (System.err);% B% {: K% w7 l4 [+ o" R4 B$ n _" ?/ U
}1 j w$ _* w+ |7 |
/ t7 s+ n3 \5 \ syncUpdateOrder ();! c4 j9 ?) ~$ U* @7 P/ B
) l, _' ]! t+ _2 L& a try {
' A# W# b; i0 r% u R9 G: ^( I modelActions.createActionTo$message : w: x! d% U7 H
(heat, new Selector (heat.getClass (), "updateLattice", false));2 Z$ W; V" H% s; {4 B
} catch (Exception e) {
5 ]% x9 w+ C9 ^ System.err.println("Exception updateLattice: " + e.getMessage ());
/ M+ h/ J- n& T l; j }8 O _7 W! K5 ?5 D6 @# i7 m/ O& B
- N: U% E: `2 ?, n // Then we create a schedule that executes the( _' N2 n9 S/ d
// modelActions. modelActions is an ActionGroup, by itself it8 J" R3 ^7 |% t5 V2 O
// has no notion of time. In order to have it executed in
% e7 u1 O2 y- } // time, we create a Schedule that says to use the% ^+ P; b! i8 T( D1 F
// modelActions ActionGroup at particular times. This0 O4 @0 T- @" H5 a" ?( h. ]0 w
// schedule has a repeat interval of 1, it will loop every1 j) [" E2 s) E* e6 x* V
// time step. The action is executed at time 0 relative to
: i4 H- E% Y8 |. \. {. P6 a // the beginning of the loop.
& I" x$ b. {5 n' y6 }, e: e2 s* l) B1 j" y# c8 X
// This is a simple schedule, with only one action that is; u: E+ a$ j; }/ p0 b4 P
// just repeated every time. See jmousetrap for more
! T8 E4 ]7 O' Q' [9 E- l2 m. m // complicated schedules.
( ? G& U" f/ q2 v ~2 `' g# N5 J5 E - ^0 [$ m( W# N/ r* d0 S& o+ [8 D
modelSchedule = new ScheduleImpl (getZone (), 1);* r2 m4 s+ L! {% J: t) U# c( w
modelSchedule.at$createAction (0, modelActions);( e3 h; M# Y. t4 B' d& G6 q, W
; \$ G& r! Y+ l/ a5 z return this;3 c6 G# A4 i' x# K2 r
} |