-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrdf_to_case.py
More file actions
126 lines (106 loc) · 3.88 KB
/
rdf_to_case.py
File metadata and controls
126 lines (106 loc) · 3.88 KB
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
125
126
import json
import sys
from io import TextIOWrapper
from rdflib import Graph, Literal, Namespace, URIRef
from rdflib.namespace import RDF, XSD
from case_example_mapping import JSON
STR_XSD_STRING = str(XSD.string)
def compact_xsd_strings(json_object: JSON) -> JSON:
"""
Some JSON-LD serializers export all literals with datatype
annotations. A string-literal without a datatype can be optionally
exported with a datatype annotation of ``xsd:string``, or left to be
interpreted as a ``xsd:string`` by default. This function compacts
string-literals that are ``xsd:string``-typed into a JSON String
instead of a JSON Object.
>>> from rdflib import XSD
>>> x = {
... "@id": "http://example.org/s",
... "http://example.org/p": [
... {
... "@type": str(XSD.string),
... "@value": "o"
... }
... ]
... }
>>> x
{'@id': 'http://example.org/s', 'http://example.org/p': [{'@type': 'http://www.w3.org/2001/XMLSchema#string', '@value': 'o'}]}
>>> compact_xsd_strings(x)
{'@id': 'http://example.org/s', 'http://example.org/p': ['o']}
"""
if json_object is None:
return json_object
elif isinstance(json_object, (bool, float, int, str)):
return json_object
elif isinstance(json_object, dict):
if "@value" in json_object:
# Reviewing Literal.
if json_object.get("@type") == STR_XSD_STRING:
assert isinstance(json_object["@value"], str)
return json_object["@value"]
else:
return json_object
else:
return {k: compact_xsd_strings(json_object[k]) for k in json_object.keys()}
elif isinstance(json_object, list):
return [compact_xsd_strings(x) for x in json_object]
else:
raise TypeError("Unexpected type of object: %s." % type(json_object))
def serialize_jsonld(graph: Graph, fh: TextIOWrapper) -> None:
"""
This function serializes the graph with a step taken to compact
``xsd:string`` Literals that became JSON Objects.
"""
json_string = graph.serialize(format="json-ld")
json_object = json.loads(json_string)
json_object = compact_xsd_strings(json_object)
json.dump(json_object, fh, indent=4)
def main() -> None:
# Process the command line arguments to get the output path
if len(sys.argv) == 1:
print(f"Insufficient arguments. Usage is {sys.argv[0]} output_path")
sys.exit(1)
output_path: str = sys.argv[1]
g: Graph = Graph()
g.bind("uco-core", "https://ontology.unifiedcyberontology.org/uco/core/")
g.bind(
"uco-identity",
"https://ontology.unifiedcyberontology.org/uco/identity/",
)
ns_core: Namespace = Namespace(
"https://ontology.unifiedcyberontology.org/uco/core/"
)
ns_kb: Namespace = Namespace("http://example.org/kb/")
# Define an individual.
# RDFLib Namespace provides two ways to create a URIRef of the
# namespace as a string-prefix concatenated with an argument:
# * The . operator.
# * The [] operator.
iri_my_organization = ns_kb["organization-b1534f63-b1c3-4b5c-a937-cbe7077571f2"]
g.add(
(
iri_my_organization,
RDF.type,
URIRef(
"https://ontology.unifiedcyberontology.org/uco/identity/Organization"
),
)
)
g.add(
(
iri_my_organization,
ns_core.name,
Literal("Cyber Domain Ontology", datatype=XSD.string),
)
)
# Write the CASE graph to a file
try:
with open(output_path, "w") as case_file:
serialize_jsonld(g, case_file)
print(f"CASE graph exported to: {output_path}")
except IOError:
print(f"Error writing to path: {output_path}")
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
main()