def test_readme_example2(self): from rdflib import Namespace, XSD, Literal from funowl import Ontology, DataProperty, Class, DataAllValuesFrom, DataOneOf, SubClassOf, DataSomeValuesFrom, \ ClassAssertion, OntologyDocument EX = Namespace("http://example.org/") # Ontology represents the OWLF OntologyDocument production o = Ontology(EX.myOntology, "http://example.org/myOntolology/version/0.1") # namedIndividual, objectProperty, class, et. properties add to declarations o.namedIndividuals(EX.a) # Declarations can also be added explicitly o.declarations(DataProperty(EX.dp), Class(EX.A)) # Axioms are added by type o.subClassOf(EX.A, DataAllValuesFrom(EX.dp, DataOneOf(3, Literal(4, datatype=XSD.int_)))) # or as an array o.axioms.append(SubClassOf(EX.A, DataAllValuesFrom(EX.dp, DataOneOf(Literal(2, datatype=XSD.short), Literal(3, datatype=XSD.int_))))) o.axioms.append(ClassAssertion(DataSomeValuesFrom(EX.dp, DataOneOf(3)), EX.a)) print(str(OntologyDocument(EX, ontology=o).to_functional()))
def test_cyclic_issue(self): # Create relaMath.owl in functional RELA = Namespace("http://sweet.jpl.nasa.gov/2.3/relaMath.owl") REPR = Namespace("http://sweet.jpl.nasa.gov/2.3/reprMath.owl") o = Ontology(RELA) o.imports(REPR) o.declarations(DataProperty(RELA['#hasLowerBound'])) 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( : = <http://sweet.jpl.nasa.gov/2.3/relaMath.owl> ) Prefix( repr: = <http://sweet.jpl.nasa.gov/2.3/reprMath.owl> ) Ontology( <http://sweet.jpl.nasa.gov/2.3/relaMath.owl> Import( <http://sweet.jpl.nasa.gov/2.3/reprMath.owl> ) Declaration( DataProperty( <http://sweet.jpl.nasa.gov/2.3/relaMath.owl#hasLowerBound> ) ) )""", str(OntologyDocument(RELA, repr=REPR, ontology=o))) # Execute the imports statement o.imports(RELA) o.declarations(Class(REPR['#Interval'])) o.axioms.append( DataPropertyAssertion(RELA.hasLowerBound, REPR.NormalizedRange, 0.0)) o.axioms.append( ClassAssertion(URIRef("Interval"), REPR['#NormalizedRange'])) 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( : = <http://sweet.jpl.nasa.gov/2.3/relaMath.owl> ) Prefix( repr: = <http://sweet.jpl.nasa.gov/2.3/reprMath.owl> ) Ontology( <http://sweet.jpl.nasa.gov/2.3/relaMath.owl> Import( <http://sweet.jpl.nasa.gov/2.3/reprMath.owl> ) Import( <http://sweet.jpl.nasa.gov/2.3/relaMath.owl> ) Declaration( DataProperty( <http://sweet.jpl.nasa.gov/2.3/relaMath.owl#hasLowerBound> ) ) Declaration( Class( <http://sweet.jpl.nasa.gov/2.3/reprMath.owl#Interval> ) ) DataPropertyAssertion( <http://sweet.jpl.nasa.gov/2.3/relaMath.owlhasLowerBound> <http://sweet.jpl.nasa.gov/2.3/reprMath.owlNormalizedRange> "0.0"^^xsd:double ) ClassAssertion( <Interval> <http://sweet.jpl.nasa.gov/2.3/reprMath.owl#NormalizedRange> ) )""", str(OntologyDocument(RELA, repr=REPR, ontology=o)))
def fparse(inp: bytes, start: int, consumer: Callable[[FunOwlBase], None]) -> int: """ Functional parser - work through inp pulling complete functions out and processing them. :param inp: input byte stream :param start: current 0 based position in the stream :param consumer: OWLFunc entry consumer :return: final position """ try: while True: start = skip_comments(inp, start) m = function_re.match(inp, start) if not m: break func = m.group(1).decode() start = m.span()[1] # Don't try to pre-parse the arguments for an Ontology if func == "Ontology": o = Ontology() start = skip_comments(inp, start) uri, start = uri_matcher(inp, start) if uri: o.iri = uri start = skip_comments(inp, start) vers, start = uri_matcher(inp, start) if vers: o.version = vers start = skip_comments(inp, start) start = fparse(inp, start, lambda f: o.add_arg(f)) consumer(o) start = skip_comments(inp, start) m = final_pren.match(inp, start) if not m: raise ValueError("Missing final parenthesis") break else: body, start = nested(inp, start) consumer( OWLFunc(m.group(1).decode(), parse_args(body.decode())).decl) except IndexError: pass return start
def test_readme_example1(self): from rdflib import RDFS, OWL, Namespace from funowl import OntologyDocument, Ontology EX = Namespace("http://www.example.com/ontology1#") o = Ontology("http://www.example.com/ontology1") o.imports("http://www.example.com/ontology2") o.annotation(RDFS.label, "An example") o.subClassOf(EX.Child, OWL.Thing) doc = OntologyDocument(EX, o) print(str(doc.to_functional()))
from rdflib import Namespace, Literal, XSD from funowl import Ontology, ObjectProperty, DataProperty, Class, ObjectOneOf, DifferentIndividuals, \ NamedIndividual, ObjectPropertyAssertion, DataPropertyAssertion, OntologyDocument, ClassAssertion EX = Namespace("http://example.org/test#") SP = Namespace("http://nlm.org/singleton#") o = Ontology(EX.singletonText) # General setup for singleton model # NOTE 1: SingletonProperty is NOT in the RDF namespace o.declaration(Class(SP.SingletonProperty)) o.declaration(ObjectProperty(SP.SingletonProperty)) o.axioms.append(ClassAssertion(SP.singletonProperty, SP.SingletonProperty)) # Specific example o.declaration(DataProperty(EX.hasStart)) o.declaration(DataProperty(EX.hasEnd)) o.declaration(ObjectProperty(EX.isMarriedTo)) o.declaration(ObjectProperty(EX.isMarriedTo1)) o.declaration(ObjectProperty(EX.isMarriedTo2)) o.subClassOf(ObjectOneOf(EX.isMarriedTo1, EX.isMarriedTo2), EX.isMarriedTo) o.axioms.append(DifferentIndividuals(EX.isMarriedTo1, EX.isMarriedTo2)) o.declaration(NamedIndividual(EX.BobDylan)) o.declaration(NamedIndividual(EX.SaraLownds)) o.declaration(NamedIndividual(EX.CarolDennis)) o.axioms.append( ObjectPropertyAssertion(EX.isMarriedTo1, EX.BobDylan, EX.SaraLownds))