Debian Codegraph @

docs/source/Tutorials/OO_Approach.rst

raw
1 =============================================================
2 Tutorial: An Object Oriented Approach to Using SimPy
3 =============================================================
4
5 :Authors: K G Muller
6 :Date: 2010 April
7 :SimPy release: |release|
8 :Python-Version: 2.6 and later
9
10 .. highlight:: python
11 :linenothreshold: 5
12
13 Introduction
14 ------------
15
16 Many simulation languages support a procedural modelling style. Using them,
17 problems are decomposed into procedures (functions, subroutines) and either
18 represented by general components, such as queues, or represented in code
19 with data structures.
20
21 There are fundamental problems with using the procedural style of
22 modelling and simulation. Procedures do not correspond to real world
23 components. Instead, they correspond to methods and algorithms.
24 Mapping from the real (problem) world to the model and back is difficult
25 and not obvious, particularly for users expert in the problem domain, but
26 not in computer science. Perhaps the greatest limitation of the procedural
27 style is the lack of model extensibility. The only way in this style
28 to change simulation models is through functional extension. One can
29 add structural functionality but not alter any of its basic processes.
30
31 SimPy, on the other hand, supports an **object oriented approach**
32 to simulation modelling.
33 In SimPy, models can be implemented as collections of autonomous,
34 cooperating objects.
35 These objects are self-sufficient and independent. The actions on these
36 objects are tied to the objects and their attributes. The object-oriented
37 capabilities of Python strongly support this encapsulation.
38
39 Why does this matter for simulation models? It helps with the mapping from
40 real-world objects and their activities to modelled objects and activities,
41 and back. This not only reduces the complexity of the models, it also
42 makes for easier validation of models and interpretation of simulation
43 results in real world terms.
44
45 Object-oriented model implementation allows the development of libraries
46 of model components for specific real world domains. It also supports
47 the re-use and extension of models when model specifications change.
48
49 The most effective use of the object-oriented approach is an iteration
50 over Object Oriented Analysis, Object Oriented Design, and Object Oriented
51 Programming.
52
53 Simulation studies are typically performed to study systems to understand
54 the relationships between its components or to predict how the system
55 will perform in a changed environment. They are accomplished by building
56 a *model* of a system and experimenting with it.
57
58 In modelling, it is only necessary to consider aspects of the system
59 that affect the problems being investigated.
60
61 Identifying those aspects is rarely trivial and often requires trials with
62 a model and subsequent model refinement.
63
64 This short tutorial will attempt to show how this can be done for simulation
65 modelling with SimPy. It is no comprehensive course on the object oriented
66 approach, though. There are many publications on the web and also books
67 teaching OO in general.
68
69 Object Oriented Analysis
70 ========================
71
72 The goal of OO analysis is to identify the scenary in which the system
73 to be modelled operates and the system components in terms of objects,
74 their attributes, actions and interactions.
75
76 A good start is to write a concise scenario description in natural
77 language and to look for terms which identify objects, attributes, etc.
78
79 Unfortunately, there is no algorithm for this, only heuristics. For english
80 language analysis, the article `Natural Language Analysis for Domain and Usage Models`_
81 gives a good introduction.
82
83 .. _Natural Language Analysis for Domain and Usage Models: http://www.educery.com/papers/rhetoric/analysis/
84
85 Here is a useful set of heuristics for mapping parts of speech to model
86 components:
87
88 ======================== ======================== ========================
89
90 Part of speech Model component Examples
91
92 ======================== ======================== ========================
93 Proper noun Object Cashier
94 Common noun Class Bank customer; clerk
95 Doing verb Operation visit; withdraw money
96 Being verb Inheritance Is a clerk
97 Having verb Aggregation Has an account
98 Modal verb Constraints Clerk must be at counter
99 Adjective Attribute Number of clerks
100
101 ======================== ======================== ========================
102
103 It is essential that the scenario description be done in the terms of the
104 user and/or the system domain.
105
106 Here are further useful heuristics for identifying objects:
107
108 * recurring nouns (e.g. *transaction*)
109 * real-world entities the model should include (e.g. *a customer*, *cashier*,
110 *bank account*)
111
112 Model developers name and briefly describe the objects, their attributes, and their
113 responsibilities as they are identified. Describing objetcs, even briefly,
114 allows simulation modellers to clarify the concepts they use and avoid misunderstandings.
115 Initially, modellers need not, however, spend a lot of time detailing objects or
116 attributes. They will be refined during the unavoidable iterations and
117 revisions. At the end of the analysis process, this should result in a stable
118 and sufficiently detailed statement on objects and attributes. Such a statement
119 is esential for gathering simulation inputs from users and mapping simulation
120 results into real world terms.
121
122 Here is a very simple scenario description:
123
124 A **bank** has a number of **counters** **staffed** by **clerks**. It also has
125 a number of **Automatic Teller Machines** (ATMs). During the bank's
126 **opening hours**, **customers** **visit** the bank at different **times**
127 to **perform** one or more **transactions** requiring **service** by a bank clerk
128 at a counter or **use** of an **ATM**. All service by clerks is provided at counters.
129 Counters can be **closed** and **unstaffed**. If the clerk or ATM they need is **busy**, they
130 **wait** for him/her/it
131 to become **available**. **After having performed all their transactions**, they **leave**
132 the bank. The **waiting times** of the customers and the **load** on
133 clerks and the ATMs should be estimated by using a simulation model.
134
135 The highlighted words and the scenario text suggest objects, attributes,
136 operations and constraints:
137
138 **Objects**: bank, counters, clerks, customers, ATM, transactions, service
139
140 **Attributes**:
141
142 Of *bank*:
143 counters, clerks, ATMs, opening hours
144
145 Of *customers*:
146 arrival times, transactions, waiting times, departure times
147
148 Of *counters*:
149 status(staffed, closed)
150
151 Of *clerks*: availability, load
152
153 Of *ATMs*:
154 availability, load
155
156 **Operations**:
157
158 By *customers*:
159 arrive, perform transaction, wait, leave
160
161 By *clerks*:
162 staff counter, provide service
163 By *ATM*:
164 provide service
165
166 **Constraints**:
167
168 Counter service:
169 if clerk available
170 ATM service:
171 if ATM available
172 Customer departure:
173 after having performed all transactions
174 Customer arrival:
175 if bank open
176
177 It is highly unlikely that these initially identified objects, attributes etc.
178 are either sufficient or all necessary for the intended simulation model. They
179 do provide a starting point, though, for seeking further details (e.g.
180 by interviews of staff with domain knowledge) and building a first rough
181 object design.
182
183 Object Oriented Design
184 ========================
185
186 The next step is to develop an initial high-level object design from
187 the results from the OO analysis. This means that for each object identified,
188 a class must be defined to which the object belongs, i.e. a generalization
189 of the object. The class encloses all the properties of an object, i.e.
190 attributes and operations. The attributes define all the data members
191 of an object. The behaviors define how the object interacts with
192 other objects and changes its own attributes.
193
194 This class modelling should be done even if there is only one object of this
195 class in the scenario being modelled. It should be noted that the term *class*
196 here is not to be confused with the ``class`` construct in Python (and
197 therefore SimPy). The class here is just the description of one or more
198 similar objects. It will
199 become obvious in the following sections that the
200 availability of ``class`` in Python, the implementation language being used
201 for SimPy models, is a great benefit. It allows clear, relatively simple
202 mapping from the OO design to a SimPy program.
203
204 Class **Bank**:
205
206 Attributes:
207 counters, clerks, ATMs, opening hours
208 Operations:
209 open, close
210
211 Class **Customer**:
212
213 Attributes:
214 arrival time, transactions, waiting time, departure time
215 Operations:
216 arrive, get service for a transaction, leave
217
218 Class **Counter**:
219
220 Attributes:
221 staffing(staffed, closed)
222 Operations:
223 open
224
225 Class **Clerk**:
226
227 Attributes:
228 availability (available, busy, absent)
229 Operations:
230 provide service, staff counter
231
232 Class **ATM**
233
234 Attributes:
235 availability(available, busy)
236 Operations:
237 provide service
238
239 Control object needed to set up simulation experiment:
240
241 Class **Model**:
242
243 Attributes:
244 model components, experiment data
245 Operations:
246 get experiment data, generate component objects,
247 run model, report results
248
249
250
251 Object Oriented Programming
252 ===========================
253
254 Object Oriented Building of SimPy Simulation Models
255 ----------------------------------------------------
256
257 Basic Recipe
258 ============
259
260 * Describe the situation to be modelled in natural language.
261 * Identify the nouns and verbs as candidates for classes, objects
262 and actions, respectively.
263
264
265 Model Components Provided By SimPy
266 ==================================
267
268 User-Developed Components
269 ===========================
270
271 SimPy Model Structure
272 ======================
273
274 Re-use of Components and Models
275 ================================
276
277
278
279 Examples: The Bank World
280 =========================
281
282 Objects and Activities
283 **********************
284
285 Bank Model Components
286 *********************
287
288 Model 1: ....
289 **************
290
291 Model 2: ......
292 ***************
293
294
295