class DisjointObjectProperties(Annotatable): objectPropertyExpressions: List[ObjectPropertyExpression] annotations: List[Annotation] = empty_list_wrapper(Annotation) def __init__(self, *objectPropertyExpressions: ObjectPropertyExpression, annotations: Optional[List[Annotation]] = None) -> None: self.objectPropertyExpressions = list(objectPropertyExpressions) self.annotations = annotations or [] super().__init__() def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots(w, lambda: w.iter(self.objectPropertyExpressions)) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: # T(OPE1) owl:propertyDisjointWith T(OPE2) . # N > 2: # _:x rdf:type owl:AllDisjointProperties . # _:x owl:members T(SEQ OPE1 ... OPEn) . if len(self.objectPropertyExpressions) > 2: x = BNode() g.add((x, RDF.type, OWL.AllDisjointProperties)) self.add_triple(g, x, OWL.members, SEQ(g, self.objectPropertyExpressions)) else: self.add_triple(g, self.objectPropertyExpressions[0].to_rdf(g), OWL.propertyDisjointWith, self.objectPropertyExpressions[1].to_rdf(g))
class DisjointDataProperties(Annotatable): dataPropertyExpressions: List[DataPropertyExpression] annotations: List[Annotation] = empty_list_wrapper(Annotation) def __init__(self, *dataPropertyExpressions: DataPropertyExpression, annotations: List[Annotation] = None) -> None: dpes = [DataPropertyExpression(dpe) for dpe in dataPropertyExpressions] self.dataPropertyExpressions = ListWrapper(dpes, DataPropertyExpression) self.annotations = annotations or [] super().__init__() def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: self.list_cardinality(self.dataPropertyExpressions, 'exprs', 2) return self.annots(w, lambda: w.iter(self.dataPropertyExpressions)) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: # N == 2 # T(DPE1) owl:propertyDisjointWith T(DPE2) . # N > 2 # _:x rdf:type owl:AllDisjointProperties . # _:x owl:members T(SEQ DPE1 ... DPEn) . if len(self.dataPropertyExpressions) <= 2: self.add_triple(g, self.dataPropertyExpressions[0].to_rdf(g), OWL.propertyDisjointWith, self.dataPropertyExpressions[1].to_rdf(g)) else: x = BNode() g.add((x, RDF.type, OWL.AllDisjointProperties)) self.add_triple(g, x, OWL.members, SEQ(g, self.dataPropertyExpressions))
class DisjointClasses(Annotatable): classExpressions: List[ClassExpression] annotations: List[Annotation] = empty_list_wrapper(Annotation) def __init__(self, *classExpression: ClassExpression, annotations: List[Annotation] = None) -> None: self.classExpressions = list(classExpression) self.annotations = annotations or [] super().__init__() def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: self.list_cardinality(self.classExpressions, 'classExpressions', 2) if len(self.classExpressions) == 2: return self.annots( w, lambda: w + self.classExpressions[0] + self. classExpressions[1]) else: return self.annots(w, lambda: w.iter(self.classExpressions)) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: if len(self.classExpressions) == 2: self.add_triple(g, self.classExpressions[0].to_rdf(g), OWL.disjointWith, self.classExpressions[1].to_rdf(g)) else: subj = BNode() g.add((subj, RDF.type, OWL.AllDisjointClasses)) g.add((subj, OWL.members, SEQ(g, self.classExpressions))) self.TANN(g, subj)
class SameIndividual(Annotatable): individuals: List[Individual] annotations: List[Annotation] = empty_list_wrapper(Annotation) def __init__(self, *individuals: Individual, annotations: Optional[List[Annotation]] = None) -> None: self.individuals = list(individuals) self.annotations = annotations or [] super().__init__() def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.list_cardinality(self.individuals, 'individuals', 2).\ annots(w, lambda: w.iter(self.individuals, f=lambda o: w + o, indent=False)) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: rdf_individuals = [ind.to_rdf(g) for ind in self.individuals] for i in range(0, len(rdf_individuals)): for j in rdf_individuals[i + 1:]: g.add((rdf_individuals[i], OWL.sameAs, j)) if self.annotations: x = BNode() g.add((x, RDF.type, self.annotation_type)) for src in rdf_individuals[:-1]: g.add((x, OWL.annotatedSource, clone_subgraph(g, src) if USE_BNODE_COPIES else src)) g.add((x, OWL.annotatedProperty, OWL.sameAs)) for tgt in rdf_individuals[1:]: g.add((x, OWL.annotatedTarget, clone_subgraph(g, tgt) if USE_BNODE_COPIES else tgt)) for annotation in self.annotations: t = (x, annotation.property.to_rdf(g), annotation.value.to_rdf(g)) g.add(t) annotation.TANN(g, t)
class DifferentIndividuals(Annotatable): individuals: List[Individual] annotations: List[Annotation] = empty_list_wrapper(Annotation) def __init__(self, *individuals: Individual, annotations: Optional[List[Annotation]] = None) -> None: self.individuals = list(individuals) self.annotations = annotations or [] super().__init__() def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.list_cardinality(self.individuals, 'individuals', 2).\ annots(w, lambda: w.iter(self.individuals, f=lambda o: w + o, indent=False)) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: # N == 2 # T(a1) owl:differentFrom T(a2) . # N > 2 # _:x rdf:type owl:AllDifferent . # _:x owl:members T(SEQ a1 ... an) . # TODO: Spec says members, tooling says "distinctMembers" -- error in spec? if len(self.individuals) == 2: self.add_triple(g, self.individuals[0].to_rdf(g), OWL.differentFrom, self.individuals[1].to_rdf(g)) elif len(self.individuals) > 2: subj = BNode() g.add((subj, RDF.type, OWL.AllDifferent)) g.add((subj, OWL.distinctMembers, SEQ(g, self.individuals))) self.TANN(g, subj) return None
class Foo(FunOwlBase): v: List[Foo2] = empty_list_wrapper(Foo2) def to_functional(self, wr: FunctionalWriter) -> FunctionalWriter: return wr.iter(self.v, f=lambda e: wr.hardbr() + e) def to_functional2(self, wr: FunctionalWriter) -> FunctionalWriter: return wr.hardbr().indent().iter(self.v).outdent()
class InverseFunctionalObjectProperty(Annotatable): objectPropertyExpression: ObjectPropertyExpression annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots(w, lambda: w + self.objectPropertyExpression) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: g.add((self.objectPropertyExpression.to_rdf(g), RDF.type, OWL.InverseFunctionalProperty))
class OntologyDocument(FunOwlBase): """ prefixDeclarations are """ prefixDeclarations: List[Prefix] = empty_list_wrapper(Prefix) ontology: Ontology = None def __init__(self, default_prefix: FullIRI = None, ontology: Optional[Ontology] = None, **prefixes: FullIRI): self.prefixDeclarations = [] self.ontology = ontology if ontology is not None else Ontology() if default_prefix: self.prefixDeclarations.append(Prefix(None, default_prefix)) if prefixes: for k, v in prefixes.items(): self.prefixDeclarations.append(Prefix(k, v)) def prefixes(self, dflt: Optional[FullIRI], **prefixes: FullIRI) -> None: if dflt: self.prefixDeclarations.append(Prefix('', dflt)) for ns, iri in prefixes.items(): self.prefixDeclarations.append(Prefix(PrefixName(ns), iri)) def __setattr__(self, key: str, value) -> None: if key.startswith('_') or key in ('prefixDeclarations', 'ontology'): super().__setattr__(key, value) else: prefix = Prefix(PrefixName(key) if key else None, FullIRI(value)) self.prefixDeclarations.append(prefix) def __getattr__(self, item): # This gets called only when something isn't already in the dictionary if isinstance(item, PrefixName): for p in self.prefixDeclarations: if p.prefixName == item: return p.fullIRI return super().__getattribute__(item) def __str__(self) -> str: return self.to_functional().getvalue() def add_namespaces(self, g: Graph) -> Graph: for prefix in self.prefixDeclarations: g.namespace_manager.bind(str(prefix.prefixName or ''), str(prefix.fullIRI), True, True) return g def to_functional(self, w: Optional[FunctionalWriter] = None) -> FunctionalWriter: """ Return a FunctionalWriter instance with the representation of the OntologyDocument in functional syntax """ w = w or FunctionalWriter() self.add_namespaces(w.g) return w.iter([Prefix(ns, uri) for ns, uri in w.g.namespaces()], indent=False).hardbr() + \ (self.ontology or Ontology()) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> SUBJ: self.add_namespaces(g) return self.ontology.to_rdf(g)
class SubAnnotationPropertyOf(Annotatable): sub: AnnotationProperty super: AnnotationProperty annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots(w, lambda: w + self.sub + self.super) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: self.add_triple(g, self.sub.to_rdf(g), RDFS.subPropertyOf, self.super.to_rdf(g))
class TransitiveObjectProperty(Annotatable): objectPropertyExpression: ObjectPropertyExpression annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots(w, lambda: w + self.objectPropertyExpression) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: # T(OPE) rdf:type owl:TransitiveProperty . self.add_triple(g, self.objectPropertyExpression.to_rdf(g), RDF.type, OWL.TransitiveProperty)
class ClassAssertion(Annotatable): expr: ClassExpression individual: Individual annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots(w, lambda: w + self.expr + self.individual) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: self.add_triple(g, self.individual.to_rdf(g), RDF.type, self.expr.to_rdf(g))
class FunctionalDataProperty(Annotatable): dataPropertyExpression: DataPropertyExpression annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots(w, lambda: w + self.dataPropertyExpression) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: # T(DPE) rdf:type owl:FunctionalProperty . self.add_triple(g, self.dataPropertyExpression.to_rdf(g), RDF.type, OWL.FunctionalProperty)
class AnnotationPropertyRange(Annotatable): property: AnnotationProperty range: IRI annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots(w, lambda: w + self.property + self.range) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: self.add_triple(g, self.property.to_rdf(g), RDFS.range, self.range.to_rdf(g))
class DatatypeDefinition(Annotatable): datatype: Datatype datarange: DataRange annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots(w, lambda: w + self.datatype + ' ' + self.datarange) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: # T(DT) owl:equivalentClass T(DR) . self.add_triple(g, self.datatype.to_rdf(g), OWL.equivalentClass, self.datarange.to_rdf(g))
class DataPropertyRange(Annotatable): dataPropertyExpression: DataPropertyExpression dataRange: DataRange annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots( w, lambda: w + self.dataPropertyExpression + ' ' + self.dataRange) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: # T(DPE) rdfs:range T(DR) . return self.add_triple(g, self.dataPropertyExpression.to_rdf(g), RDFS.range, self.dataRange.to_rdf(g))
class Declaration(FunOwlChoice, Annotatable): v: Union[Class, Datatype, ObjectProperty, DataProperty, AnnotationProperty, NamedIndividual] annotations: List[Annotation] = empty_list_wrapper(Annotation) _coercion_allowed = False def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return w.func( self, lambda: w.func( self.v, lambda: self.v.to_functional(w), indent=False)) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> Optional[NODE]: return super().to_rdf(g, emit_type_arc=True)
class AnnotationAssertion(Annotatable): property: AnnotationProperty subject: AnnotationSubject value: AnnotationValue annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots( w, lambda: w + self.property + self.subject + self.value) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: self.add_triple(g, self.subject.to_rdf(g), self.property.to_rdf(g), self.value.to_rdf(g))
class DataPropertyAssertion(Annotatable): expr: DataPropertyExpression sourceIndividual: Individual targetValue: Literal annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots( w, lambda: w + self.expr + self.sourceIndividual + self.targetValue) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: self.add_triple(g, self.sourceIndividual.to_rdf(g), self.expr.to_rdf(g), self.targetValue.to_rdf(g))
class DataPropertyDomain(Annotatable): dataPropertyExpression: DataPropertyExpression classExpression: ClassExpression annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots( w, lambda: w + self.dataPropertyExpression + ' ' + self. classExpression) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: # T(DPE) rdfs:domain T(CE) . self.add_triple(g, self.dataPropertyExpression.to_rdf(g), RDFS.domain, self.classExpression.to_rdf(g))
class ObjectPropertyRange(Annotatable): objectPropertyExpression: ObjectPropertyExpression classExpression: ClassExpression annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots( w, lambda: w + self.objectPropertyExpression + self.classExpression) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: # T(OPE) rdfs:range T(CE) . self.add_triple(g, self.objectPropertyExpression.to_rdf(g), RDFS.range, self.classExpression.to_rdf(g))
class HasKey(Annotatable): classExpression: ClassExpression objectPropertyExpressions: Optional[ List[ObjectPropertyExpression]] = empty_list_wrapper( ObjectPropertyExpression) dataPropertyExpressions: Optional[List[ DataPropertyExpression]] = empty_list_wrapper(DataPropertyExpression) annotations: List[Annotation] = empty_list_wrapper(Annotation) def __init__(self, classExpression: ClassExpression, *exprs: Union[ObjectPropertyExpression, DataPropertyExpression], annotations: List[Annotation] = None): self.classExpression = classExpression self.objectPropertyExpressions = [] self.dataPropertyExpressions = [] for expr in exprs: if issubclass(type(expr), ObjectPropertyExpression): self.objectPropertyExpressions.append(expr) else: self.dataPropertyExpressions.append(expr) self.annotations = annotations or [] super().__init__() def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots( w, lambda: ((w + self.classExpression + '(').iter( self.objectPropertyExpressions) + ')' + '(').iter( self.dataPropertyExpressions) + ')') def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: # T(CE) owl:hasKey T(SEQ OPE1 ... OPEm DPE1 ... DPEn ) . self.add_triple( g, self.classExpression.to_rdf(g), OWL.hasKey, SEQ(g, self.objectPropertyExpressions + self.dataPropertyExpressions))
class SubDataPropertyOf(Annotatable): subDataPropertyExpression: DataPropertyExpression superDataPropertyExpression: DataPropertyExpression annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots( w, lambda: w + self.subDataPropertyExpression + self. superDataPropertyExpression) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: # T(DPE1) rdfs:subPropertyOf T(DPE2) . self.add_triple(g, self.subrDataPropertyExpression.to_rdf(g), RDFS.subPropertyOf, self.superDataPropertyExpression.to_rdf(g))
class ObjectPropertyChain(Annotatable): objectPropertyExpressions: List[ObjectPropertyExpression] annotations: List[Annotation] = empty_list_wrapper(Annotation) def __init__(self, *objectPropertyExpressions: ObjectPropertyExpression, annotations: List[Annotation] = None): self.objectPropertyExpressions = list(objectPropertyExpressions) self.annotations = annotations if annotations else [] super().__init__() def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return w.func(self, lambda: w.iter(self.objectPropertyExpressions)) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> BNode: return SEQ(g, self.objectPropertyExpressions)
class SubClassOf(Annotatable): subClassExpression: ClassExpression superClassExpression: ClassExpression annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots( w, lambda: (w + self.subClassExpression + self.superClassExpression)) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: """ Add subclass representation to graph :param g: Graph to add representation to :return: None - """ self.add_triple(g, self.subClassExpression.to_rdf(g), RDFS.subClassOf, self.superClassExpression.to_rdf(g))
class NegativeDataPropertyAssertion(Annotatable): expr: DataPropertyExpression sourceIndividual: Individual targetValue: Literal annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots( w, lambda: w + self.expr + self.sourceIndividual + self.targetValue) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: subj = BNode() g.add((subj, RDF.type, OWL.NegativePropertyAssertion)) g.add((subj, OWL.sourceIndividual, self.sourceIndividual.to_rdf(g))) g.add((subj, OWL.assertionProperty, self.expr.to_rdf(g))) g.add((subj, OWL.targetValue, self.targetValue.to_rdf(g))) self.TANN(g, subj)
class InverseObjectProperties(Annotatable): objectPropertyExpressions: List[ObjectPropertyExpression] annotations: List[Annotation] = empty_list_wrapper(Annotation) def __init__(self, *objectPropertyExpressions: ObjectPropertyExpression, annotations: List[Annotation] = None) \ -> None: self.objectPropertyExpressions = list(objectPropertyExpressions) self.annotations = annotations or [] def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: self.list_cardinality(self.objectPropertyExpressions, 'expressions', 2, 2) return w.func( self, lambda: w + self.objectPropertyExpressions[0] + self. objectPropertyExpressions[1]) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: g.add((self.objectPropertyExpressions[0].to_rdf(g), OWL.inverseOf, self.objectPropertyExpressions[1].to_rdf(g)))
class EquivalentObjectProperties(Annotatable): objectPropertyExpressions: List[ObjectPropertyExpression] annotations: List[Annotation] = empty_list_wrapper(Annotation) def __init__(self, *objectPropertyExpressions: ObjectPropertyExpression, annotations: Optional[List[Annotation]] = None) -> None: self.objectPropertyExpressions = list(objectPropertyExpressions) self.annotations = annotations or [] super().__init__() def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots(w, lambda: w.iter(self.objectPropertyExpressions)) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: for t1, t2 in zip(self.objectPropertyExpressions[:-1], self.objectPropertyExpressions[1:]): self.add_triple(g, t1.to_rdf(g), OWL.equivalentProperty, t2.to_rdf(g))
class SubObjectPropertyOf(Annotatable): subObjectPropertyExpression: SubObjectPropertyExpression superObjectPropertyExpression: ObjectPropertyExpression annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots( w, lambda: (w + self.subObjectPropertyExpression + self. superObjectPropertyExpression)) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: if issubclass(type(self.subObjectPropertyExpression.v), ObjectPropertyChain): self.add_triple(g, self.superObjectPropertyExpression.to_rdf(g), OWL.propertyChainAxiom, self.subObjectPropertyExpression.to_rdf(g)) else: self.add_triple(g, self.subObjectPropertyExpression.to_rdf(g), RDFS.subPropertyOf, self.superObjectPropertyExpression.to_rdf(g))
class EquivalentClasses(Annotatable): classExpressions: List[ClassExpression] annotations: List[Annotation] = empty_list_wrapper(Annotation) def __init__(self, *classExpression: ClassExpression, annotations: List[Annotation] = None) -> None: self.classExpressions = ListWrapper(list(classExpression), ClassExpression) self.annotations = annotations or [] def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: self.list_cardinality(self.classExpressions, 'classExpressions', 2) return self.annots(w, lambda: w.iter(self.classExpressions)) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: # EquivalentClasses( CE1 ... CEn ) T(CE1) owl:equivalentClass T(CE2) . # ... # T(CEn-1) owl:equivalentClass T(CEn) . for ce1, ce2 in zip(self.classExpressions[:-1], self.classExpressions[1:]): self.add_triple(g, ce1.to_rdf(g), OWL.equivalentClass, ce2.to_rdf(g))
class ObjectPropertyAssertion(Annotatable): # Should be ObjectProperty instead of ObjectPropertyExpression expr: ObjectPropertyExpression sourceIndividual: Individual targetIndividual: Individual annotations: List[Annotation] = empty_list_wrapper(Annotation) def to_functional(self, w: FunctionalWriter) -> FunctionalWriter: return self.annots( w, lambda: w + self.expr + self.sourceIndividual + self. targetIndividual) def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> None: # ObjectPropertyAssertion( OP a1 a2 ) T(a1) T(OP) T(a2) . # ObjectPropertyAssertion( ObjectInverseOf( OP ) a1 a2 ) T(a2) T(OP) T(a1) . if issubclass(type(self.expr), ObjectInverseOf): self.add_triple(g, self.targetIndividual.to_rdf(g), self.expr.v.v.to_rdf(g), self.sourceIndividual.to_rdf(g)) else: self.add_triple(g, self.sourceIndividual.to_rdf(g), self.expr.to_rdf(g), self.targetIndividual.to_rdf(g))