1 / 69

Component specification

This text discusses the importance of specification in component-based development, including the use of interfaces, syntactic and semantic specifications, and the role of contracts. It also explores substitution and interoperability between components.

gjimmy
Download Presentation

Component specification

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Component specification Main bibliography: Ivica Crnkovic, Magnus Larsson (Eds.): Building reliable component-based systems Chapter 2: Specification of Software Components Chapter 6: Semantic Integrity in Component Based Development Other readings: B.Meyer: Applying Design by Contract Mary Shaw, Truth vs Knowledge: The Difference Between What a Component Does and What We Know It Does Antoine Beugnard, Jean-Marc Jézéquel, Noël Plouzeau, Damien Watkins: Making Components Contract Aware

  2. What is a component comprised off ? • Some Code… • The code represents the operations that the component will perform when invoked • An interface… • The interface tells the component-user everything he needs to know in order to deploy the component • The interface of a component should provide all the information needed by its users • The specification of a component is therefore the specification of its interface

  3. The Specification of an Interface • This must consist solely of: • A precise definition of the component's operations. • All context dependencies.

  4. Need for component specifications • For users, • The specification provides a definition of its interface, viz. its operations and context dependencies. • Since it is only the interface that is visible to users, its specification must be precise and complete. • For developers, • The specification of a component also provides an abstract definition of its internal structure.

  5. Required and Provided Interfaces/Properties • To be composable solely on the basis of its specification, a component needs to be equipped with: • Explicit declarations of functionality, synchronization and quality • required properties • provided properties Component

  6. Component specification levels • Levels of a component specification: • Syntax: includes specifications on the programming language level. • Semantic: functional contracts • Non-functional: deals with quality of service.

  7. Component specification levels • Levels of a component specification: • Syntax: includes specifications on the programming language level. • Semantic: functional contracts • Non-functional: deals with quality of service.

  8. Components and Interfaces • A component provides: • The implementation of a set of named interfaces, or types, each interface being a set of named operations • The following diagram is a UML metamodel • This model allows an interface to be implemented by several different components, and an operation to be part of several different interfaces

  9. Metamodel of the concepts used in syntacticspecification of software components Figure 2.1 from [Crnkovic]

  10. Model explained • The model presents a generic representation of: • The relationships between components, interfaces, and operations • One can distinguish between: • Object Oriented specifications and • Procedural specifications • Some differences may appear between different component technologies • Ex: A component implements a set of classes, each implementing a set of interfaces (COM) • Ex: A component is itself a class, implementing a interface

  11. Example: component SpellChecker • Implementation as a COM component: • Uses an IDL

  12. IDL Example interface ISpellCheck : IUnknown { HRESULT check([in] BSTR *word, [out] bool *correct); }; interface ICustomSpellCheck : IUnknown { HRESULT add([in] BSTR *word); HRESULT remove([in] BSTR *word); }; library SpellCheckerLib { coclass SpellChecker { [default] interface ISpellCheck; interface ICustomSpellCheck; }; };

  13. Uses of Syntactic Specification • The primary uses of syntactic specifications are: • Type checking (static of dynamic) of client code. • Base for interoperability between independently developed components and applications. • Interoperability may be achieved in different ways: • Binary format for interfaces • IDL to programming language mapings • An important aspect of interface specifications is how they relate to substitution and evolution of components

  14. Substitution • Substituting a component Y for a component X is said to be safe if: • All systems that work with X will also work with Y • From a syntactic viewpoint, a component can safely be replaced if: • The new component implements at least the same interfaces as the older components, or • The interface of the new component is a subtype of the interface of the old component.

  15. Forms of syntactic specification • All component models use syntactic specification of interfaces: • Programming language • IDL • Examples • Microsoft’s Component Object Model (COM) • Common Object Request Broker Architecture (CORBA) • JavaBeans

  16. Component specification levels • Levels of a component specification: • Syntax: includes specifications on the programming language level. • Semantics: functional contracts • Non-functional: deals with quality of service.

  17. Contracts “Applying Design by Contract,” B. Meyer, IEEE Computer, pp. 40-51, October 1992.

  18. Design-by-contract background A Client-Server Design Server Objects Provides services for client objects to use The object whose methods are being invoked Client Object Consumes the services offered by the supplier object The object that invokes the methods of the supplier object Contract A set of benefits and obligations that are mutually agreed upon by the client and supplier In practice, specified by the supplier object Clients implicitly accept the contract by using objects of the supplier class Good contracts are always in writing!

  19. Contracts in real life - Example Table 1 from [Meyer]

  20. What is a Contract? A contract between a client and a supplier protects both sides It protects the client by specifying how much should be done to get the benefit. The client is entitled to receive a certain result. It protects the supplier by specifying how little is acceptable. The supplier must not be liable for failing to carry out tasks outside of the specified scope. If a party fulfills its obligations it is entitled to its benefits No Hidden Clauses Rule: no requirement other than the obligations written in the contract can be imposed on a party to obtain the benefits

  21. Contracts for softwareExample: add node to tree Informal description of contract: Table 2 from [Meyer]

  22. Contracts for softwareExample: add node to tree More formal description of contract, as part of the routine’s text: Fig. 2 from [Meyer]

  23. Contracts for components • Pre-conditions • Post-conditions • Invariants

  24. A Pre-condition • Is an assertion that the component assumes to be fulfilled before an operation is invoked. • Will in general be a predicate over the operation’s input parameters and this state

  25. A Post-condition • Is an assertion that the component guarantees will hold just after an operation has been invoked, provided the operation’s pre-conditions were true when it was invoked. • Is a predicate over both input and output parameters as well as the state just before the invocation and just after

  26. An Invariant • Is a predicate over the interface’s state model that will always hold • A set of invariants may be associated with an interface.

  27. Metamodel of the concepts used in semantic specification of software components Figure 2.2 from [Crnkovic]

  28. Semantic specification of components • Semantic specification of a component comprises: • Specify component interfaces • For each interface, specify: • Model of state and Invariants • Operations with pre- and post-conditions • The model allows that different interfaces act on the same state model • Inter-interface constraints Note that state models and operation semantics are associated with interfaces rather than with a component !

  29. Component specificationExample: component SpellChecker Similarly to interface specification diagrams, components specification diagrams are used to specify which interfaces components provide and require. Specifying a component that provides interfaces

  30. Interface specification diagramExample: Interface ISpellCheck • State: words • Operations: • check (in word:String, out correct:Boolean):HRESULT; • Pre: the word to be checked is non-empty string • Post: if the return value indicates success, then the value of correct is true if word was a member of words and false otherwise

  31. Interface specification diagramExample: ICustomSpellCheck • State: words • Operations: • add (in word:String):HRESULT; • Pre: the word to be added is non-empty string • Post: if the return value indicates success, then word has been added to words • remove(in word:String):HRESULT; • Pre: the word to be removed is non-empty string • Post: if the return value indicates success, then word has been removed from words

  32. Inter-interface Constraints • The component specification is completed by the specification of its inter-interface constraints, an example constraint is formulated in OCL below. context SpellChecker ISpellCheck::words = ICustomSpellCheck::words This model allows the same state to be associated with several interfaces

  33. ISomeInterface <<comp spec>> SomeComponent IAnotherInterface IUsedInterface Interface dependency

  34. 1: op1 /ISomeInterface 1.1: op2 /IUsedInterface Realization Contracts • We can also specify realization contracts using collaboration interaction diagrams. Whenever the operation op1 is called, a component supporting this operation must invoke the operation op2 in some other component

  35. Uses of Semantic Specification • Tool support for component developers • Tool support for developers of component-based applications

  36. Substitution extended with semantics • Substituting a component Y for a component X is said to be safe if: • All systems that work with X will also work with Y • From a semantic viewpoint, a component can safely be replaced if: • A client that satisfies the preconditions for X must always satisfy the preconditions specified for Y • A client that can rely on postconditions ensured by X can also be ensured it can rely on Y • Conditions for component Y: • Interfaces of Y can have weaker preconditions on operations • Interfaces of Y can have stronger postconditions on operations • State models of X and Y need not be identical

  37. Levels of Formalism for Semantic Specifications • The levels of formalism, in an increasing order of formalism: • No semantics • Intuitive semantics • Structured semantics • Executable semantics • Formal semantics

  38. Weak and strong contracts • Postconditions • specify the exit conditions guaranteed by an operation at its end provided the precondition was satisfied at the entry in the operation • The outcome when the precondition was not satisfied is explicitly left undefined [Meyer] • Strong contract: • the precondition specifies conditions for success • postcondition need to spercify only the outcome in the well-defined situations • Back-end-components usually have strong contracts • Weak contract: • the precondition is uncomplete, the component must be able to filter out invalid uses • The postconditions will specify also the outcome of the invalid uses • Front-end-components (such as GUI-components) usually have weak contracts

  39. An Example • Component RandomAccess • controlls the access to random access file of a record type R • records of a fixed size • access to the file is by record number, numbers start from 0. • It is assumed that the file is continuous, thus record numbers go up to the current maximum number, called the high water mark • Operations: • addRecord • getRecord • delRecord • getHighWaterMark

  40. The contract • Operation getRecord – retrieves a record with a given number • The precondition for • the single input parameter of the operation is the number of the record concerned, which must exist in the file. • The post-condition • If an unrecoverable system error occurs (file system error) the operation indicates a failure • Weak part of the contract: client does not have to check file status before • the result of the operation is the required data record of type R. • Strong part of the contract: assumes that record number is always correctly given

  41. Level 0: No Semantics • The following definition of the operation getRecord illustrates how a purely syntactic specification would be given:

  42. Level 1: Intuitive Semantics • Plain text, unstructured description and comments about a component and its parts • An intuitive specification of the operation getRecord:

  43. Level 2: Structured Semantics • The semantics is presented in a structured way but needs not be in accordance with any particular syntax or formalism • A structured specification of the operation getRecord:

  44. Level 3: Executable Semantics • The semantics is expressed in a way that can be executed and controlled by the system during run-time. • The executable specification is included in the implementation of the component • Limitation: not all conditions can be expressed in an executable way Executable specification for getRecord:

  45. Examples of executable semantics • Assertions: • Java: http://download.oracle.com/javase/6/docs/technotes/guides/language/assert.html • OCL (Object Constraint Language) • Contract4J http://www.contract4j.org/contract4j • MSDN: Code Contracts http://research.microsoft.com/en-us/projects/contracts/ • General rules: • The execution of the assertions should not add functionality ! • Assertions serve to detect coding errors and should not try to handle or compensate for them

  46. Trapping Offending Calls • For debugging purposes, the component itself may use the executable precondition to trap offending calls: (The example here just assumes that the System class in Java contains an assert method)

  47. Ensuring a Correct Call • The client code may also take advantage of the executable assertions by checking the precondition before the call, as illustrated below:

  48. OCL The Object Constraint Language (OCL) is a declarative language for describing rules that apply to UML models OCL can be used to describe constraints A constraint is a restriction on one or more values of a model or system. A constraint is an expression that evaluates to true or false as a query language Queries are expressions that evaluate to a value (true, false and other values) Can be used to define new attributes and operations OCL expressions are always associated with a UML model OCL expressions can be associated with any model element in UML

  49. OCL Constraints vs. Queries Examples of constraints: Duration of a flight is the same as the difference between the arrival and departure times The maximum number of passengers on a flight must be less than 1,001 The origin of a flight must be different than its destination Examples of queries: Return all the departing flights from a given airport Return all the flights departing from a given airport with a departure time after 4p.m. Derive the arrival time by adding the duration of the flight to the departure time. Flight departing Flights origin Airport departTime: Time /arrivalTime: Time duration : Interval maxNrPassengers: Integer * 1 name: String 1 * arriving Flights desti- nation

  50. Different kinds of OCL constraints Class invariant a constraint that must always be met by all instances of the class Precondition of an operation a constraint that must always be true BEFORE the execution of the operation Postcondition of an operation a constraint that must always be true AFTER the execution of the operation Constraint context: the element that the constraint restricts Every OCL expression is bound to a context Own context may be denoted by “self”

More Related