Quick reference for all major classes and functions in owlapy.
from owlapy import (
owl_expression_to_dl,
owl_expression_to_manchester,
owl_expression_to_sparql,
dl_to_owl_expression,
manchester_to_owl_expression
)Convert OWL expression to Description Logic syntax.
expr = OWLObjectSomeValuesFrom(has_child, male)
dl = owl_expression_to_dl(expr) # "∃ hasChild.Male"Convert OWL expression to Manchester syntax.
manchester = owl_expression_to_manchester(expr) # "hasChild some Male"Convert OWL expression to SPARQL query.
sparql = owl_expression_to_sparql(expr)
# SELECT DISTINCT ?x WHERE { ... }Parse Manchester syntax to OWL expression.
expr = manchester_to_owl_expression(
"Person and (hasChild some Male)",
namespace="http://example.com/onto#"
)Parse DL syntax to OWL expression.
expr = dl_to_owl_expression(
"Person ⊓ (∃ hasChild.Male)",
namespace="http://example.com/onto#"
)from owlapy.owl_ontology import SyncOntologyThread-safe ontology implementation using owlready2.
Constructor:
SyncOntology(path: str)
SyncOntology(iri: IRI)Key Methods:
classes_in_signature() -> Iterable[OWLClass]- Get all classesindividuals_in_signature() -> Iterable[OWLNamedIndividual]- Get all individualsobject_properties_in_signature() -> Iterable[OWLObjectProperty]- Get all object propertiesdata_properties_in_signature() -> Iterable[OWLDataProperty]- Get all data propertiesadd_axiom(axiom: OWLAxiom)- Add axiom to ontologyremove_axiom(axiom: OWLAxiom)- Remove axiomsave(path: str, format: str = 'rdfxml')- Save ontology
Example:
onto = SyncOntology("family.owl")
classes = list(onto.classes_in_signature())
onto.add_axiom(OWLSubClassOfAxiom(student, person))
onto.save("updated_family.owl")Alternative lightweight ontology implementation.
from owlapy.owl_ontology import Ontology
onto = Ontology("family.owl")Ontology with neural network-backed reasoning.
from owlapy.owl_ontology import NeuralOntology
onto = NeuralOntology("ontology.owl", "embeddings.pkl")from owlapy.owl_reasoner import RDFLibReasonerPure Python reasoner using SPARQL queries. No circular dependencies, efficient caching.
Constructor:
RDFLibReasoner(ontology: SyncOntology | Ontology)Key Methods:
instances(ce: OWLClassExpression, direct: bool = False) -> Iterable[OWLNamedIndividual]sub_classes(ce: OWLClass, direct: bool = False) -> Iterable[OWLClass]super_classes(ce: OWLClass, direct: bool = False) -> Iterable[OWLClass]equivalent_classes(ce: OWLClass) -> Iterable[OWLClass]disjoint_classes(ce: OWLClass) -> Iterable[OWLClass]types(ind: OWLNamedIndividual, direct: bool = False) -> Iterable[OWLClass]object_property_values(ind: OWLNamedIndividual, prop: OWLObjectProperty) -> Iterable[OWLNamedIndividual]data_property_values(ind: OWLNamedIndividual, prop: OWLDataProperty) -> Iterable[OWLLiteral]
Example:
reasoner = RDFLibReasoner(onto)
males = list(reasoner.instances(OWLClass(NS + "Male")))
subclasses = list(reasoner.sub_classes(person, direct=True))Fast Python reasoner using owlready2. Warning: Has circular dependency issue #205.
from owlapy.owl_reasoner import StructuralReasoner
reasoner = StructuralReasoner(onto)Complete OWL 2 DL reasoner using Java (HermiT, Pellet, JFact, ELK, Openllet).
from owlapy.owl_reasoner import SyncReasoner
from owlapy.static_funcs import startJVM, stopJVM
startJVM()
reasoner = SyncReasoner(onto, reasoner="HermiT")
# ... use reasoner ...
stopJVM()Additional Methods:
has_consistent_ontology() -> bool- Check consistencyis_entailed(axiom: OWLAxiom) -> bool- Check entailmentget_root_ontology() -> Ontology- Get underlying ontology with justification support
from owlapy.class_expression import OWLClass, OWLThing, OWLNothingNamed OWL class.
person = OWLClass("http://example.com/onto#Person")Top class (⊤) - contains everything.
top = OWLThingBottom class (⊥) - contains nothing.
bottom = OWLNothingfrom owlapy.class_expression import (
OWLObjectIntersectionOf,
OWLObjectUnionOf,
OWLObjectComplementOf
)Intersection (AND, ⊓).
teacher_researcher = OWLObjectIntersectionOf([teacher, researcher])Union (OR, ⊔).
student_or_employee = OWLObjectUnionOf([student, employee])Complement (NOT, ¬).
not_male = OWLObjectComplementOf(male)from owlapy.class_expression import (
OWLObjectSomeValuesFrom,
OWLObjectAllValuesFrom,
OWLObjectHasValue
)Existential quantification (∃).
has_child = OWLObjectSomeValuesFrom(has_child_prop, male) # ∃ hasChild.MaleUniversal quantification (∀).
only_male_children = OWLObjectAllValuesFrom(has_child_prop, male) # ∀ hasChild.MaleValue restriction.
johns_child = OWLObjectHasValue(has_parent_prop, john) # ∃ hasParent.{John}from owlapy.class_expression import (
OWLObjectMinCardinality,
OWLObjectMaxCardinality,
OWLObjectExactCardinality
)Minimum cardinality (≥).
at_least_two_children = OWLObjectMinCardinality(2, has_child_prop) # ≥2 hasChildMaximum cardinality (≤).
at_most_one_spouse = OWLObjectMaxCardinality(1, has_spouse_prop) # ≤1 hasSpouseExact cardinality (=).
exactly_three_sons = OWLObjectExactCardinality(3, has_child_prop, male) # =3 hasChild.Malefrom owlapy.class_expression import OWLObjectOneOfEnumeration of individuals.
specific_people = OWLObjectOneOf([john, mary, bob]) # {John, Mary, Bob}from owlapy.class_expression import (
OWLDataSomeValuesFrom,
OWLDataAllValuesFrom,
OWLDataHasValue,
OWLDataMinCardinality,
OWLDataMaxCardinality,
OWLDataExactCardinality
)Similar to object restrictions but for data properties.
from owlapy.owl_property import OWLObjectProperty, OWLObjectInverseOfObject property (relates individuals to individuals).
has_parent = OWLObjectProperty("http://example.com/onto#hasParent")Inverse property.
has_child = OWLObjectInverseOf(has_parent) # hasChild ≡ hasParent⁻from owlapy.owl_property import OWLDataPropertyData property (relates individuals to literals).
has_age = OWLDataProperty("http://example.com/onto#hasAge")from owlapy.owl_individual import OWLNamedIndividualNamed individual (instance).
john = OWLNamedIndividual("http://example.com/onto#John")from owlapy.owl_literal import OWLLiteral
from owlapy.owl_datatype import (
IntegerOWLDatatype,
DoubleOWLDatatype,
BooleanOWLDatatype,
StringOWLDatatype,
DateOWLDatatype,
DateTimeOWLDatatype
)Literal value.
age = OWLLiteral(25, IntegerOWLDatatype)
height = OWLLiteral(1.75, DoubleOWLDatatype)
name = OWLLiteral("John Doe", StringOWLDatatype)
is_student = OWLLiteral(True, BooleanOWLDatatype)from owlapy.owl_axiom import (
OWLSubClassOfAxiom,
OWLEquivalentClassesAxiom,
OWLDisjointClassesAxiom,
OWLDisjointUnionAxiom
)Subclass axiom (⊑).
axiom = OWLSubClassOfAxiom(student, person) # Student ⊑ PersonEquivalence axiom (≡).
axiom = OWLEquivalentClassesAxiom([male, not_female]) # Male ≡ ¬FemaleDisjointness axiom.
axiom = OWLDisjointClassesAxiom([male, female]) # Male ⊥ Femalefrom owlapy.owl_axiom import (
OWLClassAssertionAxiom,
OWLObjectPropertyAssertionAxiom,
OWLDataPropertyAssertionAxiom,
OWLSameIndividualAxiom,
OWLDifferentIndividualsAxiom
)Class membership.
axiom = OWLClassAssertionAxiom(john, person) # John : PersonObject property assertion.
axiom = OWLObjectPropertyAssertionAxiom(john, has_parent, mary) # John hasParent MaryData property assertion.
axiom = OWLDataPropertyAssertionAxiom(john, has_age, age_literal) # John hasAge 25from owlapy.owl_axiom import (
OWLSubObjectPropertyOfAxiom,
OWLEquivalentObjectPropertiesAxiom,
OWLInverseObjectPropertiesAxiom,
OWLTransitiveObjectPropertyAxiom,
OWLSymmetricObjectPropertyAxiom,
OWLFunctionalObjectPropertyAxiom
)from owlapy.util_owl_static_funcs import (
create_ontology,
csv_to_rdf_kg,
save_owl_class_expressions
)Create empty ontology.
onto = create_ontology("http://example.com/my-ontology")Convert CSV to RDF knowledge graph.
csv_to_rdf_kg(
csv_file="data.csv",
output_file="kg.owl",
namespace="http://example.com/data#",
class_name="DataPoint"
)Save class expressions to OWL file.
save_owl_class_expressions(
expressions=[expr1, expr2, expr3],
path="expressions.owl",
namespace="http://example.com/onto#"
)from owlapy.static_funcs import startJVM, stopJVMStart Java Virtual Machine (required for SyncReasoner).
startJVM()Stop Java Virtual Machine.
stopJVM()from owlapy.utils import CESimplifier, NNFSimplify complex class expressions.
simplifier = CESimplifier()
simplified = simplifier.simplify(complex_expression)Convert to Negation Normal Form.
nnf = NNF()
normalized = nnf.get_nnf(expression)from owlapy.utils import jaccard_similarity, f1_set_similarityCompute Jaccard similarity.
similarity = jaccard_similarity(instances1, instances2)Compute F1 similarity.
similarity = f1_set_similarity(instances1, instances2)from owlapy.agen_kg import AGenKG, DomainGraphExtractor, OpenGraphExtractorGenerate ontologies from text using LLMs.
Constructor:
AGenKG(
model: str = "gpt-4o",
api_key: str = None,
api_base: str = "https://api.openai.com/v1"
)Methods:
generate_ontology(text: str, ontology_type: str = "domain", save_path: str = None) -> SyncOntology
Example:
agent = AGenKG(
model="gpt-4o",
api_key="your-key",
api_base="https://models.github.ai/inference"
)
ontology = agent.generate_ontology(
text="path/to/document.txt",
ontology_type="domain",
save_path="generated_ontology.owl"
)Extract domain-specific knowledge graph.
extractor = DomainGraphExtractor(model="gpt-4o", api_key="key")
kg = extractor.extract(text="Medical records...")Extract open-domain knowledge graph.
extractor = OpenGraphExtractor(model="gpt-4o", api_key="key")
kg = extractor.extract(text="General text...")from owlapy.iri import IRICreate IRI from string.
iri = IRI.create("http://example.com/onto#Person")Create IRI from namespace and name.
iri = IRI("http://example.com/onto#", "Person")Methods:
as_str() -> str- Get full IRI stringget_namespace() -> str- Get namespaceget_short_form() -> str- Get local name
from owlapy.swrl import (
Rule,
ClassAtom,
ObjectPropertyAtom,
DataPropertyAtom,
IVariable,
DVariable
)# hasParent(?x, ?y) ∧ Male(?y) → hasFather(?x, ?y)
x = IVariable(IRI.create("urn:swrl:var#x"))
y = IVariable(IRI.create("urn:swrl:var#y"))
rule = Rule(
body=[
ObjectPropertyAtom(has_parent, x, y),
ClassAtom(male, y)
],
head=[
ObjectPropertyAtom(has_father, x, y)
]
)# Ontology
from owlapy.owl_ontology import SyncOntology, Ontology, NeuralOntology
# Reasoners
from owlapy.owl_reasoner import RDFLibReasoner, StructuralReasoner, SyncReasoner
# Class Expressions
from owlapy.class_expression import (
OWLClass, OWLThing, OWLNothing,
OWLObjectIntersectionOf, OWLObjectUnionOf, OWLObjectComplementOf,
OWLObjectSomeValuesFrom, OWLObjectAllValuesFrom, OWLObjectHasValue,
OWLObjectMinCardinality, OWLObjectMaxCardinality, OWLObjectExactCardinality,
OWLObjectOneOf
)
# Properties
from owlapy.owl_property import OWLObjectProperty, OWLDataProperty, OWLObjectInverseOf
# Individuals & Literals
from owlapy.owl_individual import OWLNamedIndividual
from owlapy.owl_literal import OWLLiteral
from owlapy.owl_datatype import IntegerOWLDatatype, DoubleOWLDatatype, StringOWLDatatype
# Axioms
from owlapy.owl_axiom import (
OWLSubClassOfAxiom, OWLEquivalentClassesAxiom, OWLDisjointClassesAxiom,
OWLClassAssertionAxiom, OWLObjectPropertyAssertionAxiom, OWLDataPropertyAssertionAxiom
)
# Syntax Conversion
from owlapy import (
owl_expression_to_dl, owl_expression_to_manchester, owl_expression_to_sparql,
manchester_to_owl_expression, dl_to_owl_expression
)
# Utilities
from owlapy.util_owl_static_funcs import create_ontology, csv_to_rdf_kg
from owlapy.static_funcs import startJVM, stopJVM
from owlapy.utils import CESimplifier, NNF
# IRI
from owlapy.iri import IRI
# AGenKG
from owlapy.agen_kg import AGenKG