Ngôn ngữ định nghĩa đối tƣợng (ODL)

Một phần của tài liệu Nghiên cứu cơ sở dữ liệu hướng đối tượng và áp dụng vào bài toán cụ thể với hệ quản trị DB40 (Trang 32 - 37)

ODL đƣợc dùng để tạo lƣợc đồ cơ sở dữ liệu đối tƣợng. ODL đƣợc thiết kế để hỗ trợ cấu trúc ngữ nghĩa của mô hình ODMG 2.0 và độc lập với ngôn ngữ lập trình. Nó chủ yếu đƣợc dùng để tạo ra đối tƣợng, là lớp và giao diện. ODL không phải là ngôn ngữ lập trình đầy đủ. Ngƣời sử dụng có thể mô tả lƣợc đồ cơ sở dữ liệu trong ODL độc lập với ngôn ngữ lập trình, sau đó sử dụng các ngôn ngữ cụ thể để kết hợp các xây dựng trong ODL chuyển vào ngôn ngữ lập trình nhƣ C++, Smalltalk, và Java.

Ví dụ 1.17 mô tả cách chuyển đổi từ cơ sở dữ liệu UNIVERSITY. Kiểu thực thể đƣợc chuyển thành lớp trong ODL, và kế thừa thực hiện bằng cách sử dụng

EXTENDS. Tuy nhiên, không có cách trực tiếp thực hiện đa kế thừa. Trong ví dụ 1.17,

các lớp Person, Faculty, Student, và GradStudent có các extents là persons, faculty, students, và grad_students. Faculty và Student EXTENDS Person, GradStudent EXTENDS Student. Do đó, tập của students (và tập của faculty) sẽ đƣợc ràng buộc nhƣ là tập con của tập persons tại mọi thời điểm. Tƣơng tự nhƣ vậy, tập grad_students sẽ là tập con của students. Đồng thời, các đối tƣợng Student Faculty sẽ kế thừa các tính chất (thuộc tính và liên kết) và các phép toán của Person, và đối tƣợng GradStudent sẽ kế thừa tính chất (thuộc tính và liên kết) và các phép toán của Student.

Các lớp Department, Course, Section, và CurrSection trong ví dụ 1.17 là chuyển đổi tƣơng ứng từ các thực thể trong hình 1.6. Lớp Grade tƣơng ứng với mối quan hệ M:N giữa Student và Section trong hình 1.6. Lý do nó đƣợc tạo ra nhƣ một lớp ngăn cách (không phải cặp liên kết ngƣợc) là vì nó gồm thuộc tính liên kết grade. Do đó,

mối quan hệ M:N đƣợc chuyển đổi thành lớp Grade, và hai liên kết 1:N, một giữa Student và Grade, một giữa Section và Grade. Hai liên kết này đƣợc mô tả bằng các thuộc tính liên kết: completed_sections của Student; section và student của Grade; và students của Section. Cuối cùng, lớp Degree dùng mô tả phức hợp, thuộc tính đa trị degrees của GradStudent.

Hình 1.5 Ký hiệu đồ họa

Hình 1.6 Ví dụ về một phần lƣợc đồ ODL của cơ sở dữ liệu một trƣờng đại học

Ví dụ 1.17 Thể hiện của lƣợc đồ cơ sở dữ liệu đối tƣợng của hình 1.6

Class Person

( extent persons key snn) {

attribute struct Pname {string fname, string mname, string lname} name;

attribute string ssn; Person - IF Student Giao diện Lớp Liên kết Kế thừa 1.1 1:N M:N

Giao diện (is-a) kế

thừa sử dụng “.” Lớp kế thừa sử dụng extends Person Department Faculty Student GradStudent Section CurrSection Course completed_section has_majors majors_in offers offers_by has_section of_course students registed_student registed_in on_committee_of committee advisor advises has_faculty works_in

attribute date birddate;

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 string rank;

attribute float salary;

attribute string office;

attribute string phone;

relationship Department works_in inverse Department::has_faculty;

relationshipset <GradStudent> advises inverse GradStudent::advisor;

relationshipset <GradStudent> on_committee_of

inverse GradStudent::advisor;

void give_raise(in float raise);

void promote(in string new_rank); }; (adsbygoogle = window.adsbygoogle || []).push({});

