12-1 chapter 12 object database standards, languages, and design
Post on 13-Jan-2016
221 Views
Preview:
TRANSCRIPT
12-1
Chapter 12
Object Database Standards,
Languages, and Design
12-2
Standards• advantages
– portability: the capability to execute a particular application program on different systems with minimal modifications to the program itself
– interoperability: the ability of an application to access multiple distinct systems
• ODMG (Object Data Management Group)– object model– object definition language (ODL)– object query language (OQL)– bindings to object-oriented languages
12-3
Overview of the Object Model of ODMG
• object vs. literal– object: object identifier and state (current value)– literal: no object identifier and constant value
• object– identifier: unique system-wide identifier (Object_Id)– name: entry points (e.g., extents)– lifetime: persistent object vs. transient object– structure: how an object is constructed by using the type
constructors
12-4
Overview of the Object Model of ODMG
• literal– atomic
• values of basic data types & predefined• Long, Short, Unsigned Long, Unsigned Short, Float,
Double, Boolean, Char, String, Enum, ...
– structured• values constructed using the tuple constructor• built-in structure: Date, Interval, Time, Timestamp• user-defined type structures using Struct
– collection• Set<t>, Bag<t>, List<t>, Array<t>, Dictionary<k,v>
where t is type of objects or values in the collection
12-5
Object
Iterator Collection Date Time Interval
Timestamp
Set List Bag Array Dictionary
built-in collection types of the object model
12-6
Interface Object { basic operations inherited by all objects … boolean same_as(in Object other_object); compare object ID to another, e.g., o.same_as(p) Object copy(); create a new copy of the object, e.g., p=o.copy() void delete(); delete the object};
Interface Date: Object { inherit basic Object interface enum Weekday {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; enum Month {January, February, March, April, May, June, July,August, September, October, November, December,}; unsigned short year(); unsigned short month(); unsigned short day(); … boolean is_equal(in Date other_Date); boolean is_greater(in Date other_date);…};
type inheritance
12-7
Overview of the interface definitions for part of the ODMG object model
interface Time: Object{ inherit basic Object interface … unsigned short hour(); unsigned short minute(); unsigned short second(); unsigned short millisecond(); … boolean is_equal(in Time other_ Time); boolean is_greater(in Time other_Time); … Time add_interval(in Interval some_Interval); Time subtract_interval(in Interval some_Interval); Interval subtract_time(in Time other_Time);}
12-8
interface Timestamp: Object { inherit basic Object interface…unsigned short year();unsigned short month();unsigned short day();unsigned short hour();unsigned short minute();unsigned short second();unsigned short millisecond();…Timestamp plus(in Interval some_Interval);Timestamp minus(in Interval some_Interval);boolean is_equal(in Timestamp other_timestamp);boolean is_greater(in Timestamp other_Timestamp);…
}interface Interval: Object { inherit basic Object interface
unsigned short day();unsigned short hour();unsigned short minute();unsigned short second();unsigned short millisecond();…
12-9
interval plus(in Interval some_Interval);interval minus(in Interval some_Interval);
interval product(in long some_value);interval quotient(in long some_value);boolean is_equal(in Interval other_interval);boolean is_greater(in Interval other_interval);…
}
interface Collection: Object { inherit basic Object interface…exception ElementNotFound{any element;}; o.remove_element(e) if e is not in ounsigned long cardinality(); o.cardnality() return # of element in collection oboolean is_empty(); o.is_empty()… true if the collection is empty and false otherwise
boolean contains_element(in any element); o.insert_element(e)void insert_element(in any element); insert an element e into o void remove_element(in any element) remove an element e from o
raises(ElementNotFound);Iterator create_iterator(in boolean stable); i= o.create_iterator()
… create an iterator object i}; for the collection o
12-10
Interface Iterator {exception NoMoreElements(); no more element in i.next_position()…boolean is_stable();boolean at_end();void reset(); i.reset() sets the iterator at 1st element in a collectionany get_element() raises(NoMoreElements); i.get_element retrievesvoid next_position() raises(NoMoreElements); current element… i.next_position() set the iterator to the next element
}
Interface Set: Collection { Set<t>: a set whose elements are of type tSet create_union(in Set other_set); p=o.create_union(s)… p of type Set<t>: union of o and sboolean is_subset_of(in Set other_set); o.is_subset_of(s)… o is a subset of some other set s
}
Interface Bag: Collection { allow duplicate elements in the collectionunsigned long occurrences_of(in any element); o.occurrences_of(e): # of duplicateBag create_union(in Bag other_bag); occurrences of element e in bag o…
};
Bag<t>
12-11
Interface List: Collection { the order of the elements is importantexception Invalid_Index{unsigned_long index;};void remove_element_at(in unsigned long position)
raises(InvalidIndex);any retrieve_element_at(in unsigned long position)
raises(InvalidIndex);void replace_element_at(in any element, in unsigned long position)
raises(InvalidIndex);void insert_element_after(in any element, in unsigned long position)
raises(InvalidIndex); o.insert_element_after(e,i)… insert the element e after the ith element in the list ovoid insert_element_first(in any element); o.insert_element_first(e)… insert the element e before the first element in the list ovoid remove_first_element() raises(InvalidIndex);… e=o.remove_first_element() removes and return the 1st elementany retrieve_first_element() raises(InvalidIndex);… e=o.retrieve_first_element retrieves the 1st element form oList concat(in List other_list); p=o.concat(l) creates p that isvoid append(in List other_list); the concatenations of the lists o and l
}; o.append(i) appends the elements of list i to the end of list o
List<t>
12-12
interface Array: Collection { a fixed number of elementsexception Invalid_Index{unsigned_long index;};void remove_element_at(in unsigned long index)
raises(InvalidIndex);any retrieve_element_at(in unsigned long index)
raises(InvalidIndex);void replace_element_at(in unsigned long index, in any element)
raises(InvalidIndex);void resize(in unsigned long new_size); o.resize(n)
}; change # of array elements in o to n
struct Association {any key; any value;};
interface Dictionary: Collection { create pairs <k, v> where all k (key) values are uniqueexception KeyNotFound{any key;};void bind(in any key, in any value); bind value v to key k in o.void unbind(in any key) raises(KeyNotFound); remove key k from oany lookup(in any key) raises(KeyNotFound); return v with k in oboolean contains_key(in any key);
};
Array<t>
12-13
Atomic (User-Defined) Objects
• atomic object– any user-defined object that is not a collection object– specified by the keyword class– structured object defined as a class by specifying
• attributes: a property that describes some aspect of an object
• relationships: a property that specifies two objects are related together
• operations: operation signatures specify the operation name, its argument types, and its returned value
properties value (1) literal(simple or complex structure)value (1) literal(simple or complex structure) (2) object_id (2) object_id (3) calculated by methods (3) calculated by methods
12-14
class Employee( extent all_employees key ssn ){ attribute string name; attribute string ssn; attribute date birthdate; attribute enum Gender{M,F} sex;
attribute short age; relationship Department works_for inverse Department::has_emps;
void reassign_emp(in string new_dname) raises(dname_not_valid);};
Set<Employee>:Set<Employee>: contain all persistent contain all persistent objects of Employee objects of Employee
simple structuressimple structures
1. Binary relationship1. Binary relationship2. A pair of inverse 2. A pair of inverse references references
Maintain the referentialMaintain the referentialintegrity automaticallyintegrity automatically
12-15
class Department( extent all_department key dname, dnumber ){
attribute string dname;attribute short dnumber;attribute struct Dept_Mgr{Employee manager, date startdate}
mgr;attribute set<string> locations;attribute struct Projs {string projname, time weekly_hours}
proj;relationship set <Employee> has_emps
inverse Employee::works_for;void add_emp(in string new_ename) raises (ename_not_valid); void change_manager(in string new_mgr_name; in date startdate);
};
Set<Department>:Set<Department>: contain all persistent objects contain all persistent objects of Department of Department
complex structureRepresented relationship in
only one direction
Inverse reference: maintain referential integrity automatically
12-16
Interfaces and Classes• behavior: operations• state: properties (attributes and relationships)• interface
– a specification of the abstract behavior– noninstantiable: one cannot create objects that correspond to an
interface definition
• class– a specification of both abstract behavior and abstract state– instantiable: one can create individual object instances
corresponding to a class definition
12-17
Inheritance
• behavior inheritance (: symbol)
– abstract operations inherited by class or other interfaces
– multiple inheritance is allowed
• EXTENDS (extends keyword)– inherit both state and behavior among classes– multiple inheritance is not permitted
(1) supertype is an interface(1) supertype is an interface(2) subtype is a class or another interface(2) subtype is a class or another interface
state properties (attributes and relationships)state properties (attributes and relationships)
Both supertype and Both supertype and subtype must be classessubtype must be classes
12-18
Extents• a set object that holds all persistent objects of the class• examples
• enforce the set/subset relationship between the extents of a supertype and its subtype
– if A and B have extents all_A and all_B, andB is a subtype of A, then the collection of objects in all_B must be a subset of those in all_A
class Employee( extent all_employees key ssn )
class Department( extent all_department key dname, dnumber )
Set<A> Set<B>
Composite key: (state, license_number)Composite key: (state, license_number)
12-19
Keys
• one or more properties whose values are constrained to be unique for each object in the extent– may have more than one key– may be a composite key, e.g., (state, license_number)
class Employee( extent all_employees key ssn )
class Department( extent all_department key dname, dnumber )
12-20
factory object
interface ObjectFactory { Object new(); return a new object with an Object_Id};
interface DateFactory: ObjectFactory { exception InvalidDate{}; … Date calendar_date( in unsigned short year, in unsigned short month, in unsigned short day) raises(InvalidDate); … Date current();};
an object that can be used to generate or create individual objects via its operations provide the constructor operations for new objects
12-21
interface DatabaseFactory { Database new();};
interface Database { void open(in string database_name); void close(); void bind(in any some_object, in string object_name); Object unbind(in string name); Object lookup(in string object_name) raises(elementNotFound); …};
Database
Different databases with their own schemasinterfaces for DatabaseFactory and Database objects
Each database has its own database nameAssign individual unique names
to persistent objects in a particular database
Remove the name of a persistent named object
Return an object from the database that has the specifiedobject_name
12-22
Object Definition Language
• Support the semantic constructs of the ODMG 2.0 object model
• independent of any particular programming language
• create classes and interfaces
12-23
Graphical notation for representing ODL schemas
Person-IF
Student
Interface
Class
relationships
inheritance
1:1
1:N
M:N
Interface(is-a)inheritanceusing “:”
Classinheritanceusing extends
objectspecification
12-24
A graphical object database schema for UNIVERSITY database
Person Department
Course
Section
CurrSection
Student
Gradstudent
Faculty
registered_students
committee
advisor
advises
In_committee_of
Work_in
Has_majorsHas_faculty
offers
Offered_by
Has_sections
students
Of_course
Major_in
Completed_sections
Registered_in
12-25
class Person( extent persons key ssn ){ attribute struct Pname {string fname, string mname, string lname}
name; attribute string ssn; attribute date birthdate; attribute enum Gender{M,F}sex; attribute struct Address {short no, string street, short aptno, string city, string state, short zip}
address; short age();};class Faculty extends Person( extent faculty){ attribute struct rank; attribute float salary; attribute string office; attribute string phone; relationship Department works_in inverse Department::has_faculty; relationship set<GradStudent> advises inverse GradStudent::advisor; relationship set<GradSection> on_committee_of inverse GradStudent::advisor; void give_raise(in float raise); void promote(in string new_rank);}
attributes
operation
attributes
relationship
properties
• The collection of faculty is constrained to be a subset of the collection of Persons• Individual faculty object inherits properties & operations of Person
n-side
n-side
operations
12-26
class Grade M:N relationship is i.t.o. 2 1:N relationships( extent grade ){ attribute enum GradeValues{A,B,C,D,F,I,P} grade; relationship Section section inverse Section:: students; relationship Student student inverse Student :: completed_sections;
class Student Extends Person( extent students ){ attribute string class; attribute Department minors_in; relationship Department majors_in inverse Department::has_major; relationship set<Grade> completed_sections inverse Grade::student; relationship set<CurrSection > registered_in inverse CurrSection::registered_students; void change_major(in string dname) raises(dname_not_valid); float gpa(); void register(in short secno) raises (section_not_valid); void assign_grade(in short secno; in GradeValue grade) raises(section_not_valid, Grade_not_valid);
• The collection of students is constrained to be a subset of the collection of persons• Individual student object inherits properties & operations of Person
undirectional relationship i.t.o attribute
n-side
1-side
12-27
class degree{ attribute string college; attribute string degree; attribute string year;};class GradStudent extends Student( extent grad_students ){ attribute set<Degree> degrees; relationship Faculty advisor inverse Faculty:: advises; relationship set<Faculty> committee inverse Faculty::on_committee_of; void assign_advisor(in string Iname; in string fname) raises(faculty_not_valid); void assign_committee_member(in string Iname; In string fname) raises (faculty_not_valid);}; class Department( extent departments key dname ){ attribute string dname; attribute string dphone; attinbute string doffice; attribute string college; attribute Faculty chair; relationship set<Faculty> has_faculty inverse Faculty::works_in;
• The collection of grad_students is constrained to be a subset of the collection of students• Individual Gradstudent inherits those of student
12-28
relationship set<Student> has_majors inverse Student::majors_in;relationship set<Course> offers inverse Course::offered_by;};class Course( extent department key cno ){ attribute string cname; attribute string cno; attribute string description; relationship set<Section>has_sections inverse Section::of_course; relationship Department offered_by inverse Department::offers;};class Section( extent sections ){ attribute short secno; attribute string year; attribute enum Quarter {Fall, Winter, Spring, Summer}qtr; relationship set<Grade> students inverse Grade::section; relationship Course of_course inverse Course::has_sections;};class CurrSection extends Section( extent current_sections ){relationship set<Student>registered_students inverse Student::registered_in void register_student(in string ssn) raises(student_not_valid, section_not_valid, section_full);};
12-29
Graphical schema for geometric objects
GeometryObject
Triangle CircleTriangle ...
interface GeometryObject{
attribute enum Shape{Rectangle,Triangle, Circle,…}shape; attribute struct Point {short x, short y} reference_point; float perimeter(); float area(); void translate(in short x_translation; in short y_translation); void rotate(in float angle_of_rotation);};
noninstantiable:noninstantiable: no objects can be createdno objects can be created
only operations are inherited, not propertiesonly operations are inherited, not properties
12-30
class Rectangle: GeometryObject( extent rectangles){ attribute struct Point{short x, short y} reference_point; attribute short length; attribute short height; attribute float orientation_angle;};
class Triangle: GeometryObject( extent triangles){ attribute struct Point{short x, short y} reference_point; attribute short side_1; attribute short side_2; attribute float side1_side2_angle; attribute float side1_orientation_angle;};
class Circle: GeometryObject( extent circles){ attribute struct Point {short x, short y} reference_point; attribute short radius;};…
12-31
Object Query Language
• basic OQL syntax– select … from … where …– Retrieve the names of all departments in the
college of ‘Engineering’
Q0: SELECT d.dname FROM d in departments WHERE d.college = ‘Engineering’;
How to refer to a persistent object?Entry point (named persistent object; or name of the extent of a class)
iterator variable
extent name d in departmentsdepartments ddepartments as d
12-32
Query Results and Path Expressions• Q1: departments; persistent name as a query
– return a reference to the collection of all persistent department objects, whose type is set<Department>
• Q1a: csdepartment;– assume a persistent name csdepartment denotes a
single department object– return a reference to that individual object of type
Department
• path expression – Q2: csdepartment.chair;– Q2a: csdepartment.chair.rank;– Q2b: csdepartment.has_faculty;
Single values
set collection
Persistent object name‧ attribute namerelationship name
12-33
• Return the ranks of computer science faculty– Q3’: csdepartment.has_faculty.rank; ()– Q3a: select f.rank
from f in csdepartment.has_faculty;– Q3b: select distinct f.rank
from f in csdepartment.has_faculty;
• Return the collection of graduate students that are advised by the chair of the computer science department– Q4: csdepartment.chair.advises;– Q4a: select struct (name:struct (last_name: s.name.lname,
first_name: s. name.fname), degrees:(select struct (deg: d.degree, yr: d.year, college: d.college) from d in s.degrees)) from s in csdepartment.chair.advises;
bag
set
Retrieve the last and first namesof graduate students
complex structure
Set<Gradstudent>Set<Gradstudent>
12-34
OQL is orthogonal with respect to specifying path expressions
Retrieve the grade point average of all senior students majoring in computer science, with the result ordered by gpa, and within that bylast and first name
Q5a: select struct (last_name: s.name. lname, first_name: s.name. fname, gpa: s.gpa) from s in csdepartment.has_majors where s.class=‘senior’ order by gpa desc, last_name asc, first_name asc;
Q5b: select struct (last_name: s.name. lname, first_name: s.name.fname, gpa: s. gpa) from s in students where s.majors_in.dname =‘Computer science’ and s.class =‘senior’ order by gpa desc, last_name asc, first_name asc;
Attributes, relationships and operation names can be used interchangeably
attribute
operation
relationshipattributeattribute
operationrelationship
attribute
12-35
Specifying Views as Named Queries
• Retrieve the set of objects for students minoring in a given department
• Return a bag of students minoring in the Computer Science department
V1: define has_minors (deptname) as select s from s in students where s.minors_in.dname = deptname;
has_minors(‘Computer Science’);
(regard as a view)define: specify an identifier of named query
name argument
attribute as a undirectional relationship
Infrequentinverserelationship(12-26)
12-36
Extracting Single Elements from Singleton Collections
• Return the single object reference to the computer science department
• type of the result
Q6: element (select d from d in departments where d.dname = ‘computer Science’);
d:Department
Return a single element from a singleton collection raise exception when ψ>1
12-37
Collection Operators (Aggregate Functions)
• aggregate operators: min, max, count, sum, avg
• Return the number of students minoring in ‘Computer Science’
• Return the average gpa of all seniors majoring in computer science
Q7: count (s in has_minors(‘Computer Science’));
Q8: avg (select s.gpa from s in students where s.majors_in.dname = ‘Computer Science’ and s.class = ‘senior’);
12-38
• Retrieve all department names that have more than 100 majors
Q9: select d.dname from d in departments where count (d.has_majors) > 100;
12-39
Collection Operators (Quantifiers)• (e in c)
– return true if element e is a member of collection c
• (for all v in c: b)– return true if all the elements of collection c satisfying b
• (exists v in c: b)– return true if there is at least one element in c satisfying b
• Retrieve the names of all students who completed the course called ‘Database Systems’
Q10: select s.name.lname, s.name.fname from s in students where ‘Database Systems’ in (select c.cname from c in s.completed_sections.section.of_course);
type of result: bag<struct(string, string)>
12-40
• Is Jeremy a computer science minor?
• Are all computer science graduate students advised by computer science faculty?
• Does any graduate computer science major have a 4.0 gpa?
Q11: Jeremy in has_minors(‘Computer Science’);
Q12: for all g in (select s from s in grad_students where s.majors_in.dname = ‘Computer Science’) : g.advisor in csdepartment.has_faculty;
Q13: exists g in ( select s from s in grad_students where s.major_in.dname =‘Computer Science’) : g.gpa = 4;
12-41
Ordered (Indexed) Collection Expressions
• Retrieve the last name of the faculty member who earns the highest salary?
• Retrieve the top three computer science majors based on gpa?
Q14: first (select struct(faculty: f.name.lname, salary: f.salary) from f in faculty order by f.salary desc);
Q15:(select struct (last_name: s.name.lname, first_name: s.name.fname, gpa: s.gpa) from s in csdepartment.has_majors order by gpa desc) [0:2];
list
return 1st,2nd,3rd elements
12-42
The Grouping Operator
• Retrieve the number of majors in each department
• Retrieve for each department having more than 100 majors, the average gpa of its majors
Q16: select struct (deptname, number_of_majors: count (partition)) from s in students group by deptname: s.majors_in.dname;
Q17: select deptname, avg_gpa: avg (select p.s.gpa from p in partition) from s in students group by deptname: s.major_in.dname having count (partition) > 100;
refer to each partition
12-43
Object Database Conceptual Design
• differences between ODB and RDB– representation of relationships
• ODB: relationship properties or reference attributes
• RDB: attributes with matching values, e.g., foreign keys
– inheritance• ODB: built-in into model, e.g., derived(:) and EXTENDS
• RDB: no built-in constructs
– specification of operations• ODB: operations are part of the class specifications
• RDB: implementation phase
ODB: any kind of structures ODA: m×n→RDB: atomic values RDB: m×n→relation
12-44
Mapping an EER Schema to an ODB Schema
Step 1: entity type or subclasses Create an ODL class for each EER entity type or subclass.
(1) multivalued attributes list constructor: attribute values are ordered
bag constructor: duplicates are allowedset constructor: otherwise
(2) composite attributes - using tuple constructors(3) specify key attributes as keys of extent
(4) Declare an extent for each class and specify any key attributes
PERSON
Sex
Name
Ssn BDate
Address
FName Minit LName No Street AptNo City State Zip
12-45
class Person( extent persons key ssn ){ attribute struct Pname {string fname, string mname, string lname} name; attribute string ssn; attribute date birthdate; attribute enum Gender {M, F} sex; attribute struct Address {short no, string street, short aptno, string city, string state, short zip} address; short age( );};
Mapping an EER Schema to an ODB Schema (Cont.)
12-46
Step 2: relationship(1) binary relationship Add relationship properties or reference attributes into ODL classes
that participate in the relationship (a) references in both direction
relationship properties that are inverses of one another (b) references in only one direction attribute in the referencing class whose type is referenced class name
(2) cardinality ratio (a) 1:1 or N:1 directions the relationship properties or reference attributes are single-valued (b) 1:N or M:N directions the relationship properties or reference attributes are set-valued or list-valued
FACULTY
BELONGS ADVISOR COMMITTEE
GRAD_STUDENTDEPARTMENT
m
1
1
n
m
n
class Faculty extends Person( extent faculty){ attribute string … ; relationship Department works_in inverse Department::has_faculty; relationship set<GradStudent> advises inverse GradStudent::advisor; relationship set<GradSection> on_committee_of inverse GradStudent::advisor;
12-47
STUDENT
REGISTERED TRANSCRIPTGrade
N
M
MAJOR
DEPARTMENT
CURRENT_SECTION
SECTION
m
n
m
1
class Student Extends Person( extent students ){ attribute string class; attribute Department minors_in; relationship Department majors_in inverse Department::has_major; relationship set<Grade> completed_sections inverse Grade::student; relationship set<CurrSection > registered_in inverse CurrSection::registered_students;…...
12-48
Step 3: Include appropriate operations for each class.(a) constructor method: check constraints that must hold when a new object
is created.(b) destructor method: check any constraints that may be violated when an
object is deleted. (c) …Step 4: An ODL class that corresponds to a subclass
===> EXTENDS
Step 5: weak entity type===> mapped the same way as regular entity type===> if one one relationship composite multivalued attributes of the owner entity type set<struct<…>> or list<struct<…>>
step 6: categories (union types)===> declare a class to represent the category and define 1:1 relationships
between the category and each of its superclass
step 7: n-ary relationship (n>2)
===> mapped into a separate class with appropriate references to each participating class
Specific attributes, relationship references, and operations are specified (1)-(3)Specific attributes, relationship references, and operations are specified (1)-(3)
12-49
Class SUPPLY(extent SUPPLIES key (SName, ProjName, PartNo)){ … attribute short quantity … relationship set <SUPPLIER> R11 inverse SUPPLIER::R12; relationship set <PART> R12 inverse PART::R22; relationship set <PROJECT> R31 inverse PROJECT::R32;}…
12-50
WORKS_ONEMPLOYEE PROJECT
Hours
An m:n binary relationship containing relationship attributesAn m:n binary relationship containing relationship attributes
NumberSSN …………
Class WORKS_ON(extent WORKS_ONS key (SSN, Number){ … attribute short Hours … relationship set <EMPLOYEE> WE inverse EMPLOYEE::EW; relationship set <PROJECT> WP inverse PROJECT::PW; …};
12-51
Examples of ODBMSs
• O2 system (Ardent)– data definition: C++ or JAVA language bindings– data manipulation: C++ (or JAVA) O2 bindings
• ObjectStore
12-52
Struct Ename { d_String fname; d_String mname; d_String Iname;};
struct Address { d_UShort no; d_String street; d_UShort aptno; d_String city; d_String state; d_Ushort zip;};
class Person:public d_Object{public://attributes Ename ename; d_String ssn; d_date birthdate; enum Gender{M,F} sex Address address;//Operations Person(const char * pname); d_Ushort age;//Extent
static d_Set<d_Ref<Person>> persons;static const char* const extent_name;};
class Faculty:public Person {public://Attributes d_String rank; d_Float salary; d_String office; d_String phone;//Relationship(syntax is ODMG 1.1 compliant) d_Ref<Department> works_in inverse Department::has_faculty; d_Set<d_Ref<GradStudent>> advises inverse Gradstudent::advisor; d_Set<d-Ref<GradStudent>> on_committee_of inverse GradStudent::committee;
//Operations Faculty(const char *fname, d_Float salary); void give_raise(in d_Float raise); void promote(in d_String new_rank);// Extentstatic d_Set<d_Ref<Faculty>> faculty;static const char* const extent_name};
Figure 12.8a Example illustrating O2.Defining part of the UNIVERSITY schema using the C++ O2 binding.
12-53
//Faculty Class
const char* const Faculty::extent_name= “faculty”;
//Faculty constructor hereFaculty::Faculty (const char* fname, d_Float fsalary): Person(fname){ salary=fsalary; //Put this new faculty into the extension faculty->insert_element(this);}
void Faculty::give_raise (d_Float raise);{ salary +=raise;}
void Faculty::promote(d_String new_rank);{ rank=new_rank;}
Figure 12.8b Example illustratingO2.c++ O2 schema implementation for the Faculty class.
12-54
struct Phone { int area_code; int number;}
struct Date{ int year int month int dya;}
class Person {public: char ssn[9]; struct { char* firstname; char*middlename; char*lastname; } name; struct { int number char*street; char*apt_no; char*city; char*state;
/* this is the file univ_schema.H this includes the database class declarations */ char*zipcode; }address; Date birthdate; char sex;
int age ();}
struct Transcript { char grade; float ngrade; Section *Section;}
class Student:public Person { /*student inherits (is derived form Person )*/public: char* class; Department *majors_in; Department *minor_in; os_Set<Section*> registered_in; os_Set<Transcript*> transcript;
float grade_point_average(); void change_class (); void change_major (Department *new_major);}
Figure 12.9 ObjectStoreC++ class declarations for part of the UNIVERSITY database.
12-55
Struct Degree { char* college; char* degree; int year;}
class Grad_student: public Student { /* Grad_student inherits (is derived form ) Student */ public: os_Set<Degree*> degrees; Faculty *advisor;}
class Faculty: public person { /*Faculty ingerits (is derived form) Person */ float salary; char* rank; char* foffice; char* fphone; os_Set<Department*> belongs_to; Department *chairs os_Set<Grand*> grants; os_Set<Student*> advises;} class Department { char* dname; char* office; Phone dphone; os_Set<Faculty> memebers; os_Set<Student> majors; Faculty chairperson; os_Set<Couse*> courses;
void add_major (Student *s); void remove_major(Student *s);}
Figure 12.9(continued)
12-56
extern database *univ_db;
class Person;
class Faculty: public Person { /* Faculty inherits (is derived from) person */ float salary; char*rank; char* foffice; char* fphone; os_relationship_m_m(Faculty,belongs_to, department, members,os_Set<department*>), os_relationship_1_1(Faculty, chairs, Department, chairperson, Department*); os_Set<Grant*> grants; os_Set<Student*> advises; Faculty(char s[9]){ssn=new(univ_db)char[9];strcpy(ssn,s);} /* the ssn attribute is inherited form Person */ void promote_faculty (); void give_raise (float percent); } class department { char* dname; char* office; Phone dphone; os_realtionship_m_m(Department, members. Faculty, belongs_to, os_Set<Faculty*>); os_Set<Student*> majors; os_relationship_1_1(Department, chairperson, Faculty, chairs,Faculty*); os_Set<course*>courses;
12-57
Department(char* d){dname=new (univ_db)char[strlen(d)+1];strcpy(dname, d);} void add_faculty (Faculty *f); void add_mojor (Student *s); int remove_major(Student *s); os_rel_m_m_body(Faculty, belongs_to, Department, members); os_rel_m_m_body(Department, members, Faculty, belong_to); os_rel_1_1_body(Faculty,chair, department, chairperson); os_rel_1_1_body(Department, chairperson, Faculty, chairs);}
Figure 12.10 Declaring inverse relationships in ObjectStore c++
12-58
(a)int Person::age ( ){ Date d=today (); int a=d.year-birthdate.year; if (d.month<birthdate.month> II (d.month== birthdate.month)&&d.day<birthdate.day>))--a; return a;};
float Student::grade_point_average (){ float sum=0.0; int count = 0; Transcript* t; os_Cursor<Transcript*>cur(transcript); for(t=cur.first ();cur.more();t=cur.next sum +=t->ngrade; ++count; /*increments sum by ngrade, count by 1 */ return sum/count;};
void Student::change_major(Department *new_major){majors_in=new_major;}void Department::remove_major (Student *s)(majors->remove(s);}void Department::add_faculty (Faculty *f){members-> insert(f);}void Department::add_major(Student* s){mojors->inserts(s);}
Figure12.11 ObjectStore c++ programming.(a)Code for some of the member functions (operations) in Figure 12.9.
12-59
(b)main (){ database *univ_db=database::open(“/database/univ”);
transaction::begin();/* create two persistent sets to hold all Faculty and all Department objects * os_Set<Faculty*> &all_faculty =os_Set<Faculty*>::create(univ_db); os_Set<Department*> &all_depts=os_Set<Department*>::create(univ_db);
/*create a new faculty object and a new Department object using the constructors */ Faculty *f=new(univ_db)Faculty(“123456789”); Department *d=new (univ_db)Department (“Computer Science”);/*relate the objects by invoking the appropriate methods */ d->add_faculty (f);/*add the objects to their persistent sets */ all_faculty.insert(f);all_dept.insert(d);
transaction::commit();}
Figure 12.11 (b) Creating persistent object.
12-60
os_Set <Faculty*> &asst_profs = all_faculty-> query(“Faculty*”,”!strcmp(rank,\”Assistant Professor\”)”,db);os_Set <Faculty*> &rich_asst_prof= all_faculty-> query(“Faculty*”,”salary>5000.00”,db);os_Set<Faculty*> &dept_chairs = all_faculty-> query (“Faculty*”,”chairs !=NULL,db”);os_Set<Faculty*> &cs_faculty= all_faculty->query (“Faculty*”, “belong_to[:dname = =“Computer Science\”]”,db);
Figure 12.12 Queries in ObjectStore C++.
12-61
Client TargetObject
IDL Stubs
DIIIDL Skeleton DSI Object
Adapter*
Object Request Broker
ORBInterface
*Really the Basic Object Adapter
Figure 12.13 The CORBA 2.0 ORB Architecture. Pearson Education Ltd. From CORBA Distributed Objects by Sean Baker © 1997
Addison Wesley Longman.(p,429)
Control Control
12-62
Application Object
Application Object
CORBA Services CORBA Services
Object Request BrokerObject Request Broker
Figure 12.14 The CORBA Object Management Architecture(OMA). Pearson Education Ltd. From CORBA Distributed Objects bu Sean Baker © 1997 Addison Wesley Longman(p.430)
top related