-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdf2rdfviz.py
executable file
·127 lines (110 loc) · 4.33 KB
/
rdf2rdfviz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#============================================
# File name: rdf2rdfviz.py
# Author: Olivier Rey
# Date: May 12 2019
# License: GPL v3
# Purpose: Transforms rdf representations into
# representable rdf
#============================================
#!/usr/bin/env python3
import rdflib, getopt, sys, os
from rdf_representations import *
from rdf_utils import *
from rdflib import Graph, URIRef, Literal, BNode, RDF
from rdflib.namespace import FOAF, DC
#------------------------------------------ Constants
GA_DOMAIN = "https://orey.github.io/graphapps-V1#"
GA_Node = URIRef(GA_DOMAIN + "NodeID")
GA_Edge = URIRef(GA_DOMAIN + "EdgeID")
GA_source = URIRef(GA_DOMAIN + "source")
GA_target = URIRef(GA_DOMAIN + "target")
def enrich_graph_with_navigation(rdfgraph, option=0):
'''
Creates a RDF file from the RDF file with the RDF decorations that
enable to make the graph representable.
The option 0 is creating a decoration with 5 triples (+ 3 type
triples, and the option 1 is creating the new decoration with only
4 triples.
'''
node_dict = {}
rel_dict = {}
# First: insert the graph in dictionaries to provide them with IDs
for s, p, o in rdfgraph:
source = add_to_nodes_dict(RDFNode(s),node_dict)
target = add_to_nodes_dict(RDFNode(o),node_dict)
add_to_rels_dict(RDFRel(p, source, target),rel_dict)
# Enrich each unique node by 2 triples, one being the value and the other the type
for elem in node_dict.values():
node = URIRef(GA_DOMAIN + elem.get_id())
rdfgraph.add((node, RDF.type, GA_Node))
rdfgraph.add((node, RDF.value, elem.get_rdf()))
if option == 0:
# Enrich for each unique edge by 4 triples, one being the visualizer
for elem in rel_dict.values():
edge = URIRef(GA_DOMAIN + elem.get_id())
rdfgraph.add((edge, RDF.type, GA_Edge))
rdfgraph.add((edge, GA_source, URIRef(GA_DOMAIN + elem.get_source_id())))
rdfgraph.add((edge, GA_target, URIRef(GA_DOMAIN + elem.get_target_id())))
rdfgraph.add((edge, RDF.value, elem.get_rdf()))
else:
# Enrich for each unique edge by 3 triples, one being the visualizer (rdf:value)
for elem in rel_dict.values():
edge = URIRef(GA_DOMAIN + elem.get_id())
rdfgraph.add((edge, RDF.type, GA_Edge))
rdfgraph.add((URIRef(GA_DOMAIN + elem.get_source_id()),
edge,
URIRef(GA_DOMAIN + elem.get_target_id())))
rdfgraph.add((edge, RDF.value, elem.get_rdf()))
return rdfgraph
# toto: implement the rest
def usage():
print('RDF to RDFVIZ utility: Parses the full RDF file and adds unique IDs ' +
'for displays')
print('Usage:')
print('$ python3 rdf2rdfviz.py -i [input_file_or_url] -o [output_dir] (other_options)')
print('----\nMain options')
print('=> -i or --input NAME: filename or URL')
print('=> -o or --output NAME: directory name. Default will be "./outputs/"')
print('----\nOther options')
print('=> -f or --format: "xml", "n3", "ntriples" or other format supported')
print(' by Python rdflib. Default format is "turtle".')
print('=> -h or --help: usage')
if __name__ == '__main__':
try:
opts, args = getopt.getopt(sys.argv[1:],"hi:o:f:v", [])
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(2)
input = None
outputdir = './outputs'
myformat = 'turtle'
verbose = False
for o, a in opts:
if o == "-v":
verbose = True
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-i", "--input"):
input = a
elif o in ("-o", "--output"):
outputdir = a
elif o in ("-f", "--format"):
myformat = a
else:
assert False, "unhandled option"
# Check input
if input == None:
#print("Input file or URL cannot be void.")
usage()
sys.exit()
name = build_name(input)
# Check output dir
if not os.path.exists(outputdir):
os.makedirs(outputdir)
store = Graph()
result = store.parse(input, format=myformat)
enrich_graph_with_navigation(store)
# Dump the rdfgraph into a file
rdfgraph.serialize(rdf_file, myformat)