Data Management

profilekrish21
DMA605DataManagement-Week5.pdf

Structural Modeling Dr. Ardeshir Badr

Objectives

• Understand the rules and style guidelines, and processes used for creating class diagrams and object diagrams

• Be able to create class diagrams, and object diagrams

• Understand the relationship among the structural models

• Understand the relationship between the structural and functional models

• A structural model defines the structure of the objects (people, places, things and how they are related) that support the business processes of an organization

• Structural modeling supports the creation of a static view of the system

Structural Modeling

Analysis Workflow

Conceptual model: How the objects are stored, created, or manipulated (Logical organization of the objects)

Design Workflow

Design model: How the objects will be organized in databases and software

• In analysis phase, the structural model contains classes, attributes, operations, and the relationships, however they do not represent software components or classes in an object oriented programming language. These initial classes are refined into programming level objects at later stages

• The structural models in analysis workflow should represent the responsibilities of each class and the collaborations among classes

• Elements of a structural model: • Classes: A template used for creating specific instances or objects. All objects of a given class are

identical in structure and behavior, but can contain different data in their attributes. We have two class types: • Concrete: Used to create objects • Abstract: No objects are created from them, but are useful abstractions

• Attributes: Information relevant to the description of a class, for example, employee class has name as an attribute • Operations: Defines the behavior of a class, or actions to which the instances of a class are capable of responding

Elements of Structural Models

• Relationships are in three data abstraction mechanism categories: • Generalization relationships: A superclass that contains the basic attributes and operations will

be used in several subclasses. The subclasses inherit the attributes and operations of their superclass and can also contain attributes and operations unique to them (a-kind-of). For example, a pilot class (subclass) and a professor (subclass) class can be generalized into a person class (superclass)

• Aggregation relationships: Relate parts to the whole or a-part-of relationship, for example, the relationship between an engine and a car is: an engine is a part of a car

• Association relationships: Relationships that do not fit into generalization or aggregation, for example, a patient schedules an appointment. The patient is not a part of an appointment and patient is not a kind of appointment. We can say there is an association relationship between patient and an appointment

Elements of Structural Models - Relationships

Class types and generalization relationship

Person

Doctor Patient

General Practitioner Specialist

Superclass for Doctor and

Patient

Superclass for General

Practitioner and Specialist

Abstract class

Abstract class

Concrete class

Concrete class

Concrete class

Classes are arranged in a hierarchy of superclasses or general, and subclasses or specific classes

Subclasses inherit the appropriate attributes and methods from the superclasses above them

Concrete classes are the ones that have instances

Classes that produce no instance and are used as templates for other more specific classes are called abstract classes

• To identify objects for structural model: • Textual analysis

• Brainstorming

• Common objects lists

• Patterns

Object Identification

• Creates an initial cut of a structural model

• Review use case diagrams and use case descriptions to identify potential objects, attributes, operations, and relationships

• Basic guidelines: • A noun is an indicative of a class or objects • An adjective implies an attribute of an object • A doing verb or transitive verb implies an operation • A being verb or having verb implies a relationship

Object Identification – Textual Analysis

Campus Housing Services Example: From the use case diagrams and use case descriptions we can identify the following: • Objects are apartment and apartment owner • Attributes of apartment are: location, number of bedrooms, monthly rent, and the distance from the

campus • Attributes of owner are: name, address, phone number, email • The relationship is: one to many relationship between an apartment owner and an apartment • Operations are Add Apartment and Delete Apartment

Use Case Diagram for Campus Example

Use Case Descriptions for Campus Example

• Is a discovery technique

• Brainstorming process: • A set of individuals sitting around a table suggest potential classes • A facilitator asks the set of individuals to address a specific question • For example facilitator asks the analysts and users to think about their experience with making Doctor

appointments and to identify candidate classes based on their past experience • The analysts and users might come up with classes name, for example, Doctor, Patient, Nurse,

Receptionist, Health Insurance. Then they can narrow down the number of classes, and have another brainstorming on identifying the attributes, operations and relationships

• In brainstorming we do not use the functional models developed before (use cases, use case descriptions or activity diagrams)

• Brainstorming Guidelines: • Suggestions should be taken seriously • All participants should be thinking fast and furiously • Facilitator must mange the fast and furious thinking process to avoid chaotic sessions • A round robin approach is recommended • Facilitator should use humor to break the ice

Object Identification – Brainstorming

