HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ o n5 \3 M0 g& l2 z9 d: u, f( P% j+ q# s* y7 E
public Object buildActions () {7 r) T# w5 I% X$ I( x) H
super.buildActions();& _4 y9 p* Q2 C r, ~: a! ^
7 _7 l4 ?6 g, ~3 g( L+ N1 F // Create the list of simulation actions. We put these in
2 {# d6 {, I% ^. p, s+ N, u" u // an action group, because we want these actions to be7 e3 L4 u' g) A5 W4 B8 a5 F' b
// executed in a specific order, but these steps should
% f3 H, J6 L$ u: L! Z // take no (simulated) time. The M(foo) means "The message$ \1 l" a! p5 p! ^3 \* s" ^1 J/ m
// called <foo>". You can send a message To a particular0 y6 L# i. d: N- m. u
// object, or ForEach object in a collection.
& E2 x) M3 E1 ]* `+ c4 N5 p) k
( O% q' t$ i1 Q4 v // Note we update the heatspace in two phases: first run4 W }, r9 i4 i, b7 e6 U6 U, F
// diffusion, then run "updateWorld" to actually enact the
( o$ V( _: j; b // changes the heatbugs have made. The ordering here is
% x- c7 ^+ v t5 O n( H, G U" l1 o // significant!) A) @: G" u6 _
# m. F+ I! ?* p* a8 b
// Note also, that with the additional
- R$ e) U' ~, c. b9 q2 U$ f) @3 L // `randomizeHeatbugUpdateOrder' Boolean flag we can
: G9 ]. j9 S$ c' ~ // randomize the order in which the bugs actually run
# K6 n/ U- P. ]% ]! ?, F# H; B5 `& w2 h // their step rule. This has the effect of removing any
* A4 H) d7 Q3 a" q // systematic bias in the iteration throught the heatbug- e1 K" g& P! m. C- x7 T0 E
// list from timestep to timestep
2 ]. n- P3 j) V6 k2 `. c % q! U$ p3 `; G' O
// By default, all `createActionForEach' modelActions have6 B5 i: V7 z/ w/ e7 | J& O
// a default order of `Sequential', which means that the
" a/ H5 k- q7 X% p5 W r% q2 B // order of iteration through the `heatbugList' will be
, a2 e$ ~ ?: A4 S) Z // identical (assuming the list order is not changed
" B2 a! X3 Y1 d5 e2 Y // indirectly by some other process).
]" d& b/ z5 p1 x7 S
! p) }' Q- o9 _7 M( b/ o. S( M: } modelActions = new ActionGroupImpl (getZone ());
( t( D0 Y. G2 [" }* n% |$ [8 s- ^% }: }8 R
try {
2 a! l* k/ t5 I. V& b* M4 v# |& M" L modelActions.createActionTo$message
2 U/ X0 _% x/ A (heat, new Selector (heat.getClass (), "stepRule", false));
3 k" E v2 V* d- T& W } catch (Exception e) {& k% N% [( w0 h) `" S/ o
System.err.println ("Exception stepRule: " + e.getMessage ());
( M* \' v6 B; `3 h& v9 B }
6 b( i/ N3 H0 d7 c) p, e5 [; H" s8 {
try {' t7 z4 Z5 A7 W D) U# W E1 t
Heatbug proto = (Heatbug) heatbugList.get (0);1 H0 y$ F3 y" n4 x9 d9 }4 |
Selector sel = ! ^ J2 |9 L! V, m: R
new Selector (proto.getClass (), "heatbugStep", false);
. H0 E# d& {3 ^' O8 T) r actionForEach =$ p% m2 J& K7 B. V) b1 k z/ v
modelActions.createFActionForEachHomogeneous$call4 l. q' U* B8 D& h6 P6 V0 ~( Z6 J
(heatbugList,
2 u8 V' v/ [! F new FCallImpl (this, proto, sel,
3 @/ F- R G* \8 l l* S! f new FArgumentsImpl (this, sel)));+ \- @) ~% N1 u- l4 z
} catch (Exception e) {) E# V5 o' i- x- v% m
e.printStackTrace (System.err);+ }# e0 m: L8 }
}& }2 W D$ s) w4 \- L# L7 H
" o" k9 ]' a" t9 u: z5 f; n
syncUpdateOrder ();
7 h5 n3 Z2 s5 z9 P: B; z+ _' b& o1 G" z
try {
' d, t( Z$ L' y% A* |# J3 { modelActions.createActionTo$message ) Y. a# c+ T' B+ h
(heat, new Selector (heat.getClass (), "updateLattice", false)); X! `. q3 V# _, Z \6 x7 n3 |4 G
} catch (Exception e) {
9 m# Y. f: D; M0 K! l+ [ System.err.println("Exception updateLattice: " + e.getMessage ());
9 o( D! S. U1 i; g }
{1 c2 R1 P" V2 A; }" X 0 z# S) }% ` e. j
// Then we create a schedule that executes the
- C" V# J2 B9 P' N4 [7 s1 x! ^& ^' q // modelActions. modelActions is an ActionGroup, by itself it
2 R% b- I/ @1 G! C8 `; [, X // has no notion of time. In order to have it executed in
- F3 k; ~; A2 f$ C9 e: | // time, we create a Schedule that says to use the
$ W2 t3 Z6 v- D // modelActions ActionGroup at particular times. This6 {) w: b6 ]1 a) c
// schedule has a repeat interval of 1, it will loop every |; U, {7 l2 k3 y
// time step. The action is executed at time 0 relative to3 j, D2 j* i3 g" j5 a+ H
// the beginning of the loop.# c: _4 ^) ^. E' [
. `4 f$ p# G! ` // This is a simple schedule, with only one action that is
7 x8 B& s" g2 T3 y // just repeated every time. See jmousetrap for more$ E1 u4 D2 T" {- t4 }( g2 I$ w; h: `
// complicated schedules.
7 o' ^& h7 p. ]* l- [* N, { ( |% N. ]; F& }1 i) A* ^5 ?
modelSchedule = new ScheduleImpl (getZone (), 1);
_0 N5 z$ [5 a; k4 j5 w7 D, G modelSchedule.at$createAction (0, modelActions);: v2 b" O& k7 J/ S" ?
& M7 u% o& D: Q& w" H. `& Y
return this;- O9 |' T- C; n2 ?5 F6 H# Z/ E) e
} |