From 723400afde24b2267447be7ff5a8b037f1c28b4c Mon Sep 17 00:00:00 2001 From: alexis mcclimans Date: Mon, 13 Jan 2020 17:04:12 -0800 Subject: [PATCH] fix data graph bug --- setup.py | 2 +- shaclgen/schema.py | 27 ++++++++------- shaclgen/shaclgen.py | 79 ++++++++++++++++++++++++-------------------- 3 files changed, 59 insertions(+), 49 deletions(-) diff --git a/setup.py b/setup.py index bb7ef00..f90f4fd 100644 --- a/setup.py +++ b/setup.py @@ -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, diff --git a/shaclgen/schema.py b/shaclgen/schema.py index e09a8e1..e656042 100644 --- a/shaclgen/schema.py +++ b/shaclgen/schema.py @@ -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) @@ -74,10 +73,10 @@ 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] = {} @@ -85,6 +84,9 @@ def extract_classes(self): 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 @@ -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() diff --git a/shaclgen/shaclgen.py b/shaclgen/shaclgen.py index 4524f22..0663920 100644 --- a/shaclgen/shaclgen.py +++ b/shaclgen/shaclgen.py @@ -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: @@ -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] = {} @@ -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 = [] @@ -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#') @@ -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()) - + \ No newline at end of file