-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathontology.py
162 lines (134 loc) · 5.68 KB
/
ontology.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from rdflib import Graph
from rdflib.util import guess_format
import os
import hashlib
def getGraph(path,ext):
g = Graph()
#g.parse(path,format=ext)
g.parse(path,format=guess_format(path))
return g
def getClasses(g):
query = """
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT DISTINCT ?classe (str(?title) as ?title) (str(?comment) as ?comment) ?objectOp ?dataOp ?mae WHERE{
{?classe rdf:type owl:Class.}
UNION
{?classe rdf:type rdfs:Class.}
FILTER (!isBlank(?classe))
FILTER ( !strstarts(str(?classe), "http://www.menthor.net/ontouml#") )
OPTIONAL{
?classe rdfs:subClassOf ?mae.
FILTER (!isBlank(?mae))
FILTER ( !strstarts(str(?mae), "http://www.menthor.net/ontouml#") )
}
OPTIONAL{
{?classe rdfs:label ?title.}
UNION
{?classe dc:title ?title.}
}
OPTIONAL{
?classe rdfs:comment ?comment
}
OPTIONAL {
?objectOp rdf:type owl:ObjectProperty;
rdfs:domain ?classe
}
OPTIONAL {
?dataOp rdf:type owl:DatatypeProperty;
rdfs:domain ?classe
}
}
"""
result = g.query(query)
classes = {}
for row in result:
id_hash = hashlib.md5(str(row["classe"]).encode('utf-8')).hexdigest()
classe = {'id':id_hash,'uri':row["classe"].n3(),'labels':[],'comments':[],'dataTypeProperties':[],'objectProperties':[],'mae':[]}
if id_hash in classes:
classe = classes[id_hash]
if str(row["title"]) not in classe['labels'] and str(row["title"]) != "None":
classe['labels'].append(str(row["title"]))
if str(row["comment"]) not in classe['comments'] and str(row["comment"]) != "None":
classe['comments'].append(str(row["comment"]))
if str(row["dataOp"]) != "None":
dp_hash = hashlib.md5(str(row["dataOp"]).encode('utf-8')).hexdigest()
if dp_hash not in classe['dataTypeProperties']:
classe['dataTypeProperties'].append(dp_hash)
if str(row["objectOp"]) != "None":
ob_hash = hashlib.md5(str(row["objectOp"]).encode('utf-8')).hexdigest()
if ob_hash not in classe['objectProperties']:
classe['objectProperties'].append(ob_hash)
if str(row["mae"]) != "None" and str(row["mae"]) != 'http://www.w3.org/2002/07/owl#Thing':
mae_hash = hashlib.md5(str(row["mae"]).encode('utf-8')).hexdigest()
if mae_hash not in classe['mae']:
classe['mae'].append(mae_hash)
classes[id_hash] = classe
return classes
def getProperties(g):
query = """
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT DISTINCT ?propriedade ?tipo (str(?title) as ?title) (str(?comment) as ?comment) ?domain ?range ?mae WHERE{
{
?propriedade rdf:type rdf:Property
BIND("rdf:Property" as ?tipo)
}
UNION{
?propriedade rdf:type owl:ObjectProperty
BIND("owl:ObjectProperty" as ?tipo)
}
UNION{
?propriedade rdf:type owl:DatatypeProperty;
BIND("owl:DatatypeProperty" as ?tipo)
}
FILTER ( !strstarts(str(?propriedade), "http://www.menthor.net/ontouml#") )
OPTIONAL{
{?propriedade rdfs:label ?title.}
UNION
{?propriedade dc:title ?title.}
}
OPTIONAL{
?propriedade rdfs:comment ?comment
}
OPTIONAL{
?propriedade rdfs:domain ?domain
FILTER (!isBlank(?domain))
FILTER ( !strstarts(str(?domain), "http://www.menthor.net/ontouml#") )
}
OPTIONAL{
?propriedade rdfs:range ?range
FILTER (!isBlank(?range))
FILTER ( !strstarts(str(?domain), "http://www.menthor.net/ontouml#") )
}
OPTIONAL{
?propriedade rdfs:subPropertyOf ?mae.
FILTER ( !strstarts(str(?mae), "http://www.menthor.net/ontouml#") )
}
}
"""
result = g.query(query)
propriedades = {}
for row in result:
id_hash = hashlib.md5(str(row["propriedade"]).encode('utf-8')).hexdigest()
propriedade = {'id':id_hash,'uri':row["propriedade"].n3(),'labels':[],'comments':[],'type':str(row['tipo']),'domain':[],'range':[],'mae':[]}
if id_hash in propriedades:
propriedade = propriedades[id_hash]
if str(row["title"]) not in propriedade['labels'] and str(row["title"]) != "None":
propriedade['labels'].append(str(row["title"]))
if str(row["comment"]) not in propriedade['comments'] and str(row["comment"]) != "None":
propriedade['comments'].append(str(row["comment"]))
if str(row["domain"]) != "None":
domain_hash = hashlib.md5(str(row["domain"]).encode('utf-8')).hexdigest()
if domain_hash not in propriedade['domain']:
propriedade['domain'].append(domain_hash)
if str(row["range"]) != "None":
range_hash = hashlib.md5(str(row["range"]).encode('utf-8')).hexdigest()
if str(row['tipo']) == 'owl:DatatypeProperty':
range_hash = str(row["range"])
if range_hash not in propriedade['range']:
propriedade['range'].append(range_hash)
if str(row["mae"]) != "None" and str(row["mae"]) != 'http://www.w3.org/2002/07/owl#topObjectProperty' and str(row["mae"]) != 'http://www.w3.org/2002/07/owl#topDataProperty':
mae_hash = hashlib.md5(str(row["mae"]).encode('utf-8')).hexdigest()
if mae_hash not in propriedade['mae']:
propriedade['mae'].append(mae_hash)
propriedades[id_hash] = propriedade
return propriedades