Design by Contract SpringContracts Beispiele Vertragsgrundlagen

Slides:



Advertisements
Ähnliche Präsentationen
Finding the Pattern You Need: The Design Pattern Intent Ontology
Advertisements

Word Order in German Subordiante Clauses
R. Zankl – Ch. Oelschlegel – M. Schüler – M. Karg – H. Obermayer R. Gottanka – F. Rösch – P. Keidler – A. Spangler th Expert Meeting Business.
The difference between kein and nicht.
Fakultät für informatik informatik 12 technische universität dortmund Specifications Peter Marwedel TU Dortmund, Informatik 12 Graphics: © Alexandra Nolte,
Peter Marwedel TU Dortmund, Informatik 12
Wenn…… the conditional. Using the conditional tense The conditional tense is used to talk about something that happens only after something else happened.
Three minutes presentation I ArbeitsschritteW Seminar I-Prax: Inhaltserschließung visueller Medien, Spree WS 2010/2011 Giving directions.
Modulare Programmierung
Introduction to BOS Supplier Guidelines v6
Munz – IT/TG - Lörrach. Goals of this intensive lecture To learn: To learn: –What does it means programming in Java ? –What is JAVA good/bad for ? –Which.
Deutsche Gesellschaft für Technische Zusammenarbeit GmbH Integrated Experts as interface between technical cooperation and the private sector – An Example.
Seminar Telematiksysteme für Fernwartung und Ferndiagnose Basic Concepts in Control Theory MSc. Lei Ma 22 April, 2004.
This presentation is timed so you will only need to click on the left mouse button when it is time to move to the next slide. Right click on this screen.
Comparative Adjectives. The term comparison of adjectives is used when two or more persons or things have the same quality (height, size, color, any characteristic)
Das Perfekt Wiederholen (hoffentlich!). Think of 5 things you did in your holidays but think of sentences in the PRESENT TENSE. 1.Am Montag schlafe ich.
Adjektive Endungen von Frau Templeton.
Zu + Infinitiv : eine Erklärung
Question words and word order By the end of this lesson you will have revised question words By the end of this lesson you will be able to use question.
Laurie Clarcq The purpose of language, used in communication, is to create a picture in the mind and/or the heart of another.
Was ich alles tun würde! All the things I would do!
Write the answers to these questions down on a piece of paper from your notebook. Was bedeutet Alles kann besser werden auf Engisch? What are some social.
Machen Sie sich schlau am Beispiel Schizophrenie.
die Zeiten (The Tenses) das Aktiv (Active Voice)
How many more nouns can you think of?
Institut AIFB, Universität Karlsruhe (TH) Forschungsuniversität gegründet 1825 Towards Automatic Composition of Processes based on Semantic.
Sanjay Patil Standards Architect – SAP AG April 2008
| DC-IAP/SVC3 | © Bosch Rexroth Pneumatics GmbH This document, as well as the data, specifications and other information set forth in.
Das Perfekt (Present Perfect Tense). Think of 5 things you did in your holidays but think of sentences in the PRESENT TENSE. 1.Am Montag schlafe ich viel.
Plural Forms of Nouns & Wie viel? or Wie viele?
BAS5SE | Fachhochschule Hagenberg | Daniel Khan | S SPR5 MVC Plugin Development SPR6P.
3rd Review, Vienna, 16th of April 1999 SIT-MOON ESPRIT Project Nr Siemens AG Österreich Robotiker Technische Universität Wien Politecnico di Milano.
Deutsch 1 G Stunde. Dienstag, der 11. September 2012 Deutsch 1 (G Stunde)Heute ist ein E - Tag Unit: Introduction to German & Germany Objectives: Learn.
Ich möchte ein Eisberg sein. Last time … 3 icebergs Triangels Unique connections Ich möchte ein Eisberg sein
Christoph Durt: Wittgenstein on the possibility of philosophy: The importance of an intercultural approach
Deutsch 1 G Stunde. Donnerstag, der 18. Oktober 2012 Deutsch 1, G Stunde Heute ist ein E- Tag Unit: Family & homeFamilie & Zuhause Objectives: Phrases.
Deutsch 1 G Stunde. Montag, der 10. September 2012 Deutsch 1 (G Stunde)Heute ist ein D - Tag Unit: Introduction to German & Germany Objectives: Introducing.
Things I really, really need to remember.. AKKUSATIVDATIV DurchAus FürAußer GegenBei OhneMit UmNach BisSeit Von Zu These prepositions always trigger these.
Donnerstag, den 28. November FUTURE HOLIDAYS Lernziel: to learn how to express future ideas. Starter: Finde ein Paar! will plane...zu werde hoffe...zu.
Wortschatz angenehm comfortable anstrengend tiring ausgezeichnet outstanding bequem comfortable berühmt famous besser better blöd stupid einfach easy fantastisch.
You need to use your mouse to see this presentation © Heidi Behrens.
Department of Computer Science Homepage HTML Preprocessor Perl Database Revision Control System © 1998, Leonhard Jaschke, Institut für Wissenschaftliches.
Wortschatz der Schulhof the playground die Aula the hall
INTAKT- Interkulturelle Berufsfelderkundungen als ausbildungsbezogene Lerneinheiten in berufsqualifizierenden Auslandspraktika DE/10/LLP-LdV/TOI/
DEUTSCHLAND UND DIE MEDIEN
DEUTSCHLAND UND DIE MEDIEN
Learning Target / Lernziel: 1.Hausaufgabenkontrolle (Vok 1-1) 2.Kultur 3.Forming questions Heute ist Dienstag, der 3. September 2013 Hausaufgaben GH #1,#2.
Verben Wiederholung Deutsch III Notizen.
Tage der Woche German Early Level Montag Dienstag Mittwoch Donnerstag
Kölner Karneval By Logan Mack
Einführung Bild und Erkenntnis Einige Probleme Fazit Eberhard Karls Universität Tübingen Philosophische Fakultät Institut für Medienwissenschaft Epistemic.
Ein Projekt des Technischen Jugendfreizeit- und Bildungsvereins (tjfbv) e.V. kommunizieren.de Blended Learning for people with disabilities.
“Weil” und “Denn”.
Design Patterns Ein Muster (pattern) ist eine Idee, die sich in einem praktischen Kontext als nützlich erwiesen hat und dies auch in anderen sein wird.
Talking about yourself
Hätte gern vs. Möchte gern
Adjectiv Endungen Lite: Adjective following articles and pre-ceeding nouns. Colors and Clothes.
AVL-Trees (according to Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search.
Greetings and goodbyes Deutschland v. USA
Sentence Structure Subject and verb are always together. Subject and verb are always together. Subject and verb must agree Subject and verb must agree.
German Word Order explained!
Present Tense Most regular verbs follow this pattern:
Separable Verbs Turn to page R22 in your German One Book R22 is in the back of the book There are examples at the top of the page.
THE PERFECT TENSE IN GERMAN
Selectivity in the German Mobility Panel Tobias Kuhnimhof Institute for Transport Studies, University of Karlsruhe Paris, May 20th, 2005.
Technische Universität München 1 CADUI' June FUNDP Namur G B I The FUSE-System: an Integrated User Interface Design Environment Frank Lonczewski.
By Martin L. Loeffler.  The basic sentence has a subject and a verb.  The subject and verb need to be together.  The subject and verb need to agree.
THE CONVERSATIONAL PAST
Your next assignment is not a test but rather an essay. In order to help you write this essay, we are going to discuss the parts of an essay in German.
1.Usage/Purpose 2.Forms Present Tense Simple Past Tense 2.Meanings 3.Word Order/Placement modal + infinitive omission of infinitives 4. Saying what you.
 Präsentation transkript:

Design by Contract SpringContracts Beispiele Vertragsgrundlagen Vertragsgestaltung Beispiele @Precondition Analyse & Design Framerules commands Queries Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Kollaboration von Akteuren Motivation Kollaboration von Akteuren Klient nutzt Dienste eines Anbieters Wie kann sich Klient auf die korrekte Abwicklung des Dienstes verlassen ? Wie kann sich Anbieter auf die korrekte Inanspruchnahme seines Dienstes verlassen ? Auf welcher semantischen Grundlage interagieren Objekte miteinander ? Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

... ausreichend durch Bezeichnung der Dienstleistung ? Semantik ... ausreichend durch Bezeichnung der Dienstleistung ? (Klassennamen, Methodennamen, Argumentenbezeichner) ... ausreichend durch Einschränkung auf Typen ? (Argumenttypen, Typ des Rückgabetyps) ... ausreichend durch Nutzungsbeispiele ? (Unit-Tests) Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Beschreibt die Dienstleistung des Anbieters - Zusicherungen Vertrag Beschreibt die Dienstleistung des Anbieters - Zusicherungen Beschreibt die Gegenleistungen des Dienstenutzers - (Vor-) Bedingungen Beide Seiten müssen dem Vertrag zustimmen ... ... damit eine 'Geschäftsbeziehung' zustandekommt ... damit sich beide Seiten bei der Ausführung des Geschäfts vertrauen Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Vorbedingungen Design By Contract (vor Aufruf und Ausführung einer Methode) specifies the circumstances under wich it is valid to call a feature - beziehen sich auf aktuellen Zustand der Klasse (Anbieter) - beziehen sich auf aktuellen Zustand der übergebenen Parameter (Klient) @Precondition( condition =" not empty arg1 and this.size < this.capacity" ) public void push( Object element ); Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Nachbedingungen Design By Contract (nach Aufruf und Ausführung einer Methode) specifies the effect of calling a feature - beziehen sich auf den aktuellen Zustand der Klasse (Anbieter) - beziehen sich auf Zustand der Klasse vor dem Methodenaufruf ('old') - beziehen sich auf Zustand des Rückgabewertes - beziehen sich auf aktuellen / alten Zustand der übergebenen Parameter @Postcondition( condition = " this.size == old:this.size + 1 and this.top == arg1" ) public void push( Object element ); Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Invarianten Design By Contract (gelten über den gesamten Lebenszeitraum des Anbieters) specifies unchanging properties of an object of a class - beziehen sich auf aktuellen Zustand der Klasse @Invariant( condition = " this.size >= 0 and this.size <= this.capacity" ) public interface S {...} Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Contracs are Spec's n Check's - Contracts specify the expected behaviour - Contracts document the 'WHAT' without the need of knowing 'HOW' -> the 'documentation' accurately describes what the code actually does ... n' Checks - Contracts can be checked at runtime -> help test and debug the implementation -> fail fast

Design by Contract für Java SpringContracts Design by Contract für Java - Annotierung der Vertragselemente über Annotations ( @Precondition, @Postcondition, @Invariant ) @Precondition( condition = "not empty arg1" ) @Postcondition( message = " Element hinzufuegen: {arg1}", condition = " this.size == old:this.size + 1 " + " and this.top == arg1" ) public void push( Object element ); Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

SpringContracts – Conditions - Formulierung der Conditions mittels Expression Language + Extensions Beispiel: Interface Stack{ public void push( Object element ); ... } Nachbedingungen eines erfolgreichen 'push' ??? Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

SpringContracts – Conditions - Formulierung der Conditions mittels Expression Language + Extensions Beispiel: Interface Stack{ public void push( Object element ); ... } - Anzahl der Elemente des Stacks erhöht sich um 1 - gepushtes Element ist oberstes Element des Stacks Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

SpringContracts – Conditions - Formulierung der Conditions mittels Expression Language + Extensions Instanz der Klasse Property der Klasse this.size == old:this.size + 1 and this.top == arg1 Wert des Ausdrucks VOR Methodenaufruf 1. Argument der Methode Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

SpringContracts – Conditions Beispiel: Interface Map{ public Object get( Object key ); ... } Nachbedingung get() : WENN Map kein Key-Value-Paar mit dem übergebenen Key enthält ( public boolean containsKey( Object key ) ) DANN liefert Map null Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

SpringContracts – Conditions Forts. Implikation not this.containsKey(arg1) ==> empty return Rückgabewert der Methode Methodenaufrufe Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

SpringContracts – Conditions Beispiel: Interface Account{ public int getBalance; ... } Interface Bank{ public List<Account> getAccounts(); Invariante Bank: Alle Konten der Bank weisen jederzeit einen positiven Saldo auf Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

SpringContracts – Conditions Forts. Collection-Property der Instanz All-Quantor all( account : this.accounts, account.balance > 0 ) temp. Variable Bedingung Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

SpringContracts – Conditions Interface Account{ public Customer getOwner(); ... } Interface Customer{ public String getName(); Interface Bank{ public ... getStatementSummary( Customer customer ); Precondition getStatementSummary(): Die Bank führt mindestens ein Konto des Kunden Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

SpringContracts – Conditions Forts. Collection-Property der Instanz Existenz-Quantor exist( account : this.accounts, account.owner.name == arg1.name ) temp. Variable Bedingung Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

SpringContracts – Symbolische Namensbindung Bindung des 1. Arguments an den symbolischen Namen 'customer' bindArgs = “arg1 = customer“ Referenzierung des 1. Arguments über dessen symbolischen Namen Collection-Property der Instanz Existenz-Quantor exist( account : this.accounts,account.owner.name == customer.name ) temp. Variable Bedingung Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

ContractPreprocessor Überblick Large-Scale Design SpringContracts <AOP Advisor> <JoinPoint Provider> ContractAdvisor Method ContractBuilder Contract ContractElement AttributeSource ContractPreprocessor ExpressionEvaluator Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Card Game: Regeln: Vertragsgestaltung Es gibt 52 verschiedene Karten (Card) Es gibt ein Spielfeld (Grid) mit einer festen Anzahl an Kartenpositionen (Position) Regeln: Jede Karte darf genau einmal auf dem Spielfeld platziert werden Jede Karte darf beliebig oft auf dem Spielfeld verschoben werden Jede Karte darf nur auf eine freie Position platziert bzw. verschoben werden Eine platzierte Karte darf nicht mehr vom Spielfeld zurückgenommen werden Das Spielfeld kann nicht mehr Karten aufnehmen als an Kartenpositionen (Kapazität) zur Verfügung stehen Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Card Vorbereitung Card Game @invariant( condition = "this.number >= 1 and this.number <= 52" ) class Card{ private int number; public Card( int number ){ this.number = number; } public int getNumber(){ return number; Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Position Vorbereitung Card Game @invariant( condition = "this.placeNumber > 0" ) class Position{ private int placeNumber; public Position( int placeNumber ){ this.placeNumber = placeNumber; } public int getPlaceNumber(){ return placeNumber; ... Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Separate features into different categories: Design Prinzipien Separate features into different categories: Separate Commands and Queries Queries: - return a result but do not change the visible properties of the object - provide a conceptional model of objects of the model Commands - might change the object, but do not return a result Separate basic Queries from derived Queries derived Queries can be specified in the terms of basic Queries Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Separate Commands and Queries Forts. interface Grid{ // Commands public void place( Card card, Position position ); public void move( Position from, Position to ); // Queries public int getCapacity(); public int cardsOnGrit(); public Card cardAt( Position position ); public List<Position> getAllPositions(); } Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

public boolean isAssigned( Position position ); Derived Queries Forts. public boolean isAssigned( Position position ); -> cardAtPosition( position ) public boolean isFull(); -> getAllPositions() + cardAtPosition( position ) -> oder getCapacity() + getCardsOnGrit() public boolean isAllreadyOnGrid( Card card ); Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Writing appropriate Preconditions: Design Prinzipien Writing appropriate Preconditions: For every Query and Command decide on a suitable Precondition constrain when clients may call queries and commands Add physical constraints where appropriate typically these will constraints that variables should not be void Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Design Prinzipien - Preconditions public void place( Card card, Position position ); Argumente müssen gesetzt sein (physical constraint): all( arg : args, not empty arg ) Jede Karte darf genau einmal auf dem Spielfeld platziert werden : not this.isAllreadyOnGrid( card ) Die Karte muss auf einer freien Position platziert werden : not this.isAssigned( position ) Die Position muss innerhalb des gültigen Spielfelds liegen : position.placeNumber <= this.capacity Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Design Prinzipien - Preconditions bindArgs = “ arg1 = card, arg2 = position“, condition = “all( arg : args, not empty arg ) and not this.isAllreadyOnGrid( card ) not this.isAssigned( position ) position.placeNumber <= this.capacity“ message = “place card {card.number} on pos {position.placeNumber}“ ) public void place( Card card, Position position ); Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

? ? ? Design Prinzipien - Preconditions public void move( Position from, Position to ); ? ? ? Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Design Prinzipien - Preconditions public void move( Position from, Position to ); Auf der Ausgangsposition 'from' muss eine Karte platziert sein this.isAssigned( from ) Die Karte muss auf eine freie Zielposition 'to' verschoben werden : not this.isAssigned( to ) Die Zielposition 'to' muss innerhalb des gültigen Spielfelds liegen : to.placeNumber <= this.capacity Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Writing coherent Postconditions: Design Prinzipien Writing coherent Postconditions: For each derived Query, write a Postcondition that specifies what result will be returned in terms of one ore more basic queries -> then if we know the values of the basic queries, we also know the values of the derived queries Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

? ? ? Design Prinzipien - Postconditions public boolean isAssigned( Position position ); Auf der Position ist eine Karte platziert : return == not empty this.cardAt( position ) public boolean isFull(); ? ? ? Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

? ? ? Design Prinzipien - Postconditions public boolean isAssigned( Position position ); Auf der Position ist eine Karte platziert : return == not empty this.cardAt( position ) public boolean isFull(); Auf allen Positionen ist schon eine Karte platziert : return == all( position : this.allPositions, not empty this.cardAt( position ) ) alt: return == ( this.cardsOnGrit == this.capacity() ) public boolean isAllreadyOnGrid( Card card ); ? ? ? Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Design Prinzipien - Postconditions public boolean isAssigned( Position position ); Auf der Position ist eine Karte platziert : return == not empty this.cardAt( position ) public boolean isFull(); Auf allen Positionen ist schon eine Karte platziert : return == all( position : this.allPositions, not empty this.cardAt( position ) ) public boolean isAllreadyOnGrid( Card card ); Es gibt eine Position, auf welcher die Karte platziert ist : return == exist( position : this.allPositions, this.cardAt( position ) == card ) Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Writing coherent Postconditions Forts: Design Prinzipien Writing coherent Postconditions Forts: For each Command write a Postcondition that specifies the value of every basic Query -> taken together with principle 3 this means that we now know the total visible effect of each Command Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Design Prinzipien - Postconditions public void place( Card card, Position position ); Position ist nach Platzierung der Karte belegt not empty this.cardAt( position ) besser: die platzierte Karte liegt auf der Position this.cardAt( position ) == card Es liegt eine weitere Karte auf dem Spielfeld this.cardOnGrit == old:this.cardsOnGrit + 1 Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

? ? ? Design Prinzipien - Postconditions public void move( Position from, Position to ); ? ? ? Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Design Prinzipien - Postconditions public void move( Position from, Position to ); Ausgangsposition 'from' ist nicht mehr belegt empty this.cardAt( from ) Zielposition 'to' ist mit der verschobenen Karte belegt this.cardAt( to ) == card Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Frame-Rules - beschreiben, was sich NICHT ändert public void move( Position from, Position to ); Anzahl der Karten auf dem Spielfeld bleibt unverändert this.cardOnGrit == old:this.cardsOnGrit Frame-Rules - beschreiben, was sich NICHT ändert -> Beschreibung der begrenzten Auswirkung von Commands -> Beschreibung der Nicht-Antastbarkeit von Argumenten Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Design Prinzipien Writing Invariants: Write Invariants to define unchanging properties of objects concentrate on properties that help reader build on appropriate conceptual model of the abstraction that the class embodies Constrain attributes using an invariant when a derived query is implemented as an attribute it can be constrained to be consistend with other queries by an assertion in the class's invariant section Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

? ? ? Design Prinzipien - Invariants Die Anzahl der Karten überschreitet nie die Kapazität des Spielfelds this.cardsOnGrid >= 0 and this.cardsOnGrit <= this.capacity Alle Karten auf dem Spielfeld sind paarweise verschieden ? ? ? Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Design Prinzipien - Invariants Die Anzahl der Karten überschreitet nie die Kapazität des Spielfelds this.cardsOnGrid >= 0 and this.cardsOnGrit <= this.capacity Alle Karten auf dem Spielfeld sind paarweise verschieden not exist( position : this.positionen, all( otherPosition : this.positionen, ( not empty this.cardAt( position ) and not empty this.cardAt( otherPosition ) position.placeNumber != otherPosition.placeNumber ) ==> this.cardAt( position ) == this.cardAt( otherPosition ) Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Vererbung Redefinition - common: Redefinition of a feature that is inherited from a superclass - new : Redefinition of the Contract of that feature Beispiel: public interface Courier{ @Precondition( condition = “package.weight < 3“ ) @Postcondition( condition = “this.deliveryTime <= 5“ ) public void subscribe( Package package ); ... } Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

You can weaken the precondition Vererbung You can weaken the precondition this.@Precondition OR super(s).@Precondition public interface BikeCourier extends Courier{ @Precondition( condition = “package.weight < 8“ ) ... public void subscribe( Package package ); } Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

You can strengthen the postcondition Vererbung You can strengthen the postcondition this.@Postcondition AND super(s).@Postcondition public interface BikeCourier extends Courier{ ... @Postcondition( condition = “this.deliveryTime <= 3“ ) public void subscribe( Package package ); } Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006

Online – Auktion Workshop Bei einem Online-Auktion-Shop können Artikel registriert und anschliessend versteigert werden Eine Online-Auktion besitzt eine Menge von Kategorien, in welche die Artikel eingeteilt werden Ein Artikel kann in bis zu drei Kategorien bei der Online-Auktion registriert werden Ein Artikel kann nach der Registrierung (in mind. einer Kategorie) genau einmal zur Versteigerung gestartet werden Bis zum Auktionsstart darf ein Artikel auch wieder aus der Online-Auktion entfernt werden (und damit aus allen derzeit registrierten Kategorien). Nach dem Auktionsstart darf der Artikel nicht mehr weiteren Katgorien zugeordnet werden Jeder Artikel hat einen Startpreis, welcher bis zum Auktionsstart verändert werden darf Für einen Artikel können nach Auktionsstart bis zum Auktionssende Gebote abgegeben werden Jeder Artikel besitzt eine Gebotshistorie Ein Gebot wird nicht berücksichtigt, wenn es niedriger als das derzeit höchste Gebot bzw. Startpreis ist. Für jeden Artikel kann die Versteigerung genau einmal geschlossen werden. Mario Gleichmann Mario Gleichmann Mario Gleichmann Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

Analyse - Typ-Modell Workshop AuctionShop Category n 0..3 n Article 1 Bid Mario Gleichmann Mario Gleichmann Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

Angebotshöhe muss grösser 0 sein Workshop BID Angebotshöhe muss grösser 0 sein @Invariant( condition = “this.amount > 0“ ) public class Bid{ int amount; @Precondition( condition = “arg1 > 0“ ) public Bid( int amount ){ this.amount = amount; } Mario Gleichmann Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

public int getInitialPrice(){...} Workshop Article ( Queries ) public class Article{ public int getInitialPrice(){...} public List<Bid> getBids(){...} public Bid getHighestBid(){...} public int countBids(){...} public boolean isAuctionOpened(){...} public boolean isAuctionClosed(){...} ... } Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

? ? ? Article (Queries Forts.) public Bid getHighestBid(){...} Workshop Article (Queries Forts.) public Bid getHighestBid(){...} Wert aller anderen Gebote ist kleiner als das gelieferte höchste Gebote ? ? ? Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

Article (Queries Forts.) Workshop Article (Queries Forts.) public Bid getHighestBid(){...} Wert aller anderen Gebote ist kleiner als das gelieferte höchste Gebote @Postcondition( condition = “all( bid : this.bids, ( bid != return ) ==> ( bid.amount < return.amount ) )“ ) Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

Article (Invarianten) Workshop Article (Invarianten) Alle Gebote liegen über dem Startpreis @Invariant( condition = “all( bid : this.bids, bid.amount > this.initialPrice“ ) public class Article{ ... } Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

Article (Invarianten) Workshop Article (Invarianten) Es gibt keine zwei Gebote mit dem gleichen Gebotswert not exist( bid : this.bids, all( otherBid : this.bids, bid != otherBid ==> bid.amount != otherBid.amount ) Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

public void raise( Bid newBid ) Workshop Article (Commands) public void raise( Bid newBid ) Vorbedingungen Neues Gebot muss höher sein als Startwert bzw. alle anderen bereits abgegebenen Gebote newBid > this.initialPrice and all( existingBid : this.bids, newBid.amount > existingBid.amount ) Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

public void raise( Bid newBid ) Workshop Article (Commands) public void raise( Bid newBid ) Vorbedingungen Auktion muss schon geöffnet sein und darf noch nicht geschlossen sein this.auctionOpened and not this.auctionClosed Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

public void raise( Bid newBid ) Workshop Article (Commands) public void raise( Bid newBid ) Nachbedingungen Neues Gebot ist das höchste Gebot Anzahl der abgegebenen Gebote hat sich um 1 erhöht this.highestBid == newBid and this.countBids() == old:this.countBids() + 1 Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

// wg. Regel 'mind. in einer Category registriert' Workshop Article (Commands) protected void startAuction() // darf nur über AuctionShop gestartet werden // wg. Regel 'mind. in einer Category registriert' Vorbedingung Auction ist noch nicht eröffnet not this.auctionOpened Nachbedingung Auction ist geöffnet this.auctionOpened Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

public void closeAuction() Workshop Article (Commands) public void closeAuction() Vorbedingung Auction ist schon geöffnet, aber noch nicht geschlossen this.auctionOpened and not this.auctionClosed Nachbedingung Auction 'ist nicht mehr geöffnet', dafür aber geschlossen not this.auctionOpened and this.auctionClosed Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

condition = “all( article : this.Articles, Workshop Category @Invariant( condition = “all( article : this.Articles, this.offersArticle( article ) == true )“ ) interface Category{ public List<Article> getArticles(); @Postcondition( condition = “return == true ==> exist( existingArticle : this.articles, existingArticle == searchedArticle )“ ) public boolean offersArticle( Article searchedArticle ); } Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

AuctionShop (Queries + Invariant) Workshop AuctionShop (Queries + Invariant) @Invariant( condition = “all( category : this.categories, all( article : category.articles, this.countCategoriesFor( article ) <= 3 ) )“ ) public class AuctionShop{ public Set<Category> getCategories(){...} public int countCategoriesFor( Article article ){...} } Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

AuctionShop (Commands) Workshop AuctionShop (Commands) @Precondition( condition = “ not category.offersArticle( article ) and this.countCategoriesFor( article ) < 3 not article.auctionOpened“ ) @Postcondition( condition = “ category.offersArticle( article ) == true this.countCategoriesFor( article ) == old:this.countCategoriesFor( article ) + 1 category.articles.size = old:category.articles.size + 1“ ) public void registerArticle( Category category, Article article ) Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

AuctionShop (Commands) Workshop AuctionShop (Commands) @Precondition( condition = “ not article.auctionOpened“ ) @Postcondition( condition = “ this.countCategoriesFor( article ) == 0“ ) public void removeArticle( Article article ) condition = “ this.countCategoriesFor( article ) >= 1“ ) condition = “ article.auctionOpened“ ) public void openAuction( Article article ) Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

More systematic Design Diskussion Benefits Better Design More systematic Design Programmers are encouraged to think about such matters as preconditions, ... the approach makes the concept explicit Clearer Design Obligations and benefits are shared between client and supplier and are clearly stated Simpler Designs Limitations on the use of a routine are clearly expressed Programmers are encouraged NOT to build routines that are too general, but to design classes with small, single-purpose routines Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

Benefits Improved Reliability Diskussion Better understood You say the same thing twice in different ways – helps you understand more clearly what the routine does Better tested Assertions are checked at runtime, thereby testing that routines fulfill their stated contracts ... and anything that helps testing will lead to code with fewer bugs Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

Benefits Better Documentation Diskussion Clearer Documentation Contracts for part of the public (client) view of a class Reliable Documentation Assertions are checked at runtime, thereby testing that the stated contracts are consistent with what the routine actually do (maintained with the code) Support for precise Specification Contracts provide a means to gain some of the benefits of precisespecifications while allowing programmers to work with their familiar operational intuitions Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

Support for Maintenance Diskussion Benefits Support for Maintenance if programms are delivered with assertion checking switched on, customers can provide developers with more accurate information on the circumstances surrounding a failure. Supports for Reuse Good documentation for library users – contracts clearly explain what routines in library classes do and what constraints are on using them well written preconditions give client programmers an accurate analysis when calling a library class under the wrong circumstances Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

False sense of security Diskussion Costs and Limitations Takes practice writing good contracts is a skill. False sense of security contracts cannot express all the desirable properties of programms Quality is not always the primary goal for some developments the most important goal is an early release ... Best for sequential programming in a distributed system you want routines that support 'shoot first, ask questions later' (instead of clients that 'look before they leap') Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006

Object-oriented software construction, 2nd ed. Prentice Hall, 1997 Quellen Meyer, B. Object-oriented software construction, 2nd ed. Prentice Hall, 1997 Mitchell, R. And McKim, J. Design by Contract, by Example Addison Wesley, 2002 Building bug-free O-O software: An introduction to Design by Contract http://archive.eiffel.com/doc/manuals/technology/contract Design by Contract A Conversation with Bertrand Meyer, Part II http://www.artima.com/intv/contracts.html Design by Contract: The Lessons of Ariane http://archive.eiffel.com/doc/manuals/technology/contract/ariane/page.html Mario Gleichmann Mario Gleichmann Mario Gleichmann XPUG Rhein/Main 2006 XPUG Rhein/Main 2006