Object model example#

This example present the Python code to specify a model (illustrated in the following figure) consisting of a Structural model and a Object model.

Library object model

The Python code to specify the structural and object models (using the B-UML libraries) is presented below., including its classes, attributes, and relationships, is presented in the following code (lines 1-44).

  1
  2from besser.BUML.metamodel.structural import DomainModel, Class, Property, \
  3    Multiplicity, BinaryAssociation,Constraint,\
  4    StringType, IntegerType, DateType
  5from besser.BUML.metamodel.object import *
  6import datetime
  7
  8#############################################
  9#   Library - structural model definition   #
 10#############################################
 11
 12# Library attributes definition
 13library_name: Property = Property(name="name", type=StringType)
 14address: Property = Property(name="address", type=StringType)
 15# Library class definition
 16library: Class = Class (name="Library", attributes={library_name, address})
 17
 18# Book attributes definition
 19title: Property = Property(name="title", type=StringType)
 20pages: Property = Property(name="pages", type=IntegerType)
 21release: Property = Property(name="release", type=DateType)
 22# Book class definition
 23book: Class = Class (name="Book", attributes={title, pages, release})
 24
 25# Author attributes definition
 26author_name: Property = Property(name="name", type=StringType)
 27email: Property = Property(name="email", type=StringType)
 28# Author class definition
 29author: Class = Class (name="Author", attributes={author_name, email})
 30
 31# Library-Book association definition
 32located_in: Property = Property(name="locatedIn",type=library, multiplicity=Multiplicity(1, 1))
 33has: Property = Property(name="has", type=book, multiplicity=Multiplicity(0, "*"))
 34lib_book_association: BinaryAssociation = BinaryAssociation(name="lib_book_assoc", ends={located_in, has})
 35
 36# Book-Author association definition
 37publishes: Property = Property(name="publishes", type=book, multiplicity=Multiplicity(0, "*"))
 38written_by: Property = Property(name="writtenBy", type=author, multiplicity=Multiplicity(1, "*"))
 39book_author_association: BinaryAssociation = BinaryAssociation(name="book_author_assoc", ends={written_by, publishes})
 40
 41constraintPageNumber: Constraint = Constraint(name = "libraryPageNumber",context=library,expression="context Library inv inv1: self.has ->forAll(b:Book|b.pages>0)",language="OCL")
 42
 43constraintBookPageNumber: Constraint = Constraint(name = "BookPageNumber",context=book,expression="context Book inv inv2: self.pages>0",language="OCL")
 44
 45constraintTitleIncorrect: Constraint = Constraint(name = "BookTitleInc",context=book,expression="context Book inv inv2: self.title= 'NI')",language="OCL")
 46
 47constraintTitleCorrect: Constraint = Constraint(name = "BookTitle",context=book,expression="context Book inv inv2: self.title <> 'NI'",language="OCL")
 48
 49constraintLibraryExists: Constraint = Constraint(name = "LibarayExistsConst",context=library,expression="context Library inv inv3: self.has->exists( i_book : Book | i_book.pages <= 110 )",language="OCL")
 50
 51# constraintLibraryExclude: Constraint = Constraint(name = "LibarayExclude",context=library,expression="context Library inv inv3: self.has->excludes()",language="OCL")
 52constraintLibrarySize: Constraint = Constraint(name = "LibaraySize",context=library,expression="context Library inv inv3: self.has->size()>1",language="OCL")
 53
 54constraintLibraryCollect: Constraint = Constraint(name = "LibaryCollect",context=library,
 55expression="context Library inv inv3: self.has->collect(i_book : Book | i_book.pages <= 110)"
 56           "->size()>0",language="OCL")
 57
 58constraintLibraryIf: Constraint = Constraint(name ="LibaryIf", context=library,
 59expression="context Library inv inv3: if self.name <> 'NI' then self.has->exists"
 60           "( i_book : Book | i_book.pages <= 110 ) else self.has->forAll(b:Book|b.pages>0)"
 61           " endif", language="OCL")
 62
 63constraintLibraryElse: Constraint = Constraint(name ="LibaryElse", context=library,
 64expression="context Library inv inv3: if self.name = 'NI' then self.has->exists"
 65           "( i_book : Book | i_book.pages <= 110 ) else self.has->forAll(b:Book|b.pages>0)"
 66           " endif", language="OCL")
 67
 68constraintLibraryElseFalse: Constraint = Constraint(name ="LibaryElseFalse", context=library, expression="context Library inv inv3: if self.name = 'NI' then self.has->exists( i_book : Book | i_book.pages <= 110 ) else self.has->forAll(b:Book|b.pages<0) endif", language="OCL")
 69
 70
 71constraintNameOCLIsTypeOf: Constraint = Constraint(name ="BookIsTypeOfStr", context=book, expression="context Book inv inv3: self.title.oclIsTypeOf(String)", language="OCL")
 72
 73constraintNameOCLIsTypeOfInt: Constraint = Constraint(name ="BookIsTypeOfInt", context=book, expression="context Book inv inv3: self.pages.oclIsTypeOf(Integer)", language="OCL")
 74
 75constraintNameOCLIsTypeOfIntFalse: Constraint = Constraint(name ="BookIsTypeOfIntFalse", context=book, expression="context Book inv inv3: self.pages.oclIsTypeOf(String)", language="OCL")
 76constraintifElseSize: Constraint = Constraint(name ="constraintifElseSize", context=library, expression="context Library inv inv3: if self.name <> 'NI' then self.has->exists( i_book : Book | i_book.pages <= 110 )->size()<3 else self.has->forAll(b:Book|b.pages>0) endif", language="OCL")
 77
 78
 79# Domain model definition
 80library_model : DomainModel = DomainModel(name="Library_model", types={library, book, author}, 
 81                                          associations={lib_book_association, book_author_association},
 82                                          constraints={
 83                                              constraintBookPageNumber,
 84                                                       # constraintTitleIncorrect,
 85                                                       constraintPageNumber,
 86                                                       constraintTitleCorrect,
 87                                                       constraintLibraryExists,
 88                                                       constraintLibrarySize,
 89                                                       constraintLibraryCollect,
 90                                                       constraintLibraryIf,
 91                                                       constraintLibraryElse,
 92                                                       constraintLibraryElseFalse,
 93                                                       constraintNameOCLIsTypeOf,
 94                                                       constraintNameOCLIsTypeOfInt,
 95                                                       constraintNameOCLIsTypeOfIntFalse
 96                                          }
 97
 98                                            # constraints={constraintLibrarySize}
 99                                          )
