forked from usnistgov/mgi-json-schema
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BuildMatSchemaDictionary.py
executable file
·129 lines (108 loc) · 4.13 KB
/
BuildMatSchemaDictionary.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
125
126
127
128
129
#!/usr/bin/env python
import json
from MatVocabUtils import *
with open('mat-schema-extended.json','r') as f:
data = json.loads(f.read())
graph_dict = dict()
for item in data["@graph"]:
graph_dict[item["@id"]] = item
for k,v in graph_dict.items():
check_dict_wrap_list(v,"rdfs:subClassOf")
check_dict_wrap_list(v,"schema:domainIncludes")
check_dict_wrap_list(v,"schema:rangeIncludes")
check_dict_wrap_list(v,"mat:domainIncludes")
check_dict_wrap_list(v,"mat:rangeIncludes")
for k,v in graph_dict.items():
if v["@type"] == "rdfs:Class":
v["propertiesInclude"] = []
for k,v in graph_dict.items():
if "schema:domainIncludes" in v:
for item in v["schema:domainIncludes"]:
graph_dict[item["@id"]]["propertiesInclude"].append({"@id":v["@id"]})
dictionary = dict()
dictionary["$schema"] = "http://json-schema.org/draft-07/schema#"
dictionary["$defs"] = dict()
defs = dictionary["$defs"]
defs["Date"] = {"title":"Date","type":"string","format":"date"}
defs["Boolean"] = {"title":"Boolean","type":"boolean"}
defs["DateTime"] = {"title":"DateTime","type":"string","format":"date-time"}
defs["Time"] = {"title":"Time","type":"string","format":"time"}
defs["Number"] = {"title":"Number","type":"number"}
defs["Float"] = {"title":"Float","type":"number"}
defs["Integer"] = {"title":"Integer","type":"number"}
defs["Text"] = {"title":"Text","type":"string"}
defs["ObjectLink"] = {
"type":"object",
"title": "ObjectLink",
"properties":{
"@type":{
"title":"@type",
"type":"string"},
"@id":{"title":"@id","type":"string"}
}
}
skip_list = ["DataType"]
for k,v in graph_dict.items():
label = None
rdfType = v['@type']
if "rdfs:label" in v:
if isinstance(v["rdfs:label"], str):
label = v["rdfs:label"]
if isinstance(v["rdfs:label"], dict):
label = v["rdfs:label"]["@value"]
#print(label)
else:
print("no label in:",v)
if label in defs.keys():
continue
if label in skip_list:
continue
if rdfType == "rdf:Property":
if "schema:rangeIncludes" not in v:
continue
anyOfList = list()
anyOfList.append({"$ref": "#/$defs/ObjectLink"})
for rangeItem in v["schema:rangeIncludes"]:
className = rangeItem["@id"].split(":")[1]
anyOfList.append({"$ref": "#/$defs/"+className})
defs[label] = {
"oneOf":[
{
"anyOf":anyOfList
},
{
"type": "array",
"items": {
"anyOf":anyOfList
}
}
]
}
if rdfType == "rdfs:Class":
numClassProperties = len(v["propertiesInclude"])
if "rdfs:subClassOf" in v.keys():
defs[label] = {"allOf": []}
for parentClass in v["rdfs:subClassOf"]:
parentLabel = parentClass["@id"].replace("schema:","")
if parentLabel in skip_list:
continue
defs[label]["allOf"].append({"$ref": "#/$defs/"+parentLabel})
if numClassProperties > 0:
classProperties = dict()
classProperties["type"] = "object"
classProperties["properties"] = dict()
for pi in v["propertiesInclude"]:
pi_label = pi["@id"].split(":")[1]
classProperties["properties"][pi_label] = {"$ref": "#/$defs/"+pi_label}
defs[label]["allOf"].append(classProperties)
else:
if numClassProperties > 0:
defs[label] = dict()
defs[label]["type"] = "object"
defs[label]["properties"] = dict()
properties = defs[label]["properties"]
for pi in v["propertiesInclude"]:
pi_label = pi["@id"].split(":")[1]
properties[pi_label] = {"$ref": "#/$defs/"+pi_label}
with open('mat-dictionary.json','w') as f:
json.dump(dictionary, f, sort_keys=True, indent=4)