Lecture 20: Inheritance 2016-02-04 Prof. Dr. Andreas Podelski, Dr. - - PowerPoint PPT Presentation

lecture 20 inheritance
SMART_READER_LITE
LIVE PREVIEW

Lecture 20: Inheritance 2016-02-04 Prof. Dr. Andreas Podelski, Dr. - - PowerPoint PPT Presentation

Software Design, Modelling and Analysis in UML Lecture 20: Inheritance 2016-02-04 Prof. Dr. Andreas Podelski, Dr. Bernd Westphal 20 2016-02-04 main Albert-Ludwigs-Universit at Freiburg, Germany Contents & Goals Last


slide-1
SLIDE 1

– 20 – 2016-02-04 – main –

Software Design, Modelling and Analysis in UML

Lecture 20: Inheritance

2016-02-04

  • Prof. Dr. Andreas Podelski, Dr. Bernd Westphal

Albert-Ludwigs-Universit¨ at Freiburg, Germany

slide-2
SLIDE 2

Contents & Goals

– 20 – 2016-02-04 – Sprelim –

2/30

Last Lecture:

  • Firedset, Cut
  • Automaton construction
  • Transition annotations

This Lecture:

  • Educational Objectives: Capabilities for following tasks/questions.
  • What’s the Liskov Substitution Principle?
  • What is late/early binding?
  • What is the subset / uplink semantics of inheritance?
  • What’s the effect of inheritance on LSCs, State Machines, System States?
  • Content:
  • Inheritance in UML: concrete syntax
  • Liskov Substitution Principle — desired semantics
  • Two approaches to obtain desired semantics
slide-3
SLIDE 3

Inheritance: Syntax

– 20 – 2016-02-04 – main –

3/30

slide-4
SLIDE 4

Abstract Syntax

– 20 – 2016-02-04 – Ssyntax –

4/30

A signature with inheritance is a tuple S = (T, C, V, atr , E , F, mth, ⊳) where

  • (T, C, V, atr , E ) is a signature with signals and behavioural features (F/mth are

methods, analogous to V/atr attributes), and

  • ⊳ ⊆ (C × C ) ∪ (E × E )

is an acyclic generalisation relation, i.e. C ⊳+ C for no C ∈ C . In the following (for simplicity), we assume that all attribute (method) names are of the form C::v and C::f for some C ∈ C ∪ E (“fully qualified names”). Read C ⊳ D as. . .

  • D inherits from C,
  • C is a generalisation of D,
  • D is a specialisation of C,
  • C is a super-class of D,
  • D is a sub-class of C,
  • . . .
slide-5
SLIDE 5

Helper Notions

– 20 – 2016-02-04 – Ssyntax –

5/30

Definition. (i) For classes C0, C1, D ∈ C , we say D inherits from C0 via C1 if and only if there are C1

0, . . . Cn 0 , C1 1, . . . Cm 1 ∈ C , n, m ≥ 0, s.t.

C0 ⊳ C1

0 ⊳ . . . Cn 0 ⊳ C1 ⊳ C1 1 ⊳ . . . Cm 1 ⊳ D.

(ii) We use ⊳∗ to denote the reflexive, transitive closure of ⊳.

slide-6
SLIDE 6

Inheritance: Concrete Syntax

– 20 – 2016-02-04 – Ssyntax –

6/30

Common graphical representations (of ⊳= {(C, D1), (C, D2)}):

C D1 D2 C D1 D2 C D1 D2

Mapping Concrete to Abstract Syntax by Example:

C0

x : Int

C1 D

x : Int

C2

Note: we can have multiple inheritance.

slide-7
SLIDE 7

Inheritance: Desired Semantics

– 20 – 2016-02-04 – main –

7/30

slide-8
SLIDE 8

Desired Semantics of Specialisation: Subtyping

– 20 – 2016-02-04 – Slsp –

8/30

There is a classical description of what one expects from sub-types, which is closely related to inheritance in object-oriented approaches: The principle of type substitutability Liskov (1988); Liskov and Wing (1994) (Liskov Substitution Principle (LSP)).

