Skip to content

Commit

Permalink
Adds option to use implicit class targets
Browse files Browse the repository at this point in the history
  • Loading branch information
mielvds committed Apr 26, 2021
1 parent abc224c commit 38ab85a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions shaclgen/__main__.py
Expand Up @@ -52,6 +52,7 @@
parser.add_argument("-o", "--ontology", action="store_true", help='input file(s) or URL(s) is a schema or ontology')
parser.add_argument("-s", "--serialization", help='result graph serialization, default is turtle. example: -s nt')
parser.add_argument("-ns","--namespace", nargs='+',help="optional shape namespace declaration. example: -ns http://www.example.com exam")
parser.add_argument("-i", "--implicit", action="store_true", help='use implicit class targets with RDFS')


args = parser.parse_args()
Expand All @@ -64,6 +65,8 @@ def main():
kwargs['serial'] = args.serialization
if args.namespace:
kwargs['namespace'] = args.namespace
if args.implicit:
kwargs['implicit_class_target'] = args.implicit
g.gen_graph(**kwargs)
else:
kwargs = {'serial': 'turtle'}
Expand All @@ -76,6 +79,8 @@ def main():
kwargs['serial'] = args.serialization
if args.namespace:
kwargs['namespace'] = args.namespace
if args.implicit:
kwargs['implicit_class_target'] = args.implicit
print('## shape file generated by SHACLGEN')
g.gen_graph(**kwargs)

Expand Down
17 changes: 12 additions & 5 deletions shaclgen/schema.py
Expand Up @@ -256,7 +256,7 @@ def extract_restrictions(self):
self.REST[rest]['value'] = rest_val[0]


def gen_graph(self, serial='turtle', namespace=None):
def gen_graph(self, serial='turtle', namespace=None, implicit_class_target=False):
self.gen_prefix_bindings()
self.extract_props()
self.extract_classes()
Expand Down Expand Up @@ -288,13 +288,20 @@ def gen_graph(self, serial='turtle', namespace=None):

# add class Node Shapes
for c in self.CLASSES.keys():
subject = c
clabel = self.CLASSES[c]['label']
ng.add((EX[clabel], RDF.type, SH.NodeShape))
ng.add((EX[clabel], SH.targetClass, c))

if not implicit_class_target:
subject = EX[clabel]
ng.add((subject, SH.targetClass, c))
else:
ng.add((subject,RDF.type, RDFS.Class ))

ng.add((subject, RDF.type, SH.NodeShape))
# ng.add((EX[clabel], SH.name, Literal(self.CLASSES[c]['shape_name']+' Node shape')))
ng.add((EX[clabel], SH.nodeKind, SH.BlankNodeOrIRI))
ng.add((subject, SH.nodeKind, SH.BlankNodeOrIRI))
if self.CLASSES[c]['definition'] is not None:
ng.add((EX[clabel], SH.description, Literal((self.CLASSES[c]['definition']))))
ng.add((subject, SH.description, Literal((self.CLASSES[c]['definition']))))


for p in self.PROPS.keys():
Expand Down
2 changes: 1 addition & 1 deletion shaclgen/shaclgen.py
Expand Up @@ -149,7 +149,7 @@ def extract_contraints(self):
self.PROPS[prop]['nodekind'] = 'Literal'


def gen_graph(self, serial='turtle', graph_format=None, namespace=None, verbose=None):
def gen_graph(self, serial='turtle', graph_format=None, namespace=None, verbose=None, implicit_class_target=False):
self.extract_props()
self.gen_prefix_bindings()
self.extract_contraints()
Expand Down

0 comments on commit 38ab85a

Please sign in to comment.