Skip to content

Commit 0fdc2a2

Browse files
committed
Assigment4 by Jorge GaraciaV2
1 parent 80b4f09 commit 0fdc2a2

3 files changed

Lines changed: 258 additions & 0 deletions

File tree

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
from rdflib import Graph, Namespace, Literal, XSD
2+
from rdflib.namespace import RDF, RDFS
3+
4+
VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/")
5+
FOAF = Namespace("http://xmlns.com/foaf/0.1/")
6+
7+
class Report:
8+
def __init__(self):
9+
self.__report = ""
10+
11+
def domain_and_range_correspond_to_input(self, g,propertyURI,correct_domain,correct_range):
12+
domain = g.value(subject=propertyURI, predicate=RDFS.domain)
13+
range = g.value(subject=propertyURI, predicate=RDFS.range)
14+
if domain is None or range is None:
15+
return False
16+
if domain != correct_domain or range != correct_range:
17+
return False
18+
return True
19+
20+
def does_it_have_label(self, g, entity):
21+
label = g.value(subject=entity, predicate=RDFS.label)
22+
if label is None:
23+
return False
24+
return True
25+
26+
def namespace_is_correct_class(self, entity):
27+
if entity is None:
28+
return False
29+
if "http://oeg.fi.upm.es/def/people#" not in entity:
30+
return False
31+
return True
32+
33+
def namespace_is_correct_instance(self, entity):
34+
if entity is None:
35+
return False
36+
if "http://oeg.fi.upm.es/resource/person/" not in entity:
37+
return False
38+
return True
39+
40+
def is_subClassOf(self, g, subClass, superClass):
41+
candidate = g.value(subject=subClass, predicate=RDFS.subClassOf, object=None)
42+
if candidate is None or superClass not in candidate:
43+
return False
44+
return True
45+
46+
def __add_to_report(self, message):
47+
print(message)
48+
self.__report = self.__report + message + "\n"
49+
50+
def validate_task_06_01(self, g):
51+
error = False
52+
professorURI = g.value(subject=None, predicate=RDFS.label, object=Literal("Professor", datatype=XSD.string))
53+
personURI = g.value(subject=None, predicate=RDFS.label, object=Literal("Person", datatype=XSD.string))
54+
associateProfessorURI = g.value(subject=None, predicate=RDFS.label, object=Literal("AssociateProfessor", datatype=XSD.string))
55+
interimURI = g.value(subject=None, predicate=RDFS.label, object=Literal("InterimAssociateProfessor", datatype=XSD.string))
56+
fProfessorURI = g.value(subject=None, predicate=RDFS.label, object=Literal("FullProfessor", datatype=XSD.string))
57+
classes = [professorURI,personURI,associateProfessorURI,interimURI, fProfessorURI]
58+
# check namespace and existence
59+
for i in classes:
60+
if i is None:
61+
self.__add_to_report("ERROR: One of the classes is missing its correct label! I cannot retrieve it")
62+
error = True
63+
return
64+
if self.namespace_is_correct_class(i):
65+
print("The namespace is correct for " + str(i))
66+
else:
67+
self.__add_to_report("ERROR: The namespace is not correct for " + str(i))
68+
error = True
69+
# check class hierarchy
70+
if self.is_subClassOf(g, professorURI, personURI) and \
71+
self.is_subClassOf(g, associateProfessorURI, professorURI) and \
72+
self.is_subClassOf(g, interimURI, associateProfessorURI) and \
73+
self.is_subClassOf(g, fProfessorURI, professorURI):
74+
self.__add_to_report("Hierarchy OK")
75+
else:
76+
self.__add_to_report("ERROR: Hierarchy is missing a subclassOf statement")
77+
error = True
78+
if error:
79+
self.__add_to_report("ERROR IN TASK 6.1")
80+
else:
81+
self.__add_to_report("TASK 6.1 OK")
82+
83+
def validate_task_06_02(self, g):
84+
# check properties
85+
error = False
86+
hasColleague = g.value(subject=None, predicate=RDFS.label, object=Literal("hasColleague", datatype=XSD.string))
87+
hasName = g.value(subject=None, predicate=RDFS.label, object=Literal("hasName", datatype=XSD.string))
88+
hasHomePage = g.value(subject=None, predicate=RDFS.label, object=Literal("hasHomePage", datatype=XSD.string))
89+
personURI = g.value(subject=None, predicate=RDFS.label, object=Literal("Person", datatype=XSD.string))
90+
fullProfessorURI = g.value(subject=None, predicate=RDFS.label, object=Literal("FullProfessor", datatype=XSD.string))
91+
properties = [hasColleague, hasName, hasHomePage]
92+
for i in properties:
93+
if i is None:
94+
self.__add_to_report("ERROR: One of the properties is missing its correct label! I cannot retrieve it")
95+
error = True
96+
return
97+
if not self.domain_and_range_correspond_to_input(g,hasColleague,personURI,personURI):
98+
self.__add_to_report("ERROR: hasColleague has an incorrect domain or range")
99+
error = True
100+
if not self.domain_and_range_correspond_to_input(g,hasName,personURI,RDFS.Literal):
101+
self.__add_to_report("ERROR: hasName has an incorrect domain or range")
102+
error = True
103+
if not self.domain_and_range_correspond_to_input(g,hasHomePage,fullProfessorURI,RDFS.Literal):
104+
self.__add_to_report("ERROR: hasHomePage has an incorrect domain or range")
105+
error = True
106+
if error:
107+
self.__add_to_report("ERROR IN TASK 6.2")
108+
else:
109+
self.__add_to_report("TASK 6.2 OK")
110+
111+
def validate_task_06_03(self, g):
112+
# check all individuals can be retrieved through their label
113+
error = False
114+
oscar = g.value(subject=None, predicate=RDFS.label, object=Literal("Oscar", datatype=XSD.string))
115+
asun = g.value(subject=None, predicate=RDFS.label, object=Literal("Asun", datatype=XSD.string))
116+
raul = g.value(subject=None, predicate=RDFS.label, object=Literal("Raul", datatype=XSD.string))
117+
if oscar is None or asun is None or raul is None:
118+
self.__add_to_report("ERROR: One of the individuals is missing its correct label! I cannot retrieve it")
119+
error = True
120+
# check all individuals have the correct namespace
121+
if not self.namespace_is_correct_instance(oscar):
122+
self.__add_to_report("ERROR: Oscar has an incorrect namespace")
123+
error = True
124+
if not self.namespace_is_correct_instance(asun):
125+
self.__add_to_report("ERROR: Asun has an incorrect namespace")
126+
error = True
127+
if not self.namespace_is_correct_instance(raul):
128+
self.__add_to_report("ERROR: Raul has an incorrect namespace")
129+
error = True
130+
# check all individuals have their properties
131+
oscar_properties = []
132+
for p in g.predicates(subject=oscar):
133+
oscar_properties.append(p)
134+
asun_properties = []
135+
for p in g.predicates(subject=asun):
136+
asun_properties.append(p)
137+
if oscar_properties is None or asun_properties is None:
138+
self.__add_to_report("ERROR: One of the individuals has no properties")
139+
error = True
140+
if len(oscar_properties) != 4 or len(asun_properties) != 4:
141+
# oscar: type, label, hasColleague, hasName.
142+
# asun: type, label, hasHomePage, hasColleague
143+
self.__add_to_report("ERROR: One of the individuals has the wrong number of properties")
144+
error = True
145+
if error:
146+
self.__add_to_report("ERROR IN TASK 6.3")
147+
else:
148+
self.__add_to_report("TASK 6.3 OK")
149+
150+
def validate_task_06_04(self, g):
151+
error = False
152+
target_properties = [VCARD.Given, VCARD.Family, FOAF.email]
153+
#retrieve all triples from Oscar.
154+
oscar_properties = []
155+
oscar = g.value(subject=None, predicate=RDFS.label, object=Literal("Oscar", datatype=XSD.string))
156+
for p in g.predicates(subject=oscar):
157+
oscar_properties.append(p)
158+
if oscar_properties is None:
159+
self.__add_to_report("ERROR: Oscar has no properties")
160+
error = True
161+
# do they have the correct ns?
162+
for i in target_properties:
163+
if i not in oscar_properties:
164+
self.__add_to_report("ERROR: One of the properties from Oscar has no correct namespace or does not exist. Please double check")
165+
error = True
166+
if error:
167+
self.__add_to_report("ERROR IN TASK 6.4")
168+
else:
169+
self.__add_to_report("TASK 6.4 OK")
170+
171+
def save_report(self, task):
172+
report_name = "report_result" + task + ".txt"
173+
with open(report_name, "w", encoding="utf-8") as f:
174+
f.write(self.__report)
175+
176+
def validate_07_01(self, result, task):
177+
error = False
178+
if len(result) != 7:
179+
self.__add_to_report("ERROR: The number of classes returned is not correct")
180+
error = True
181+
for c,sc in result:
182+
# Anything except Person and Animal must have a superclass
183+
if sc == None and "Person" not in str(c) and "Animal" not in str(c):
184+
self.__add_to_report("The class "+str(c)+" has no superclass")
185+
error = True
186+
if "Person" not in str(c) and "Animal" not in str(c) \
187+
and "Professor" not in str(c) and "Student" not in str(c) \
188+
and "FullProfessor" not in str(c) and "AssociateProfessor" not in str(c) \
189+
and "AssociateProfessor" not in str(c) and "Instructor" not in str(c) \
190+
and "InterimAssociateProfessor" not in str(c):
191+
self.__add_to_report("ERROR: incorrect class retrieved")
192+
error = True
193+
if not error:
194+
self.__add_to_report(task+" OK")
195+
196+
def validate_07_1a(self, result):
197+
self.validate_07_01(result, "TASK 7.1a")
198+
199+
def validate_07_1b(self, query, g):
200+
aux = g.query(query)
201+
aux_dict = []
202+
for r in g.query(query):
203+
aux_dict.append((r.c, r.sc))
204+
self.validate_07_01(aux_dict, "TASK 7.1b")
205+
206+
def validate_07_02(self,result, task):
207+
error = False
208+
if len(result) != 3:
209+
self.__add_to_report("ERROR: The number of individuals returned is not correct")
210+
error = True
211+
for i in result:
212+
if "Asun" not in i and "Raul" not in i and "Oscar" not in i:
213+
self.__add_to_report("ERROR: The individual "+str(i)+" is not correct")
214+
error = True
215+
if error == False:
216+
self.__add_to_report(task+" OK")
217+
218+
219+
def validate_07_02a(self, individuals):
220+
self.validate_07_02(individuals, "TASK 7.2a")
221+
222+
def validate_07_02b(self, g, query):
223+
error = False
224+
aux = g.query(query)
225+
aux_dict = []
226+
for r in g.query(query):
227+
if (r.ind is None):
228+
self.__add_to_report("ERROR: Variable used to retrieve the individuals is not correct!")
229+
error = True
230+
else:
231+
aux_dict.append(r.ind)
232+
self.validate_07_02(aux_dict, "TASK 7.2b")
233+
234+
def validate_07_03(self, g, query):
235+
error = False
236+
entities = g.query(query)
237+
if len(list(entities)) != 3:
238+
self.__add_to_report("ERROR: The number of individuals returned is not correct")
239+
error = True
240+
for i in entities:
241+
if "Asun" not in i.name and "Raul" not in i.name and "Fantasma" not in i.name:
242+
self.__add_to_report("ERROR: An individual returned is not correct")
243+
error = True
244+
if not error:
245+
self.__add_to_report("TASK 7.3 OK")
246+
247+
def validate_07_04(self, g, query):
248+
error = False
249+
entities = g.query(query)
250+
if len(list(entities)) != 3:
251+
self.__add_to_report("ERROR: The number of individuals returned is not correct")
252+
error = True
253+
for i in entities:
254+
if "Asun" not in i.name and "Raul" not in i.name and "Oscar" not in i.name:
255+
self.__add_to_report("ERROR: An individual returned is not correct")
256+
error = True
257+
if not error:
258+
self.__add_to_report("TASK 7.4 OK")

0 commit comments

Comments
 (0)