DerwenAI / kglab

Graph Data Science: an abstraction layer in Python for building knowledge graphs, integrated with popular graph libraries – atop Pandas, NetworkX, RAPIDS, RDFlib, pySHACL, PyVis, morph-kgc, pslpython, pyarrow, etc.
https://derwen.ai/docs/kgl/
MIT License
575 stars 65 forks source link

Integration with rdflib-sqlalchemy, oxrdflib #203

Open davidshumway opened 2 years ago

davidshumway commented 2 years ago

Perhaps this is a Google Colab-only issue?

!pip install rdflib-sqlalchemy
import kglab
from rdflib import plugin, Graph, Literal, URIRef
from rdflib.store import Store
store = plugin.get("SQLAlchemy", Store)(identifier=URIRef("rdflib_test"))
graph = Graph(store)
graph.open(Literal("sqlite://"), create=True)
kg = kglab.KnowledgeGraph(
  name = "...",
  import_graph = graph
)

AttributeError Traceback (most recent call last)

in () ---> 50 import_graph = graph 51 ) 3 frames /content/gdrive/MyDrive/ONR/kglab/kglab/kglab.py in __init__(self, name, base_uri, language, use_gpus, import_graph, namespaces) 111 112 # import relations from another RDF graph, or start from blank --> 113 if import_graph: 114 self._g = import_graph 115 else: /usr/local/lib/python3.7/dist-packages/rdflib/graph.py in __len__(self) 527 return 1 528 --> 529 def __eq__(self, other): 530 return isinstance(other, Graph) and self.identifier == other.identifier 531 /usr/local/lib/python3.7/dist-packages/rdflib_sqlalchemy/store.py in __len__(self, context) 205 (literal, literalContext, 206 ASSERTED_LITERAL_PARTITION), ] --> 207 q = union_select(selects, distinct=True, select_type=COUNT_SELECT) 208 else: 209 selects = [ /usr/local/lib/python3.7/dist-packages/rdflib_sqlalchemy/sql.py in union_select(select_components, distinct, select_type) 54 55 if select_type == COUNT_SELECT: ---> 56 select_clause = table.count(whereClause) 57 elif select_type == CONTEXT_SELECT: 58 select_clause = expression.select([table.c.context], whereClause) AttributeError: 'Alias' object has no attribute 'count'

And oxrdflib:

!pip install oxrdflib
import kglab
import rdflib
kg = kglab.KnowledgeGraph(
  name = "...",
  import_graph = rdflib.Graph(store="OxMemory")
)

ModuleNotFoundError Traceback (most recent call last)

in () ---> 49 import_graph = rdflib.Graph(store="OxMemory") 51 ) 3 frames /content/gdrive/MyDrive/ONR/kglab/kglab/kglab.py in __init__(self, name, base_uri, language, use_gpus, import_graph, namespaces) 114 self._g = import_graph 115 else: --> 116 self._g = rdflib.Graph() 117 118 # initialize the namespaces /usr/local/lib/python3.7/dist-packages/rdflib/graph.py in __init__(self, store, identifier, namespace_manager, base) 325 if self.__namespace_manager is None: 326 self.__namespace_manager = NamespaceManager(self) --> 327 return self.__namespace_manager 328 329 def _set_namespace_manager(self, nm): /usr/local/lib/python3.7/dist-packages/rdflib/plugin.py in get(name, kind) 108 109 --> 110 try: 111 from pkg_resources import iter_entry_points 112 except ImportError: /usr/local/lib/python3.7/dist-packages/rdflib/plugin.py in getClass(self) 69 module = __import__(self.module_path, globals(), locals(), [""]) 70 self._class = getattr(module, self.class_name) ---> 71 return self._class 72 73 ModuleNotFoundError: No module named 'rdflib.plugins.stores.memory'
ceteri commented 2 years ago

Hi @davidshumway, thank you for reporting this. We've begun to work with Oxrdflib and have support in v0.5.3 of kglab:

import kglab
kglab.KnowledgeGraph(store="Oxigraph")

NB: note the change in the name of the store, as of Oxrdflib v0.3.0 on 2022-03-19 https://github.com/oxigraph/oxrdflib/releases/tag/v0.3.0

Pros:

Cons:

We've been able to isolate these issues with a minimum Python code + RDF + SPARQL example, and I'll be opening an issue for Oxrdflib which is linked to this issue.

See also: PRs #240, #241, #242

The SQLAlchemy issues are a different matter. Ostensibly, our new support for specifying a store="foo" in the KnowledgeGraph constructor similarly applies here. The exceptions listed seem to be in the SQLAlchemy store's support for required methods, specifically in the support for contexts. Perhaps they didn't quite get that far?

FWIW, we've got a new RDFlib.Store plugin in development which is based on NumPy / cuNumeric and it does implement the contexts support.

cc: @paoespinozarias @neobernad @jelisf @Mec-iS

ceteri commented 2 years ago

See issue https://github.com/oxigraph/oxrdflib/issues/8