Skip to content

Commit

Permalink
Add saving an OWL 2 DL variant of the ontology
Browse files Browse the repository at this point in the history
  • Loading branch information
Ostrzyciel committed Oct 29, 2023
1 parent 4bb22b8 commit cc0c00c
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions tasks/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,37 @@ def main():
'SubObjectProperty ObjectPropertyRange ObjectPropertyDomain',
'--include-indirect', 'true',
'--output', temp_dir / 'inferred.ttl'
])
], check=True)

print('Merging...')
g_inf = Graph()
g_inf.parse(temp_dir / 'inferred.ttl', format='turtle')
g = Graph()
g.parse(input_dir / 'stax.ttl', format='turtle')
original_size = len(g)

print('Adding version...')
g.add((URIRef(STAX_MAIN), OWL.versionInfo, Literal(version)))
g.add((URIRef(STAX_MAIN), OWL.versionIRI, URIRef(STAX_MAIN.replace('ontology', version + '/ontology'))))
now_iso = datetime.now(timezone.utc).isoformat()[:19]
g.add((URIRef(STAX_MAIN), DCTERMS.modified, Literal(now_iso, datatype=XSD.dateTime)))
# Add the type of the dcterms:modified property (required in OWL 2 DL)
g.add((URIRef(DCTERMS.modified), RDF.type, OWL.AnnotationProperty))
g.namespace_manager.bind('stax_ont', None, replace=True)
g.namespace_manager.bind('schema', SCHEMA, replace=True)

print('Serializing the OWL 2 DL ontology...')
serialize(g, output_dir, 'dl')

print("Validating the OWL 2 DL ontology's profile...")
# We have changed the ontology, so let's revalidate it, just in case
subprocess.run([
'java', '-jar', str(parent_dir / 'robot.jar'),
'validate-profile', '--input', output_dir / 'dl.ttl', '--profile', 'DL',
'--output', temp_dir / 'dl_validation.txt'
], check=True)

print('Merging...')
g_inf = Graph()
g_inf.parse(temp_dir / 'inferred.ttl', format='turtle')

g.parse(input_dir / 'authors.ttl', format='turtle')
print(f'Added {len(g) - original_size} triples about authors')
original_size = len(g)
Expand All @@ -74,24 +97,19 @@ def main():
g.add((s, p, o))

print(f'Added {len(g) - original_size} inferred triples')

print('Adding version...')
g.add((URIRef(STAX_MAIN), OWL.versionInfo, Literal(version)))
g.add((URIRef(STAX_MAIN), OWL.versionIRI, URIRef(STAX_MAIN.replace('ontology', version + '/ontology'))))
now_iso = datetime.now(timezone.utc).isoformat()[:19]
g.add((URIRef(STAX_MAIN), DCTERMS.issued, Literal(now_iso, datatype=XSD.dateTime)))
g.namespace_manager.bind('stax_ont', None, replace=True)
g.namespace_manager.bind('schema', SCHEMA, replace=True)

print('Serializing...')
print('Serializing the merged ontology...')
try:
os.mkdir(output_dir)
except FileExistsError:
pass
g.serialize(destination=output_dir / 'stax.ttl', format='turtle', encoding='utf-8')
g.serialize(destination=output_dir / 'stax.rdf', format='xml', encoding='utf-8')
g.serialize(destination=output_dir / 'stax.jsonld', format='json-ld', encoding='utf-8')
g.serialize(destination=output_dir / 'stax.nt', format='nt', encoding='utf-8')
serialize(g, output_dir, 'stax')


def serialize(g: Graph, output_dir: Path, filename: str):
g.serialize(destination=output_dir / f'{filename}.ttl', format='turtle', encoding='utf-8')
g.serialize(destination=output_dir / f'{filename}.rdf', format='xml', encoding='utf-8')
g.serialize(destination=output_dir / f'{filename}.jsonld', format='json-ld', encoding='utf-8')
g.serialize(destination=output_dir / f'{filename}.nt', format='nt', encoding='utf-8')


if __name__ == '__main__':
Expand Down

0 comments on commit cc0c00c

Please sign in to comment.