Skip to content

Commit

Permalink
Fixing topquadrant inference (#108)
Browse files Browse the repository at this point in the history
* fixing topquadrant inferenc to use maxiterations and skolemize to preserve bnode identities

* aider: Reverted changes to the `expand` method in `brickschema/graph.py`.

# Aider chat conversation:

USER: I did `git reset --hard HEAD~1` to discard the last edits.

ASSISTANT: Understood. Since you've performed a hard reset to undo the last commit, I will now provide the changes to revert the `expand` method and the `_expand_helper` method to their state before the commit with hash `b27e5cc`.

Here are the diffs for those changes:

```diff
--- brickschema/graph.py
+++ brickschema/graph.py
@@ ... @@
     def expand(
         self, profile, backend=None, simplify=True, ontology_graph=None, iterative=True
     ):
         """
         Expands the current graph with the inferred triples under the given entailment regime
         and with the given backend. Possible profiles are:
         - 'rdfs': runs RDFS rules
         - 'owlrl': runs full OWLRL reasoning
         - 'vbis': adds VBIS tags
         - 'shacl': does SHACL-AF reasoning (including tag inference, if the extension is loaded)
@@ ... @@
         elif profile == "owlrl":
-            self._inferbackend = OWLRLNaiveInferenceSession()
-            try:
-                if backend is None or backend == "reasonable":
-                    self._inferbackend = OWLRLReasonableInferenceSession()
-                    backend = "reasonable"
-            except ImportError:
-                warn(
-                    "Could not load Reasonable reasoner. Needs 'reasonable' option during install."
-                )
-                self._inferbackend = OWLRLNaiveInferenceSession()
-
-            try:
-                if backend is None or backend == "allegrograph":
-                    self._inferbackend = OWLRLAllegroInferenceSession()
-                    backend = "allegrograph"
-            except (ImportError, ConnectionError):
-                warn(
-                    "Could not load Allegro reasoner. Needs 'allegro' option during install."
-                )
-                self._inferbackend = OWLRLNaiveInferenceSession()
+            self._inferbackend = OWLRLReasonableInferenceSession()
         elif profile == "vbis":
             self._inferbackend = VBISTagInferenceSession(
                 brick_version=self._brick_version
             )
         else:
             raise Exception(f"Invalid profile '{profile}'")
-        self._inferbackend.expand(self)
+        OWLRLNaiveInferenceSession().expand(self)

         if simplify:
             self.simplify()
         return self
```

This diff will revert the changes made to the `expand` method in the `brickschema/graph.py` file, effectively undoing the commit with hash `b27e5cc`.

* issues with bnodes mean some of these need to use pyshacl

* bump versions

* handling skolemization of the data graph

* remove extra bnodes -- they should all be skolemized

* fixing topquadrant inferenc to use maxiterations and skolemize to preserve bnode identities

Reverted changes to the `expand` method in `brickschema/graph.py`.

USER: I did `git reset --hard HEAD~1` to discard the last edits.

ASSISTANT: Understood. Since you've performed a hard reset to undo the last commit, I will now provide the changes to revert the `expand` method and the `_expand_helper` method to their state before the commit with hash `b27e5cc`.

Here are the diffs for those changes:

```diff
--- brickschema/graph.py
+++ brickschema/graph.py
@@ ... @@
     def expand(
         self, profile, backend=None, simplify=True, ontology_graph=None, iterative=True
     ):
         """
         Expands the current graph with the inferred triples under the given entailment regime
         and with the given backend. Possible profiles are:
         - 'rdfs': runs RDFS rules
         - 'owlrl': runs full OWLRL reasoning
         - 'vbis': adds VBIS tags
         - 'shacl': does SHACL-AF reasoning (including tag inference, if the extension is loaded)
@@ ... @@
         elif profile == "owlrl":
-            self._inferbackend = OWLRLNaiveInferenceSession()
-            try:
-                if backend is None or backend == "reasonable":
-                    self._inferbackend = OWLRLReasonableInferenceSession()
-                    backend = "reasonable"
-            except ImportError:
-                warn(
-                    "Could not load Reasonable reasoner. Needs 'reasonable' option during install."
-                )
-                self._inferbackend = OWLRLNaiveInferenceSession()
-
-            try:
-                if backend is None or backend == "allegrograph":
-                    self._inferbackend = OWLRLAllegroInferenceSession()
-                    backend = "allegrograph"
-            except (ImportError, ConnectionError):
-                warn(
-                    "Could not load Allegro reasoner. Needs 'allegro' option during install."
-                )
-                self._inferbackend = OWLRLNaiveInferenceSession()
+            self._inferbackend = OWLRLReasonableInferenceSession()
         elif profile == "vbis":
             self._inferbackend = VBISTagInferenceSession(
                 brick_version=self._brick_version
             )
         else:
             raise Exception(f"Invalid profile '{profile}'")
-        self._inferbackend.expand(self)
+        OWLRLNaiveInferenceSession().expand(self)

         if simplify:
             self.simplify()
         return self
```

This diff will revert the changes made to the `expand` method in the `brickschema/graph.py` file, effectively undoing the commit with hash `b27e5cc`.

* issues with bnodes mean some of these need to use pyshacl

* bump versions

* handling skolemization of the data graph

* remove extra bnodes -- they should all be skolemized

* new skolemize function to deal with spaces in literals

* Replace `infer` function to run shaclinfer multiple times until skolemized data graph stops changing in size. Then, return the deskolemized data graph.

* small changes

* bumping deps

* add imports back

* updating

* limiting external loops

* fix pragma handling
  • Loading branch information
gtfierro authored Feb 10, 2024
1 parent c0f40b2 commit 35edb1d
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 234 deletions.
25 changes: 25 additions & 0 deletions brickschema/abbrmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
abbrmap = {
"ahu": ["ahu"],
"avg": ["average"],
"cmd": ["command"],
"elec": ["electrical"],
"equip": ["equipment"],
"freq": ["frequency"],
"occ": ["occupied"],
"rtu": ["rtu"],
"roof": ["rooftop"],
"sp": ["setpoint"],
"spt": ["setpoint"],
"stpt": ["setpoint"],
"temp": ["temperature"],
"unocc": ["unoccupied"],
"volt": ["voltage"],
"ctl": ["control"],
"sa": ["supply", "air"],
"ea": ["exhaust", "air"],
"da": ["discharge", "air"],
"hw": ["hot", "water"],
"chw": ["chilled", "water"],
"z": ["zone"],
"zn": ["zone"],
}
77 changes: 41 additions & 36 deletions brickschema/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,16 @@ def get_most_specific_class(self, classlist: List[rdflib.URIRef]):
"""
equivalent = set(x[0] for x in self.query(equivalent_query))

if len(closure.intersection(classlist)) == 0 or closure.intersection(classlist).issubset(equivalent):
if len(closure.intersection(classlist)) == 0 or closure.intersection(
classlist
).issubset(equivalent):
specific.append(c)

return specific

def validate(self, shape_graphs=None, default_brick_shapes=True, engine: str='pyshacl'):
def validate(
self, shape_graphs=None, default_brick_shapes=True, engine: str = "pyshacl"
):
"""
Validates the graph using the shapes embedded w/n the graph. Optionally loads in normative Brick shapes
and externally defined shapes
Expand All @@ -113,7 +117,7 @@ def validate(self, shape_graphs=None, default_brick_shapes=True, engine: str='py
if shape_graphs is not None and isinstance(shape_graphs, list):
for sg in shape_graphs:
shapes += sg
if engine == 'pyshacl':
if engine == "pyshacl":
return pyshacl.validate(
self,
shacl_graph=shapes,
Expand All @@ -122,16 +126,20 @@ def validate(self, shape_graphs=None, default_brick_shapes=True, engine: str='py
abort_on_first=True,
allow_warnings=True,
)
elif engine == 'topquadrant':
elif engine == "topquadrant":
# check if 'java' is in the path
import shutil
if shutil.which('java') is None:
raise Exception("TopQuadrant SHACL validation requires Java to be installed and in the PATH")

if shutil.which("java") is None:
raise Exception(
"TopQuadrant SHACL validation requires Java to be installed and in the PATH"
)
from brickschema.topquadrant_shacl import validate

if shape_graphs is not None and isinstance(shape_graphs, list):
for sg in shape_graphs:
shapes += sg
return validate(self+shapes)
return validate(self, shapes)

def serve(self, address="127.0.0.1:8080", ignore_prefixes=[]):
"""
Expand Down Expand Up @@ -233,10 +241,19 @@ def expand(
# TODO: currently nothing is cached between expansions
"""
og = None
if ontology_graph:
og = ontology_graph.skolemize()

if "+" in profile:
for prf in profile.split("+"):
self.expand(prf, backend=backend, simplify=simplify)
self.expand(
prf,
backend=backend,
simplify=simplify,
ontology_graph=og,
iterative=iterative,
)
return

if profile == "brick":
Expand All @@ -245,16 +262,23 @@ def expand(
owlrl.DeductiveClosure(owlrl.RDFS_Semantics).expand(self)
return
elif profile == "shacl":
og = None
if ontology_graph:
og = ontology_graph.skolemize()
if backend == 'topquadrant':
if backend == "topquadrant":
# check if 'java' is in the path
import shutil
if shutil.which('java') is None:
raise Exception("TopQuadrant SHACL validation requires Java to be installed and in the PATH")

if shutil.which("java") is None:
raise Exception(
"TopQuadrant SHACL validation requires Java to be installed and in the PATH"
)
from brickschema.topquadrant_shacl import infer
infer(self, og or rdflib.Graph())

# 'res' is a de-skolemized graph. We want to replace the contents
# of this graph with the de-skolemized version because topquadrant requires
# that skolemization is applied to the input graph in order to preserve
# identity of inferred subject/objects
res = infer(self, og or rdflib.Graph())
self.remove((None, None, None))
self += res
return self
valid, _, report = pyshacl.validate(
data_graph=self,
Expand All @@ -271,33 +295,14 @@ def expand(
self._iterative_expand(og)
return self
elif profile == "owlrl":
self._inferbackend = OWLRLNaiveInferenceSession()
try:
if backend is None or backend == "reasonable":
self._inferbackend = OWLRLReasonableInferenceSession()
backend = "reasonable"
except ImportError:
warn(
"Could not load Reasonable reasoner. Needs 'reasonable' option during install."
)
self._inferbackend = OWLRLNaiveInferenceSession()

try:
if backend is None or backend == "allegrograph":
self._inferbackend = OWLRLAllegroInferenceSession()
backend = "allegrograph"
except (ImportError, ConnectionError):
warn(
"Could not load Allegro reasoner. Needs 'allegro' option during install."
)
self._inferbackend = OWLRLNaiveInferenceSession()
self._inferbackend = OWLRLReasonableInferenceSession()
elif profile == "vbis":
self._inferbackend = VBISTagInferenceSession(
brick_version=self._brick_version
)
else:
raise Exception(f"Invalid profile '{profile}'")
self._inferbackend.expand(self)
OWLRLNaiveInferenceSession().expand(self)

if simplify:
self.simplify()
Expand Down
4 changes: 2 additions & 2 deletions brickschema/persistent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rdflib import plugin
from rdflib.store import Store
from rdflib_sqlalchemy import registerplugins
from sqlalchemy import text
import pickle
from .graph import Graph, BrickBase

Expand Down Expand Up @@ -93,8 +94,7 @@ def __init__(self, uri: str, *args, **kwargs):
self._postcommit_hooks = OrderedDict()

with self.conn() as conn:
conn.execute("PRAGMA journal_mode=WAL;")
# conn.execute("PRAGMA synchronous=OFF;")
conn.execute(text("PRAGMA journal_mode=WAL;"))
conn.execute(changeset_table_defn)
conn.execute(changeset_table_idx)
conn.execute(redo_table_defn)
Expand Down
Loading

0 comments on commit 35edb1d

Please sign in to comment.