laws of pattern composition hong zhu and ian bayley department of computing and electronics oxford...

35
Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: [email protected]

Upload: victoria-daley

Post on 28-Mar-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Laws of Pattern Composition

Hong Zhu and Ian Bayley

Department of Computing and ElectronicsOxford Brookes University

Oxford OX33 1HX, UkEmail: [email protected]

Page 2: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 2Nov. 2010

Introduction Software Design Patterns (DP)

codified reusable solutions to recurring design problems,

increasingly important role in the development of software systems

Research on DP identified and catalogued (e.g. Gang of Four book) formally specified (Mik 1998, Taibi 2003, Gasparis et

al. 2008, Bayley&Zhu 2009) included in software tools for pattern instantiation and

pattern recovery (e.g. PINOT, Mapelsden et al. 2002)

Page 3: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 3Nov. 2010

Example: The Object Adapter DP In GoF book:

Indicate where messages can be sent to rather than a component of the pattern!

In general, there can be a set of such methods!

Page 4: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 4Nov. 2010

Pattern Specification A design pattern P can be defined abstractly as

an ordered pair <V, Pr>, where V = { v1:T1, …, vn:Tn } is a set of variable declarations

that specify the components of the patternPr is a predicate that specifies the structural and

behavioural features of the pattern The semantics of the specification:

v1:T1 … vn:Tn . (Pr) Notations:

Spec(P) to denote the predicate above, Vars(P) for the set of variables declared in V, and Pred(P) for the predicate Pr. m P : a design model m conforms to pattern P

vi are variables that range over the type Ti of software elements.

Page 5: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 5Nov. 2010

Formal Spec of the Object Adapter DP

Page 6: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 6Nov. 2010

Pattern Composition Why compose patterns

Patterns are usually to be found composed with each other in real applications

How pattern compositions are represented in practice:Annotation:Role representation in graphic

diagram

Page 7: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 7Nov. 2010

Command Processor: command processorStrategy: context Strategy: strategy

Memento: memento

Memento: originator

Command: concrete commandComposite: leafMemento: caretaker

Command: concrete commandComposite: compositeMemento: caretaker

Strategy: concrete strategy

Command Processor: commandCommand: commandComposite: componentMemento: caretaker

Example of Pattern Composition

ClientCommand Processor

Logging

Command

Concrete Logging

Strategy B

Concrete Logging

Strategy A

MementoConcrete

Command AConcrete

Command BComposite Command

Application

Page 8: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 8Nov. 2010

Formal Approach to DP Composition Define a set of six operators on DPs

Restriction: P Pr P Superposition: P P P Extension: P V Pr P Flatten: P V P Generalisation: P V P Lift: P V P

Represent DP compositions as expressions Prove a set of algebraic laws to reason about

the equivalence of DP compositions

I. Bayley & H. Zhu, A Formal Language of Pattern Composition, Proc. of PATTERNS’ 10, Lisbon, Portugal, 21-26 Nov. 2010. (In press)

Page 9: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 9Nov. 2010

Restriction Operator P[c] Let P be a given pattern and c be a

predicate defined on the components of P. A restriction of P with constraint c, written as P[c], is the pattern obtained from P by imposing the predicate c as an additional condition on the pattern. Formally,

Page 10: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 10Nov. 2010

Superposition Operator P * Q Let

P and Q be two patterns. the component variables of P and Q are disjoint, i.e.,

Vars(P) Vars(Q) = . Definition:

The superposition of P and Q, written P * Q, is a pattern that consists of both pattern P and pattern Q as is formally defined as follows.

Page 11: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 11Nov. 2010

Extension Operator P#(V c) Let

P be a pattern, V be a set of variable declarations that are disjoint

with P's component variables, i.e.,

Vars(P) V = c be a predicate with variables in Vars(P) V.

Definition:The extension of pattern P with components V and

linkage condition c, written as P#(V c), is defined as follows.

Page 12: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 12Nov. 2010

Flatten Operator P x\ x' Let

P be a pattern, Vars(P)= {x: P(T), x1:T1, …, xk:Tk},

Pred(P)=p(x,x1,…,xk), and x' Vars(P).

Definition:The flattening of P on variable x, written Px\ x',

is the pattern that has the following property.

where

the power set of T

We also write Px

Page 13: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 13Nov. 2010

Generalisation Operator P x\x' Let

P be a pattern, x Vars(P)={x:T, x1:T1, …, xk:Tk}.

Definition:The generalisation of P on variable x,

written P x\x', is defined as follows.

Notation: Similar to the operator, we also write P x, and

P X

Page 14: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 14Nov. 2010

Lift Operator Let

P be a pattern CVars(P)={x1:T1, …, xn: Tn}, n>0

OPred(P)=p(x1, …, xn).

X={x1, …, xk}, 1 k < n, be a subset of the variables in the pattern.

Definition:The lifting of P with X as the key, written PX, is the

pattern defined as follows.

the existentially quantified class components

the remainder of the predicate

Page 15: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 15Nov. 2010

Example

