Skip to content

Commit

Permalink
fix data graph bug
Browse files Browse the repository at this point in the history
  • Loading branch information
alexiskeely committed Jan 14, 2020
1 parent 877a592 commit 723400a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 49 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name = 'shaclgen',
version = '0.1.4',
version = '0.1.5',
packages = ['shaclgen'],
description='Shacl graph generator',
long_description=l_description,
Expand Down
27 changes: 15 additions & 12 deletions shaclgen/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,24 @@ def extract_props(self):
#gather property values
count = 0
for prop in self.PROPS.keys():
count = count +1
count = count + 1
s = URIRef(prop)
self.PROPS[prop]['domain']= None
self.PROPS[prop]['range']= None
self.PROPS[prop]['e_prop'] = None
self.PROPS[prop]['label'] = self.gen_shape_labels(prop)+str(count)

for o in self.G.objects(subject=s, predicate=RDFS.domain):
if type(o) != BNode:
self.PROPS[prop]['domain'] = o
for domain in self.G.objects(subject=s, predicate=RDFS.domain):
if type(domain) != BNode:
self.PROPS[prop]['domain'] = domain

for o in self.G.objects(subject=s, predicate=RDFS.range):
if type(o) != BNode:
self.PROPS[prop]['range'] = o
for ranges in self.G.objects(subject=s, predicate=RDFS.range):
if type(ranges) != BNode:
self.PROPS[prop]['range'] = ranges

for o in self.G.objects(subject=s, predicate=OWL.equivalentProperty):
self.PROPS[prop]['e_prop'] = o
for equal in self.G.objects(subject=s, predicate=OWL.equivalentProperty):
self.PROPS[prop]['e_prop'] = equal

for o in self.G.objects(subject=s, predicate=RDFS.label):
self.PROPS[prop]['label'] = self.gen_shape_labels(prop)+str(count)



Expand All @@ -74,17 +73,20 @@ def extract_classes(self):
pass
for s,p,o in self.G.triples((None,RDF.type,RDFS.Class)):
if type(s) != BNode:

classes.append(s)
else:
pass

count = 0
for c in sorted(classes):
self.CLASSES[c] = {}
for c in self.CLASSES.keys():
count = count +1
self.CLASSES[c]['label'] = self.gen_shape_labels(c)+str(count)




def extract_restrictions(self):
# does not handle nested restrictions within other class descriptions

Expand Down Expand Up @@ -155,6 +157,7 @@ def gen_graph(self, serial='turtle'):
label = self.CLASSES[c]['label']
ng.add((EX[label], RDF.type, SH.NodeShape))
ng.add((EX[label], SH.targetClass, c))
ng.add((EX[label], SH.nodeKind, SH.BlankNodeOrIRI))
for p in self.PROPS.keys():
if self.PROPS[p]['domain'] is not None:
blank = BNode()
Expand Down
79 changes: 43 additions & 36 deletions shaclgen/shaclgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@ def __init__(self, args:list):
self.PROPS = collections.OrderedDict()
self.OUT = []


def extract_pairs(self):

classes = []
for s,p,o in self.G.triples( (None, RDF.type, None) ):
classes.append(o)

classes = sorted(list(set(classes)))

tupes = []
count = 0
for clas in classes:
count = count +1
for s,p,o in self.G.triples((None, RDF.type, clas)):
for s,p1,o1 in self.G.triples((s, None, None)):
tupes.append((count,clas,p1))

tupes = list(set(tupes))

tupes = [x for x in tupes if x[2] != rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')]

c = Counter(x[2] for x in tupes)
self.OUT = [x + ('unique',) if c[x[2]] == 1 else x + ('repeat',) for x in tupes]
print(self.OUT)
#
# def extract_pairs(self):
#
# classes = []
# for s,p,o in self.G.triples( (None, RDF.type, None) ):
# classes.append(o)
#
# classes = sorted(list(set(classes)))
#
# tupes = []
# count = 0
# for clas in classes:
# count = count +1
# for s,p,o in self.G.triples((None, RDF.type, clas)):
# for s,p1,o1 in self.G.triples((s, None, None)):
# tupes.append((count,clas,p1))
#
# tupes = list(set(tupes))
#
# tupes = [x for x in tupes if x[2] != rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')]
#
# c = Counter(x[2] for x in tupes)
# self.OUT = [x + ('unique',) if c[x[2]] == 1 else x + ('repeat',) for x in tupes]
# print(self.OUT)

def gen_shape_labels(self, URI):
if '#' in URI:
Expand All @@ -56,21 +56,23 @@ def extract_classes(self):
classes = []
for s,p,o in self.G.triples((None, RDF.type, None)):
classes.append(o)

for c in sorted(classes):
self.CLASSES[c] = {}
count = 0
for clas in self.CLASSES.keys():

for class_item in self.CLASSES.keys():
count = count +1
self.CLASSES[clas]['label'] = self.gen_shape_labels(clas)+str(count)
self.CLASSES[class_item]['label'] = self.gen_shape_labels(class_item)+str(count)



def extract_props(self):
self.extract_classes()
props = []
prop = []
for predicate in self.G.predicates(object=None, subject=None):
props.append(predicate)
props = [x for x in props if x != rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')]
prop.append(predicate)
props = [x for x in prop if x != rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')]

for p in sorted(props):
self.PROPS[p] = {}
Expand All @@ -82,8 +84,9 @@ def extract_props(self):

self.PROPS[p]['label'] = self.gen_shape_labels(p)+str(count)
prop_classes = []

for sub,pred,obj in self.G.triples((None, p, None)):
for sub, pred1, obj1 in self.G.triples( (sub, RDF.type, None) ):
for sub1, pred1, obj1 in self.G.triples( (sub, RDF.type, None) ):
prop_classes.append(obj1)

uris = []
Expand All @@ -99,12 +102,12 @@ def extract_props(self):
else:
self.PROPS[p]['type'] = 'repeat'





def gen_graph(self, serial='turtle', graph_format=None):
self.extract_props()



ng = rdflib.Graph()

SH = Namespace('http://www.w3.org/ns/shacl#')
Expand Down Expand Up @@ -134,16 +137,20 @@ def gen_graph(self, serial='turtle', graph_format=None):
pass

else:
ng.add( (EX[self.PROPS[p]['classes'][0]], SH.property, EX[self.PROPS[p]['label']]) )
for class_prop in self.PROPS[p]['classes']:
ng.add( (EX[class_prop], SH.property, EX[self.PROPS[p]['label']]) )
ng.add( (EX[self.PROPS[p]['label']], RDF.type, SH.PropertyShape) )
ng.add( (EX[self.PROPS[p]['label']], SH.path, p) )

else:
ng.add( (EX[self.PROPS[p]['label']], RDF.type, SH.PropertyShape) )
ng.add( (EX[self.PROPS[p]['label']], SH.path, p) )
ng.add( (EX[self.PROPS[p]['classes'][0]], SH.property, EX[self.PROPS[p]['label']]) )
ng.add( (EX[self.PROPS[p]['label']], RDF.type, SH.PropertyShape) )
ng.add( (EX[self.PROPS[p]['label']], SH.path, p) )
#
for class_prop in self.PROPS[p]['classes']:
ng.add( (EX[class_prop], SH.property, EX[self.PROPS[p]['label']]) )


print(ng.serialize(format=serial).decode())


0 comments on commit 723400a

Please sign in to comment.