{"id":6761,"library":"owlrl","title":"OWL-RL","description":"OWL-RL is a Python library that provides a simple implementation of the OWL2 RL Profile, as well as basic RDFS inference, on top of RDFLib. It performs mechanical forward chaining to compute the deductive closure of RDF graphs. The current version is 7.1.4, and it is actively maintained with a focus on semantic web inference capabilities.","status":"active","version":"7.1.4","language":"en","source_language":"en","source_url":"https://github.com/RDFLib/OWL-RL","tags":["RDF","OWL","inference","semantic web","knowledge graph","RDFLib"],"install":[{"cmd":"pip install owlrl","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for RDF graph representation and manipulation, required >=7.1.4.","package":"rdflib","optional":false}],"imports":[{"note":"The module was renamed from RDFClosure to owlrl in version 5.1.0. Ensure you're using the correct module name for newer versions.","wrong":"from RDFClosure import DeductiveClosure","symbol":"DeductiveClosure","correct":"from owlrl import DeductiveClosure"},{"symbol":"RDFS_Semantics","correct":"from owlrl import RDFS_Semantics"},{"symbol":"OWLRL_Semantics","correct":"from owlrl import OWLRL_Semantics"},{"symbol":"Graph","correct":"from rdflib import Graph"}],"quickstart":{"code":"from rdflib import Graph, Literal, Namespace, RDF, RDFS\nfrom owlrl import DeductiveClosure, OWLRL_Semantics\n\n# Define a simple namespace for demonstration\nex = Namespace(\"http://example.org/ontology#\")\n\n# Create an RDFLib graph\ngraph = Graph()\ngraph.bind(\"ex\", ex)\n\n# Add some initial triples (asserted facts)\ngraph.add((ex.Person, RDF.type, RDFS.Class))\ngraph.add((ex.Student, RDFS.subClassOf, ex.Person))\ngraph.add((ex.John, RDF.type, ex.Student))\n\n# Add a rule that says if someone is a Person, they are also an Agent\ngraph.add((ex.Agent, RDF.type, RDFS.Class))\ngraph.add((ex.Person, RDFS.subClassOf, ex.Agent))\n\nprint(f\"Graph size before OWL RL expansion: {len(graph)}\")\n\n# Initialize and run the OWL 2 RL deductive closure\nclosure = DeductiveClosure(OWLRL_Semantics)\nclosure.expand(graph)\n\n# The graph now contains inferred triples, e.g., John is a Person, and also an Agent\nprint(f\"Graph size after OWL RL expansion: {len(graph)}\")\n\n# Check for an inferred triple\nif (ex.John, RDF.type, ex.Agent) in graph:\n    print(\"Inferred: John is an Agent.\")\n\n# You can also run RDFS inference separately\nrdfs_graph = Graph()\nrdfs_graph.bind(\"ex\", ex)\nrdfs_graph.add((ex.Teacher, RDFS.subClassOf, ex.Person))\nrdfs_graph.add((ex.Jane, RDF.type, ex.Teacher))\n\nprint(f\"\\nGraph size before RDFS expansion: {len(rdfs_graph)}\")\nfrom owlrl import RDFS_Semantics\nrdfs_closure = DeductiveClosure(RDFS_Semantics)\nrdfs_closure.expand(rdfs_graph)\nprint(f\"Graph size after RDFS expansion: {len(rdfs_graph)}\")\nif (ex.Jane, RDF.type, ex.Person) in rdfs_graph:\n    print(\"Inferred: Jane is a Person.\")","lang":"python","description":"This quickstart demonstrates how to create an RDFLib graph, populate it with some triples, and then use `owlrl.DeductiveClosure` with `OWLRL_Semantics` or `RDFS_Semantics` to infer new triples based on the respective OWL 2 RL or RDFS rules. The `expand` method modifies the graph in place, adding all possible derived triples. It shows how to check for an inferred triple after expansion."},"warnings":[{"fix":"Upgrade Python to 3.9+ or pin `owlrl` to an older version compatible with your Python environment (e.g., `owlrl<7.0.0` for Python 3.5-3.8, or `owlrl<5.0.0` for Python 2.7).","message":"OWL-RL version 7.x, following RDFLib's dependency updates, now requires Python 3.9 or higher. Older projects using Python 2.7 or earlier Python 3 versions (e.g., 3.5, 3.6) will need to upgrade their Python environment or use an older `owlrl` version.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Update your import statements to use `from owlrl import ...` instead of `from RDFClosure import ...`.","message":"In version 5.1.0, the primary module for the library was renamed from `RDFClosure` to `owlrl`. If you are migrating an older codebase, import statements like `from RDFClosure import DeductiveClosure` must be updated to `from owlrl import DeductiveClosure`.","severity":"breaking","affected_versions":">=5.1.0"},{"fix":"Pre-process your graph to interpret `owl:imports` statements, for example:\n```python\nfrom rdflib import Graph\nfrom owlrl import interpret_owl_imports\n\ng = Graph().parse('my_ontology_with_imports.owl')\ninterpret_owl_imports('auto', g)\n# Now proceed with DeductiveClosure().expand(g)\n```","message":"The `DeductiveClosure` class does not automatically interpret `owl:imports` statements. Any imported ontologies must be explicitly loaded and merged into the graph (e.g., using `owlrl.interpret_owl_imports`) before calling `expand()` for the inference process to consider them.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To use the stricter datatype conversions, call `DeductiveClosure.use_improved_datatypes_conversions()` before expanding graphs, or initialize `DeductiveClosure(improved_datatypes=True)`.\n```python\nfrom owlrl import DeductiveClosure, OWLRL_Semantics\n\nDeductiveClosure.use_improved_datatypes_conversions() # Apply globally\n# or: closure = DeductiveClosure(OWLRL_Semantics, improved_datatypes=True)\n```","message":"RDFLib's default datatype handling may not meet the stricter requirements of OWL 2 RL, potentially leading to incorrect inferences for complex datatypes. `owlrl` provides improved datatype conversion routines, which can be explicitly enabled.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Represent double datatypes in their full lexical form (e.g., `\"1.234E56\"^^xsd:double`) or use a different serialization format if this issue is critical.","message":"A known bug in RDFLib's Turtle parser can cause exceptions when parsing abbreviated double datatypes (e.g., `1.234E56`). It's advisable to avoid this specific literal syntax in your Turtle files if you encounter parsing errors.","severity":"gotcha","affected_versions":"All versions (due to underlying RDFLib issue)"},{"fix":"Avoid using the `convert_graph` entry point if encountering this issue. Ensure your Python environment's `PYTHON_PATH` doesn't lead to ambiguity, or consider upgrading `owlrl` which has renamed scripts to avoid `.py` extensions.","message":"On Windows systems, if both the `owlrl` package and an `owlrl.py` command-line script are present and accessible in `PYTHON_PATH`, attempting to `from owlrl import convert_graph` (an older entry point) could lead to an import collision, as Python might incorrectly try to import from the script file.","severity":"gotcha","affected_versions":"<7.x (likely mitigated by script renaming in newer versions)"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}