aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-01-09 21:49:27 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2017-01-09 21:49:27 +0100
commit3e25696ac167461ffc6b9f6088a719784ddca6e0 (patch)
tree7b6494ec16be91a7bfe3a11c37160c17852db8cc
parent78bc8446b18f555feb9d8c872c0848a27ceed8b9 (diff)
downloadeda-rdf-master.tar.gz
eda-rdf-master.tar.bz2
eda-rdf-master.tar.xz
eda-rdf-master.zip
More OWL stuff.HEADmaster
-rw-r--r--owl/digikey.owl15
-rw-r--r--owl/eda.owl60
-rw-r--r--owl/kicad.owl3
-rw-r--r--trygvis/eda/__init__.py5
-rw-r--r--trygvis/eda/cli/__init__.py8
-rwxr-xr-xtrygvis/eda/cli/digikey_download_metadata.py13
-rw-r--r--trygvis/eda/digikey/__init__.py63
-rw-r--r--trygvis/eda/digikey/rdf.py12
8 files changed, 147 insertions, 32 deletions
diff --git a/owl/digikey.owl b/owl/digikey.owl
new file mode 100644
index 0000000..6f57250
--- /dev/null
+++ b/owl/digikey.owl
@@ -0,0 +1,15 @@
+Prefix(:=<https://trygvis/purl/digikey#>)
+Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
+Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
+Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)
+Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
+Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)
+
+
+Ontology(<https://trygvis/purl/digikey#>
+Import(<https://trygvis/purl/eda#>)
+
+Declaration(Class(:AttributeType))
+Declaration(Class(:AttributeValue))
+
+) \ No newline at end of file
diff --git a/owl/eda.owl b/owl/eda.owl
new file mode 100644
index 0000000..c6e35b4
--- /dev/null
+++ b/owl/eda.owl
@@ -0,0 +1,60 @@
+Prefix(:=<https://trygvis/purl/eda#>)
+Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
+Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
+Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)
+Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
+Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)
+
+
+Ontology(<https://trygvis/purl/eda#>
+
+Declaration(Class(:Capacitor))
+Declaration(Class(:PackagingClass))
+Declaration(Class(:PackagingValue))
+Declaration(Class(:PartFeature))
+Declaration(Class(:PartType))
+Declaration(Class(:Resistor))
+Declaration(Class(:SMD_Package))
+Declaration(Class(:0402))
+Declaration(Class(:0805))
+Declaration(Class(:1206))
+Declaration(NamedIndividual(<https://trygvis.io/purl/digikey-attribute-type#pv16>))
+############################
+# Classes
+############################
+
+# Class: :Capacitor (:Capacitor)
+
+SubClassOf(:Capacitor :PartType)
+
+# Class: :PackagingClass (:PackagingClass)
+
+SubClassOf(:PackagingClass :PartFeature)
+
+# Class: :Resistor (:Resistor)
+
+SubClassOf(:Resistor :PartType)
+
+# Class: :0402 (:0402)
+
+SubClassOf(:0402 :SMD_Package)
+
+# Class: :0805 (:0805)
+
+SubClassOf(:0805 :SMD_Package)
+
+# Class: :1206 (:1206)
+
+SubClassOf(:1206 :SMD_Package)
+
+
+############################
+# Named Individuals
+############################
+
+# Individual: <https://trygvis.io/purl/digikey-attribute-type#pv16> (Package / Case)
+
+ClassAssertion(:PackagingClass <https://trygvis.io/purl/digikey-attribute-type#pv16>)
+
+
+) \ No newline at end of file
diff --git a/owl/kicad.owl b/owl/kicad.owl
index 3177595..3887a9d 100644
--- a/owl/kicad.owl
+++ b/owl/kicad.owl
@@ -7,7 +7,8 @@ Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)
Prefix(kicad-random:=<https://trygvis/purl/kicad-random/>)
-Ontology(<https://trygvis/purl/kicad>
+Ontology(<https://trygvis/purl/kicad#>
+Import(<https://trygvis/purl/eda#>)
Declaration(Class(:component))
Declaration(Class(:field))
diff --git a/trygvis/eda/__init__.py b/trygvis/eda/__init__.py
index 408bd7c..26181d4 100644
--- a/trygvis/eda/__init__.py
+++ b/trygvis/eda/__init__.py
@@ -1,2 +1,7 @@
+from rdflib import Namespace
+
+EDA = Namespace("https://trygvis.io/purl/eda#")
+
+
class EdaException(Exception):
pass
diff --git a/trygvis/eda/cli/__init__.py b/trygvis/eda/cli/__init__.py
index 422cf48..3807fb3 100644
--- a/trygvis/eda/cli/__init__.py
+++ b/trygvis/eda/cli/__init__.py
@@ -5,13 +5,14 @@ from os import mkdir
from os.path import dirname, isdir
from typing import Callable
-from rdflib import store, ConjunctiveGraph, Graph, RDF, RDFS
+from rdflib import store, ConjunctiveGraph, Graph, RDF, RDFS, OWL
from rdflib.plugins.sparql import prepareQuery
import rdflib.plugins.stores.sparqlstore as sparqlstore
import configparser
# noinspection PyUnresolvedReferences
import argparse
+from .. import EDA
from ..digikey import rdf as digikey_rdf
from ..kicad import rdf as kicad_rdf
@@ -84,6 +85,9 @@ def with_database(tx):
def create_graph(digikey=False, kicad=False) -> Graph:
g = Graph()
+ g.bind("owl", OWL)
+ g.bind("eda", EDA)
+
if digikey:
g.bind("dk", digikey_rdf.DIGIKEY)
g.bind("dk-part", digikey_rdf.DIGIKEY_PART)
@@ -124,6 +128,8 @@ def write_graph(gen_g: Callable[[], Graph], filename: str = None, force_write: b
_initNs = {
"rdf": RDF,
"rdfs": RDFS,
+ "owl": OWL,
+ "eda": EDA,
"dk": digikey_rdf.DIGIKEY,
"dk-attr-type": digikey_rdf.DIGIKEY_ATTRIBUTE_TYPE,
"dk-attr-value": digikey_rdf.DIGIKEY_ATTRIBUTE_VALUE,
diff --git a/trygvis/eda/cli/digikey_download_metadata.py b/trygvis/eda/cli/digikey_download_metadata.py
index 60880ab..59c8099 100755
--- a/trygvis/eda/cli/digikey_download_metadata.py
+++ b/trygvis/eda/cli/digikey_download_metadata.py
@@ -1,4 +1,6 @@
-from trygvis.eda import cli
+from rdflib.term import URIRef
+
+from trygvis.eda import cli, EDA
from trygvis.eda.digikey import *
@@ -19,11 +21,15 @@ def do_categories(db: DigikeyDatabase, client: DigikeyClient, output: str):
def do_category(client: DigikeyClient, sc: DigikeyProductCategory, output: str):
- cli.info('Downloading categories for %s' % sc.label)
+ cli.info('Downloading attributes for %s' % sc.label)
def make_graph():
attributes = download_attribute_types_from_category(sc, client)
- g = cli.create_graph()
+ g = cli.create_graph(digikey=True)
+
+ g.add((sc.url(), RDF.type, OWL.Ontology))
+ g.add((sc.url(), OWL.imports, URIRef(rdf.DIGIKEY)))
+
for a in attributes:
[g.add(node) for node in a.to_nodes()]
@@ -31,6 +37,7 @@ def do_category(client: DigikeyClient, sc: DigikeyProductCategory, output: str):
cli.write_graph(make_graph, output, force_write=True)
+
def run(args):
db = DigikeyDatabase()
client = DigikeyClient()
diff --git a/trygvis/eda/digikey/__init__.py b/trygvis/eda/digikey/__init__.py
index 8fe18c8..2f93858 100644
--- a/trygvis/eda/digikey/__init__.py
+++ b/trygvis/eda/digikey/__init__.py
@@ -7,7 +7,7 @@ from cachecontrol.caches.file_cache import FileCache
from cachecontrol.heuristics import ExpiresAfter
from lxml import html
from rdflib import Literal
-from rdflib.namespace import RDF, RDFS
+from rdflib.namespace import RDF, RDFS, OWL
import trygvis.eda.digikey.rdf
@@ -41,8 +41,8 @@ class DigikeyDatabase(object):
return sc
return None
- def merge_attribute_types(self, attributeTypes):
- for a in attributeTypes:
+ def merge_attribute_types(self, attribute_types):
+ for a in attribute_types:
if a.id in self.attributeTypes:
# TODO: implement merging
continue
@@ -78,11 +78,12 @@ class DigikeyProductCategory(object):
node = self.url()
nodes = [
(node, RDF.type, rdf.DIGIKEY.productCategory),
+ # (node, RDF.type, OWL.Class),
(node, RDFS.label, Literal(self.label)),
]
if self.parent is not None:
- parentUrl = rdf.DIGIKEY_PRODUCT_CATEGORY[self.parent.id]
- nodes.append((node, rdf.DIGIKEY.parent, parentUrl))
+ parent_url = rdf.DIGIKEY_PRODUCT_CATEGORY[self.parent.id]
+ nodes.append((node, rdf.DIGIKEY.parent, parent_url))
if self.digikey_url is not None:
nodes.append((node, rdf.DIGIKEY.url, Literal(self.digikey_url)))
return nodes
@@ -101,17 +102,34 @@ class DigikeyAttributeType(object):
assert self.options is not None
def to_nodes(self):
- nodes = []
node = rdf.DIGIKEY_ATTRIBUTE_TYPE[self.id]
- nodes.append((node, RDF.type, rdf.DIGIKEY.attributeType))
- nodes.append((node, RDFS.label, Literal(self.label)))
+ node_value = rdf.DIGIKEY_ATTRIBUTE_TYPE[self.id + "-value"]
+ node_option = rdf.DIGIKEY_ATTRIBUTE_TYPE[self.id + "-option"]
+ nodes = [
+ (node, RDF.type, rdf.DIGIKEY.attributeType),
+ (node, RDF.type, OWL.Class),
+ (node, RDFS.subClassOf, rdf.DIGIKEY['AttributeType']),
+ (node, RDFS.label, Literal(self.label)),
+
+ (node_option, RDF.type, OWL.ObjectProperty),
+ (node_option, RDFS.domain, node),
+ (node_option, RDFS.range, node_value),
+ (node_option, RDFS.label, Literal('Options for ' + self.label)),
+
+ (node_value, RDF.type, OWL.Class),
+ (node_value, RDFS.subClassOf, rdf.DIGIKEY['AttributeValue']),
+ (node_value, RDFS.label, Literal(self.label + ' values')),
+ ]
for o in self.options:
- optionNode = rdf.DIGIKEY_ATTRIBUTE_VALUE[self.id + '-' + o.id]
+ option_node = rdf.DIGIKEY_ATTRIBUTE_VALUE[self.id + '-' + o.id]
nodes.extend([
- (optionNode, rdf.DIGIKEY.id, Literal(o.id)),
- (optionNode, RDFS.label, Literal(o.label)),
- (node, rdf.DIGIKEY.value, optionNode)])
+ (option_node, RDF.type, OWL.Class),
+ (option_node, RDFS.subClassOf, node_value),
+ (option_node, rdf.DIGIKEY.id, Literal(o.id)),
+ (option_node, RDFS.label, Literal(o.label)),
+ # (node, rdf.DIGIKEY.value, option_node)])
+ (node, node_option, option_node)])
return nodes
@@ -190,17 +208,20 @@ def download_category_tree(database: DigikeyDatabase, client: DigikeyClient,
for h2 in items:
label = _to_string(h2)
# print(h2)
- pcId = None
+ if not label == 'Resistors':
+ continue
+
+ pc_id = None
for a in h2.getchildren():
url = a.get('href')
- pcId = _id_from_url(url)
- if pcId is None:
+ pc_id = _id_from_url(url)
+ if pc_id is None:
continue
- if pcId is None:
+ if pc_id is None:
continue
- pc = DigikeyProductCategory(pcId, label)
+ pc = DigikeyProductCategory(pc_id, label)
n = h2.getnext()
if n.tag == 'span':
n = n.getnext()
@@ -230,13 +251,13 @@ def download_attribute_types_from_category(category: DigikeyProductCategory, cli
td = select.getparent()
index = td.getparent().index(td)
try:
- attributeLabel = headers[index]
+ attribute_label = headers[index]
except:
continue
- attributeId = select.get('name')
- print("label: " + attributeLabel + ", id: " + attributeId)
+ attribute_id = select.get('name')
+ print("label: " + attribute_label + ", id: " + attribute_id)
options = []
- type = DigikeyAttributeType(category, attributeId, attributeLabel, options)
+ type = DigikeyAttributeType(category, attribute_id, attribute_label, options)
for o in select.xpath("./option"):
id = o.get('value')
label = _to_string(o)
diff --git a/trygvis/eda/digikey/rdf.py b/trygvis/eda/digikey/rdf.py
index 7a21275..5f1dede 100644
--- a/trygvis/eda/digikey/rdf.py
+++ b/trygvis/eda/digikey/rdf.py
@@ -1,7 +1,7 @@
-import rdflib
+from rdflib import Namespace
-DIGIKEY = rdflib.Namespace("https://trygvis.io/purl/digikey#")
-DIGIKEY_ATTRIBUTE_TYPE = rdflib.Namespace("https://trygvis.io/purl/digikey-attribute-type#")
-DIGIKEY_ATTRIBUTE_VALUE = rdflib.Namespace("https://trygvis.io/purl/digikey-attribute-value#")
-DIGIKEY_PART = rdflib.Namespace("https://trygvis.io/purl/digikey-part#")
-DIGIKEY_PRODUCT_CATEGORY = rdflib.Namespace("https://trygvis.io/purl/digikey-product-category#")
+DIGIKEY = Namespace("https://trygvis.io/purl/digikey#")
+DIGIKEY_ATTRIBUTE_TYPE = Namespace("https://trygvis.io/purl/digikey-attribute-type#")
+DIGIKEY_ATTRIBUTE_VALUE = Namespace("https://trygvis.io/purl/digikey-attribute-value#")
+DIGIKEY_PART = Namespace("https://trygvis.io/purl/digikey-part#")
+DIGIKEY_PRODUCT_CATEGORY = Namespace("https://trygvis.io/purl/digikey-product-category#")