HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! `" G8 c# ?7 [ `: r3 e" O& F @9 ^+ p/ ]+ X4 L) {
public Object buildActions () {1 H0 O; w: k) m- @) I; k
super.buildActions();
0 f6 C9 S F, s/ y ) k# m6 V* g$ `, o7 V6 V
// Create the list of simulation actions. We put these in
* v# R& S$ j7 z" v // an action group, because we want these actions to be, C5 g- h# ?4 Z( E/ A
// executed in a specific order, but these steps should
! F; t U: a! G! ^+ | ~, V# e // take no (simulated) time. The M(foo) means "The message
6 b/ e$ g6 d: o: y // called <foo>". You can send a message To a particular
: p7 T: K- {( `- _. ?$ R) M // object, or ForEach object in a collection.8 q/ }; B- R& ]# F- Y5 I* D
$ A4 I/ M6 W" \7 G* B+ ]9 w3 ^0 T // Note we update the heatspace in two phases: first run p( ^& {$ U3 X/ K
// diffusion, then run "updateWorld" to actually enact the. j- C( `5 ~# E* e$ _$ O
// changes the heatbugs have made. The ordering here is
. T. O0 d( ] V: B& p // significant!
- u( {1 x, H4 Z6 O7 f5 U
; k3 J: F& |+ E6 v: V( W // Note also, that with the additional* W: T$ S2 T8 m
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& U8 I" \$ j G+ m# e // randomize the order in which the bugs actually run
0 A5 @! V: n$ W, }* { // their step rule. This has the effect of removing any+ E- q6 Y; V* _- E2 V4 {
// systematic bias in the iteration throught the heatbug+ p1 W: r* {8 ^0 Y5 `
// list from timestep to timestep
& s; F4 E9 V" t* f1 S) w- `
9 I# d; i! b' D // By default, all `createActionForEach' modelActions have* s+ R& [: k) U {% H! V f6 }6 {
// a default order of `Sequential', which means that the4 L# N" Q' e8 b2 }" e6 F! d' z9 v
// order of iteration through the `heatbugList' will be0 N2 W# K9 Z2 w$ V: l8 ]
// identical (assuming the list order is not changed5 [' k% N* P: Q" y% d' | c
// indirectly by some other process).9 ^8 ?! E* z1 @' w, D2 M. ?' g
% T9 l4 @8 T3 U4 p* s3 p modelActions = new ActionGroupImpl (getZone ());
6 W, ?, ]. d, P1 |9 q |
+ q) T8 q& T; L/ H try {
3 D( m- T& O: J" U* M modelActions.createActionTo$message
6 p8 N% ?0 \# o: Q3 a% w (heat, new Selector (heat.getClass (), "stepRule", false));$ [" U0 W$ n% @ q2 V" d0 m: i6 [$ F
} catch (Exception e) {
1 ]# `6 f% [" q$ r5 \3 ]/ V) ` System.err.println ("Exception stepRule: " + e.getMessage ());
$ Z# `& a; c2 @! Y: m4 Q }: H* w7 ]+ c \, F" E- v
1 ~( q. T: N1 l4 H% W5 }
try {- y4 F! V- d# U. C0 O
Heatbug proto = (Heatbug) heatbugList.get (0);4 x s# I H7 x8 j3 x
Selector sel =
7 n6 R! v1 t4 n9 m" N% i new Selector (proto.getClass (), "heatbugStep", false);- F# ~) _ o- ]2 C+ ?
actionForEach =
) K2 K& a8 z' R( r modelActions.createFActionForEachHomogeneous$call4 I+ s- x; K' X9 G' O5 o8 z* x" w
(heatbugList,, R* H) O8 W r Z5 f- M
new FCallImpl (this, proto, sel,, M/ K2 F2 g7 V" J' n
new FArgumentsImpl (this, sel)));
/ ]. m: H# [( b* U0 C } catch (Exception e) {
7 R; e8 ?0 i3 ^" I9 s a! {" [ e.printStackTrace (System.err);4 @" E* E: G* y e1 I& {7 \ f3 y/ t
}
/ N! f/ x# Q8 X8 I" ` 8 y1 f3 N# G$ v, J$ ]
syncUpdateOrder ();
2 m# @* k3 o5 L" s+ e# a9 j8 x0 s) m4 I5 B
try {
; J* R% x# H' t/ ?* G. [8 ]5 M# \ modelActions.createActionTo$message
0 ^1 Z+ u; l# ^$ B) X) \ (heat, new Selector (heat.getClass (), "updateLattice", false));
& d5 Z; `2 s3 d7 M% j6 s1 g, \ } catch (Exception e) {$ x" W$ _( Y0 E+ Q
System.err.println("Exception updateLattice: " + e.getMessage ());9 U6 K7 G) m: ^, F
}5 v9 I! ~9 v% M
! L! {+ H( u; e# {: b$ X // Then we create a schedule that executes the( u- \9 A% X D. Q
// modelActions. modelActions is an ActionGroup, by itself it' a/ Z+ b0 r! u5 f& G- X
// has no notion of time. In order to have it executed in H# a! O0 T* `- i# b8 C! M
// time, we create a Schedule that says to use the# C* v0 m. F! ?3 }- G8 ?
// modelActions ActionGroup at particular times. This
1 y6 e: r) o/ A; G6 L // schedule has a repeat interval of 1, it will loop every
2 M+ z2 }4 S2 `% H1 { // time step. The action is executed at time 0 relative to1 O$ m. F- U& l. t3 S4 a N; U
// the beginning of the loop.! ~3 P( `0 f$ Z) K
$ s3 e( d8 J1 j2 D) M1 N! {
// This is a simple schedule, with only one action that is J8 p" \1 o+ W+ y2 E
// just repeated every time. See jmousetrap for more
; N4 O( O/ K( g9 z* ~$ |2 t // complicated schedules.
( t2 w5 h q0 H+ V/ R 7 D J5 P$ g& C) X' O6 v
modelSchedule = new ScheduleImpl (getZone (), 1);
7 W6 M" ]; k, D modelSchedule.at$createAction (0, modelActions);- y9 E' A* q* n
$ H8 b4 ^0 c8 I; n0 P. m ~4 S return this;
# ^4 v0 X1 Q4 Y: s } |