|
| 1 | +import json |
| 2 | +from collections import defaultdict |
| 3 | +import os |
| 4 | + |
| 5 | +defs = json.load(open('definitions.json', 'r')) |
| 6 | + |
| 7 | +classes = defaultdict(list) |
| 8 | +props = defaultdict(lambda: defaultdict(list)) |
| 9 | + |
| 10 | +wrap = lambda x: "nimads:{}".format(x) if ':' not in x else x |
| 11 | + |
| 12 | +for t_name, data in defs.items(): |
| 13 | + for p_name, p_type in data['properties']: |
| 14 | + ## Type properties |
| 15 | + classes[t_name].append({ |
| 16 | + "@id": wrap(p_name), |
| 17 | + "schema:domainIncludes": {"@id": wrap(t_name)} |
| 18 | + }) |
| 19 | + # Property mappings |
| 20 | + props[p_name]["domainIncludes"].append({"@id": wrap(t_name)}) |
| 21 | + props[p_name]["rangeIncludes"].append({"@id": wrap(p_type)}) |
| 22 | + |
| 23 | + if 'nimads' in p_type: |
| 24 | + p_type = p_type.split(':')[1] |
| 25 | + classes[p_type].append({ |
| 26 | + "@id": wrap(p_name), |
| 27 | + "schema:rangeIncludes": {"@id": wrap(p_type)} |
| 28 | + }) |
| 29 | + |
| 30 | +context = { |
| 31 | + "schema": "http://schema.org/", |
| 32 | + "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", |
| 33 | + "rdfs": "http://www.w3.org/2000/01/rdf-schema#", |
| 34 | + "nimads": "http://neurostuff.org/nimads/" |
| 35 | +} |
| 36 | + |
| 37 | +for c, c_props in classes.items(): |
| 38 | + graph = [ |
| 39 | + { |
| 40 | + "@id": wrap(c), |
| 41 | + "@type": "rdfs:Class", |
| 42 | + "rdfs:comment": "A NIMADS {}".format(c), |
| 43 | + "rdfs:label": c, |
| 44 | + "rdfs:subClassOf": {"@id": "schema:Thing"} |
| 45 | + } |
| 46 | + ] + c_props |
| 47 | + |
| 48 | + data = { |
| 49 | + "@context": context, |
| 50 | + "@graph": graph |
| 51 | + } |
| 52 | + |
| 53 | + with open(os.path.join('nimads', c + '.jsonld'), 'w') as f: |
| 54 | + json.dump(data, f, indent=2) |
| 55 | + |
| 56 | +for c, entries in props.items(): |
| 57 | + data = { |
| 58 | + "@context": context, |
| 59 | + "@id": wrap(c), |
| 60 | + "@type": "rdf:Property", |
| 61 | + "rdfs:label": c |
| 62 | + } |
| 63 | + data.update(entries) |
| 64 | + with open(os.path.join('nimads', '_' + c + '.jsonld'), 'w') as f: |
| 65 | + json.dump(data, f, indent=2) |
0 commit comments