Skip to main content

Convert ArangoDB graphs to RDF & vice-versa.

Project description

ArangoRDF

build CodeQL Coverage Status Last commit

PyPI version badge Python versions badge

License Code style: black Downloads

Convert RDF Graphs to ArangoDB, and vice-versa.

About RDF

RDF is a standard model for data interchange on the Web. RDF has features that facilitate data merging even if the underlying schemas differ, and it specifically supports the evolution of schemas over time without requiring all the data consumers to be changed.

RDF extends the linking structure of the Web to use URIs to name the relationship between things as well as the two ends of the link (this is usually referred to as a "triple"). Using this simple model, it allows structured and semi-structured data to be mixed, exposed, and shared across different applications.

This linking structure forms a directed, labeled graph, where the edges represent the named link between two resources, represented by the graph nodes. This graph view is the easiest possible mental model for RDF and is often used in easy-to-understand visual explanations.

Resources to get started:

Installation

Latest Release

pip install arango-rdf

Current State

pip install git+https://github.com/ArangoDB-Community/ArangoRDF

Quickstart

Run the full version with Google Colab: Open In Colab

from rdflib import Graph
from arango import ArangoClient
from arango_rdf import ArangoRDF

db = ArangoClient(hosts="http://localhost:8529").db("_system_", username="root", password="")

adbrdf = ArangoRDF(db)

g = Graph()
g.parse("https://raw.githubusercontent.com/stardog-union/stardog-tutorials/master/music/beatles.ttl")

# RDF to ArangoDB
###################################################################################

# 1.1: RDF-Topology Preserving Transformation (RPT)
adbrdf.rdf_to_arangodb_by_rpt("Beatles", g, overwrite_graph=True)

# 1.2: Property Graph Transformation (PGT) 
adbrdf.rdf_to_arangodb_by_pgt("Beatles", g, overwrite_graph=True)

g = adbrdf.load_meta_ontology(g)

# 1.3: RPT w/ Graph Contextualization
adbrdf.rdf_to_arangodb_by_rpt("Beatles", g, contextualize_graph=True, overwrite_graph=True)

# 1.4: PGT w/ Graph Contextualization
adbrdf.rdf_to_arangodb_by_pgt("Beatles", g, contextualize_graph=True, overwrite_graph=True)

# 1.5: PGT w/ ArangoDB Document-to-Collection Mapping Exposed
adb_mapping = adbrdf.build_adb_mapping_for_pgt(g)
print(adb_mapping.serialize())
adbrdf.rdf_to_arangodb_by_pgt("Beatles", g, adb_mapping, contextualize_graph=True, overwrite_graph=True)

# ArangoDB to RDF
###################################################################################

# Start from scratch!
g = Graph()
g.parse("https://raw.githubusercontent.com/stardog-union/stardog-tutorials/master/music/beatles.ttl")
adbrdf.rdf_to_arangodb_by_pgt("Beatles", g, overwrite_graph=True)

# 2.1: Via Graph Name
g2, adb_mapping_2 = adbrdf.arangodb_graph_to_rdf("Beatles", Graph())

# 2.2: Via Collection Names
g3, adb_mapping_3 = adbrdf.arangodb_collections_to_rdf(
    "Beatles",
    Graph(),
    v_cols={"Album", "Band", "Class", "Property", "SoloArtist", "Song"},
    e_cols={"artist", "member", "track", "type", "writer"},
)

print(len(g2), len(adb_mapping_2))
print(len(g3), len(adb_mapping_3))

print('--------------------')
print(g2.serialize())
print('--------------------')
print(adb_mapping_2.serialize())
print('--------------------')

Development & Testing

  1. git clone https://github.com/ArangoDB-Community/ArangoRDF
  2. cd arango-rdf
  3. (create virtual environment of choice)
  4. pip install -e .[dev]
  5. (create an ArangoDB instance with method of choice)
  6. pytest --url <> --dbName <> --username <> --password <>

Note: A pytest parameter can be omitted if the endpoint is using its default value:

def pytest_addoption(parser):
    parser.addoption("--url", action="store", default="http://localhost:8529")
    parser.addoption("--dbName", action="store", default="_system")
    parser.addoption("--username", action="store", default="root")
    parser.addoption("--password", action="store", default="")

Additional Info: RDF to ArangoDB

RDF-to-ArangoDB functionality has been implemented using concepts described in the paper Transforming RDF-star to Property Graphs: A Preliminary Analysis of Transformation Approaches.

In other words, ArangoRDF offers 2 RDF-to-ArangoDB transformation methods:

  1. RDF-topology Preserving Transformation (RPT): ArangoRDF.rdf_to_arangodb_by_rpt()
  2. Property Graph Transformation (PGT): ArangoRDF.rdf_to_arangodb_by_pgt()

RPT preserves the RDF Graph structure by transforming each RDF Statement into an ArangoDB Edge.

PGT on the other hand ensures that Datatype Property Statements are mapped as ArangoDB Document Properties.

@prefix ex: <http://example.org/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
ex:book ex:publish_date "1963-03-22"^^xsd:date .
ex:book ex:pages "100"^^xsd:integer .
ex:book ex:cover 20 .
ex:book ex:index 55 .
RPT PGT
image image

RPT

The ArangoRDF.rdf_to_arangodb_by_rpt method will store the RDF Resources of your RDF Graph under the following ArangoDB Collections:

- {graph_name}_URIRef: The Document collection for `rdflib.term.URIRef` resources.
- {graph_name}_BNode: The Document collection for`rdflib.term.BNode` resources.
- {graph_name}_Literal: The Document collection for `rdflib.term.Literal` resources.
- {graph_name}_Statement: The Edge collection for all triples/quads.

PGT

In contrast to RPT, the ArangoRDF.rdf_to_arangodb_by_pgt method will rely on the nature of the RDF Resource/Statement to determine which ArangoDB Collection it belongs to. This is referred as the ArangoDB Collection Mapping Process. This process relies on 2 fundamental URIs:

  1. <http://www.arangodb.com/collection> (adb:collection)

    • Any RDF Statement of the form <http://example.com/Bob> <adb:collection> "Person" will map the Subject to the ArangoDB "Person" document collection.
  2. <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> (rdf:type)

    • This strategy is divided into 3 cases:

      1. If an RDF Resource only has one rdf:type statement, then the local name of the RDF Object is used as the ArangoDB Document Collection name. For example, <http://example.com/Bob> <rdf:type> <http://example.com/Person> would create an JSON Document for <http://example.com/Bob>, and place it under the Person Document Collection. NOTE: The RDF Object will also have its own JSON Document created, and will be placed under the "Class" Document Collection.

      2. If an RDF Resource has multiple rdf:type statements, with some (or all) of the RDF Objects of those statements belonging in an rdfs:subClassOf Taxonomy, then the local name of the "most specific" Class within the Taxonomy is used (i.e the Class with the biggest depth). If there is a tie between 2+ Classes, then the URIs are alphabetically sorted & the first one is picked.

      3. If an RDF Resource has multiple rdf:type statements, with none of the RDF Objects of those statements belonging in an rdfs:subClassOf Taxonomy, then the URIs are alphabetically sorted & the first one is picked. The local name of the selected URI will be designated as the Document collection for that Resource.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

arango_rdf-0.1.0.tar.gz (57.2 kB view hashes)

Uploaded Source

Built Distribution

arango_rdf-0.1.0-py3-none-any.whl (48.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page