Package: rdf2gremlin
rdf2gremlinIt has never been easier to transform your RDF data into a property graph based on TinkerPop-Gremlin.IntroductionApache TinkerPopis a graph computing framework for both graph databases (OLTP) and graph analytic systems (OLAP).Gremlinis the graph traversal language of TinkerPop.TheResource Description Framework (RDF)is a standard model for data interchange on the Web originally designed as a metadata data model. It has come to be used as a general method for conceptual description or modeling of information that is implemented in web resources, using a variety of syntax notations and data serialization formats. This linking structure forms adirected labeled graph, where theedgesrepresent the named link between two resources, represented by thegraph nodes. This graph view is the easiest possible mental model for RDF and is often used in easy-to-understand visual explanations. Resources are denoted by IRIs. The general convention is to usehttpURIs.RDF graphs are queries using SPARQL language.SPARQL 1.1is a set of specifications that provide languages and protocols to query and manipulate RDF graph content on the Web or in an RDF store.This library,rdf2gremlin, provides an easy way to load the RDF data-sets into a property graph in order to benefit from the features of traversal language, which are not available in SPARQL pattern matching language.Installation# switch to your local virtual environment../venv/activate

pipinstallrdf2gremlin# ... enjoy ...Note: The librarytornadoshall satisfytornado>=4.4.1,<5.0version restriction inherited from the python_gremlin library.PrerequisitesGremlin-Python is designed to connect to a "server" that is hosting a TinkerPop-enabled graph system. That "server"
could beGremlin Serveror aremote Gremlin providerthat exposes protocols by which Gremlin-Python can connect. This requirement is inherited by the current library as well.In order to use this library it is necessary to have a Gremlin service available locally or on a remote location. The easiest way is to run a TinkerProp server locally. This can be done by either:
(a) downloading and running TinkerPopwgethttps://archive.apache.org/dist/tinkerpop/3.4.3/apache-tinkerpop-gremlin-server-3.4.3-bin.zip
unzipapache-tinkerpop-gremlin-server-3.4.3-bin.zipcdapache-tinkerpop-gremlin-server-3.4.3/
./bin/gremlin-server.shstart# ... to stop the server ..../bin/gremlin-server.shstopor (b) running it as a Docker container.dockerpulltinkerpop/gremlin-server
dockerrun--namegremlin-server-p8182:8182tinkerpop/gremlin-server# ... to stop the server ...dockerstopgremlin-serverGetting startedConnect to a property graph service.A typical connection to a server running on "localhost" that supports the Gremlin Server protocol using websockets from the Python shell looks like this:fromrdf2gimportsetup_graphDEFAULT_LOCAL_CONNECTION_STRING="ws://localhost:8182/gremlin"g=setup_graph(DEFAULT_LOCAL_CONNECTION_STRING)Onceghas been created using a connection, it is then possible to start writing Gremlin traversals to query the remote graph.Load a graphRead an RDF graph.importrdflibimportpathlibOUTPUT_FILE_LAM_PROPERTIES=pathlib.Path("../resource/celex_project_properties_v2.ttl").resolve()rdf_graph=rdflib.Graph()rdf_graph.parse(str(OUTPUT_FILE_LAM_PROPERTIES),format="ttl")Load the RDF graph into a property graph.fromrdf2gimportload_rdf2gload_rdf2g(g,rdf_graph)The created property graph follows the following set ofconventions.URIs and Blank nodes are transformed into property graph nodes.Predicates connecting an URI to another URI or a blank node are transformed into property graph edges. Edge labels correspond to qualified IRIs generated using the prefix definitions available in the RDF data-set.Node labels correspond to qualified IRIs generated using the prefix definitions available in the RDF data-set.RDF Litarals are transformed into values of the node properties, while the preceding predicates into keys of the node properties. In other wordsPredicates connecting an URI to a RDF Literal are transformed into {key:value} pairs and added as node properties.Nodes have a special property 'iri' that is equivalent to the absolute URI of the RDF resource.Get a nodeGet a node referring to it either by label, iri or idskos_concept_iri=rdflib.URIRef("http://www.w3.org/2004/02/skos/core#Concept")v1=rdf2g.get_node(g,skos_concept_iri)skos_concept_label="skos:Concept"v2=rdf2g.get_node(g,skos_concept_label)hypothetical_node_id=880v3=rdf2g.get_node(g,hypothetical_node_id)print(v1==v2==v3)# should be trueGet nodes by their supposed rdf:type. This concept is inherited, of course, from RDF world.skos_concept_label="skos:Concept"list_of_concept=rdf2g.get_nodes_of_type(g,skos_concept_label)# print the list of concepts in the graphprint(list_of_concept)Generate a traversal treeIt is possible to traverse the property graph and then generate the traversal tree from it. This is especially useful when the graph serves as structured document content say JSON or XML serialisation.To do that first, get two levels deep traversal tree and the edges between them for all the nodes in the graph that haveiri == known_iri. Further please see theGremlin reference documentationat Apache TinkerPop for more information on usage.known_iri='http://publications.europa.eu/resources/authority/celex/md_CODE's=g.V().has('iri',known_iri).outE().inV().tree().next()Altenatively use the functionrdf2g.generate_traversal_treenode=rdf2g.get_node(self.g,known_iri)s=rdf2g.generate_traversal_tree(self.g,node)Then expand and simplify that tree. First, simplify the dict structure to simple Python types, removing the Gremlin objects. Second, expand by providing the properties for each visited node, while the edges are considered as special properties leading to a another node dictionary.frompprintimportpprintresult=rdf2g.expand_tree(g,s)pprint(result)The traversal tree nodes contain, in addition to original RDF content, two special properties@idand@labelwhich correspond to the standard Gremlinidandlabelproperties. The@sign is used to distinguish the original RDF from the Gremlin features. Property graph edges, are reduced to keys in the final dict and for this reason they have no additional descriptions just like in the original RDF graph.ContributingYou are more than welcome to help expand and mature this project.When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change.Please note we have a code of conduct, please follow it in all your interactions with the project.LicenseThis project is Licensed under the GPL v3 License - seeLICENSEfile