BIỂU DIỄN BÀI TOÁN BẰNG ONTOLOGY OWL

Một phần của tài liệu sử dụng ontology và owl biểu diễn và giải các bài toán logic LOGIC PUZZLEs (Trang 75)

15. Người Nauy sống kế bên căn nhà màu xanh dương.

4.2BIỂU DIỄN BÀI TOÁN BẰNG ONTOLOGY OWL

Bài toán này có nhiều cách giải khác nhau theo kiểu lập trình cấu trúc bằng các ngôn ngữ C, C#, Java, ... nhưng ta vẫn phải nghĩ chiến thuật giải rồi mới đi viết thuật toán cho máy tính chạy, có nghĩa là ở đây chương trình vẫn không đủ thông minh giải giúp ta, ta chỉ việc mô tả bài toán thôi.

Còn nếu dùng Ontology để giải công việc rất nhẹ nhàng, ta chỉ việc mô tả bài toán thành ngôn ngữ Ontology và để cho nó tự động suy ra cho ta đáp án.

Hạn chế của cách giải bằng pp cũ là ta chỉ tìm ra nghiệm đề bài yêu cầu, ở đây sử dụng Ontology ta có thể vét tất cả các đáp án, đây cũng là điểm mạnh của Ontology.

File ontology mô tả bài toán nằm trong đường dẫn sau :

GIẢI THÍCH CÁCH MÔ TẢ BẰNG ONTOLOGY

Sau đây ta đi biểu diễn bài toán thành Ontology ở dạng format Turtle

http://en.wikipedia.org/wiki/Turtle_(syntax)

• Ta đặt lần lượt các khái niệm Quốc tịch (Nationality) là N, Uống (Drink) là D, Thú nuôi (Pet) là P, Hút (Smoke) là S, và Màu sắc (Color) là C

Ta biểu diễn các mối quan hệ theo sơ đồ sau :

Mô hình ý niệm của bài toán

 File mở đầu với phần khai báo namespace mô tả cho ontology

@prefix : <http://www.uit.edu.vn/thanhvu/what/2012/01/08/zebra#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

@prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

<http://www.uit.edu.vn/thanhvu/what/2012/01/08/zebra> rdf:type owl:Ontology

Tất cả lớp được khai báo disjoint từng đôi một

 Khai báo các lớp như mô tả ở bài toán

:Color rdf:type owl:Class ; owl:equivalentClass [ rdf:type owl:Class ;

owl:oneOf ( :Blue :Yellow :Ivory :Green :Red ) ] . :Drink rdf:type owl:Class ;

owl:equivalentClass [ rdf:type owl:Class ;

owl:oneOf ( :OrangeJuice :Water :Tea :Milk :Coffee ) ] . :Pet rdf:type owl:Class ;

owl:equivalentClass [ rdf:type owl:Class ;

owl:oneOf ( :Horse :Zebra :Fox :Dog :Snails ) ] . :Smoke rdf:type owl:Class ;

owl:equivalentClass [ rdf:type owl:Class ;

owl:oneOf ( :Kools :Chesterfield :LuckyStrike :OldGold :Parliament ) ] . :House rdf:type owl:Class ;

owl:equivalentClass [ rdf:type owl:Class ;

owl:oneOf ( :House5 :House4 :House2 :House3 :House1 ) ] . :Person rdf:type owl:Class ;

owl:equivalentClass [ rdf:type owl:Class ;

owl:oneOf ( :Norwegian :Spaniard :Ukrainian :Japanese :Englishman ) ] .

 Ta khai báo các mối quan hệ của Person-Drink, Pet, Smoke và House, và giữa House và Color được định nghĩa bằng cách thiết lập domain và range chính xác. Ngoài ra, tất cả

những thuộc tính này của đối tượng được định nghĩa như là các hàm và các hàm đảo của nó. Nó giúp ta đảm bảo các ràng buộc trên quan hệ được chính xác, ví dụ, một người chỉ uống nhiều nhất là 1 loại nước uống và nước đó chỉ được uống nhiều nhất bởi 1 người.

:drinks rdf:type owl:FunctionalProperty , owl:InverseFunctionalProperty , owl:ObjectProperty ;

rdfs:range :Drink ; rdfs:domain :Person .

:hasColor rdf:type owl:FunctionalProperty , owl:InverseFunctionalProperty , owl:ObjectProperty ;

rdfs:range :Color ; rdfs:domain :House .

:hasPet rdf:type owl:FunctionalProperty , owl:InverseFunctionalProperty , owl:ObjectProperty ;

rdfs:domain :Person ; rdfs:range :Pet .

:livesIn rdf:type owl:FunctionalProperty , owl:InverseFunctionalProperty , owl:ObjectProperty ;

rdfs:range :House ; rdfs:domain :Person .

:smokes rdf:type owl:FunctionalProperty , owl:InverseFunctionalProperty , owl:ObjectProperty ;

 Ngoài ra ta còn đảm bảo các ràng buộc, một người không được uống 0 Drink (nghĩa là 1 người phải uống 1 món), tương tự chúng ta xét cho các quan hệ House, 1 căn nhà có 1 Color.