• A list of objects common to the business domain:

• Physical or tangible things, for example, books, desks, chairs, office equipment

• Incidents: events that occur in the business domain, for example, meetings, flights,

performances

• Roles: reviewing the use case can identify the roles, for example, student, apartment owner

• Interaction: a transaction that takes place in business domain, for example, sales transaction

• Places, containers, organizations

• Processes sometimes contain information in them

• Library of reusable objects, for example, Common Open Source Medical Objects

Object Identification – Common Object List

• A pattern is a useful group of collaborating classes that provide a solution to commonly occurring problems, which makes them reusable

• Using patterns from different sources enable the development team to create more complete and robust models of the problem domain

• Useful patterns that are already developed: • Accounting

• Actor-Role

• Assembly-Part

• Container-Content

• Contract

• Document

• Employment

• Geographic Locations

• Material Requirements Planning

• Organization and Party

• Trading

Object Identification – Patterns

Object Identification – Patterns

Application of Patterns to Making a Doctor Appointment

Elements of a class Diagram

Class: Represents a kind of person, place, or thing about which the system should capture and store information

Attribute: Represents properties that describe the state of an object

Operation: Represents the actions or functions a class can perform. Can be constructor, query, or update types

Association: Represents a relationship between multiple classes or a class and itself, and can have multiplicity which represents the minimum and maximum times a class instance can be associated with the related class instance

Generalization: Represents a kind of relationship between multiple classes

Aggregation: Represents a logical a-part-of relationship between multiple classes or a class and itself

Composition: Represents a physical a-part-of relationship between multiple classes or a class and itself

Class Diagram for Campus Housing Service

Object Oriented Programming in Python

#Create a class for Employee with attribute first_name, last_name, salary class Employee:

def __init__(self, first_name, last_name, salary): self.first_name = first_name self.last_name = last_name self.salary = salary self.email = first_name + '.' + last_name + '@education.com'

employee1 = Employee('Ardeshir','Badr',50000) employee2 = Employee('Adam', 'Adrian', 60000)

print(employee1.email) print(employee2.email)

Object Oriented Programming in Python

#Create a class for Employee with attribute first_name, last_name, salary #Add a method to print the full name class Employee:

def __init__(self, first_name, last_name, salary): self.first_name = first_name self.last_name = last_name self.salary = salary self.email = first_name + '.' + last_name + '@education.com'

def full_name(self): return self.first_name + ' ' + self.last_name

employee1 = Employee('Ardeshir','Badr',50000) employee2 = Employee('Adam', 'Adrian', 60000)

print(employee1.email) print(employee2.email)

print(employee1.full_name()) print(Employee.full_name(employee2))

Object Oriented Programming in Python #Add a class variable raise_amount and a method apply_raise() to increase the salary. Class variables stay the same for all #instances, and should be accessed by self. class Employee:

raise_amount = 1.04

def __init__(self, first_name, last_name, salary): self.first_name = first_name self.last_name = last_name self.salary = salary self.email = first_name + '.' + last_name + '@education.com'

def full_name(self): return self.first_name + ' ' + self.last_name

def apply_raise(self): self.salary = int(self.salary * self.raise_amount)

employee1 = Employee('Ardeshir','Badr',50000) employee2 = Employee('Adam', 'Adrian', 60000)

print(employee1.salary) employee1.apply_raise() print(employee1.salary)

Object Oriented Programming in Python

#Use instance_name.__dict__ to get all information on attributes of an object class Employee:

raise_amount = 1.04

def __init__(self, first_name, last_name, salary): self.first_name = first_name self.last_name = last_name self.salary = salary self.email = first_name + '.' + last_name + '@education.com'

def full_name(self): return self.first_name + ' ' + self.last_name

def apply_raise(self): self.salary = int(self.salary * self.raise_amount)

employee1 = Employee('Ardeshir','Badr',50000) employee2 = Employee('Adam', 'Adrian', 60000)

print(employee1.__dict__)

{'first_name': 'Ardeshir', 'last_name': 'Badr', 'salary': 50000, 'email': '[email protected]'}

Object Oriented Programming in Python #Use class_name.__dict__ to get all information on attributes of a class class Employee:

raise_amount = 1.04

def __init__(self, first_name, last_name, salary): self.first_name = first_name self.last_name = last_name self.salary = salary self.email = first_name + '.' + last_name + '@education.com'

