Esempio n. 1
0
class ObjectInverseOf(FunOwlChoice):
    v: Union[ObjectProperty, str] = exclude([str])

    def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
        return w.func(self, lambda: w + self.v)

    def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> Node:
        x = BNode()
        assert isinstance(self.v, ObjectProperty)
        g.add((x, OWL.inverseOf, self.v.to_rdf(g)))
        return x
Esempio n. 2
0
class StringLiteralWithLanguage(FunOwlBase):
    literal: Union[StringLiteralNoLanguage, str, rdflib.Literal] = exclude([str, rdflib.Literal])
    language: Optional[Union[LanguageTag, str]] = exclude([str], default=None)

    def __init__(self, literal: Union[StringLiteralNoLanguage, "StringLiteralWithLanguage", str, rdflib.Literal],
                 language: Optional[Union[LanguageTag, str]] = None) -> None:
        if isinstance(literal, rdflib.Literal):
            self.literal = literal.value
            self.literal = literal.language
        elif isinstance(literal, StringLiteralWithLanguage):
            self.literal = literal.literal
            self.language = literal.language
        else:
            assert language, "Language must be supplied"
            self.literal = StringLiteralNoLanguage(str(literal))
            self.language = language

    def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
        return w.concat(self.literal, self.language)

    def __str__(self) -> str:
        return str(self.literal) + '@' + str(self.language)
Esempio n. 3
0
class IRI(FunOwlChoice):
    """ IRI := fullIRI | abbreviatedIRI """
    v: Union[AbbreviatedIRI, FullIRI, URIRef, str] = exclude([URIRef, str])
    rdf_type: ClassVar[URIRef] = None

    # def __post_init__(self):
    #     print(f"Just constructed a {type(self)} value {str(self.v)}")

    def full_uri(self, g: Graph) -> Optional[URIRef]:
        if isinstance(self.v, URIRef):
            return self.v
        if isinstance(self.v, AbbreviatedIRI):
            # TODO: find the code in rdflib that does this
            ns, local = self.v.split(':', 1)
            for ns1, uri in g.namespaces():
                if ns == ns1:
                    return (Namespace(uri)[local])
            logging.warning(f"IRI: {self.v} - {ns} not a valid prefix")
            return None
        if isinstance(self.v, FullIRI):
            return URIRef(self.v)

    def to_functional(self, w: FunctionalWriter) -> FunctionalWriter:
        fulluri = self.full_uri(w.g)
        return w + (fulluri.n3(w.g.namespace_manager) if fulluri else self.v)

    def to_rdf(self, g: Graph, emit_type_arc: bool = False) -> URIRef:
        fulluri = self.full_uri(g)

        def is_builtin_namespace(n) -> bool:
            # Never add assertions that consist entirely of builtin types
            return (n.startswith(str(XSD)) or n.startswith(str(RDF))
                    or n.startswith(str(RDFS)) or n.startswith(str(OWL)))

        if self.rdf_type:
            # Filter: for (undocumented?) reasons, never assert owl:thing a owl:Class
            if emit_type_arc or not is_builtin_namespace(
                    fulluri) or not is_builtin_namespace(self.rdf_type):
                g.add((fulluri, RDF.type, self.rdf_type))
        return fulluri
Esempio n. 4
0
class ObjectPropertyExpression(FunOwlChoice):
    # The order below is important
    v: Union[ObjectProperty, ObjectInverseOf, str] = exclude([str])
Esempio n. 5
0
class AnnotationValue(FunOwlChoice):
    v: Union[IRI, AnonymousIndividual, Literal, str] = exclude([str])