Class Grade ( extent grade ) {

attribute enum Grade Values{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 student ) {

attribute string class;

attribute Department minors_in;

relationship Department majors_in inverse Department::has_majors;

relationshipset <Grade> completed_sections inverse Grade::student;

relationshipset <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); };

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;

relationshipset <Faculty> committee inverse Faculty::on_committee_of;

void assign_advisor(in string lname; in string fname) raises(faculty_not_valid);

void assign_committee_member(in string lname; in string fname)

raises(faculty_not_valid); };

Class Department

( extent Department key dname ) {

attribute string dname;

attribute string dphone; (adsbygoogle = window.adsbygoogle || []).push({});

attribute string doffice;

attribute string college;

attribute Faculty chair

relationshipset <Faculty> has_faculty inverse Faculty::works_in;

relationshipset <Student> has_majors inverse Student::majors_in;

relationshipset <Course> offers inverse Course::offered_by; };

Class Course

( extent course key cno ) {

attribute string cname;

attribute string dno;

attribute string description;

relationship set <Section> has_sections inverse Section::of_course;

relationship Department offered_by inverse Department::offers; };

Class Section ( extent section ) {

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 section ) {

relationshipset <Student> registered_students

inverse Student::registered_in;

void register_student(in string ssn) raises (student_not_valid, section_full); };

Ví dụ 1.18 dƣới đây minh hoạ giao diện và kế thừa giao diện. Hình 1.7 là một phần của lƣợc đồ lƣu trữ các đối tƣợng hình học. Giao diện GeometryObject gồm các phƣơng thức để tính toán chu vi, diện tích của các đối tƣợng hình học và các phép toán dịch chuyển, quay một đối tƣợng. Một số lớp (Rectangle, Triangle, Circle, . . .) kế thừa giao diện GeometryObject. Từ khi tạo ra giao diện GeometryObject, nó là

noninstantiable, có nghĩa là không đối tƣợng nào đƣợc tạo ra trực tiếp dựa trên giao diện này. Tuy nhiên, các đối tƣợng có kiểu Rectangle, Triangle, Circle,… có thể tạo ra, và các đối tƣợng này kế thừa tất cả các phép toán của giao diện GeometryObject. Khi thừa kế giao diện, chỉ các phép toán đƣợc thừa kế, không thừa kế các tính chất (thuộc tính, liên kết). Do vậy, nếu cần thừa kế các tính chất trong lớp kế thừa, nó phải đƣợc viết lại trong định nghĩa lớp, nhƣ thuộc tính reference_point trong hình 1.7. Các phép toán đƣợc thừa kế có thể thực hiện khác nhau trong mỗi lớp. Ví dụ, thực hiện phép toán tính diện tích và chu vi là khác nhau cho các đối tƣợng Rectangle, Triangle, và Circle.

Các lớp đƣợc phép đa kế thừa giao diện, cũng nhƣ đa kế thừa giao diện của giao diện. Tuy nhiên, với kế thừa EXTENDS (class), đa kế thừa là không đƣợc phép. Do đó, lớp kế thừa thông qua EXTENDS nhiều nhất là một lớp

Hình 1.7 Kế thừa thông qua

“:”

Ví dụ 1.18 Minh họa sự kế thừa thông qua “:”

Interface GeometryObject { (adsbygoogle = window.adsbygoogle || []).push({});

attribute enum Shape{Rectange, 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); };

( 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; };

Một phần của tài liệu Nghiên cứu cơ sở dữ liệu hướng đối tượng và áp dụng vào bài toán cụ thể với hệ quản trị DB40 (Trang 32 - 37)