def full_name(self): return self.first_name + ' ' + self.last_name

def apply_raise(self): self.salary = int(self.salary * self.raise_amount)

employee1 = Employee('Ardeshir','Badr',50000) employee2 = Employee('Adam', 'Adrian', 60000)

print(Employee.__dict__)

{'__module__': '__main__', 'raise_amount': 1.04, '__init__': <function Employee.__init__ at 0x000002E60D63EE18>, 'full_name': <function Employee.full_name at 0x000002E60D63EEA0>, 'apply_raise': <function Employee.apply_raise at 0x000002E60D63EF28>, '__dict__': <attribute '__dict__' of 'Employee' objects>, '__weakref__': <attribute '__weakref__' of 'Employee' objects>, '__doc__': None}

Object Oriented Programming in Python #The class variable num_employees can be incremented as more employees are created class Employee:

num_employees = 0 raise_amount = 1.04

def __init__(self, first_name, last_name, salary): self.first_name = first_name self.last_name = last_name self.salary = salary self.email = first_name + '.' + last_name + '@education.com'

Employee.num_employees += 1

def full_name(self): return self.first_name + ' ' + self.last_name

def apply_raise(self): self.salary = int(self.salary * self.raise_amount)

print(Employee.num_employees) employee1 = Employee('Ardeshir','Badr',50000) employee2 = Employee('Adam', 'Adrian', 60000) print(Employee.num_employees)

0 2

Object Oriented Programming in Python #Class methods are indicated by @classmethod and can be used for setting class variables. Class variables are the same for all instances of that class. class Employee:

num_employees = 0 raise_amount = 1.04

def __init__(self, first_name, last_name, salary): self.first_name = first_name self.last_name = last_name self.salary = salary self.email = first_name + '.' + last_name + '@education.com'

Employee.num_employees += 1

def full_name(self): return self.first_name + ' ' + self.last_name

def apply_raise(self): self.salary = int(self.salary * self.raise_amount)

@classmethod def set_raise_amount(cls, amount):

cls.raise_amount = amount

employee1 = Employee('Ardeshir','Badr',50000) employee2 = Employee('Adam', 'Adrian', 60000)

Employee.set_raise_amount(1.07)

print(Employee.raise_amount) print(employee1.raise_amount) print(employee2.raise_amount)

1.07 1.07 1.07

Object Oriented Programming in Python #Static methods act like functions, they do not have any dependencies on the class, but have some logical relationship with the class, for example, no self needed as first arg. class Employee:

num_employees = 0 raise_amount = 1.04

def __init__(self, first_name, last_name, salary): self.first_name = first_name self.last_name = last_name self.salary = salary self.email = first_name + '.' + last_name + '@education.com'

Employee.num_employees += 1

def full_name(self): return self.first_name + ' ' + self.last_name

def apply_raise(self): self.salary = int(self.salary * self.raise_amount)

@classmethod def set_raise_amount(cls, amount):

cls.raise_amount = amount

@staticmethod def is_working_day(day):

if day.weekday() == 5 or day.weekday() == 6: return False

return True

import datetime my_date = datetime.date(2018,4,30) print(Employee.is_working_day(my_date)

True

Object Oriented Programming in Python #Inheritance allows us to inherit attributes and methods from the parent class. We can create sub classes with all attributes # and methods of parent class and add new attributes and methods without affecting the parent class. Now we want to # create/inherit developers and managers from Employee

class Developer(Employee): pass

developer_1 = Developer('Adrian','Badr', 67000) developer_2 = Developer('Adam','Badr', 78000)

print(developer_1.email)

[email protected]

Object Oriented Programming in Python #Now if we print the help on Developer class, we will see the details of inherited attributes and methods

print(help(Developer))

class Developer(Employee) | Method resolution order: | Developer | Employee | builtins.object | | Methods inherited from Employee: | | __init__(self, first_name, last_name, salary) | Initialize self. See help(type(self)) for accurate signature. | | apply_raise(self) | | full_name(self) | | ---------------------------------------------------------------------- | Class methods inherited from Employee: | | set_raise_amount(amount) from builtins.type | | ---------------------------------------------------------------------- | Static methods inherited from Employee: | | is_working_day(day)

| ---------------------------------------------------------------------- | Data descriptors inherited from Employee: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) | | ---------------------------------------------------------------------- | Data and other attributes inherited from Employee: | | num_employees = 2 | | raise_amount = 1.04

