def validate_owl2(fileloc: str) -> bool: print(f"Validating {os.path.basename(fileloc)}") # 1) convert the functional syntax to nn Ontology: # Ontology = f(functional_repr) with open(fileloc) as f: func_repr = f.read() logging.info('\n===== Original Input =====\n' + func_repr) ontology_doc = to_python(func_repr) if not ontology_doc: return False logging.info('\n===== Pass 1 Output =====\n' + str(ontology_doc.to_functional())) # 2) determine whether the RDF representation of the Ontology is what is expected # g(f(functional_repr) == RDF if do_rdf: expected_rdf = Graph() expected_rdf.load(fileloc.replace('.func', '.ttl'), format="turtle") actual_rdf_graph = Graph() actual_rdf_graph.add = lambda t: add(actual_rdf_graph, t) ontology_doc.to_rdf(actual_rdf_graph) rslts = compare_rdf(expected_rdf, actual_rdf_graph) if rslts: logging.info('\n========== pass 1 rdf output =================\n' + actual_rdf_graph.serialize(format="turtle").decode()) logging.info('\n---------- expected rdf ------------\n' + expected_rdf.serialize(format="turtle").decode()) print(rslts) return False # 3) convert the ontology back into functional syntax # functional_repr_prime = f**-1(f(functional_repr)) ontology_2 = to_python(ontology_doc.to_functional().getvalue()) if not ontology_2: logging.error(f"Failed to parse emitted functional syntax") return False logging.info('\n===== Pass 2 Output =====\n' + str(ontology_2.to_functional())) # 4) Convert the functional syntax back into an Ontology # Ontology_prime = f(f**-1(f(functional_repr))) if do_rdf: roundtrip_rdf = Graph() ontology_2.to_rdf(roundtrip_rdf) logging.info('\n========== Round Trip RDF =================\n' + roundtrip_rdf.serialize(format="turtle").decode()) # 5) Make sure that the RDF representation stll matches # g(f(functional_repr)) == g(f(f**-1(f(functional_repr)) rslts = compare_rdf(expected_rdf, roundtrip_rdf) if rslts: print(rslts) return False return True
def verify(self, loc: Any) -> None: self.maxDiff = None doc = to_python(loc) if os.path.exists(pizza_fun): with open(pizza_fun) as f: expected = f.read() self.assertEqual(expected, str(doc.to_functional())) else: with open(pizza_fun, 'w') as f: f.write(str(doc.to_functional())) self.fail(f"{pizza_fun} written to disc - run test again")
def test_owlf_sample(self): import os from funowl.converters.functional_converter import to_python from rdflib import Graph, SKOS, DCTERMS clue = to_python(owlf) g = Graph() clue.to_rdf(g) g.bind('skos', SKOS) g.bind('dct', DCTERMS) owlrdf = g.serialize(format='turtle').decode().strip() # print(owlrdf) self.assertEqual(expected.strip(), owlrdf)
def test_declaration(self): parsed = to_python(text) actual = str(parsed.to_functional()) self.assertEqual( """Prefix( xml: = <http://www.w3.org/XML/1998/namespace> ) Prefix( rdf: = <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ) Prefix( rdfs: = <http://www.w3.org/2000/01/rdf-schema#> ) Prefix( xsd: = <http://www.w3.org/2001/XMLSchema#> ) Prefix( owl: = <http://www.w3.org/2002/07/owl#> ) Prefix( pizza: = <http://www.co-ode.org/ontologies/pizza/pizza.owl#> ) Ontology( <http://www.co-ode.org/ontologies/pizza> <http://www.co-ode.org/ontologies/pizza/2.0.0> Declaration( Class( pizza:American ) ) )""", actual)
def test_snomed(self): doc = to_python('/Users/solbrig/data/terminology/snomedct/' 'SnomedCT_InternationalRF2_PRODUCTION_20190731T120000Z/Snapshot/Terminology/snomed.owl') self.assertTrue(False, "Implement Me")
Prefix(document_ontology:=<https://loinc.org/document_ontology/#>) Prefix(untitled-ontology-17:=<https://loinc.org/dmbaorto/ontologies/2019/5/untitled-ontology-17#>) Ontology(<https://loinc.org/document_ontology/> AnnotationAssertion(rdfs:label <https://loinc.org/document_ontology/#93024-8> "Pharmacist Consult note (D)"^^xsd:string) AnnotationAssertion(document_ontology:hasCode <https://loinc.org/document_ontology/#93024-8> "93024-8"^^xsd:string) SubClassOf(<https://loinc.org/document_ontology/#93024-8> document_ontology:Loincs) SubClassOf(<https://loinc.org/document_ontology/#93024-8> ObjectSomeValuesFrom(document_ontology:document-kind document_ontology:LP173418-7)) SubClassOf(<https://loinc.org/document_ontology/#93024-8> ObjectSomeValuesFrom(document_ontology:document-role document_ontology:LP181523-4)) SubClassOf(<https://loinc.org/document_ontology/#93024-8> ObjectSomeValuesFrom(document_ontology:document-type-of-service document_ontology:LP173110-0)) ) """ ontologydoc = to_python(owl_functional) equivalents: Dict[ClassExpression, List[Axiom]] = dict() # Convert all subclass expressions into the equivalents for axiom in ontologydoc.ontology.axioms: # Note that we can't use isinstance because of type cooercion if issubclass(type(axiom), SubClassOf): equivalents.setdefault(axiom.subClassExpression, []).append(axiom) for class_expression, axioms in equivalents.items(): if len(axioms) == 1: ontologydoc.ontology.equivalentClasses(class_expression, axioms[0].superClassExpression) else: ontologydoc.ontology.equivalentClasses(
This is an example ontology that contains all constructs required for the various versions of the Pizza Tutorial run by Manchester University (see http://owl.cs.manchester.ac.uk/publications/talks-and-tutorials/protg-owl-tutorial).""", lang="en"))) pizza.axioms.append(SubObjectPropertyOf(PIZZA.hasBase, PIZZA.hasIngredient)) pizza.axioms.append(InverseObjectProperties(PIZZA.hasBase, PIZZA.isBaseOf)) pizza.axioms.append(FunctionalObjectProperty(PIZZA.hasBase)) pizza.axioms.append(InverseFunctionalObjectProperty(PIZZA.hasBase)) pizza.axioms.append(ObjectPropertyDomain(PIZZA.hasBase, PIZZA.Pizza)) pizza.axioms.append(ObjectPropertyRange(PIZZA.hasBase, PIZZA.PizzaBase)) print(pizza_doc.to_functional().getvalue()) #%% md ## Ontologies in functional format can be read from files or URL's #%% from funowl.converters.functional_converter import to_python from funowl.writers.FunctionalWriter import FunctionalWriter pizza_doc = to_python( "https://raw.githubusercontent.com/hsolbrig/funowl/master/tests/data/pizza.owl" ) w = FunctionalWriter(g=pizza_doc.add_namespaces(Graph())) for axiom in pizza_doc.ontology.axioms: if isinstance(axiom, SubClassOf) and str( axiom.subClassExpression) == 'pizza:AmericanHot': w.add(axiom) print(str(w))
def test_snomed(self): start_time = time.time() doc = to_python(os.path.join(datadir, 'ontology-after-conversion.owl')) print("--- %s seconds ---" % (time.time() - start_time)) self.assertTrue(False, "Implement Me")