def test_annotation_annotation(self): self.assertEqual( a1, Annotation(RDFS.comment, "Outer", [ Annotation(RDFS.comment, "Middle 1"), Annotation(RDFS.label, "Middle 2", [Annotation(RDFS.comment, "Inner")]) ]).to_functional(self.w).getvalue())
def test_ontology_document(self): doc = OntologyDocument() self.assertEqual(expected([]), str(doc.to_functional().getvalue())) doc.ontology.iri = "http://snomed.info/sct/" self.assertEqual(expected(['<http://snomed.info/sct/>']), str(doc.to_functional().getvalue())) doc.ontology.version = "http://snomed.info/sct/version/201802017" self.assertEqual(expected(['<http://snomed.info/sct/> <http://snomed.info/sct/version/201802017>']), doc.to_functional().getvalue()) doc.prefixes(RDFS, owl=OWL, rdf=RDF) 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://www.w3.org/2000/01/rdf-schema#> ) Ontology( <http://snomed.info/sct/> <http://snomed.info/sct/version/201802017> )''', str(doc.to_functional())) doc.ontology.imports(SKOS) doc.ontology.imports("http://example.org/dep") doc.ontology.annotations.append(Annotation(RDFS.label, "foo")) 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://www.w3.org/2000/01/rdf-schema#> ) Ontology( <http://snomed.info/sct/> <http://snomed.info/sct/version/201802017> Import( <http://www.w3.org/2004/02/skos/core#> ) Import( <http://example.org/dep> ) Annotation( :label "foo" ) )''', str(doc.to_functional()))
def test_annotatable_constructor(self): """ A single annotation should get transformed into a list by the annotation constructor """ @dataclass class Foo(Annotatable): annotations: List[Annotation] = empty_list() a = Annotation(RDFS.comment, "Not a list") x = Foo(a) self.assertEqual(x.annotations, [a])
def test_classassertion(self): self.assertEqual('ClassAssertion( a:GriffinFamilyMember a:Peter_Griffin )', ClassAssertion(A.GriffinFamilyMember, A.Peter_Griffin).to_functional(self.wa).getvalue()) self.wa.reset() self.assertEqual("""ClassAssertion( Annotation( rdfs:comment "It runs in..." ) a:GriffinFamilyMember a:Peter_Griffin )""", ClassAssertion(A.GriffinFamilyMember, A.Peter_Griffin, Annotation(RDFS.comment, "It runs in...")).to_functional(self.wa).getvalue())
def test_annotatable(self): @dataclass class Foo(Annotatable): props: List[IRI] = empty_list() annotations: List[Annotation] = empty_list() def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots(w, lambda: w.iter(self.props, indent=False)) self.assertEqual("Foo( )", Foo().to_functional(self.w).getvalue()) f = Foo(annotations=[Annotation(RDFS.comment, "t1")]) self.w.reset() self.assertEqual("""Foo( Annotation( rdfs:comment "t1" ) )""", f.to_functional(self.w).getvalue()) self.w.reset() f.props += [RDF.type, RDFS.label] f.props.append(OWL.Ontology) self.assertEqual( """Foo( Annotation( rdfs:comment "t1" ) rdf:type rdfs:label owl:Ontology )""", f.to_functional(self.w).getvalue()) self.w.reset() f.annotations[0].annotations.append( Annotation(RDFS.comment, "This is great")) self.assertEqual( """Foo( Annotation( Annotation( rdfs:comment "This is great" ) rdfs:comment "t1" ) rdf:type rdfs:label owl:Ontology )""", f.to_functional(self.w).getvalue())
def test_annotation_domain_range(self): self.assertEqual( a2d, AnnotationPropertyDomain( RDFS.comment, RDFS.Resource).to_functional(self.w).getvalue()) self.w.reset() self.assertEqual( a2r, AnnotationPropertyRange(RDFS.comment, XSD.string, [Annotation(RDFS.comment, "test") ]).to_functional(self.w).getvalue())
def test_differentindividuals(self): self.assertEqual('DifferentIndividuals( a:Peter a:Peter_Griffin )', DifferentIndividuals(A.Peter, A.Peter_Griffin).to_functional(self.wa).getvalue()) with self.assertRaises(ValueError): # Only one individual DifferentIndividuals(A.Peter).to_functional(self.wa) self.wa.reset() self.assertEqual("""DifferentIndividuals( Annotation( rdfs:comment "We know better" ) a:Alex a:Bob a:Charlie )""", DifferentIndividuals(A.Alex, A.Bob, A.Charlie, annotations=Annotation(RDFS.comment, "We know better")).to_functional(self.wa).getvalue())
def test_sameindividual(self): self.assertEqual('SameIndividual( a:Peter a:Peter_Griffin )', SameIndividual(A.Peter, A.Peter_Griffin).to_functional(self.wa).getvalue()) with self.assertRaises(ValueError): # Only one individual SameIndividual(A.Peter).to_functional(self.wa) self.wa.reset() self.assertEqual("""SameIndividual( Annotation( rdfs:comment "Many aliases" ) a:Alex a:Bob a:Charlie )""", SameIndividual(A.Alex, A.Bob, A.Charlie, annotations=Annotation(RDFS.comment, "Many aliases")).to_functional(self.wa).getvalue())
def test_differentindividuals_rdf(self): g = Graph() g.bind('owl', str(OWL)) DifferentIndividuals(A.Alex, A.Bob).to_rdf(g) self.assertEqual("""@prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix xml: <http://www.w3.org/XML/1998/namespace> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <http://example.org/a#Alex> a owl:NamedIndividual ; owl:differentFrom <http://example.org/a#Bob> . <http://example.org/a#Bob> a owl:NamedIndividual .""", g.serialize(format="turtle").decode().strip()) g = Graph(namespace_manager=g.namespace_manager) DifferentIndividuals(A.Alex, A.Bob, A.Fred, A.Joe).to_rdf(g) print(g.serialize(format="turtle").decode()) g = Graph(namespace_manager=g.namespace_manager) DifferentIndividuals(A.Alex, A.Bob, annotations=[Annotation(RDFS.label, "Test")]).to_rdf(g) print(g.serialize(format="turtle").decode())
def annotation( self, prop: AnnotationProperty.types(), value: AnnotationValue.types() ) -> "Ontology": self.annotations.append(Annotation(prop, value)) return self
def test_annotation_anon(self): self.assertEqual( "Annotation( rdfs:label _:12345 )", Annotation(RDFS.label, '_:12345').to_functional(self.w).getvalue())
def test_annotation_IRI(self): self.assertEqual( "Annotation( rdfs:label rdfs:Resource )", Annotation(RDFS.label, RDFS.Resource).to_functional(self.w).getvalue())
def test_basic_annotation(self): self.assertEqual( 'Annotation( rdfs:label "This is a test" )', Annotation(RDFS.label, "This is a test").to_functional(self.w).getvalue())