Looking “Behind” Some Languages - Describing with Help of UML (Many Simplifications) Jan Pettersen Nytun UiA
Outline UML XML Entity-Relationship (ER) model Relational Database Model
UML It is object-oriented by nature. Probably the most used modelling language for doing modelling of software. It is object-oriented by nature.
Object-Oriented Model Example 1 owns Person Car * name Model Model Instance :Car id1:Person :Car name=“Bob” The arrow is the instanceOf-relation
What is the model saying - if you should express it in English? 1 owns Person Car * name Model Model Instance :Car id1:Person A person have name and may own several cars. A car will always be own by one person. A person with name Bob (with id id1) owns two cars. The computer only knows that a there can be objects of class Person, which have names, etc.. We map it to description of “real people” be interpreting the names (but what if it is another languages then English, where Person means apple, etc. ?). :Car name=“Bob” Semantics: …the meaning of words and phrases…
Semantics - Our Example Human Perspective: A person have name and may own several cars. A car will always be own by one person. A person with name Bob (with id id1) owns two cars. We map it to description of “real people” by interpreting the names (but what if it is another languages then English, where Person means apple…?). Computer Perspective: The computer “knows” that a there can be objects of class Person, with slots storing text, etc..
A Model (like a statement) is about something, expressed with the building blocks of a language. Multiplicity (Role) Name Association Class 1 owns Property Person Car * name Object :Car id1:Person :Car name=“Bob” Slot Value Link
Some building blocks for defining class models (Using UML to Describe UML) Multiplicity Association (Role) Name Class 1 owns Person Car Association Property * name name 0..1 member- End 2 Class Property 1 name isAbstract * name isComposite multiplicity general owned- Attribute :Car id1:Person 1 Operation name parameters :Car name=“Bob” Model is only using things it describing itself - it is self describing (not minimal self describing?). owned- Operation This model describes how you may represent parts of the structure of class diagrams. It does not tell how a class diagram should look like.
What about a model for representing the objects shown on an object diagram? :Car id1:Person :Car name=“Bob” Slot Link Value
Building blocks for defining instance models (Using UML to Describe UML) 1 owns Person Car * name Owning- Instance Object id : String * Slot 1 link- Object 1 0..1 :Car id1:Person 1 instanceOf value Value Class * :Car name=“Bob” 1 Link String StringValue:String instanceOf Slot Link Object Association instanceOf 1 Value Where is the instanceOf found in object diagram? Property * owned- Attribute This model describes how you may represent the structure of an object diagram. It does not show how an object diagram should look like.
Outline UML XML Entity-Relationship (ER) model Relational Database Model
XML XML is a general format while html is meant for human readable web pages! XML has no fixed tag vocabulary – hence, users can define their own custom element and attribute names. It have some keywords, e.g. xmlns.
XML syntax Well-formed documents The document must have exactly one root element. The following is a well-formed XML document: <journal>This is a journal. </journal> The text enclosed by the root tags may contain an arbitrary number of XML elements. The basic syntax for one element is: <name attribute="value“…>content</name> start-tag content end-tag
XML syntax Continues… The root element can be preceded by an optional XML declaration. E.g., <?xml version="1.0" encoding="UTF-8"?> XML requires that elements be properly nested, i.e. no overlap. Well-formed example: Start-tag and end-tag of element medicalsystem Attribute <medicalsystem> <journal name=“Ola Norman" id=“1"> <journalrecord>May be a little overweight</journalrecord> </journal> <journal name=“Kari Norman" id=“2"> <journalrecord>Lack of iron.</journalrecord> <journalrecord>A slight attack of shopomania?</journalrecord> </journal> </medicalsystem>
xmlns - XML Namespace Gives unique names to elements and attributes. xmlns can be used to solve name conflicts inside one xml page, e.g., “table” may be a furniture or a data structure.
Namespace Examples An xml name space is often a web address. The address may go to a none existing web page - only the URI (URL) addressing mechanism is used. [1]: <root xmlns:h=“http://www.w3.org/TR/html4/” xmlns:f="http://www.w3schools.com/furniture"> <h:table> …. </h:table> <f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> </root> Xmlns is by definition bound to the namespace name http://www.w3.org/2000/xmlns/ which is an URI (URL)).
XML - UML Data Medical System UML Model <medicalsystem> :Journal :JournalRecord name=“Ola” SSN=“1” description=“May be a little bit fat?” * Journal JournalRecord 1 :Journal :JournalRecord name:String SSN:String description:String name=“Kari” SSN=“2” description=“Lack of iron.” :JournalRecord Data description=“A slight attack of shopomania?” <medicalsystem> <journal name=“Ola Norman" ssn=“1"> <journalrecord>May be a little bit fat?</journalrecord> </journal> <journal name=“Kari Norman" ssn=“2"> <journalrecord>Lack of iron.</journalrecord> <journalrecord>A slight attack of shopomania?</journalrecord> </journal> </medicalsystem>
What is meant with the following statement: “XML is Syntax” What is meant with the following statement: “XML is Syntax”? Syntax: …the rules about how words are arranged and connected to make phrases and sentences… XM does not have semantics like Java,… It is more like a way of structuring data, e.g., does not say anything about reasoning, execution, etc.
XML “is Syntax” The custom tags may indicate the meaning of the document's content, e.g., <musician>, and <record> - this can make XML documents at least partly self-describing (to humans). But what is meant by a tag, e.g., <musician>, is not given (the term “musician” is used but not explained).
From Wikipedia: XML is a generic framework for storing text or any data whose structure can be represented as a tree. Why a tree? <medicalsystem> <journal name=“Ola Norman" id=“1"> <journalrecord>May be a little overweight</journalrecord> </journal> <journal name=“Kari Norman" id=“2"> <journalrecord>Lack of iron.</journalrecord> <journalrecord>A slight attack of shopomania? </journalrecord> </journal> </medicalsystem> DirectedTree 0..1 Why tree? 0..1 topRoot 1 root Node child * edge
“May be a little overweight " “A slight attack of shopomania?" <medicalsystem> <journal name=“Ola Norman" id=“1"> <journalrecord>May be a little overweight</journalrecord> </journal> <journal name=“Kari Norman" id=“2"> <journalrecord>Lack of iron.</journalrecord> <journalrecord>A slight attack of shopomania? </journalrecord> </journal> </medicalsystem> medicalsystem journal journal name id journalrecord name id journalrecord journalrecord “Ola Norman" “1" Why tree? “Kari Norman" “2" “Lack of iron." “May be a little overweight " “A slight attack of shopomania?" Where did the start- and end-tags go? What about the order of things?
Building blocks for defining an XML document (Using UML) Element Attribute Text content Node * Building blocks for defining an XML document (Using UML) name : String Each XML document consists of nodes (in the model represented by Node class) and each node can contain either attributes (represented by Attribute class) or nested nodes. Both attribute and node have name (this and the Contains association are the reasons why there is a root abstract class named Element), in addition attribute has a value. Contains association expressing the fact that a node can contain both attributes and nodes is ordered on the elements end – this is because order of nodes is significant in XML. Text nodes are represented by a separate subclass of Node named TextNode. As each XML document consists of exactly one root node, it is convenient to define a separate subclass of Node class called RootNode for representing a root node in a document. This class also represents the document itself – it contains an additional attribute documentName. 0..1 Element Attribute container textContent : String value : String Is this an instance model? (A bit simplified)
An XML Schema functioning as model Example (simplified): <xsd:schema …. > <xsd:element name = “Building” type = “BuildingType”/> <xsd:complexType name=" BuildingType"> <xsd:attribute name=“name" type="xsd:string"/> <xsd:attribute name=“area" type="xsd:integer"/> </xsd:complexType> XML document (= instance of some XML schema)): <?xml version=1.0” …> … <Building name=“BlueBox" area = 300/> ….. Each XML document consists of nodes (in the model represented by Node class) and each node can contain either attributes (represented by Attribute class) or nested nodes. Both attribute and node have name (this and the Contains association are the reasons why there is a root abstract class named Element), in addition attribute has a value. Contains association expressing the fact that a node can contain both attributes and nodes is ordered on the elements end – this is because order of nodes is significant in XML. Text nodes are represented by a separate subclass of Node named TextNode. As each XML document consists of exactly one root node, it is convenient to define a separate subclass of Node class called RootNode for representing a root node in a document. This class also represents the document itself – it contains an additional attribute documentName.
Outline UML XML Entity-Relationship (ER) model Relational Database Model
Entity-Relationship (ER) model From Wikipedia: In software engineering, an entity–relationship model (ER model) is a data model for describing a database in an abstract way. An object-relational database (ORD), is a database management system (DBMS) similar to a relational database, but with an object-oriented database model: objects, classes and inheritance are directly supported in database schemas and in the query language. …at the other extreme are OODBMSes in which the database is essentially a persistent object store for software written in anobject-oriented programming language, with a programming API for storing and retrieving objects, and little or no specific support for querying. A hierarchical database model is a data model in which the data is organized into a tree-like structure. The structure allows representing information using parent/child relationships: each parent can have many children, but each child has only one parent (also known as a 1-to-many relationship). All attributes of a specific record are listed under an entity type. In a database an entity type is the equivalent of a table. The network model is a database model conceived as a flexible way of representing objects and their relationships. Its distinguishing feature is that the schema, viewed as a graph in which object types are nodes and relationship types are arcs, is not restricted to being a hierarchy or lattice. While the hierarchical database model structures data as a tree of records, with each record having one parent record and many children, the network model allows each record to have multiple parent and child records, forming a generalized graph structure. Until the early 1980s the performance benefits of the low-level navigational interfaces offered by hierarchical and network databases were persuasive for many large-scale applications, but as hardware became faster, the extra productivity and flexibility of the relational model led to the gradual obsolescence of the network model in corporate enterprise usage.
Entity-Relationship model ER-metamodel (simplified abstract syntax): Role Relationsship 2..* minCardinality maxCardinality name * 1 Entity Attribute 1 name * minCardinality maxCardinality isDerived ER-model example (using ER concrete syntax): examination examinee Course Student name name
some specified visual notation for instances): examiner examination examinee Course Student 1 * name name Showing instances with some ad hoc notation (it does not seems like ER have some specified visual notation for instances): Course examination Student
Often an entity-relationship model is mapped to a Relational database model Student Student name StudentId name
Outline UML XML Entity-Relationship (ER) model Relational Database Model
Relational Database Model From Wikipedia: The most popular example of a database model is the relational model, which uses a table-based format. An object-relational database (ORD), is a database management system (DBMS) similar to a relational database, but with an object-oriented database model: objects, classes and inheritance are directly supported in database schemas and in the query language. …at the other extreme are OODBMSes in which the database is essentially a persistent object store for software written in anobject-oriented programming language, with a programming API for storing and retrieving objects, and little or no specific support for querying. A hierarchical database model is a data model in which the data is organized into a tree-like structure. The structure allows representing information using parent/child relationships: each parent can have many children, but each child has only one parent (also known as a 1-to-many relationship). All attributes of a specific record are listed under an entity type. In a database an entity type is the equivalent of a table. The network model is a database model conceived as a flexible way of representing objects and their relationships. Its distinguishing feature is that the schema, viewed as a graph in which object types are nodes and relationship types are arcs, is not restricted to being a hierarchy or lattice. While the hierarchical database model structures data as a tree of records, with each record having one parent record and many children, the network model allows each record to have multiple parent and child records, forming a generalized graph structure. Until the early 1980s the performance benefits of the low-level navigational interfaces offered by hierarchical and network databases were persuasive for many large-scale applications, but as hardware became faster, the extra productivity and flexibility of the relational model led to the gradual obsolescence of the network model in corporate enterprise usage.
Relational Database Model Table description Building BuildingId name area Table with values Building [2] BuildingId name area b1 BlueBox 300 Example of instance b7 Galleriet 300
Object-oriented UML Relational Mapping
Why This Mapping? Object technology and relational databases often used together. They are different: The relational paradigm is based on mathematical principles: Set theory. View on sets of “objects”. The object paradigm is somewhat different: Inheritance, behaviour, encapsulation,…view on “single object”. (However, the later version of SQL incooperats more if these things.)
Why This Mapping Continues... In the object paradigm objects are traversed by following links between objects. In the relational paradigm rows in different tables are connected with foreign keys (joining rows in different tables). (Mapping from UML class diagrams to tables and then back to class diagrams may lead to loss of information.)
UML to Relational Mapping: Class to Table Class diagram b is a derived attribute. C a : Integer c : Double /b : Integer NB! Not all classes are persistent. How to map this class?
UML to Relational Mapping: Class to Table Class diagram b is a derived attribute. C a : Integer c : Double /b : Integer NB! Not all classes are persistent. Table Table: C A: Integer C: Double
UML to Relational Mapping: one-to-one associations X Y x_pkey x_a1 y_pkey y_a1 1 Total participation Class diagram Object diagram X1 Y2 X2 Y3 X3 Y1 How to map this?
UML to Relational Mapping: one-to-one associations X Y x_pkey x_a1 y_pkey y_a1 1 Total participation Class diagram Object diagram X1 Y2 X2 Y3 X3 Y1 x_pkey Tables: Three different solutions x_a1 y_pkey y_a1 Table: XY Table: X and Y Table: Y and X
UML to Relational Mapping: one-to-many associations Object diagram X Y x_pkey x_a1 y_pkey y_a1 1 * Class diagram X1 Y2 X2 Y1 Y3 X3 Y4 X4 How to map this?
UML to Relational Mapping: one-to-many associations Object diagram X Y x_pkey x_a1 y_pkey y_a1 1 * Class diagram X1 Y2 X2 Y1 Y3 X3 Y4 X4 Tables: One possible solution y_pkey y_a1 x_pkey x_a1 Table: Y Table: X
UML to Relational Mapping: many-to-many associations Object diagram X1 Y2 Y3 X2 Y1 X4 Y4 X3 X Y x_pkey x_a1 y_pkey y_a1 * Class diagram How to map this?
UML to Relational Mapping: many-to-many associations Object diagram X1 Y2 Y3 X2 Y1 X4 Y4 X3 X Y x_pkey x_a1 y_pkey y_a1 * Class diagram Tables: One possible solution y_pkey y_a1 x_a1 Table: Y x_pkey Table: X Table: XY
References [1] http://www.w3schools.com/xml/xml_namespaces.asp [2] http://www.ibm.com/developerworks/rational/library/05/503_sebas/