Static model

CS551
Static Models: Object Attributes
and Invariants
(Chap. 2 of UML)
Yugi Lee
STB #555
(816) 235-5932
[email protected]
www.cstp.umkc.edu/~yugi
1
CS551 - Lecture 7
Object Modelling
• Static model: the state of an object at any given moment
(object attributes, relationships, constraints between
objects)
• Dynamic model: the changes that happen to the state as
event occur (actions affect objects, using the changes of
objects and attributes in the object state)
• Interactive model: interactions between objects (the
responsibility for achieving goal is divided among
collaborating objects)
CS551 - Lecture 7
2
CS551 - Lecture 7
3
Modeling Objects: Definitions
• Type: a collection of objects with similar behaviors,
satisfying the specification of that type
• Type model: Behaviors are specified in terms of
attributes, a valid abstract model of many possible
implementations.
• Action is described in terms of its effect on the attributes
of the participating objects and the output it produces.
• Abstract interaction: joint actions and collaboration;
specific interactions as refinements of more abstract
interactions.
CS551 - Lecture 7
4
CS551 - Lecture 7
5
Type & Class
• A class is a programming implementation unit:
– for all instances of that class, it defines the procedural methods and data
stored as instance variables.
– Each method: read and modify the instance variables of that instance,
communicate with other objects referred to via those instance variables or
passed in as parameters to the method.
• A type is a modeling and specification construct applicable to OO
code.
– Is abstracts algorithmic or procedural behaviors with specifications of actions;
and abstracts data or information representation by attributes.
– Every method implementation must meet the net result specified by the action
specification; and the stored data must represent enough information in some
form so that every attribute can be determined from that representation.
CS551 - Lecture 7
6
Type & Class
• A class implements some (number of) types(s). Each type
can be implemented by some (number of) class(es).
– For example, a class Product may implement the type Sellable, by
providing methods for each operation defined on Sellable, and a
particular set of stored instance variables to represent the abstract
state required by that type. Another class Service can also
implement Sellable, with very different implementations of its
methods and different instance variables to represent the sale of
services.
– The same class Product can also implement a different type
ReStockable; the class Service is not likely to implement this type.
CS551 - Lecture 7
7
Example: Type and Class
Sellable <<type>>
op1()
op2()
op3(…)
op4(…)
ReStockable <<type>>
op1()
….
Product <<class>
implement
op1()
op5()
op7(…)
op9(…)
Service <<class>>
op1()
….
CS551 - Lecture 7
8
Type & Class
• One type can be defined as an extension of another i.e. as a
subtype of another. The subtype has at least all the attributes and
action specifications that the supertype has.
• One class can have its implementation defined as an extension or
modification of another class. The subclass inherits all instance
variables and methods from the superclass. It may add more
instance variables and methods, and can override selected methods
implementations from its superclass with its own.
• An abstract class is a partial implementation unit: it cannot be
instantiated on its own as it has certain pieces missing (typically,
method implementations) that need to be filled in by a subclass.
CS551 - Lecture 7
9
CS551 - Lecture 7
10
Intermediate: Modeling State
• The state of an object - information that can be associated with it at
any given point in time (individual identity, characteristic behavior,
and state (a combination of stored state and executable code))- is
modeled by a static model on a type that describes that object.
• The static model consists of a set of attributes - properties that
define information on the state of the object at any point in time;
and a set of static invariants - rules on what combinations of
attribute values are valid for that object's state to be valid.
– For example, the static model of an Order may include attributes for a
set of LineItems (each, in turn, for some quantity of a product), the
orderTotal, and the Customer. The static invariants on Order may
include rules like "no two LineItems can be for the same Product", or
"the orderTotal cannot be greater than the customer's credit limit".
CS551 - Lecture 7
11
Modeling Objet State: Attribute
• A convenience attribute:
– describe things without it by referring to some more basic attributes,
– simplifies what you need to say and may provide some de-coupling from the
more detailed attributes.
– has a static invariant that defines its value in terms of other attributes.
• e.g., you may not strictly need to define orderTotal as an attribute, since you
could simply refer to the quantity of each of its line items, and the unit price of
the corresponding product, with equivalent effect.
• However, introducing orderTotal simplifies your descriptions of other activities
such as invoicing and payment, de-couples those descriptions from changes in the
way the order total is determined, and reduces duplication.
• We would need to add a static invariant which said: the orderTotal is the same as
the sum of each LineItem's quantity times that product's unit price.
CS551 - Lecture 7
12
Modeling Object State: Association
• An association graphically depicts an inverse pair of
attributes.
– For example, the customer attribute of an Order, and the setOfOrders
attribute of a Customer could be defined as a single association
between the two object types.
order
Order
*
Customer
CS551 - Lecture 7
13
CS551 - Lecture 7
14
Modeling Object State: Snapshot and
filmstrip
• A snapshot shows a set
of objects and their
configuration at a point in
time (the values of their
attributes or links to other
objects).
• A filmstrip shows a
sequence of snapshots
evolving through the
steps of a scenario.
CS551 - Lecture 7
15
Modeling Object State: Types, Attributes
and Associations
• Attributes: model and reality;an attribute need not correspond
directly to stored data in an implementation
• Parameterized attribute: its value is a function from a list of
parameters to an object identity
– session-5.startDate.isLessThan (today)
– session-5.startDate < today
• Association: a pair of attributes that are inverses of each other,
drawn as a line joining the two types on a type model.
CS551 - Lecture 7
16
Modeling Object State: Types, Attributes
and Associations
• Collections: many attributes have that are collections of other
objects.
– Set: a collection of objects without any duplicates);
– Bag: a collection with duplicates of elements;
– Seq: a sequence-a bag with an ordering of its elements.
• Operators:
– dot (“.”) operator for a collection evaluates an attribute on every
element of the collection and returns another collection;
e.g., clients.sessions = Set {s1, s5, s9}
– The -> operator for a collection evaluates an attribute on the
collection itself rather than on each of its elements;
e.g., client3.sessions -> count =3
CS551 - Lecture 7
17
Modeling Object State: Types, Attributes
and Associations
• forAll, exists
//every one of laura’s session grades is better than pass.
laura.sessions.grade -> forAll (g|g.betterThan(Grade.pass))
//at least one of laura’s session grades is a Grade A
laura.sessions.grade-> exists(g|g=Grade.A)
• union (+), intersection (*), difference (-)
laura.sessions.course + marty.sessions.course //the courses taught by
either
laura.sessions.course * marty.sessions.course //the courses taught by both
laura.session.course - marty.sessions.course
//the course taught by Laura that are not taught by Marty
CS551 - Lecture 7
18
Static Invariants
• Static invariant: a predicate, forming part of a type model, that should
hold true on every permitted snapshot-specifically, before and after
every action in the model.
– Inv instructor:: qualifiedFor -> includesAll (sections.course)
– inv instructor:: sessions.course <= qualifiedFor
• Boolean Operators: an invariant is a Boolean expression & Quantifier
(forall, exists)
– and (&), or (|), a implies b (a ==> b), not a (!a),
– aSet -> forall (x|P(x)) is equivalent to x:aSet::P(x),
– aSet -> exists (x|P(x)) is equivalent to exist x:aSet, P(x)
• Context Operator(::); short for an explicit forall
// a session has an evaluation exactly when it is completed.
– inv Session::self.completed = (self.eval <> null)
– inv Session -> forall (self | self.completed = (self.eval <> null))
CS551 - Lecture 7
19
Boolean Operators
CS551 - Lecture 7
20
CS551 - Lecture 7
21
Static Invariants
• Invariants in code
boolean assignQualified ()
for (Enumeration e = session.elements(); e.hasMoreElements;){
Course course = ((Session) e.nextElement ()).course();
if(! qualifiedForCourses.contains (course))
return false;
}
return ture;
}
CS551 - Lecture 7
22
Static Invariants: Common Uses
• Derived attributes: the value of one attribute can be fully
determined by other attributes;
e.g., //clients taught = clients of past sessions I have taught
inv Instructor::ClientsTaught = sessions[date < today].client
• Derived parameterized attributes:
e.g., // for every client the balance due for that client on any date is
the sum of the fees for all sessions in the preceding 30 days
inv Client::balanceDueOn (d: Date) =
sessions[day <d and d> d - 30].fees -> sum
CS551 - Lecture 7
23
Static Invariants: Common Uses
• Subset constraints: the object linked via one attribute must be in
the set of those linked via another attribute
e.g., //my assigned instructor must be one of my qualified candidates
inv Session::candidates-> includes(instructor)
CS551 - Lecture 7
24
Static Invariants: Common Uses
• Subtype constraints: a supertype can introduce
attributes that apply to several types; each subtype
imposes constraints on the attributes.
• State-specific constraints: being in a specific state may
imply constraints on some other attributes of an object.
Inv Session:: confirmed implies instructor <> null
• Association constraints: subset (between many
associations or a 1 and a many association); redefines
(between association of subtype and supertype);
CS551 - Lecture 7
25
Dictionary
• The collected set of definitions of modeling constructs.
• The definitions must include
– the formal modeling and specification bits (relating
the formal names and symbols to each other)
– the (usually informal) description that relate the
symbols and names to things in the problem domain.
• Dictionary definitions are scoped according to package
scope rules.
CS551 - Lecture 7
26
A Typical Dictionary
CS551 - Lecture 7
27
Static Model: Summary
• Describes the state of the business or the component(s) we are
interested in, with a set of attributes, together with an invariant,
constitutes the static part of a type model.
– each concept is described with a type
– its state is described with attributes and associations
– The invariant express constraints on the state: combinations of values
that should always be observed.
• To provide a vocabulary in which to describe actions, which include
interactions in a business, between user and software, or between
objects inside the software.
• Snapshots are used to represent specific situations, and helps to
develop the static model.
CS551 - Lecture 7
28