HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 l' \1 F0 O, p4 _7 R
9 D/ B9 w( O2 ^8 K& S2 J public Object buildActions () {
3 ^; t4 q4 F, N4 N9 w super.buildActions();( r! @: t( z+ w% s. ]5 ]( P0 L
. \5 Q2 E/ z+ q
// Create the list of simulation actions. We put these in
/ d! Q; w- i" P6 T8 | [) O // an action group, because we want these actions to be# k( ?, ]' a+ V+ S7 H/ I
// executed in a specific order, but these steps should
- {6 o0 H' f! _8 {8 q+ Z& |* f5 @ // take no (simulated) time. The M(foo) means "The message8 O9 J. ^2 _/ r
// called <foo>". You can send a message To a particular
7 E9 n; T' ~4 D // object, or ForEach object in a collection.! T& Y$ J4 v. \5 R
; s" e) }) e2 k# f4 ~. x // Note we update the heatspace in two phases: first run7 ^$ g7 b: `: i2 C
// diffusion, then run "updateWorld" to actually enact the% N' c' M: G" X# r" ?7 ]8 s
// changes the heatbugs have made. The ordering here is/ r( b% F# ?* n8 q$ O
// significant!3 G; F: v6 r& x g, f% p+ W( F
8 k+ z8 \ B' ]: I( @: D8 G& W // Note also, that with the additional0 Y9 y2 R! x# f) K$ m% b, c
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. \* M$ m1 v8 V) t+ R // randomize the order in which the bugs actually run
1 \! n# w* a6 m0 o, ?+ u // their step rule. This has the effect of removing any H$ @) s! E4 j- ]; Q
// systematic bias in the iteration throught the heatbug
3 I+ o4 S) L4 ?2 ?6 [7 T) `. d. } // list from timestep to timestep
: s3 }8 H- X+ S" y/ f9 u2 x! h
U' x+ c) v; | M; k# \; y0 S // By default, all `createActionForEach' modelActions have
! T& R4 J- N0 Q2 w5 _3 M, D // a default order of `Sequential', which means that the% ]. i4 i' ?' R7 [% I: i
// order of iteration through the `heatbugList' will be
0 A2 w0 D8 M8 O6 Y- p3 t5 ^9 [ // identical (assuming the list order is not changed
! e$ r9 i0 z( u4 y: J3 t8 g" `2 M2 @1 i // indirectly by some other process).' f$ W9 x, ?5 Y- Z
4 r0 \. V' i& P$ q) o: z
modelActions = new ActionGroupImpl (getZone ());, ?/ Z+ `1 ]3 Q( J- |/ }3 R$ B
" e0 S% e% h5 v0 ?4 C4 J
try {
- s' ]- J3 T" M4 Q) {; z modelActions.createActionTo$message
' e8 V# w6 {. E) F5 y. Y! y; G (heat, new Selector (heat.getClass (), "stepRule", false));" v' N! P+ ~* r
} catch (Exception e) {/ m9 t0 {: q/ P m% {& e
System.err.println ("Exception stepRule: " + e.getMessage ());
+ T- ]( I3 v) W }) B+ c# [8 C$ f2 t N
( S( {* Z: M! |
try {2 O' P# [; D6 I0 p
Heatbug proto = (Heatbug) heatbugList.get (0);
; W+ i. R. ^9 S& `1 s Selector sel = ) [$ l/ Q+ ^6 g _8 T- A; H+ x
new Selector (proto.getClass (), "heatbugStep", false);. G5 [+ d' s- n3 p* a
actionForEach =& i( ?7 G5 ]0 J' q3 x) }7 A
modelActions.createFActionForEachHomogeneous$call7 o$ t W' W( t$ ^6 x
(heatbugList,
& r$ g6 L! l2 q; E" N! R new FCallImpl (this, proto, sel,
# {3 s6 l$ z- u3 z( U9 D0 w" e# M new FArgumentsImpl (this, sel)));/ Q$ A2 s3 F V% O, B$ S
} catch (Exception e) {
( B1 J" c2 w0 g6 } e.printStackTrace (System.err);
6 b0 o4 D6 C2 v- q }
2 V1 e/ ?: ]# Q4 i; `8 d% q: |
' X7 `7 Z0 D9 [" l( H- S" f- |. x syncUpdateOrder ();! @$ F7 @* d4 \% S* {
1 l1 x+ S+ h/ z2 o/ f
try {
1 b, |, a1 j5 ^ modelActions.createActionTo$message
4 g- j+ O& ?- f1 C M6 Y; g3 H9 A (heat, new Selector (heat.getClass (), "updateLattice", false));
: |5 |5 t- C3 ^+ p6 n" @ F2 o } catch (Exception e) {2 ]& |6 ?& P) C7 @( B! R
System.err.println("Exception updateLattice: " + e.getMessage ());1 m0 d% n; {0 |* Z* b
}8 T! K; p3 L( N9 ]' p
( E+ g! o" t/ f8 E- \ }
// Then we create a schedule that executes the5 T( q; L( n0 @, G: K# K
// modelActions. modelActions is an ActionGroup, by itself it
2 g% F+ m$ _( v) H _2 N/ O2 | // has no notion of time. In order to have it executed in2 ]+ L8 P0 X( T& Z
// time, we create a Schedule that says to use the
# R" l# [7 Y2 p) e& l" J // modelActions ActionGroup at particular times. This
7 Y% M8 F+ ~+ A1 l4 F // schedule has a repeat interval of 1, it will loop every
3 R; V) D. t" R& f8 j7 g8 j // time step. The action is executed at time 0 relative to) m3 C0 ]) E6 m* |* Z( V
// the beginning of the loop.
, ]* f9 d$ X; S+ i) j
3 e* R( Y, C, w7 f // This is a simple schedule, with only one action that is
2 v2 h7 Z# @4 ?) ^0 K7 t/ b // just repeated every time. See jmousetrap for more: M3 s3 S% M6 R7 N! y
// complicated schedules.
$ _7 ~! A H9 s5 S ' @% C7 ^& L3 [; i; b
modelSchedule = new ScheduleImpl (getZone (), 1);4 W% a! u- t4 s, V& v3 x! J
modelSchedule.at$createAction (0, modelActions);3 j7 _! ^) X' J# o5 Q
" W7 F! j v! `( l
return this;
( s* K% J" L9 g } |