HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: F/ v5 u# H+ m3 o& C7 c" z" d
( o% N: a. d d% @8 ?) t/ M+ I public Object buildActions () {& i' N: z2 Q, k E- x0 o/ \" y
super.buildActions();
5 Z8 D1 ]( d& p* | ) q6 h* E5 }" T4 `# d
// Create the list of simulation actions. We put these in
, b1 t! E4 K* H1 C7 d // an action group, because we want these actions to be; {0 i0 l: O- Z H0 O% Y5 j- N
// executed in a specific order, but these steps should
9 V3 v9 ~/ s7 U0 X$ E' V // take no (simulated) time. The M(foo) means "The message
0 y8 Q- p/ A7 ~; z- t // called <foo>". You can send a message To a particular
3 H R( }% V0 T+ Z9 v" w: Y5 ~: { // object, or ForEach object in a collection.6 B E S& u4 l* s9 Q+ e- H
- g% @; a6 K) E
// Note we update the heatspace in two phases: first run7 {# M4 g0 _" c- E; x* s
// diffusion, then run "updateWorld" to actually enact the
S. ?" t6 b4 [ // changes the heatbugs have made. The ordering here is" ?5 ^& t9 u. W9 P4 a0 }$ X& _
// significant!
( \& L; B) B- S, K# g
7 K& Z( E3 \& x, ? // Note also, that with the additional
2 W8 h2 w- Y5 r1 J* G // `randomizeHeatbugUpdateOrder' Boolean flag we can( |1 r$ p- L7 ^: p/ _9 D7 \& `: |' J
// randomize the order in which the bugs actually run
( ^( h' S: }# K) n- {& u. o // their step rule. This has the effect of removing any
: U* F1 N! A/ _4 Q5 O3 x: s$ g // systematic bias in the iteration throught the heatbug
" K1 N9 e" P+ I // list from timestep to timestep
0 k! `7 S! P$ k" r
* e3 f# q' |# D" i; j' X# v4 { // By default, all `createActionForEach' modelActions have# [- ?: }. E! i
// a default order of `Sequential', which means that the
# k; v0 B5 j5 I0 B0 N" P: L8 R // order of iteration through the `heatbugList' will be# U0 _; U( J) I; |
// identical (assuming the list order is not changed
* ~8 T7 k! _2 ]; L- j // indirectly by some other process).# K% ` g3 Z6 g
% ]+ W' L6 y' w; f2 T& J modelActions = new ActionGroupImpl (getZone ());1 G5 a) L; P/ t/ ]4 I9 m: Z
! n% K$ B5 o! ]' D2 ? try {
4 } h7 j9 c& k3 I+ U modelActions.createActionTo$message6 R+ x' d5 Q1 R* W" A/ R
(heat, new Selector (heat.getClass (), "stepRule", false));$ D0 E. s" x- A- Z; V7 b$ `$ O
} catch (Exception e) {
$ A8 ~) C6 X' } System.err.println ("Exception stepRule: " + e.getMessage ());
4 F* P# {0 v q" J- J; N }* s% L8 d; W7 J* t
9 d1 M& @9 H& U7 d2 R( }) [
try {
% P( k6 l- ?3 | Heatbug proto = (Heatbug) heatbugList.get (0);3 N i: S# F& m' ^
Selector sel = & y/ Q' F' R) U8 Q
new Selector (proto.getClass (), "heatbugStep", false);
8 f2 Y A- j) j* s: Q actionForEach =
% G8 W! d. Q0 L3 k% |+ c modelActions.createFActionForEachHomogeneous$call% N' i$ u& S! [) ]0 ~) [
(heatbugList,* [: a! `! ~; ?2 o
new FCallImpl (this, proto, sel,
. H# _+ X: c2 j; N; q new FArgumentsImpl (this, sel)));
: K: e1 \0 l) ?- N2 U6 Q- q" E' E } catch (Exception e) {; H, L) B$ K4 t: B) ~
e.printStackTrace (System.err);# \: A4 ^4 B. U- G# e% c2 X
}) m( M) t' n* G. T
, q# S( |) ~0 ?1 f syncUpdateOrder ();" P' q/ d+ n5 V* ~' _, f: G
& G6 A4 @& w2 s( A" |" }
try {
9 a9 S) k1 a) I modelActions.createActionTo$message ! p9 ^6 o" \& Z" x
(heat, new Selector (heat.getClass (), "updateLattice", false)); L5 Q! l$ i& e/ K* N
} catch (Exception e) {
: z" A0 U' j6 {* B& Q9 z System.err.println("Exception updateLattice: " + e.getMessage ());- g, j. M# m4 ^4 n, p0 e
}5 n3 K u# ~/ ]! y4 \3 m, n
, x) L0 |, a7 U2 m0 ?& P8 X
// Then we create a schedule that executes the! l, g( G. u* A+ T/ i! c
// modelActions. modelActions is an ActionGroup, by itself it: g8 Y4 B* B3 q
// has no notion of time. In order to have it executed in6 g& H+ c, O+ v) q- |2 H$ O
// time, we create a Schedule that says to use the
' @5 P7 y% I( [& d$ @% h. `% X9 ? // modelActions ActionGroup at particular times. This5 w. r- u3 W$ H8 }) X5 N
// schedule has a repeat interval of 1, it will loop every4 j6 d! D5 k! X
// time step. The action is executed at time 0 relative to
( ~) p4 E; @8 A5 Y1 P( Q // the beginning of the loop.
, f$ U7 T" N3 L0 t1 m; n$ m3 k$ F4 B {! l& ^8 o3 Z
// This is a simple schedule, with only one action that is
( F3 p) Y" l3 C, U+ l& Q# b j2 e // just repeated every time. See jmousetrap for more# D% l1 h% [% o3 }
// complicated schedules.
* a" M. g R9 e! E) w
7 @/ j4 Z0 u& Q4 a3 o+ h/ m modelSchedule = new ScheduleImpl (getZone (), 1);; y5 _! p- u" f1 L8 s( L
modelSchedule.at$createAction (0, modelActions);
6 S4 H: R& ?, m9 N1 W' B# J 7 p% j+ d: P% x# R
return this;4 ?4 h. g; y! Y% K0 X, G8 b# V
} |