예제 #1
0
    def create(self, graph):
        uri = self.create_uri()
        inputUri = "#"
        g = Graph(self.store,URIRef(uri))
        g.remove((None,None,None))
        g += rebase(graph, uri, inputUri)
        g.add((URIRef(uri),RDF.type,self.cl))
        g.add((URIRef(uri), dc.created, Literal(datetime.datetime.now())))
        g.commit()

        o = Graph()
        o.add((URIRef(inputUri),OWL.sameAs,uri))
        return o
예제 #2
0
파일: filters.py 프로젝트: Jamesm4/whyis
    def construct_filter(query, graph=app.db, prefixes={}, values=None):
        def remap_bnode(x):
            if isinstance(x, URIRef) and x.startswith('bnode:'):
                return BNode(x.replace('bnode:', ''))
            else:
                return x

        namespaces = dict(app.NS.prefixes)
        namespaces.update(
            dict([(key, rdflib.URIRef(value))
                  for key, value in prefixes.items()]))
        params = {'initNs': namespaces}
        if values is not None:
            params['initBindings'] = values
        g = ConjunctiveGraph()
        for stmt in graph.query(query, **params):
            g.add(tuple([remap_bnode(x) for x in stmt]))
        print len(g)
        return g
예제 #3
0
파일: main.py 프로젝트: irl/womaas
def tobj(objname):
    SCHEMA = Namespace('http://schema.org/')
    SPDX = Namespace('http://www.spdx.org/rdf/terms#')
    n = NamespaceManager(Graph())
    n.bind("schema", SCHEMA)
    n.bind("spdx", SPDX)
    c = get_db().cursor()
    c.execute('SELECT * FROM objects WHERE id=?', (objname,))
    obj = c.fetchone()
    g = Graph()
    g.namespace_manager = n
    objuri = URIRef("http://localhost:5000/b/" + obj[0])
    robjuri = URIRef("http://localhost:5000/r/" + obj[0])
    md5node = BNode()
    g.add((md5node, SPDX.checksumValue, Literal(obj[2])))
    g.add((md5node, SPDX.algorithm, URIRef("http://packages.qa.debian.org/#checksumAlgorithm_md5sum")))
    g.add((objuri, SPDX.checksum, md5node))
    g.add((objuri, SCHEMA.fileSize, Literal(obj[1])))
    return Response(g.serialize(format="turtle"), mimetype="text/plain")
예제 #4
0
    def _render_skos_rdf(self):
        print("_render_skos_rdf")
        g = Graph()
        g.bind("dct", DCTERMS)
        g.bind("skos", SKOS)

        c = URIRef(self.concept.uri)

        # Concept SKOS metadata
        g.add((c, RDF.type, SKOS.Concept))
        g.add((c, SKOS.prefLabel,
               Literal(self.concept.prefLabel, lang=config.DEFAULT_LANGUAGE)))
        g.add((c, SKOS.definition,
               Literal(self.concept.definition, lang=config.DEFAULT_LANGUAGE)))

        for k, v in self.concept.related_instances.items():
            for k2, v2 in v.items():
                if k2 == "instances":
                    for inst in v2:
                        g.add((
                            c,
                            URIRef(k),
                            URIRef(inst[0]
                                   )  # only URIs for RDF, not prefLabels too
                        ))

        # serialise in the appropriate RDF format
        if self.mediatype in ["application/rdf+json", "application/json"]:
            graph_text = g.serialize(format="json-ld")
        else:
            graph_text = g.serialize(format=self.mediatype)

        return Response(
            graph_text,
            mimetype=self.mediatype,
            headers=self.headers,
        )
예제 #5
0
    def _render_skos_rdf(self):
        # make Collection RDF from Collection object in memory
        # (this is faster than re-querying for RDF from the main cache or a vocab's source)
        # this is the SKOS view - only SKOS stuff
        g = Graph()
        g.bind("dcterms", DCTERMS)
        g.bind("skos", SKOS)

        c = URIRef(self.collection.uri)

        # Collection metadata
        g.add((
            c,
            RDF.type,
            SKOS.Collection
        ))
        g.add((
            c,
            SKOS.prefLabel,
            Literal(self.collection.prefLabel, lang=config.DEFAULT_LANGUAGE)
        ))
        if self.collection.definition is not None:
            g.add((
                c,
                SKOS.definition,
                Literal(self.collection.definition, lang=config.DEFAULT_LANGUAGE)
            ))
        for k, v in self.collection.source.items():
            if k == "provenance" and v is not None:
                g.add((
                    c,
                    DCTERMS.provenance,
                    Literal(self.collection.source["provenance"], lang=config.DEFAULT_LANGUAGE)
                ))
            elif k == "source" and v is not None:
                g.add((
                    c,
                    DCTERMS.source,
                    URIRef(self.collection.source["source"])
                ))
            elif k == "wasDerivedFrom" and v is not None:
                g.add((
                    c,
                    DCTERMS.provenance,
                    URIRef(self.collection.source["wasDerivedFrom"])
                ))
        # vocab
        if self.collection.vocab_uri is not None:
            g.add((
                c,
                SKOS.inScheme,
                URIRef(self.collection.vocab_uri),
            ))

        # members
        for m in self.collection.members:
            g.add((
                c,
                SKOS.member,
                URIRef(m[0]),
            ))

        # serialise in the appropriate RDF format
        if self.mediatype in ["application/rdf+json", "application/json"]:
            graph_text = g.serialize(format="json-ld")
        else:
            graph_text = g.serialize(format=self.mediatype)

        return Response(
            graph_text,
            mimetype=self.mediatype,
            headers=self.headers,
        )
예제 #6
0
파일: concept.py 프로젝트: RDFLib/VocPrez
    def _render_skos_rdf(self):
        g = Graph()
        g.bind("dct", DCTERMS)
        g.bind("skos", SKOS)

        c = URIRef(self.concept.uri)

        # Concept SKOS metadata
        g.add((c, RDF.type, SKOS.Concept))
        g.add((c, SKOS.prefLabel,
               Literal(self.concept.prefLabel, lang=config.DEFAULT_LANGUAGE)))
        g.add((c, SKOS.definition,
               Literal(self.concept.definition, lang=config.DEFAULT_LANGUAGE)))

        if self.concept.related_instances is not None:
            for prop in self.concept.related_instances:
                if str(prop.value).startswith("http"):
                    g.add((c, URIRef(prop.uri), URIRef(prop.value)))
                else:
                    g.add((c, URIRef(prop.uri), Literal(prop.value)))

        if self.concept.other_properties is not None:
            for prop in self.concept.other_properties:
                if str(prop.value).startswith("http"):
                    g.add((c, URIRef(prop.uri), URIRef(prop.value)))
                else:
                    g.add((c, URIRef(prop.uri), Literal(prop.value)))

        # serialise in the appropriate RDF format
        if self.mediatype in ["application/rdf+json", "application/json"]:
            graph_text = g.serialize(format="json-ld")
        else:
            graph_text = g.serialize(format=self.mediatype)

        return Response(
            graph_text,
            mimetype=self.mediatype,
            headers=self.headers,
        )