HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' o2 p( L, q9 ~ N% r! z
( X6 l# A4 g; F& L2 w public Object buildActions () {5 A4 J. U9 V. _* d
super.buildActions();
9 w3 m$ O$ g5 {$ W# O
% w+ f/ z, Y$ t: P* e0 @3 p# H" z ] // Create the list of simulation actions. We put these in: }0 }: E- c3 W. ]6 [
// an action group, because we want these actions to be( _5 B4 }+ i& _- w
// executed in a specific order, but these steps should! A/ R- ?3 B* a5 a5 N
// take no (simulated) time. The M(foo) means "The message
: {8 V$ u1 A7 q, Z( G // called <foo>". You can send a message To a particular
: I# n1 f+ S# ^# K // object, or ForEach object in a collection.; W) g8 g2 ~& _1 n% y
4 `( @* ?, s5 e E* x2 | // Note we update the heatspace in two phases: first run$ D9 J. r& C7 r) [, U4 V
// diffusion, then run "updateWorld" to actually enact the
" f) E3 P+ {8 y+ r9 o0 f9 H // changes the heatbugs have made. The ordering here is
4 }! J( E* P! B0 C- k // significant!- z8 f3 v( O$ H2 y3 M/ z% N! A7 c$ [
7 \5 X( H! u1 B% R- p
// Note also, that with the additional
& Q6 D! q2 i3 F6 V* t/ r7 f; m // `randomizeHeatbugUpdateOrder' Boolean flag we can
" C+ w' n6 c6 B& ?4 ~/ | // randomize the order in which the bugs actually run' Y, g, E5 M I( E
// their step rule. This has the effect of removing any5 t9 F6 P3 n4 ?( l: K z7 i
// systematic bias in the iteration throught the heatbug
, [$ A$ k1 D8 W% R! n1 B9 o // list from timestep to timestep& p3 w, d, ~' g$ h3 f
h3 d( d8 S0 q' @8 n // By default, all `createActionForEach' modelActions have
& j1 H7 |6 q$ x; P4 C+ I) O2 P // a default order of `Sequential', which means that the7 T' @' j* b1 B5 L; G% D; R
// order of iteration through the `heatbugList' will be. K `# s2 h( q6 W6 L w# A, ~5 o
// identical (assuming the list order is not changed
; G0 `; E+ k/ p8 i. o // indirectly by some other process).6 z* o+ y }) o( }6 g3 }
3 A/ Z/ M/ d) H8 T1 U: @" l
modelActions = new ActionGroupImpl (getZone ());
7 ?2 k) \! M7 m+ y) }+ `2 ], R6 N. j: {0 e! V3 h ?- W
try { o6 b) b; ^3 x, K. X7 [
modelActions.createActionTo$message
# P, E4 _1 S! h8 X3 V3 ? (heat, new Selector (heat.getClass (), "stepRule", false));
+ @: n( c& m/ k6 V' S } catch (Exception e) {
4 i6 ~# h& j0 ^3 b+ k1 X4 t System.err.println ("Exception stepRule: " + e.getMessage ());+ E; h- q! y4 K3 x% X, n( M
}& s q+ n1 u F# z& y1 W! C7 D1 j3 ?
+ u7 j2 e/ Z7 y* C; p+ r
try {9 Z( b3 B8 F& r' X$ H8 V
Heatbug proto = (Heatbug) heatbugList.get (0);. ~* j* a6 ~8 P. m
Selector sel =
8 a I0 P1 J1 m& U# }0 n new Selector (proto.getClass (), "heatbugStep", false);
$ D/ ], i+ S1 u actionForEach =. F0 V. [1 Q7 Y. q; Y
modelActions.createFActionForEachHomogeneous$call
5 M" l7 b- u S! U, H. A (heatbugList,' j! V+ t" e( u0 p' H5 S! b" I
new FCallImpl (this, proto, sel,
9 }) ^' V4 a Z( J% o3 g1 M! Y new FArgumentsImpl (this, sel)));4 ~5 L# t4 _ u2 [( s7 U& t+ K5 F
} catch (Exception e) {
1 x8 F9 \7 |$ c$ m0 [" t e.printStackTrace (System.err);7 \6 k, u5 S- l1 [! J$ E
}1 K6 B7 h7 s G' F
. F, p( U9 ^. }' m4 r1 }" f* D& G0 P, F syncUpdateOrder ();
# G" v) Y" i: o' A2 F% u* y, L) m1 f; J
try {+ c1 q4 K% E3 [5 P J0 \+ f/ k% j5 t
modelActions.createActionTo$message
- P! O9 A& |1 c6 F3 \! o (heat, new Selector (heat.getClass (), "updateLattice", false)); s" U0 ^' W! E S$ q9 t/ v& r
} catch (Exception e) {0 b" l( I2 T/ P; e
System.err.println("Exception updateLattice: " + e.getMessage ());
0 W/ ` w: _* f; ?- S3 B: _3 L }5 G% R, _5 k& W4 Z- m
$ a ?6 U+ A2 n: p3 U // Then we create a schedule that executes the
( K2 J' \5 A# x3 V% _ // modelActions. modelActions is an ActionGroup, by itself it8 j' l. Y5 s5 P
// has no notion of time. In order to have it executed in
- Y! {9 s3 K+ j. j! ? // time, we create a Schedule that says to use the
. y- L( y5 w8 H% L6 e+ o4 O# [ // modelActions ActionGroup at particular times. This
* _; a3 y1 V) @+ k" B# @4 f // schedule has a repeat interval of 1, it will loop every
2 z) |6 ?8 v& q- N5 q+ t // time step. The action is executed at time 0 relative to
9 U: L5 g& Q: G5 F: B7 b // the beginning of the loop.* N5 i# w7 B9 t
6 e( [1 G4 D/ u7 t0 h9 z0 F
// This is a simple schedule, with only one action that is
, `& ?6 ^) R$ Z) @: k // just repeated every time. See jmousetrap for more
" \5 X* f% k9 h- Q, B // complicated schedules.5 |6 X2 y0 v% J4 w1 c1 ^- l/ o
8 Q5 W+ m8 Q6 |' g5 ~
modelSchedule = new ScheduleImpl (getZone (), 1);
- [# U4 K K& ?- N, w$ E. ?9 D modelSchedule.at$createAction (0, modelActions);7 ]& M t& t; s8 |3 `: x
% Z Q3 X6 i" e; K* v return this;# ^" E( J6 i0 {0 q, Y6 o; {
} |