Person rdf:type owl:Class ; rdfs:subClassOf [ rdf:type owl:Class ; owl:intersectionOf ( [ rdf:type owl:Restriction ; owl:onProperty :drinks ; owl:someValuesFrom owl:Thing ] [ rdf:type owl:Restriction ; owl:onProperty :hasPet ; owl:someValuesFrom owl:Thing ] [ rdf:type owl:Restriction ; owl:onProperty :livesIn ; owl:someValuesFrom owl:Thing ] [ rdf:type owl:Restriction ; owl:onProperty :smokes ; owl:someValuesFrom owl:Thing ] ) ] .

:House rdf:type owl:Class ;

rdfs:subClassOf [ rdf:type owl:Restriction ; owl:onProperty :hasColor ;

owl:someValuesFrom owl:Thing ] .

 Ta đi biểu diễn quan hệ, các căn nhà nằm kế nhau và nằm bên trái hay phải của nhau. Đoạn code sau biểu diễn cho các dữ kiện đề bài số 6,8,5,4. Ta xây dựng các hàm isNextTo, isRightTo, isLeftTo.

:isNextTo rdf:type owl:IrreflexiveProperty , owl:ObjectProperty ,

owl:SymmetricProperty ; rdfs:domain :House ;

rdfs:range :House .

:isRightTo rdf:type owl:FunctionalProperty , owl:InverseFunctionalProperty , owl:ObjectProperty ;

owl:inverseOf :isLeftTo ; rdfs:subPropertyOf :isNextTo .

:isLeftTo rdf:type owl:ObjectProperty ; rdfs:subPropertyOf :isNextTo .

 Phát biểu 7 : ta thêm giới hạn về lượng số max 2 cho quan hệ isNextTo với House

:House rdf:type owl:Class ; rdfs:subClassOf [ rdf:type owl:Restriction ; owl:onProperty :isNextTo ; owl:onClass :House ; owl:maxQualifiedCardinality "2"^^xsd:nonNegativeInteger ] , [ rdf:type owl:Restriction ; owl:onProperty :hasColor ; owl:someValuesFrom owl:Thing ] .  Phát biểu 1 và 2 : ta thêm

[ rdf:type owl:AllDifferent ;

owl:distinctMembers ( :Green :Ivory :Red :Yellow :Blue ) ] .

 Phát biểu 3 : để đảm bảo 5 căn nhà không đặt theo kiểu vòng tròn, ta thêm điều kiện 2 căn nhà ở 2 đầu House1 và House5, không ở sát nhau, được thực hiện bằng

NegativePropertyAssertion :House1 :isLeftTo :House2 . :House2 :isLeftTo :House3 . :House3 :isLeftTo :House4 . :House4 :isLeftTo :House5 .

[ rdf:type owl:NegativePropertyAssertion ; owl:assertionProperty :isNextTo ;

owl:targetIndividual :House1 ; owl:sourceIndividual :House5 ] .

 Tiếp theo, ta set tất cả thành phần của mỗi class là những cá thể biệt lập (distinct individuals).

rdf:type owl:AllDifferent ;

owl:distinctMembers ( :Green :Ivory :Red :Yellow :Blue ) ] .

[ rdf:type owl:AllDifferent ;

owl:distinctMembers ( :Spaniard :Norwegian :Englishman :Ukrainian :Japanese ) ] .

[ rdf:type owl:AllDifferent ;

owl:distinctMembers ( :Tea :OrangeJuice :Water :Coffee :Milk ) ] .

[ rdf:type owl:AllDifferent ;

chưa xác định được.

### statement 2

:Englishman :livesIn [ :hasColor :Red ] .

### statement 3

:Spaniard :hasPet :Dog .

### statement 4 [] :drinks :Coffee ;

:livesIn [ :hasColor :Green ] .

### statement 5

:Ukrainian :drinks :Tea .

### statement 6 [] :hasColor :Green ;

:isRightTo [ :hasColor :Ivory ] .

### statement 7 [] :smokes :OldGold ; :hasPet :Snails .

### statement 8 [] :smokes :Kools ;

:livesIn [ :hasColor :Yellow ] .

### statement 9 [] :drinks :Milk ;

:livesIn :House3 .

### statement 10

:Norwegian :livesIn :House1 .

### statement 11 [] :smokes :Chesterfield ; :livesIn [ :isNextTo _:x11 ] . [] :livesIn _:x11 ; :hasPet :Fox . ### statement 12 [] :smokes :Kools; :livesIn [ :isNextTo _:x12 ] . [] :livesIn _:x12 ; :hasPet :Horse . ### statement 13 [] :smokes :LuckyStrike ; :drinks :OrangeJuice . ### statement 14

:Japanese :smokes :Parliament .

### statement 15

:Norwegian :livesIn [ :isNextTo [ :hasColor :Blue ] ] .

Đến đây ta có được file OWL hoàn chỉnh, kế tiếp ta sẽ dùng file này và bộ suy luận của Protégé để giải bài toán cho ta.

Một phần của tài liệu sử dụng ontology và owl biểu diễn và giải các bài toán logic LOGIC PUZZLEs (Trang 75)