HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 z8 O- ?( ~. {9 Q: M' J4 l- e% Z4 q
public Object buildActions () {
$ f2 [4 n9 y1 ]2 B' W( H5 a6 R super.buildActions();
# _* h- d& b% X W
1 \2 D, I F5 U# D# r! w. d( V // Create the list of simulation actions. We put these in
' `8 C4 |* y4 W- ]% i4 Q. \ // an action group, because we want these actions to be; C& r) l6 v, T7 H/ d) V+ m+ {
// executed in a specific order, but these steps should
* ], b" D$ {, u1 Q% \ // take no (simulated) time. The M(foo) means "The message' S' _) _/ h4 J, X
// called <foo>". You can send a message To a particular4 G) A3 f& v) W6 |* o1 b
// object, or ForEach object in a collection.
/ F5 H& m3 @% U' o7 @
& K% D+ F% Y0 b // Note we update the heatspace in two phases: first run
( L; @, Y$ J" s( v/ J5 ?5 \ s$ g // diffusion, then run "updateWorld" to actually enact the' k8 R: v5 z: U! A. R+ }# O
// changes the heatbugs have made. The ordering here is! A; A# B5 H" x7 }5 Q) Z
// significant!* {$ O* T6 a! z, z6 I
0 ]6 j ~6 U3 q( ?" L // Note also, that with the additional
% s! x1 k1 S8 }. u // `randomizeHeatbugUpdateOrder' Boolean flag we can. e7 a- n: ]! m9 g* L4 {4 V) z
// randomize the order in which the bugs actually run0 Y9 x7 [1 P: E
// their step rule. This has the effect of removing any
- a; p. l9 Z4 Q0 R* R0 \) s* N% e2 { // systematic bias in the iteration throught the heatbug3 k+ b+ u7 c% B" f6 [! A5 S1 k, y/ Q, _
// list from timestep to timestep
D: y" v$ ]5 B S$ I, E 5 J1 A9 X8 K9 g9 N/ ?
// By default, all `createActionForEach' modelActions have
/ {& }& e2 D6 h! O: D; H5 _6 ] // a default order of `Sequential', which means that the1 W; \9 o6 w* w5 v8 V
// order of iteration through the `heatbugList' will be
5 m G7 L4 R7 n3 }8 x4 n9 G& o" Q2 K // identical (assuming the list order is not changed
7 {- y z7 b X7 r/ q" J5 H // indirectly by some other process).2 U9 c& w0 L1 u Q
$ B) V( J$ S# e* a+ F3 ?
modelActions = new ActionGroupImpl (getZone ());6 \- l4 |6 r" @9 R# X
" k9 G! X2 r% K3 w try {3 }- _8 k/ [9 ^ U
modelActions.createActionTo$message& i2 f0 B: H. p/ J* d4 j" I: [
(heat, new Selector (heat.getClass (), "stepRule", false));* ]* X8 ?8 g6 p! I! ^
} catch (Exception e) {! i( g/ ]/ ^: m
System.err.println ("Exception stepRule: " + e.getMessage ());& G, f$ X6 T- W8 d' N
}
) U m/ L2 f6 r& b$ u) R& x; ^. S# [) V
try {
9 {' O0 I/ G$ `3 k, K Heatbug proto = (Heatbug) heatbugList.get (0); m6 w$ _% ~( Q
Selector sel =
2 i1 N" K& M5 m) T: k new Selector (proto.getClass (), "heatbugStep", false);
! N$ V- Y0 C4 Q- N" V actionForEach =
& V+ `" n2 b& O& y9 N5 @: f: S2 l; |+ R8 J modelActions.createFActionForEachHomogeneous$call
1 S# N( a+ }" A& O) f) E (heatbugList,
( p% s! I* O4 A% R new FCallImpl (this, proto, sel,
& q0 U1 Z T. M3 U! N* Q new FArgumentsImpl (this, sel)));1 ?5 |# F3 e# t2 Q4 C; |2 |
} catch (Exception e) {2 i2 ?% o! h& h$ p" I+ C0 Z
e.printStackTrace (System.err);
! ?# l- C' g/ Y) w2 F }; r9 ^4 w" _+ m6 I. E: t A- }
5 g m- i/ {. Q* p syncUpdateOrder ();
0 g& \3 i; }3 d k, U: a; ]" ~
try {
0 \2 `3 @& R' \. q4 f6 m modelActions.createActionTo$message ! L3 Z6 S% m* e9 F
(heat, new Selector (heat.getClass (), "updateLattice", false));5 P" A- [: A# }# ~# ^: T
} catch (Exception e) {
. j) [ Q/ K. Q0 [0 Y System.err.println("Exception updateLattice: " + e.getMessage ());
- u) S$ A u+ y$ G7 W }% b# z0 s# \5 h C0 R& Q. r1 [; R0 [
1 C# Q+ d9 R4 X // Then we create a schedule that executes the
$ R2 F5 w2 {; ~7 a2 ^* W8 G // modelActions. modelActions is an ActionGroup, by itself it
* }+ g, i! a8 X2 V // has no notion of time. In order to have it executed in- v; S) [5 L% d& j- K- h; B9 A
// time, we create a Schedule that says to use the
9 | Z2 R: \% n0 C& g6 D // modelActions ActionGroup at particular times. This' ]5 w3 D3 y& T% {- v
// schedule has a repeat interval of 1, it will loop every
( }+ {: @3 {+ v5 E4 `9 I4 L1 p // time step. The action is executed at time 0 relative to
\# A+ S' g$ [0 U // the beginning of the loop.
1 s- u t( @ u- f O' N7 \3 W+ } J2 c
// This is a simple schedule, with only one action that is/ I8 t6 h3 z& j0 G
// just repeated every time. See jmousetrap for more
4 K; u4 }* ]2 ? // complicated schedules.
( B7 V% ?, P I1 Z( h8 {+ _ 5 K2 X4 ]- `; Y8 s
modelSchedule = new ScheduleImpl (getZone (), 1);
8 p0 k* Y: g Q modelSchedule.at$createAction (0, modelActions);0 z' ]) Q$ I4 o6 \2 T6 D8 P
/ _4 k# h" P( g1 w0 Q return this;
. I2 a0 |2 d' y } |