“If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.”

In other words: Fischer and Wehrheim (2000)

“An instance of the sub-type shall be usable whenever an instance of the supertype was expected, without a client being able to tell the difference.”

slide-9
SLIDE 9

Subtyping: Example

– 20 – 2016-02-04 – Slsp –

9/30

Teacher Student

att : Int

GenStWorker

workload : Int

Genius Polite Clown

s

0, 1

t

0, 1

s1 s2

  • [s.att > 0]/s ! Q

GoodAns/ WrongAns/ : Teacher : Student att = 3

s t

s1 s2

  • Q/

/t ! Silence /t ! GoodAns /t ! WrongAns

: Teacher : Genius

s t

s1 s2

  • Q/

/t ! GoodAns

: Teacher : Polite

s t

s1 s2

  • Q/

/t ! Silence /t ! GoodAns

: Teacher : Clown

s t

s1 s2

  • Q/

/t ! StupidJoke

: Teacher : GenStWorker

s t

s1 s2

  • Q/

/t ! GoodAns

s3

  • Task/
  • SMTeacher

SMStudent SMGenius SMPolite SMClown SMGenStWorker

slide-10
SLIDE 10

Domain Inclusion Semantics

– 20 – 2016-02-04 – main –

10/30

slide-11
SLIDE 11

Domain Inclusion Structure

– 20 – 2016-02-04 – Sdiocl –

11/30

A domain inclusion structure D for signature S = (T, C, V, atr , E , F, mth, ⊳)

  • [as before] maps types, classes, associations to domains,
  • [for completeness] maps methods to transformers,
  • [as before] has infinitely many object identities per class in D(D),
  • [changed] the indentities of a super-class comprise all identities of sub-classes, i.e.

∀ C ⊳ D ∈ C : D(D) D(C) and indentities of instances of classes not (transitively) related by generalisation are disjoint, i.e. C ⊳+ D and D ⊳+ C implies D(C) ∩ D(D) = ∅.

Note: the old setting coincides with the special case ⊳ = ∅.

slide-12
SLIDE 12

Domain Inclusion System States

– 20 – 2016-02-04 – Sdiocl –

12/30

A system state of S wrt. (domain inclusion structure) D is a type-consistent mapping σ : D(C ) → (V

  • → (D(T ) ∪ D(C0,1) ∪ D(C∗)))

that is, for all u ∈ dom(σ) ∩ D(C),

  • [as before] σ(u)(v) ∈ D(T) if v : T,
  • [changed] σ(u), u ∈ D(C), has values for all attributes of C and all of its superclasses,

i.e. dom(σ(u)) =

  • C0⊳∗C

atr(C0).

Example: C

x : Int

D

x : Int y : Int n

0, 1

Note: the old setting still coincides with the special case ⊳ = ∅.

slide-13
SLIDE 13

OCL Syntax and Typing

– 20 – 2016-02-04 – Sdiocl –

13/30

  • Recall (part of the) OCL syntax and typing (C, D ∈ C , v, r ∈ V )

expr ::= v(expr 1) : τC → T(v), if v : T ∈ atr(C), T ∈ T | r(expr 1) : τC → τD, if r : D0,1 ∈ atr(C) | r(expr 1) : τC → Set(τD), if r : D∗ ∈ atr(C)

The syntax basically stays the same:

expr ::= C::v(expr 1) : τC → T(v), if C::v : T ∈ atr(C), T ∈ T | . . . | v(expr 1) : τC → T(v), | r(expr 1) : τC → τD, | r(expr 1) : τC → Set(τD),

but typing rules change: we require a unique biggest superclass C0 ⊳∗ C ∈ C with, e.g., v ∈ atr(C0) and for this v we have v : T. Example:

C

x : Int z : Int

D

x : Int y : Int n

0, 1

Note: the old setting still coincides with the special case ⊳ = ∅.

slide-14
SLIDE 14

Visibility and Inheritance

