HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 u( M& ~, E8 w& _) C! N$ z* }: Y/ V& v5 D+ Z* T* T5 _( H# H& E/ t
public Object buildActions () {
- N3 C& T4 Y; m3 B6 c super.buildActions();
, |$ [( \: x- [6 |; s) B2 ^# s * \1 |( c5 Z9 B' Y w7 y! ]
// Create the list of simulation actions. We put these in) w3 J$ L9 t9 f! ?: v( i
// an action group, because we want these actions to be2 G9 Z/ [( r v7 A
// executed in a specific order, but these steps should
: m6 ?% Y j* C/ c1 ?. _ // take no (simulated) time. The M(foo) means "The message
& {1 J6 i' K! k- W // called <foo>". You can send a message To a particular
# m9 Y+ g! b( M! d( v4 n& i! b // object, or ForEach object in a collection.+ U$ X. D5 O. g" O
9 d4 j( W( ?9 `4 D // Note we update the heatspace in two phases: first run
( k5 L7 n: k+ `& a( Y // diffusion, then run "updateWorld" to actually enact the) w& a* _8 H- @3 y/ E+ C+ H; w8 |
// changes the heatbugs have made. The ordering here is
w2 X# s, y- r$ G // significant!
9 t) W% O8 i) H* e! _ . n! C; u! z2 Y! w+ _. T
// Note also, that with the additional# D6 H7 o. J# b. L
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 J' C! t, t1 b$ h- _* |
// randomize the order in which the bugs actually run2 a5 d( I9 o: h: e) j( `% Z
// their step rule. This has the effect of removing any
; a' g6 P! \: T" x4 w2 y // systematic bias in the iteration throught the heatbug
, X. f# K2 v }! h4 u; v+ I // list from timestep to timestep5 t% \# D: U! _- ~( w0 {( Z6 K
- g0 ^* t% I/ O8 R# V9 B* [ // By default, all `createActionForEach' modelActions have
2 Q( m: x; I: X // a default order of `Sequential', which means that the `# E+ X3 x0 l' I9 N9 _
// order of iteration through the `heatbugList' will be
; r# T& _2 V. T // identical (assuming the list order is not changed
/ F: ^9 D e. _+ [4 U: B // indirectly by some other process).& {# u- Z8 Q( e: _0 x7 l b
) }8 g' V4 l0 Q0 o2 q$ s+ B
modelActions = new ActionGroupImpl (getZone ());4 q% ] L. x1 Q1 I4 G
8 g* @6 ?/ O* O, F5 u: ?
try {
+ t2 u4 c* E% J/ O- ` modelActions.createActionTo$message
; |6 j" T2 P, T (heat, new Selector (heat.getClass (), "stepRule", false));/ ~, a5 t/ B7 `/ B7 h% Z2 R
} catch (Exception e) {
3 _ v( g: T0 l System.err.println ("Exception stepRule: " + e.getMessage ());$ ]1 O2 ?* p; f
}" S$ N) X4 y, _- S1 k# u
* t5 d4 y, C5 }0 [
try {
6 L6 b6 l9 L: w6 f Heatbug proto = (Heatbug) heatbugList.get (0);
9 s5 g9 v1 q3 X! b# w Selector sel =
% i3 G' r2 B% X3 {9 G new Selector (proto.getClass (), "heatbugStep", false);! r( m; a. i9 p3 g0 ?7 @
actionForEach =' H+ v7 c" m% I) p
modelActions.createFActionForEachHomogeneous$call5 q$ N X/ ?" |- |- G( V
(heatbugList,1 ~0 L9 \: S9 v+ K- g
new FCallImpl (this, proto, sel,/ Q8 \7 b/ |9 V
new FArgumentsImpl (this, sel)));
) g& x0 |, w$ J% E& _ } catch (Exception e) {0 e9 X( J! I/ _- s7 D' a
e.printStackTrace (System.err);$ R- U( G: [/ n8 U
}" G/ V0 | \. \% k# s
: b, S% ^0 X% Q) F8 b syncUpdateOrder ();4 {# G* ]8 P5 a( ^( O- S' W
% B" z) _+ ?* t' v7 ` try {
* G- {- ?3 y6 |8 |/ g& N1 {% b modelActions.createActionTo$message
: \/ ~* Y4 |! @% p& M9 K (heat, new Selector (heat.getClass (), "updateLattice", false));5 n; C& k6 t; m% L7 _4 q$ q: {
} catch (Exception e) {
8 T$ ~3 l6 m3 l+ a p System.err.println("Exception updateLattice: " + e.getMessage ());
1 `3 X$ H1 G0 j6 V% z+ ~ }2 g. ~9 n, i& Z- P
, Q2 L. Z& E: N- U2 c+ K' _2 o // Then we create a schedule that executes the
" |; x% e* ]7 K' D // modelActions. modelActions is an ActionGroup, by itself it4 L, b% L; |- N5 u, S( m
// has no notion of time. In order to have it executed in
$ Z( P) y" i2 R // time, we create a Schedule that says to use the
! @ Z4 F- Z; {2 v // modelActions ActionGroup at particular times. This) ?8 R' J: o0 U( Z9 X' y- N- `; _
// schedule has a repeat interval of 1, it will loop every
- c$ P4 Z2 I# X5 x/ G // time step. The action is executed at time 0 relative to; ]0 f. e1 {' x8 L
// the beginning of the loop.
/ I! [# |6 F3 I& E) j
0 d& L/ u: f; x {9 b3 r5 b7 Z // This is a simple schedule, with only one action that is
" d, B1 U! @! Z // just repeated every time. See jmousetrap for more
- o+ R( W; e* |1 n% K9 u // complicated schedules.
) ~' ?; T* u: Q) T: U
, s4 q1 O0 d- `2 h; a8 e modelSchedule = new ScheduleImpl (getZone (), 1);
0 v, V ?1 M$ k( x/ u) |9 w modelSchedule.at$createAction (0, modelActions);
+ _3 ?* Q) w3 d9 j3 u
" H1 X1 y5 ?+ t return this;# j. a0 ^( K5 F5 y$ u) P
} |