HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 k% |% f. N: X$ D
$ m8 Y6 r4 ^) n3 e8 R public Object buildActions () {: t7 C- w `1 R
super.buildActions();
/ D* ^' r! _- f% A2 } , ?1 p3 W+ |/ _- X, y+ x
// Create the list of simulation actions. We put these in
2 k4 K% v% Y: i9 B0 E7 Z // an action group, because we want these actions to be# j C- a4 N/ Y9 g4 j/ w: F F/ [6 g9 c
// executed in a specific order, but these steps should
* Y! N0 c, T: s2 V) r" N. j // take no (simulated) time. The M(foo) means "The message: e* u5 p9 j& z1 ^" r
// called <foo>". You can send a message To a particular
( ~; b: N% b+ z' o$ P+ [& K# o // object, or ForEach object in a collection./ C3 A% G3 H/ L A7 R$ }
5 L2 F; g" G e$ `! v9 u! _ // Note we update the heatspace in two phases: first run7 ~: A( d" d) p
// diffusion, then run "updateWorld" to actually enact the
g; [5 Y0 a! p: j6 b8 M5 K3 z // changes the heatbugs have made. The ordering here is
# _5 C5 b, u+ W) s6 A2 @4 c C // significant!/ J: g! }6 A0 r
0 L! U$ G( m* \9 ?; g; y7 o$ ^3 p
// Note also, that with the additional- c; r5 V* I- j3 h F- G9 F' S
// `randomizeHeatbugUpdateOrder' Boolean flag we can: i4 A5 o5 g, J, L X& j! y/ f
// randomize the order in which the bugs actually run) \) t5 `/ @' n8 H
// their step rule. This has the effect of removing any7 P: |6 g0 ^- S" b3 |4 `
// systematic bias in the iteration throught the heatbug
% \: o- A. {9 F# d) d% i // list from timestep to timestep
C) d4 Q; ]! R5 b+ ?2 H. y8 ]
7 e7 \1 ?4 W9 X0 ^ // By default, all `createActionForEach' modelActions have# G) j e/ {$ E8 \ p
// a default order of `Sequential', which means that the
- x" S3 U0 N5 e) H+ J, H0 b( d7 `9 N // order of iteration through the `heatbugList' will be
1 c# h4 P; e8 C" ~/ \+ o5 m // identical (assuming the list order is not changed
7 e- W$ G4 U0 e! _( | // indirectly by some other process).9 {0 N9 J0 m3 A
" G! m: \: X& F) i modelActions = new ActionGroupImpl (getZone ());+ `2 V6 p* O+ T) k& Z g7 T. l
1 h( c7 ]9 {" F H) M# u2 k
try {9 {% Z E5 r0 B, S/ c% k2 X
modelActions.createActionTo$message5 l9 N3 Q9 m8 k$ O2 r
(heat, new Selector (heat.getClass (), "stepRule", false));7 t! S7 u4 A$ X
} catch (Exception e) {3 F+ v+ ?$ U% _" i) {# M: T
System.err.println ("Exception stepRule: " + e.getMessage ());
2 y; ?, Z c E+ N ~ }
2 h" m; x8 A: }) O) U F. V9 [/ d" f; M/ }3 R4 }
try {
9 Q/ _, F/ n3 Z0 C5 p9 y: X8 @" B Heatbug proto = (Heatbug) heatbugList.get (0);
1 X1 y" P U/ z' H8 h Selector sel =
9 E7 J% ~* B- a5 ~ new Selector (proto.getClass (), "heatbugStep", false);
- D/ o R' C: U W- D; z actionForEach = d$ [( p/ p$ I( ~; E
modelActions.createFActionForEachHomogeneous$call
- f- V/ T3 Y" J q$ w2 ^ (heatbugList,
2 A: E7 g" l9 h/ s6 |4 ]$ H8 S# v new FCallImpl (this, proto, sel, m* l* A8 c( o; k9 K: o/ K
new FArgumentsImpl (this, sel)));
, c( _0 N" i3 G+ V- W9 j } catch (Exception e) {
/ Y5 P8 @) t1 N' b e.printStackTrace (System.err);3 L- r# b- n3 n( }; J2 D- ?/ A; h
}
% h* Q7 b$ W: i3 `* Z
6 s5 |1 ?% M0 x1 o: x syncUpdateOrder ();+ K, E. p8 s: h3 c
. H& h9 Z3 I6 `. @+ z$ B try {
! J# q- N/ c1 g8 u+ y% U+ o B modelActions.createActionTo$message
. q% `$ F: J: U& J7 H (heat, new Selector (heat.getClass (), "updateLattice", false));' } ]& w! g/ Q; g, g6 D/ `! F
} catch (Exception e) {7 e& B, \& a4 _2 G2 e
System.err.println("Exception updateLattice: " + e.getMessage ());1 D/ ^4 X: b v7 X) w+ u
}
2 l! A2 }: @; v/ {: K 6 U0 K1 a9 r& D7 ]
// Then we create a schedule that executes the O( x: G) }+ j' }6 t- y
// modelActions. modelActions is an ActionGroup, by itself it
# T: n6 b6 Z. t8 A% V- ~$ j // has no notion of time. In order to have it executed in: }2 g. L. U+ U, i
// time, we create a Schedule that says to use the2 j n: O ?7 [2 p/ {/ j' i
// modelActions ActionGroup at particular times. This
- R9 ?0 l3 g: I I // schedule has a repeat interval of 1, it will loop every1 n8 A8 \# ?' P' x+ q
// time step. The action is executed at time 0 relative to' I1 M! p1 N; H+ H; ]4 ^: p1 ]
// the beginning of the loop.
9 j' ], @. ^, T0 e/ \; i( S$ P+ o; K2 b, @* f; B
// This is a simple schedule, with only one action that is4 Y- P1 ^8 _1 x1 i
// just repeated every time. See jmousetrap for more
3 z+ d" C. ~1 `( n! B' @9 l // complicated schedules.( g h- E( `8 G7 V; |6 [
M( |4 E7 N! \
modelSchedule = new ScheduleImpl (getZone (), 1);
, P0 E8 p9 W+ W modelSchedule.at$createAction (0, modelActions);3 ]! ]( p; \# L4 b. R- R+ `3 j
9 b: U9 q2 y# y6 p3 L return this;
# \2 x; B( B$ {3 i% o* | } |