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