def add_arg(self, arg: [IRI.types(), Import, Axiom, Annotation]): if isinstance_(arg, Axiom): self.axioms.append(arg) self._naxioms += 1 if not self._naxioms % 100000: print('H') elif not self._naxioms % 10000: print('K') elif not self._naxioms % 1000: print('k', end='') sys.stdout.flush() elif not self._naxioms % 100: print('.', end='') sys.stdout.flush() elif isinstance(arg, IRI): if not self.iri: self.iri = arg elif not self.version: self.version = arg else: raise ValueError(f"Raw IRI is not a valid argument {arg}") elif isinstance(arg, Import): self.directlyImportsDocuments.append(arg) elif isinstance(arg, Annotation): self.annotations.append(arg) else: raise ValueError(f"Unrecognized argument to Ontology: {arg}")
def __init__(self, *args: Union[FunOwlBase, IRI.types()], **kwargs: Dict[str, FunOwlBase]) -> None: args = list(args) if args and isinstance(args[0], IRI) and not isinstance_(args[0], Axiom): self.iri = args.pop(0) if args and isinstance(args[0], IRI) and not isinstance_(args[0], Axiom): self.version = args.pop(0) self.directlyImportsDocuments = cast(List[Import], []) while args and isinstance(args[0], Import): self.directlyImportsDocuments.append(args.pop(0)) self.axioms = cast(List[Axiom], []) while args and isinstance_(args[0], Axiom): self.axioms.append(args.pop(0)) self.annotations = kwargs.get('annotations', []) for k, v in kwargs.items(): cur_v = getattr(self, k, MISSING) if cur_v is MISSING: raise ValueError(f"Unknown argument to Ontology: {k}") if cur_v is None: setattr(self, k, v) elif k != 'annotations': setattr(self, k, cur_v + v) if args: raise ValueError(f"Unrecognized arguments to Ontology: {args}") self._naxioms = 0
def namedIndividuals(self, *individuals: IRI.types()) -> "Ontology": for individual in individuals: self.axioms.append(NamedIndividual(individual)) return self
class Ontology(Annotatable): iri: Optional[IRI.types()] = None version: Optional[IRI.types()] = None directlyImportsDocuments: List[Import] = empty_list() axioms: List[Axiom] = empty_list() annotations: List[Annotation] = empty_list() def __init__(self, *args: Union[FunOwlBase, IRI.types()], **kwargs: Dict[str, FunOwlBase]) -> None: args = list(args) if args and isinstance(args[0], IRI) and not isinstance_(args[0], Axiom): self.iri = args.pop(0) if args and isinstance(args[0], IRI) and not isinstance_(args[0], Axiom): self.version = args.pop(0) self.directlyImportsDocuments = cast(List[Import], []) while args and isinstance(args[0], Import): self.directlyImportsDocuments.append(args.pop(0)) self.axioms = cast(List[Axiom], []) while args and isinstance_(args[0], Axiom): self.axioms.append(args.pop(0)) self.annotations = kwargs.get('annotations', []) for k, v in kwargs.items(): cur_v = getattr(self, k, MISSING) if cur_v is MISSING: raise ValueError(f"Unknown argument to Ontology: {k}") if cur_v is None: setattr(self, k, v) elif k != 'annotations': setattr(self, k, cur_v + v) if args: raise ValueError(f"Unrecognized arguments to Ontology: {args}") self._naxioms = 0 def add_arg(self, arg: [IRI.types(), Import, Axiom, Annotation]): if isinstance_(arg, Axiom): self.axioms.append(arg) self._naxioms += 1 if not self._naxioms % 100000: print('H') elif not self._naxioms % 10000: print('K') elif not self._naxioms % 1000: print('k', end='') sys.stdout.flush() elif not self._naxioms % 100: print('.', end='') sys.stdout.flush() elif isinstance(arg, IRI): if not self.iri: self.iri = arg elif not self.version: self.version = arg else: raise ValueError(f"Raw IRI is not a valid argument {arg}") elif isinstance(arg, Import): self.directlyImportsDocuments.append(arg) elif isinstance(arg, Annotation): self.annotations.append(arg) else: raise ValueError(f"Unrecognized argument to Ontology: {arg}") # ======================= # Syntactic sugar -- fill these in as needed # ======================= def annotation( self, prop: AnnotationProperty.types(), value: AnnotationValue.types() ) -> "Ontology": self.annotations.append(Annotation(prop, value)) return self def declarations(self, *declarations: Declaration.types()) -> "Ontology": for declaration in declarations: self.axioms.append(Declaration(declaration)) return self def subClassOf(self, sub: Class.types(), sup: Class.types()) -> "Ontology": if not issubclass(type(sub), Class) and isinstance(sub, Class): sub = Class(sub) if not issubclass(type(sup), Class) and isinstance(sup, Class): sup = Class(sup) self.axioms.append(SubClassOf(sub, sup)) return self def equivalentClasses(self, *classExpressions: ClassExpression) -> "Ontology": self.axioms.append(EquivalentClasses(*classExpressions)) return self def subObjectPropertyOf(self, sub: SubObjectPropertyExpression.types(), sup: ObjectPropertyExpression.types()) \ -> "Ontology": subp = SubObjectPropertyExpression(sub) supp = ObjectPropertyExpression(sup) self.axioms.append(SubObjectPropertyOf(subp, supp)) return self def inverseObjectProperties(self, exp1: ObjectPropertyExpression.types(), exp2: ObjectPropertyExpression.types()) \ -> "Ontology": exp1p = ObjectPropertyExpression(exp1) exp2p = ObjectPropertyExpression(exp2) self.axioms.append(InverseObjectProperties(exp1p, exp2p)) return self def functionalObjectProperty( self, ope: ObjectPropertyExpression.types()) -> "Ontology": opep = ObjectPropertyExpression(ope) self.axioms.append(FunctionalObjectProperty(opep)) return self def inverseFunctionalObjectProperty( self, ope: ObjectPropertyExpression.types()) -> "Ontology": opep = ObjectPropertyExpression(ope) self.axioms.append(InverseFunctionalObjectProperty(opep)) return self def objectPropertyDomain(self, ope: ObjectPropertyExpression.types(), ce: ClassExpression) -> "Ontology": self.axioms.append(ObjectPropertyDomain(ope, ce)) return self def objectPropertyRange(self, ope: ObjectPropertyExpression.types(), ce: ClassExpression) -> "Ontology": self.axioms.append(ObjectPropertyRange(ope, ce)) return self def imports(self, import_: Union["Ontology", str]) -> "Ontology": self.directlyImportsDocuments.append( Import(import_.iri if isinstance(import_, Ontology ) else IRI(str(import_)))) return self def namedIndividuals(self, *individuals: IRI.types()) -> "Ontology": for individual in individuals: self.axioms.append(NamedIndividual(individual)) return self def dataPropertyAssertion( self, expr: DataPropertyExpression.types(), sourceIndividual: Individual.types(), targetValue: Literal.types() ) -> "Ontology": self.axioms.append( DataPropertyAssertion(expr, sourceIndividual, targetValue)) return self # ==================== # Conversion functions # ==================== def to_functional(self, w: Optional[FunctionalWriter]) -> FunctionalWriter: """ Return a FunctionalWriter instance with the representation of the ontology in functional syntax """ if self.version and not self.iri: raise ValueError( f"Ontology cannot have a versionIRI ({self.version} without an ontologyIRI" ) w = w or FunctionalWriter() return w.func(self, lambda: w.opt(self.iri).opt(self.version).br( bool(self.directlyImportsDocuments) or bool( self.annotations) or bool(self.axioms)). iter(self.directlyImportsDocuments, indent=False).iter( self.annotations, indent=False).iter(self.axioms, indent=False), indent=False) def to_rdf(self, g: Graph) -> SUBJ: ontology_uri = self.iri.to_rdf(g) if self.iri else BNode() version_uri = self.version.to_rdf(g) if self.version else None g.add((ontology_uri, RDF.type, OWL.Ontology)) if self.version: g.add((ontology_uri, OWL.versionIRI, version_uri)) for imp in self.directlyImportsDocuments: g.add((ontology_uri, OWL.imports, imp.to_rdf(g))) for axiom in self.axioms: axiom.to_rdf(g) super().to_rdf(g) return ontology_uri
class Class(IRI): v: IRI.types() = IRI.v_field() rdf_type: ClassVar[URIRef] = OWL.Class