HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- I2 T/ S) |3 ?! x6 `9 o
$ q5 L4 \* q! Q9 O! b: ^
public Object buildActions () {
: e4 [! v$ {# z) ?/ H I super.buildActions();
8 q& q# E0 L$ k. M
" D1 R$ R: _; Z // Create the list of simulation actions. We put these in
, l' {; P3 L' @% O+ x$ H# l // an action group, because we want these actions to be
J, t: W* a! w: J' r // executed in a specific order, but these steps should {) O* C" _7 ?" k
// take no (simulated) time. The M(foo) means "The message
9 E4 e- \; v+ o* n7 @7 i$ B* r' s // called <foo>". You can send a message To a particular ^ ` j! F+ Z: j
// object, or ForEach object in a collection.+ s6 P' R! k6 n3 z
3 x# ^# Z9 N$ F$ q // Note we update the heatspace in two phases: first run
7 b5 ^. ` H! e- T7 F8 J // diffusion, then run "updateWorld" to actually enact the
1 j4 `4 ?9 u$ Q: C% b# W // changes the heatbugs have made. The ordering here is
$ \6 R" L+ O6 o" ~% \ // significant!7 k! o5 C: u6 P0 `1 r& f
% M! C' i5 l3 Y* z% _6 J2 ]" L# G
// Note also, that with the additional! \# {) A# V1 d$ R
// `randomizeHeatbugUpdateOrder' Boolean flag we can3 d2 `, f2 A: A+ O& `
// randomize the order in which the bugs actually run; R( x: x0 Q, B
// their step rule. This has the effect of removing any
, j# u0 m" f7 m // systematic bias in the iteration throught the heatbug) d' ^/ `; G' k, o3 a- q+ ~
// list from timestep to timestep
9 I4 c! j9 ~! u L. l& [/ z
" {; e" ~- D5 Y+ U$ y: D4 } // By default, all `createActionForEach' modelActions have
1 Z b% M# h. b" U% ]( S5 K# `; L // a default order of `Sequential', which means that the* Q- ]+ R) H4 H( b7 i/ H& X$ j
// order of iteration through the `heatbugList' will be4 j' [/ r) N. t( O
// identical (assuming the list order is not changed
1 G' I- j- }+ h) c) Q6 m n; d // indirectly by some other process).& V, Y8 P* h2 h. F
8 \" X6 z8 i' ^
modelActions = new ActionGroupImpl (getZone ());
: S. e9 c, f m. i! R" p9 [# _6 u1 a5 V) s
try {" T) D" P2 f/ b. ^* y: m
modelActions.createActionTo$message
+ w9 }! Q; D3 d. h& y5 E (heat, new Selector (heat.getClass (), "stepRule", false));* D0 d* V0 t% f; E
} catch (Exception e) {
& ]% N4 {$ {- ?, N$ g/ o! s0 ~ System.err.println ("Exception stepRule: " + e.getMessage ());4 J# a$ L/ U( f" u
}
& j1 e* ]* H9 G, }
+ ]; _) W; i2 }5 y try {
6 k4 r; u( i" f3 S* S Heatbug proto = (Heatbug) heatbugList.get (0);
. i: k, S! R& c$ v" z Selector sel =
9 o/ [4 s5 z; g l. | new Selector (proto.getClass (), "heatbugStep", false);, _8 a- {' K6 h/ n& v
actionForEach = g2 q# Z; u( a& O
modelActions.createFActionForEachHomogeneous$call
1 A& l& l' b$ K# x3 p (heatbugList,# M7 h- r0 i- v# {, q) r' W
new FCallImpl (this, proto, sel,
$ Z- E1 Q4 n v; s$ h- _( v w! U- a new FArgumentsImpl (this, sel)));
/ R9 V3 @2 k- m. g6 P1 Y6 h } catch (Exception e) {8 t: }4 _* X7 B1 y
e.printStackTrace (System.err);$ g% Q- L+ H2 j. v! w
}) R8 V- `: v+ {" W
8 g) J# N- O+ L/ e! X
syncUpdateOrder ();
4 c/ b+ H* m/ R. W' s
2 J& v( W( m/ n2 h! i+ e- a try {
& ^7 R f. c/ T" y modelActions.createActionTo$message
7 i: K6 N# q, T, n7 d7 \ (heat, new Selector (heat.getClass (), "updateLattice", false));
, C* B ?* {; S4 \2 }) L( G } catch (Exception e) {
3 [: c+ `/ z, B System.err.println("Exception updateLattice: " + e.getMessage ());
: _- x5 }, m L" m" l% z }! ]% n/ r, V/ K& ]) ?* l E
8 u& e* X s+ I. z
// Then we create a schedule that executes the
g; k. i- Y. O4 e* T5 [3 Y' I // modelActions. modelActions is an ActionGroup, by itself it
& w4 V5 t& z I1 N% s9 i2 K0 C // has no notion of time. In order to have it executed in
; p2 C: k2 \$ o6 _! c Q // time, we create a Schedule that says to use the
% w' ~' c3 f& `( }+ X' s! J // modelActions ActionGroup at particular times. This
. F% q. C) ^5 d0 o // schedule has a repeat interval of 1, it will loop every
8 C$ z+ T V3 W // time step. The action is executed at time 0 relative to$ v: d- K0 l# P: x$ [
// the beginning of the loop.3 X3 `( b: K! b, e1 v! @5 Y/ ^
& t. R2 s, J: v* z // This is a simple schedule, with only one action that is
( r$ b; T& U2 j; z* o8 Q r9 a // just repeated every time. See jmousetrap for more
- Y' i# |" A2 G4 S1 U( J9 s4 q // complicated schedules.& K+ x5 a+ I8 p# E! ~( D2 S
" K. r9 M% ^9 W/ ~5 p; g2 P$ I/ l% Y
modelSchedule = new ScheduleImpl (getZone (), 1);3 \* ~) f3 Q$ x( _" N! L
modelSchedule.at$createAction (0, modelActions);
, @9 Z/ I9 j! Z3 _6 Q
8 u! {2 K% n, O6 e, V* m+ J return this;: z3 {. W1 ?* B) w
} |