– 20 – 2016-02-04 – Sdiocl –

14/30

Example:

v1 < 0 v2 < 0 v3 < 0 context C inv : context D inv : n.v1 < 0 n.v2 < 0 n.v3 < 0 context B inv :

C

− v1 : Int # v2 : Int + v3 : Int

D B

0, 1 n

E.g. v(. . . (self ) . . . ) is well-typed

  • if v is public, or
  • if v is private, and self : τC and v ∈ atr(C), or
  • if v is protected, and self : τC and D ⊳∗ C (unique, biggest) and v ∈ atr(D).
slide-15
SLIDE 15

Satisfying OCL Constraints (Domain Inclusion)

– 20 – 2016-02-04 – Sdiocl –

15/30

IDI expr(σ) := INormalise(expr)(σ) using the same textual definition of I that we have.

slide-16
SLIDE 16

Expression Normalisation

– 20 – 2016-02-04 – Sdiocl –

16/30

Normalise:

  • Given expression v(. . . (w) . . . ) with w : τD,
  • normalise v to (= replace by) C::v,
  • where C is the unique most special more general class with C::v ∈ atr(C), i.e.

∀ C ⊳∗ C0 ⊳∗ D • C0 = C. Note: existence of such an C is guaranteed by (the new) OCL well-typedness. Example:

A

x : Int

C

x : Int

D

n

0, 1

  • context D inv : x < 0
  • context C inv : x < 0
  • context A inv : x < 0
  • context D inv : n < 0
  • context C inv : n < 0
  • context D inv : A::x < 0
slide-17
SLIDE 17

OCL Example

– 20 – 2016-02-04 – Sdiocl –

17/30 A

A::x : Int

C

C::x : Int

D

D::n 0, 1

u1 : A A::x = 0 u2 : C A::x = 1 C::x = 27 u3 : D A::x = 2 C::x = 13 D::n

σ:

  • Icontext D inv : A::x < 0(σ, {self → u1})
  • Icontext D inv : x < 0(σ, {self → u3})

Iv(expr 1)(σ, β) :=

  • σ(u1)(v)

, if u1 ∈ dom(σ) ⊥ , otherwise

slide-18
SLIDE 18

Excursus: Late Binding of Behavioural Features

– 20 – 2016-02-04 – main –

18/30

slide-19
SLIDE 19

Late Binding

– 20 – 2016-02-04 – Slatebind –

19/30

What transformer applies in what situation? (Early (compile time) binding.)

f not overridden in D

C

f() : Int

D C0 s

  • m

e C someD

f overridden in D

C

f() : Int

D

f() : Int

value

  • f

someC/ someD someC -> f() C::f() C::f() u1 someD -> f() C::f() D::f() u2 someC -> f() C::f() D::f() u2

What one could want is something different: (Late binding.)

someC -> f() C::f() C::f() u1 someD -> f() D::f() D::f() u2 someC -> f() C::f() C::f() u2

slide-20
SLIDE 20

Late Binding in the Standard and Programming Languages

– 20 – 2016-02-04 – Slatebind –

20/30

  • In the standard, Section 11.3.10, “CallOperationAction”:

“Semantic Variation Points The mechanism for determining the method to be invoked as a result of a call operation is unspecified.” (OMG, 2007, 247)

  • In C++,
  • methods are by default “(early) compile time binding”,
  • can be declared to be “late binding” by keyword “virtual”,
  • the declaration applies to all inheriting classes.
  • In Java,
  • methods are “late binding”;
  • there are patterns to imitate the effect of “early binding”

Note: late binding typically applies only to methods, not to attributes. (But: getter/setter methods have been invented recently.)

slide-21
SLIDE 21

Behaviour (Inclusion Semantics)

– 20 – 2016-02-04 – main –

21/30

slide-22
SLIDE 22

Semantics of Method Calls

– 20 – 2016-02-04 – Sdistm –

22/30

  • Non late-binding: by normalisation.
  • Late-binding:

