Example #1
0
 def process_graph(self, inputGraph):
     instances = self.getInstances(inputGraph)
     results = []
     for i in instances:
         print "Processing", i.identifier, self
         output_nanopub = Nanopublication()
         o = output_nanopub.assertion.resource(
             i.identifier)  # OutputClass(i.identifier)
         error = False
         try:
             result = self.process_nanopub(i, o, output_nanopub)
         except Exception as e:
             output_nanopub.add(
                 (output_nanopub.assertion.identifier,
                  self.app.NS.sioc.content, rdflib.Literal(str(e))))
             logging.exception(
                 "Error processing resource %s in nanopub %s" %
                 (i.identifier, inputGraph.identifier))
             error = True
         print "Output Graph", output_nanopub.identifier, len(
             output_nanopub)
         for new_np in self.app.nanopub_manager.prepare(
                 rdflib.ConjunctiveGraph(store=output_nanopub.store)):
             if len(new_np.assertion) == 0 and not error:
                 continue
             self.explain(new_np, i, o)
             new_np.add((new_np.identifier, sio.isAbout, i.identifier))
             #print new_np.serialize(format="trig")
             if not self.dry_run:
                 self.app.nanopub_manager.publish(new_np)
             results.append(new_np)
     return results
Example #2
0
    def run(self, input_file, file_format="trig", was_revision_of=None):
        if was_revision_of is not None:
            wasRevisionOf = set(flask.current_app.db.objects(predicate=np.hasAssertion,
                                                               subject=rdflib.URIRef(was_revision_of)))
            if len(wasRevisionOf) == 0:
                print "Could not find active nanopublication to revise:", was_revision_of
                return
            was_revision_of = wasRevisionOf
        g = rdflib.ConjunctiveGraph(identifier=rdflib.BNode().skolemize(), store="Sleepycat")
        graph_tempdir = tempfile.mkdtemp()
        g.store.open(graph_tempdir, True)
        #g = rdflib.ConjunctiveGraph(identifier=rdflib.BNode().skolemize())

        g1 = g.parse(location=input_file, format=file_format, publicID=flask.current_app.NS.local)
        if len(list(g.subjects(rdflib.RDF.type, np.Nanopublication))) == 0:
            print "Could not find existing nanopublications.", len(g1), len(g)
            new_np = Nanopublication(store=g1.store)
            new_np.add((new_np.identifier, rdflib.RDF.type, np.Nanopublication))
            new_np.add((new_np.identifier, np.hasAssertion, g1.identifier))
            new_np.add((g1.identifier, rdflib.RDF.type, np.Assertion))

        nanopub_prepare_graph = rdflib.ConjunctiveGraph(store="Sleepycat")
        nanopub_prepare_graph_tempdir = tempfile.mkdtemp()
        nanopub_prepare_graph.store.open(nanopub_prepare_graph_tempdir, True)
        nanopubs = []
        for npub in flask.current_app.nanopub_manager.prepare(g, store=nanopub_prepare_graph.store):
            if was_revision_of is not None:
                for r in was_revision_of:
                    print "Marking as revision of", r
                    npub.pubinfo.add((npub.assertion.identifier, flask.current_app.NS.prov.wasRevisionOf, r))
            print 'Prepared', npub.identifier
            nanopubs.append(npub)
        flask.current_app.nanopub_manager.publish(*nanopubs)
        print "Published", npub.identifier
Example #3
0
 def _prep_nanopub(self, nanopub_uri, graph):
     nanopub = Nanopublication(store=graph.store, identifier=nanopub_uri)
     about = nanopub.nanopub_resource.value(app.NS.sio.isAbout)
     print nanopub.assertion_resource.identifier, about
     self._prep_graph(nanopub.assertion_resource, about.identifier)
     self._prep_graph(nanopub.pubinfo_resource, nanopub.assertion_resource.identifier)
     self._prep_graph(nanopub.provenance_resource, nanopub.assertion_resource.identifier)
     nanopub.pubinfo.add((nanopub.assertion.identifier, app.NS.dc.contributor, current_user.resUri))
     return nanopub
