From 78bc8446b18f555feb9d8c872c0848a27ceed8b9 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 8 Jan 2017 01:26:44 +0100 Subject: kicad.owl: making the model look more like kicad's own model. --- trygvis/eda/cli/kicad_import_project.py | 39 ++++++------ trygvis/eda/cli/make_bom.py | 108 ++++++++++++++++++-------------- 2 files changed, 82 insertions(+), 65 deletions(-) (limited to 'trygvis/eda/cli') diff --git a/trygvis/eda/cli/kicad_import_project.py b/trygvis/eda/cli/kicad_import_project.py index 422f24a..d43b10f 100755 --- a/trygvis/eda/cli/kicad_import_project.py +++ b/trygvis/eda/cli/kicad_import_project.py @@ -63,12 +63,14 @@ def run(args): info("Version: %s, timestamp=%s" % (version, timestamp)) with src: - (version, project_g) = export_to_graph(src, project_url, version, timestamp) + data_url = URIRef(kicad_rdf.gen_random(timestamp)) + graph = export_to_graph(src, data_url) - debug('Loaded %s tuples' % len(project_g)) + graph.add((data_url, kicad_rdf.KICAD['version-uuid'], Literal(version))) + graph.add((data_url, kicad_rdf.KICAD['version-of'], URIRef(project_url))) + graph.add((data_url, kicad_rdf.KICAD['timestamp'], Literal(timestamp))) - project_g.add((URIRef(project_url), RDF.type, kicad_rdf.KICAD['project'])) - project_g.add((URIRef(project_url), kicad_rdf.KICAD['version'], version)) + debug('Loaded %s tuples' % len(graph)) if args.output_dir is not None: parent = args.output_dir @@ -77,16 +79,16 @@ def run(args): v = timestamp.strftime("%Y-%m-%d %H:%M:%S") output_file = os.path.join(parent, "project-%s.ttl" % v) - write_graph(lambda: project_g, filename=output_file, force_write=True) + write_graph(lambda: graph, filename=output_file, force_write=True) def import_project(g): - for idx, t in enumerate(project_g.triples((None, None, None))): + for idx, t in enumerate(graph.triples((None, None, None))): g.add(t) with_database(import_project) -def export_to_graph(src: object, project_url: str, version: str, timestamp: datetime.datetime) -> (str, rdflib.Graph): +def export_to_graph(src: object, url: URIRef) -> rdflib.Graph: export = Export.from_xml_file(src) # print('components:') @@ -158,11 +160,7 @@ def export_to_graph(src: object, project_url: str, version: str, timestamp: date g = create_graph(kicad=True) - version_uri = URIRef('urn:uuid:%s#' % version) - g.add((version_uri, RDF.type, kicad_rdf.KICAD["project-version"])) - g.add((version_uri, RDFS.label, Literal('Version: %s' % timestamp.strftime("%Y-%m-%d %H:%M:%S")))) - g.add((version_uri, kicad_rdf.KICAD['version-of'], URIRef(project_url))) - g.add((version_uri, kicad_rdf.KICAD['timestamp'], Literal(timestamp))) + g.add((url, RDF.type, kicad_rdf.KICAD["project"])) # The components and fields could/should have been a BNodes. Their generated names are not very nice. # TODO: try using a hash of the current value and put that under a special generated namespace. @@ -170,10 +168,11 @@ def export_to_graph(src: object, project_url: str, version: str, timestamp: date # hash('http://example.org/my-boardC10') => 123456 # add_prefix(hash) => 'https://trygvis.io/purl/kicad/generated#123456' footprints = set() + component_ns = rdflib.Namespace(str(url + "/component")) + sheet_ns = rdflib.Namespace(str(url + "/sheet")) for c in export.components: - ns = rdflib.Namespace(str(version_uri)) - node = ns[c.ref] - g.add((version_uri, kicad_rdf.KICAD['component'], node)) + node = component_ns[c.ref] + g.add((url, kicad_rdf.KICAD['component'], node)) g.add((node, RDF.type, kicad_rdf.KICAD['component'])) g.add((node, RDFS.label, Literal(c.ref))) @@ -197,12 +196,14 @@ def export_to_graph(src: object, project_url: str, version: str, timestamp: date d = export.design if d is not None: - g.add((version_uri, kicad_rdf.KICAD['design-date'], Literal(d.date))) + g.add((url, kicad_rdf.KICAD['design-date'], Literal(d.date))) for s in d.sheets: - sheet_uri = kicad_rdf.gen_random(s.number) - g.add((sheet_uri, RDF.type, kicad_rdf.KICAD['sheet'])) label = s.title_block.title if s.title_block.title is not None and len(s.title_block.title) > 0 else s.name + + sheet_uri = sheet_ns[s.number] + g.add((url, kicad_rdf.KICAD['sheet'], sheet_uri)) + g.add((sheet_uri, RDF.type, kicad_rdf.KICAD['sheet'])) g.add((sheet_uri, RDFS.label, Literal(label))) g.add((sheet_uri, kicad_rdf.KICAD['sheet-name'], Literal(s.name))) g.add((sheet_uri, kicad_rdf.KICAD['sheet-number'], Literal(int(s.number)))) @@ -210,4 +211,4 @@ def export_to_graph(src: object, project_url: str, version: str, timestamp: date g.add((sheet_uri, kicad_rdf.KICAD['rev'], Literal(s.title_block.rev))) g.add((sheet_uri, kicad_rdf.KICAD['date'], Literal(s.title_block.date))) - return version_uri, g + return g diff --git a/trygvis/eda/cli/make_bom.py b/trygvis/eda/cli/make_bom.py index 1023c11..4e341b8 100755 --- a/trygvis/eda/cli/make_bom.py +++ b/trygvis/eda/cli/make_bom.py @@ -48,45 +48,48 @@ def work(args, g): components = {} dk_parts = {} - version = None if args.version is not None: - version = args.version + version_url = args.version + version_uuid = None + timestamp = None else: cli.info("Finding latest version") res = cli.sparql(g, """ SELECT -?project ?version ?timestamp +?project ?timestamp ?version_uuid WHERE { - ?project a kicad:project . - ?version a kicad:project-version; - kicad:timestamp ?timestamp; - kicad:version-of ?project + ?project a kicad:project ; + kicad:timestamp ?timestamp ; + kicad:version-uuid ?version_uuid ; + kicad:version-of ?orig_project . } ORDER BY DESC(?timestamp) LIMIT 1 -""", init_bindings={"project": rdflib.URIRef(project_url)}) +""", init_bindings={"orig_project": rdflib.URIRef(project_url)}) for row in res: - version = row.version + version_url = row.project + version_uuid = row.version_uuid + timestamp = row.timestamp break else: raise EdaException("Could not find any version of project %s" % project_url) - cli.info("Loading components for version %s" % version) + cli.info("Loading components for version %s, uuid=%s, timestamp=%s" % (version_url, version_uuid, timestamp)) res = cli.sparql(g, """ SELECT ?ref ?value WHERE { - ?version a kicad:project-version ; + ?version a kicad:project ; kicad:component ?cmp . ?cmp a kicad:component ; kicad:value ?value ; rdfs:label ?ref . } ORDER BY ?ref -""", init_bindings={"version": rdflib.URIRef(version)}) +""", init_bindings={"version": rdflib.URIRef(version_url)}) for row in res: c = Component(row.ref, row.value) @@ -101,18 +104,22 @@ ORDER BY ?ref SELECT ?ref ?field ?field_name ?field_value WHERE { - ?version a kicad:project-version ; + ?version a kicad:project ; kicad:component ?cmp . + ?cmp a kicad:component ; rdfs:label ?ref ; kicad:field ?field . - ?field a kicad:field ; kicad:field_name ?field_name . - ?field a kicad:field ; kicad:field_value ?field_value . + + ?field a kicad:field ; + kicad:field-name ?field_name ; + kicad:field-value ?field_value . } ORDER BY ?ref ?field_name -""", init_bindings={"version": rdflib.URIRef(version)}) +""", init_bindings={"version": rdflib.URIRef(version_url)}) for row in res: + # cli.debug('row: %s' % str(row)) c = components[row.ref] assert isinstance(c, Component) c.set_field(row.field_name, row.field_value) @@ -123,23 +130,28 @@ ORDER BY ?ref ?field_name SELECT ?ref ?part_number ?type ?value ?attr_type WHERE { - ?project kicad:component ?cmp . - ?cmp a kicad:component ; - rdfs:label ?ref ; - kicad:field ?d . - ?d kicad:field_name "digikey" ; - kicad:field_value ?part_number . - ?part a dk:part ; - rdfs:label ?label ; + ?version a kicad:project ; + kicad:component ?cmp . + + ?cmp a kicad:component ; + rdfs:label ?ref ; + kicad:field ?d . + + ?d kicad:field-name "digikey" ; + kicad:field-value ?part_number . + + ?part a dk:part ; + # rdfs:label ?label ; dk:partNumber ?part_number ; dk:attribute-value ?attr_value . - ?attr_value rdfs:label ?value . - ?attr_type a dk:attributeType ; - rdfs:label ?type ; - dk:value ?attr_value . + + ?attr_value rdfs:label ?value . + ?attr_type a dk:attributeType ; + rdfs:label ?type ; + dk:value ?attr_value . } ORDER BY ?ref ?attr_type ?attr_value -""", init_bindings={"project": rdflib.URIRef(project_url)}) +""", init_bindings={"version": rdflib.URIRef(version_url)}) for row in res: pn = row.part_number @@ -159,24 +171,28 @@ SELECT ?ref ?footprint ?part_number ?package_value ?case_value # * WHERE { - ?project kicad:component ?cmp . - ?cmp a kicad:component ; - rdfs:label ?ref ; - kicad:field ?dk_field . - optional { - ?cmp kicad:footprint ?footprint_url . - ?footprint_url rdfs:label ?footprint . - } . - ?dk_field kicad:field_name "digikey" ; - kicad:field_value ?part_number . - ?part a dk:part ; + ?version a kicad:project ; + kicad:component ?cmp . + + ?cmp a kicad:component ; + rdfs:label ?ref ; + kicad:field ?dk_field . + + optional { + ?cmp kicad:footprint ?footprint_url . + ?footprint_url rdfs:label ?footprint . + } . + + ?dk_field kicad:field-name "digikey" ; + kicad:field-value ?part_number . + ?part a dk:part ; dk:partNumber ?part_number . - optional { - ?part dk:attribute-value ?package_url . - ?package_url rdfs:label ?package_value . - ?package_type a dk:attributeType ; dk:value ?package_url . - } . + optional { + ?part dk:attribute-value ?package_url . + ?package_url rdfs:label ?package_value . + ?package_type a dk:attributeType ; dk:value ?package_url . + } . # optional { # ?part dk:attribute-value ?case_url . @@ -189,7 +205,7 @@ WHERE { # VALUES (?case_type) { (dk-attr-type:pv1291) } . } ORDER BY ?ref -""", init_bindings={"project": rdflib.URIRef(project_url), +""", init_bindings={"version": rdflib.URIRef(version_url), "package_type": digikey_rdf.DIGIKEY_ATTRIBUTE_TYPE["pv16"], "cast_type": digikey_rdf.DIGIKEY_ATTRIBUTE_TYPE["pv1291"]}) -- cgit v1.2.3