Object Oriented Programming in Python

#Changes to sub class does not affect parent class #To add a new parameter to dev constructor, we create a new __init__ and use supper().__init__

class Developer(Employee): raise_amt = 1.10

def __init__(self, first_name, last_name, salary, programming_lang): super().__init__(first_name, last_name, salary) self.programmimg_lang = programming_lang

developer_1 = Developer('Adrian','Badr', 67000, 'Python') developer_2 = Developer('Adam','Badr', 78000, 'Java')

print(developer_1.email) print(developer_1.programmimg_lang)

[email protected] Python

Object Oriented Programming in Python #We inherit a new class from employee called Manager. We add a new attribute that has the list of employees that manager manages class Manager(Employee):

def __init__(self, first_name, last_name, salary, employees=None): super().__init__(first_name, last_name, salary) if employees is None:

self.employees = [] else:

self.employees = employees

def add_employee(self, employee): if employee not in self.employees:

self.employees.append(employee)

def remove_employee(self, employee): if employee in self.employees:

self.employees.remove(employee)

def print_employees(self): for emp in self.employees:

print('--->', emp.full_name())

developer_1 = Developer('Adrian','Badr', 67000, 'Python') developer_2 = Developer('Adam','Badr', 78000, 'Java')

manager_1 = Manager('Marie', 'Well', 98000, [developer_1]) print(manager_1.email) print(manager_1.print_employees())

[email protected] ---> Adrian Badr

Object Oriented Programming in Python #Adding a new employee to the list of employees managed by manager 1 class Manager(Employee):

def __init__(self, first_name, last_name, salary, employees=None): super().__init__(first_name, last_name, salary) if employees is None:

self.employees = [] else:

self.employees = employees

def add_employee(self, employee): if employee not in self.employees:

self.employees.append(employee)

def remove_employee(self, employee): if employee in self.employees:

self.employees.remove(employee)

def print_employees(self): for emp in self.employees:

print('--->', emp.full_name())

developer_1 = Developer('Adrian','Badr', 67000, 'Python') developer_2 = Developer('Adam','Badr', 78000, 'Java')

manager_1 = Manager('Marie', 'Well', 98000, [developer_1]) manager_1.add_employee(developer_2) print(manager_1.print_employees()) ---> Adrian Badr

---> Adam Badr

Object Oriented Programming in Python #We can use isinstance to check if an instance is inherited from a class class Manager(Employee):

def __init__(self, first_name, last_name, salary, employees=None): super().__init__(first_name, last_name, salary) if employees is None:

self.employees = [] else:

self.employees = employees

def add_employee(self, employee): if employee not in self.employees:

self.employees.append(employee)

def remove_employee(self, employee): if employee in self.employees:

self.employees.remove(employee)

def print_employees(self): for emp in self.employees:

print('--->', emp.full_name())

developer_1 = Developer('Adrian','Badr', 67000, 'Python') developer_2 = Developer('Adam','Badr', 78000, 'Java')

manager_1 = Manager('Marie', 'Well', 98000, [developer_1])

print(isinstance(manager_1, Manager)) True

Object Oriented Programming in Python #We can use issubclass to verify if a class is a subclass of another class class Manager(Employee):

def __init__(self, first_name, last_name, salary, employees=None): super().__init__(first_name, last_name, salary) if employees is None:

self.employees = [] else:

self.employees = employees

def add_employee(self, employee): if employee not in self.employees:

self.employees.append(employee)

def remove_employee(self, employee): if employee in self.employees:

self.employees.remove(employee)

def print_employees(self): for emp in self.employees:

print('--->', emp.full_name())

developer_1 = Developer('Adrian','Badr', 67000, 'Python') developer_2 = Developer('Adam','Badr', 78000, 'Java')

manager_1 = Manager('Marie', 'Well', 98000, [developer_1])

print(issubclass(Manager, Developer)) False

• Develop a class diagram for graduate and undergraduate students billing system. The model calculates the tuition a student has to pay based on tuition per credit hour, the course taken. The tuition per credit hour depends on whether the student is a graduate or undergraduate. The tuition per credit hour for all graduate students is $300.00 and for all under graduate students is $250.00.

Activity

 Go through the slides of this class plus the notes you have taken

 Go through the exercises, discussions, and examples

 Read the slides for next class (will be available one day prior to next class)

 Continue working on your project

 Continue working on your assignments

 Work on your presentation

Before next class