Notation:• P[x' := x]: systematically renaming x to x’;• P[v:= x=y]: the syntactic sugar for P[x=y][v:= x][v:=y];• P[v:= x y]: abbreviates P[x y][v:= x]

Page 16: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 16Nov. 2010

Case Study: Expressiveness of the operators Subject:

Compositions of patterns suggested in the GoF book• In the sections of Related Patterns in GoF book

Example: (page 106 of the GoF book) “A Composite is what the builder often builds''.

This can be formally specified as follows.(Builder * Composite) [Product = Component].

Method: To formalise them all as expressions with the

proposed operators Result:

all suggested compositions were successfully expressed in the operators

See [Bayley & Zhu: PATTERNS’ 10]

Page 17: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 17Nov. 2010

Algebraic Laws of the OperatorsLet P and Q be design patterns. Definition: (Specialisation relation)

Let P and Q be design patterns. Pattern P is a specialisation of Q, written P Q, if for all models m, whenever m conforms to P, then, m also conforms to Q.

Definition: (Equivalence relation)Pattern P is equivalent to Q, written P = Q, if P Q and Q P.

m |= P

Page 18: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 18Nov. 2010

The TRUE and FALSE Patterns Definition:

Pattern TRUE is the pattern such that for all models m, m |= TRUE.

Pattern FALSE is the pattern such that for no model m, m |= FALSE.

LemmaFor all patterns P, Q and R, we have that

Page 19: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 19Nov. 2010

Laws of Restriction Let vars(p) denote the set of free variables in a predicate p. For all predicates c, c1, c2 such that vars(c), vars(c1) and

vars(c2) Vars(P), the following equalities hold.

Page 20: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 20Nov. 2010

Laws of Superposition For all patterns P and Q, we have the following equations.

From this and reflexivity of , it follows that superposition is idempotent.

TRUE and FALSE are the unit and zero of superposition.

Superposition is also commutative and associative

Page 21: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 21Nov. 2010

Laws of Extension Let

U be any set of component variables that is disjoint to Vars(P), and c1, c2 be any given predicates such that vars(ci) Vars(P) U,

i=1,2.

Let U and V be any sets of component variables that are disjoint to

Vars(P) and to each other, c1 and c2 be any given predicates vars(c1) Vars(P) U and vars(c2) Vars(P) V.

Page 22: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 22Nov. 2010

Laws of Flattening and Generalisation

Page 23: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 23Nov. 2010

Laws Connecting Several Operators For all predicates c such that vars(c) Vars(P),

For all X Vars(P),

For all X Vars(P) Vars(Q),

where

Page 24: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 24Nov. 2010

For all sets of variables X such that

X vars(P) =, and all predicates c such that

vars(c) (Vars(P) X),

where

For all x Vars(P) such that x : P(T)

Page 25: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 25Nov. 2010

Page 26: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 26Nov. 2010

where

Page 27: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 27Nov. 2010

Proving Equivalence: Example 1 Consider the Composite DP:

Composite1 = Composite [ ||Leaves|| = 1] Composite1 = Composite Leaves\ Leaf Composite = Composite1 Leaf \ Leaves

We can prove that:1. Composite [ ||Leaves|| = 1]= Composite

Leaves\ Leaf2. Composite= (Composite Leaves\ Leaf) Leaf \

Leaves

3. Composite1=(Composite1 Leaf \ Leaves) Leaves\ Leaf

Page 28: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 28Nov. 2010

The Composite DP

In general, there can be many leaves!

Page 29: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 29Nov. 2010

Proof of Equation (1)

Composite [ ||Leaves|| = 1] = Composite Leaves\ Leaf

Proof. For all sets X, we have that

||X||=1 x(X={x})

Therefore,

Composite[ ||Leaves||=1]

= Composite [ Leaf (Leaves={Leaf}) ]

= Composite # (Leaf Leaves={Leaf}) (Slide 24)

= Composite Leaves \ Leaf (Slide 24)

QED

Page 30: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 30Nov. 2010

Proving Equivalence: Example 2 Composition of the Composite and the

Adapter patternsA subset of leaves in Composite DP are

adapted as the targets of Adaptor DP

Page 31: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 31Nov. 2010

First Way of Composition One of the Leaves in the Composite pattern is the Target in

the Adapter pattern. This leaf is renamed as the AdaptedLeaf.

Lift the adapted leaf to enable several of these Leaves to be adapted. lift the OneAdaptedLeaf pattern with AdaptedLeaf as the key flatten those components in the composite part of the pattern

(i.e. the components in the Composite pattern remain unchanged).

Page 32: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 32Nov. 2010

Second Way of Composition Lift the Adapter with target as the key Superpose it to the Composite patterns

We can prove that the two ways are equivalent by applying the algebraic laws.

Page 33: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 33Nov. 2010

Related Work Taibi’s work 2006

Pattern composition is a combination of formal statements of structural and behavioural features of patterns

Illustrated by an example on how two patterns can be composed

Bayley &Zhu, 2008 Defined One DP composition operator with a notion of

overlaps, which can be• One-to-One overlaps• One-to-Many overlaps• Many-to-many overlaps

Page 34: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 34Nov. 2010

Work in Progress and Future Work Work in Progress

The logic properties of the operators• Preserving the soundness, adequacy and

semantics of DPs

The completeness of the algebraic laws Future work

Automated reasoning about pattern compositions

Page 35: Laws of Pattern Composition Hong Zhu and Ian Bayley Department of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

ICFEM 2010, Shanghai 35Nov. 2010

References1. Taibi, T.: Formalising design patterns

composition. Software, IEE Proceedings 153(3), 126–153 (June 2006)

2. Bayley, I., Zhu, H.: On the composition of design patterns. In: Proc. of QSIC 2008. pp.27–36. IEEE Computer Society, (Aug 2008)

3. Bayley, I. and Zhu, H.: Formal specification of the variants and behavioural features of design patterns. Journal of Systems and Software 83(2), 209–221 (Feb 2010)

4. Bayley, I. and Zhu, H., A formal language of pattern composition. In: Proc. of PATTERNS 2010, Lisbon, Portugal (Nov. 2010) (In Press)