Example #4
0
 def process(self, i, o):
     npub = Nanopublication(store=o.graph.store)
     triples = self.app.db.query(
         '''CONSTRUCT {\n%s\n} WHERE {\n%s \nFILTER NOT EXISTS {\n%s\n\t}\nFILTER (regex(str(%s), "^(%s)")) .\n}''' % (
         self.construct, self.where, self.construct, self.resource, i.identifier), initNs=self.prefixes)
     for s, p, o, c in triples:
         print("Deductor Adding ", s, p, o)
         npub.assertion.add((s, p, o))
     npub.provenance.add((npub.assertion.identifier, prov.value,
                          rdflib.Literal(flask.render_template_string(self.explanation, **self.get_context(i)))))
Example #5
0
    def process_nanopub(self, i, o, new_np):
        assertion = i.graph
        nanopub = Nanopublication(i.graph.store, i.identifier)
        quads = nanopub.serialize(format="nquads")
        i.identifier.split('/')[-1]
        fileid = self.app.nanopub_depot.create(
            quads,
            i.identifier.split('/')[-1] + '.nq', "application/n-quads")
        o.add(rdflib.RDF.type, whyis.ArchivedNanopublication)
        new_np.pubinfo.add(
            (new_np.identifier, rdflib.RDF.type, whyis.FRIRNanopublication))
        expressions = dict([
            (part.identifier, self.expression_digest(part)) for part in
            [nanopub.assertion, nanopub.provenance, nanopub.pubinfo]
        ])
        expressions[nanopub.identifier] = sum(expressions.values())
        nanopub_expression_uri = pexp[hex(expressions[nanopub.identifier])[2:]]
        for work, expression in expressions.items():
            exp = pexp[hex(expression)[2:]]
            o.graph.add((work, frbr.realization, exp))
            o.graph.add((work, rdflib.RDF.type, frbr.Work))
            o.graph.add((exp, rdflib.RDF.type, frbr.Expression))

        with self.app.nanopub_depot.get(fileid) as stored_file:
            manifestation_id = self.manifestation_digest(stored_file)
        manifestation = pmanif[hex(manifestation_id)[2:]]
        o.graph.add((nanopub_expression_uri, frbr.embodiment, manifestation))

        o.graph.add((manifestation, rdflib.RDF.type, pv.File))
        o.graph.add((manifestation, whyis.hasFileID, rdflib.Literal(fileid)))
        o.graph.add(
            (manifestation, dc.created, rdflib.Literal(datetime.utcnow())))
        o.graph.add((manifestation, NS.ov.hasContentType,
                     rdflib.Literal("application/n-quads")))
        o.graph.add((manifestation, rdflib.RDF.type,
                     NS.mediaTypes["application/n-quads"]))
        o.graph.add((NS.mediaTypes["application/n-quads"], rdflib.RDF.type,
                     dc.FileFormat))
Example #6
0
 def process(self, i, o):
     self.app.db.store.nsBindings = {}
     npub = Nanopublication(store=o.graph.store)
     triples = self.app.db.query(
         '''%s CONSTRUCT {\n%s\n} WHERE {\n%s \nFILTER NOT EXISTS {\n%s\n\t}\nFILTER(str(%s)="%s") .\n}'''
         % (self.prefixes, self.construct, self.where, self.construct,
            self.resource, i.identifier)
     )  # init.bindings = prefix, dict for identifier (see graph.query in rdflib)
     for s, p, o, c in triples:
         print "Deductor Adding ", s, p, o
         npub.assertion.add((s, p, o))
     npub.provenance.add(
         (npub.assertion.identifier, prov.value,
          rdflib.Literal(
              flask.render_template_string(self.explanation,
                                           **self.get_context(i)))))