Construct a method call transformer, which looks up the method transformer corresponding to the class we are an instance of.

slide-23
SLIDE 23

Transformers (Domain Inclusion)

– 20 – 2016-02-04 – Sdistm –

23/30

  • Transformers also basically remain the same, e.g. [VL 12, p. 18]

update(expr 1, v, expr 2) : (σ, ε) → (σ′, ε) with σ′ = σ[u → σ(u)[v → IDI expr 2(σ)]] where u = IDI expr 1(σ) — after normalisation, e.g. assume v qualified.

slide-24
SLIDE 24

Inheritance and State-Machines: Example

– 20 – 2016-02-04 – Sdistm –

24/30

  • signal, env
  • E
  • signal, env
  • F

s1 s2

  • /n ! F

SMA:

s1 s2

  • E/

SMD:

C A D

n

0, 1

u1 : A st = s1 stable = 0 u2 : D st = s1 stable = 1 n

slide-25
SLIDE 25

– 20 – 2016-02-04 – Sdistm –

25/30

(ii) Dispatch

– 13 – 2015-12-17 – Sstmrtc –

9/29

(σ, ε)

(cons,Snd)

− − − − − − − →

u

(σ′, ε′) if

  • u ∈ dom(σ) ∩ D(C) ∧ ∃ uE ∈ D(E) : uE ∈ ready(ε, u)
  • u is stable and in state machine state s, i.e. σ(u)(stable) = 1 and σ(u)(st) = s,
  • a transition is enabled, i.e.

∃ (s, F, expr, act, s′) ∈→ (SMC) : F = E ∧ Iexpr(˜ σ, u) = 1 where ˜ σ = σ[u.paramsE → uE].

and

  • (σ′, ε′) results from applying tact to (σ, ε) and removing uE from the ether, i.e.

(σ′′, ε′) ∈ tact[u](˜ σ, ε ⊖ uE), σ′ = (σ′′[u.st → s′, u.stable → b, u.paramsE → ∅])|D(C)\{uE} where b depends (see (i))

  • Consumption of uE and the side effects of the action are observed, i.e.

cons = {uE}, Snd = Obstact [u](˜ σ, ε ⊖ uE).

slide-26
SLIDE 26

Inheritance and Interactions

– 20 – 2016-02-04 – Sdistm –

26/30

  • signal, env
  • E
  • signal, env
  • F

a : A c : C E

C A D

n

0, 1

slide-27
SLIDE 27

Domain Inclusion vs. Uplink Semantics

– 20 – 2016-02-04 – main –

27/30

slide-28
SLIDE 28

System States with Inheritance

– 20 – 2016-02-04 – main –

28/30

Wanted: a formal representation of “if C ⊳∗ D then D ‘is a’ C”, that is,

(i) D has the same attributes and behavioural features as C, and (ii) D objects (identities) can replace C objects.

Two approaches to semantics:

  • Domain-inclusion Semantics

(more theoretical)

  • Uplink Semantics

(more technical)

slide-29
SLIDE 29

References

– 20 – 2016-02-04 – main –

29/30

slide-30
SLIDE 30

References

– 20 – 2016-02-04 – main –

30/30 Fischer, C. and Wehrheim, H. (2000). Behavioural subtyping relations for object-oriented

  • formalisms. In Rus, T., editor, AMAST, number 1816 in Lecture Notes in Computer Science.

Springer-Verlag. Liskov, B. (1988). Data abstraction and hierarchy. SIGPLAN Not., 23(5):17–34. Liskov, B. H. and Wing, J. M. (1994). A behavioral notion of subtyping. ACM Transactions on Programming Languages and Systems (TOPLAS), 16(6):1811–1841. OMG (2007). Unified modeling language: Superstructure, version 2.1.2. Technical Report formal/07-11-02. OMG (2011a). Unified modeling language: Infrastructure, version 2.4.1. Technical Report formal/2011-08-05. OMG (2011b). Unified modeling language: Superstructure, version 2.4.1. Technical Report formal/2011-08-06.