100
101
102#########################################
103#   Library - object model definition   #
104#########################################
105
106
107# Library object attributes
108library_obj_name: AttributeLink = AttributeLink(attribute=library_name, value=DataValue(classifier=StringType, value="Library test"))
109library_obj_address: AttributeLink = AttributeLink(attribute=address, value=DataValue(classifier=StringType, value="street 123"))
110# Library object
111library_obj: Object = Object(name="Library_Object", classifier=library, slots=[library_obj_name, library_obj_address])
112
113# Book object attributes
114book_obj_name: AttributeLink = AttributeLink(attribute=title, value=DataValue(classifier=StringType, value="Book tittle"))
115book_obj_pages: AttributeLink = AttributeLink(attribute=pages, value=DataValue(classifier=IntegerType, value=100))
116book_obj_release: AttributeLink = AttributeLink(attribute=release, value=DataValue(classifier=DateType, value=datetime.datetime(2020, 3, 15)))
117# Book object
118book_obj: Object = Object(name="Book_Object", classifier=book, slots=[book_obj_name, book_obj_pages])
119
120# Book_2 object attributes
121
122book_obj_name_2: AttributeLink = AttributeLink(attribute=title, value=DataValue(classifier=StringType, value="Book tittle_2"))
123book_obj_pages_2: AttributeLink = AttributeLink(attribute=pages, value=DataValue(classifier=IntegerType, value=400))
124book_obj_release_2: AttributeLink = AttributeLink(attribute=release, value=DataValue(classifier=DateType, value=datetime.datetime(2024, 3, 15)))
125# Book object
126book_obj_2: Object = Object(name="Book_2_Object", classifier=book, slots=[book_obj_name_2, book_obj_pages_2])
127
128# Author object attributes
129author_obj_name: AttributeLink = AttributeLink(attribute=author_name, value=DataValue(classifier=StringType, value="John Doe"))
130author_obj_email: AttributeLink = AttributeLink(attribute=email, value=DataValue(classifier=StringType, value="john@doe.com"))
131# Author object
132author_obj: Object = Object(name="Author_Object", classifier=author, slots=[author_obj_name, author_obj_email])
133
134# Book object and Author object link
135book_link_end1: LinkEnd = LinkEnd(name="book_end1", association_end=publishes, object=book_obj)
136author_link_end: LinkEnd = LinkEnd(name="author_end", association_end=written_by, object=author_obj)
137author_book_link: Link = Link(name="author_book_link", association=book_author_association, connections=[book_link_end1,author_link_end])
138
139# Book Library and Book object link
140book_link_end2: LinkEnd = LinkEnd(name="book_end2", association_end=has, object=book_obj)
141library_link_end: LinkEnd = LinkEnd(name="library_end", association_end=located_in, object=library_obj)
142library_book_link: Link = Link(name="library_book_link", association=book_author_association, connections=[book_link_end2,library_link_end])
143
144# Book object and Author object link
145book_link_end2: LinkEnd = LinkEnd(name="book_end3", association_end=publishes, object=book_obj_2)
146author_link_end2: LinkEnd = LinkEnd(name="author_end2", association_end=written_by, object=author_obj)
147author_book_link2: Link = Link(name="author_book_link2", association=book_author_association, connections=[book_link_end2,author_link_end2])
148
149# Book Library and Book object link
150book_link_end3: LinkEnd = LinkEnd(name="book_end4", association_end=has, object=book_obj_2)
151library_link_end3: LinkEnd = LinkEnd(name="library_end3", association_end=located_in, object=library_obj)
152library_book_link3: Link = Link(name="library_book_link3", association=book_author_association, connections=[book_link_end3,library_link_end3])
153
154
155
156# Object model definition
157object_model: ObjectModel = ObjectModel(name="Object_model", instances={library_obj, author_obj, book_obj,book_obj_2}, links={author_book_link, library_book_link,author_book_link2,library_book_link3})