Example #7
0
    def process(self, i, o):
        values = self.app.db.query('''
SELECT ?prefix ?sdd_file ?data_file ?content_type ?delimiter
WHERE {
    ?data_file dcterms:conformsTo ?sdd_file ;
       ov:hasContentType ?content_type ;
       csvw:delimiter ?delimiter .

   ?dataset dcat:distribution ?data_file ;
       void:uriSpace ?prefix .
    MINUS {
        ?setl_script prov:wasDerivedFrom ?sdd_file;
            a setl:SemanticETLScript.
        ?dataset prov:wasGeneratedBy ?setl_script;
            prov:wasGeneratedBy ?template.
        ?template a setl:Transform;
            prov:used/prov:wasGeneratedBy ?extract.
        ?extract a setl:Extract;
            prov:used ?data_file.
    }
}
''',
                                   initBindings={"sdd_file": i.identifier},
                                   initNs=NS.prefixes)
        for prefix, sdd_file, data_file, content_type, delimiter in values:
            resource = self.app.get_resource(sdd_file)
            fileid = resource.value(self.app.NS.whyis.hasFileID)
            if fileid is not None:
                sdd_data = self.app.file_depot.get(fileid.value)
            else:
                sdd_data = sdd_file

            output = sdd2setl(sdd_data, prefix.value, data_file,
                              content_type.value, delimiter.value)
            npub = Nanopublication()
            npub.assertion.parse(data=output, format="turtle")

            sdd_setl = npub.assertion.value(predicate=rdflib.RDF.type,
                                            object=setl.SemanticETLScript)
            npub.assertion.add((sdd_setl, prov.wasDerivedFrom, sdd_file))
            new_nps = self.app.nanopub_manager.prepare(npub)
            self.app.nanopub_manager.publish(*new_nps)
Example #8
0
 def run(self, input_file, file_format="trig", was_revision_of=None):
     if was_revision_of is not None:
         wasRevisionOf = set(flask.current_app.db.objects(predicate=np.hasAssertion,
                                                            subject=rdflib.URIRef(was_revision_of)))
         if len(wasRevisionOf) == 0:
             print "Could not find active nanopublication to revise:", was_revision_of
             return
         was_revision_of = wasRevisionOf
     g = rdflib.ConjunctiveGraph(identifier=rdflib.BNode().skolemize())
     g1 = g.parse(location=input_file, format=file_format)
     if len(list(g1.subjects(rdflib.RDF.type, np.Nanopublication))) == 0:
         new_np = Nanopublication(store=g1.store)
         new_np.add((new_np.identifier, rdflib.RDF.type, np.Nanopublication))
         new_np.add((new_np.identifier, np.hasAssertion, g1.identifier))
         new_np.add((g1.identifier, rdflib.RDF.type, np.Assertion))
     for npub in flask.current_app.nanopub_manager.prepare(g):
         if was_revision_of is not None:
             for r in was_revision_of:
                 print "Marking as revision of", r
                 npub.pubinfo.add((npub.assertion.identifier, flask.current_app.NS.prov.wasRevisionOf, r))
         flask.current_app.nanopub_manager.publish(npub)
         print "Published", npub.identifier
Example #9
0
 def process_graph(self, inputGraph):
     instances = self.getInstances(inputGraph)
     results = []
     for i in instances:
         print "Processing", i.identifier, self
         output_nanopub = Nanopublication()
         o = output_nanopub.assertion.resource(
             i.identifier)  # OutputClass(i.identifier)
         result = self.process_nanopub(i, o, output_nanopub)
         print "Output Graph", output_nanopub.identifier, len(
             output_nanopub)
         for new_np in self.app.nanopub_manager.prepare(
                 rdflib.ConjunctiveGraph(store=output_nanopub.store)):
             if len(new_np.assertion) == 0:
                 continue
             self.explain(new_np, i, o)
             new_np.add((new_np.identifier, sio.isAbout, i.identifier))
             #print new_np.serialize(format="trig")
             if not self.dry_run:
                 self.app.nanopub_manager.publish(new_np)
             results.append(new_np)
     return results