コード例 #1
0
ファイル: nanopub.py プロジェクト: charishruthi/whyis
 def _reserve_id(self):
     # This needs to be a two-step write, since we need to store
     # the identifier in the nanopub for consistency, and we don't
     # get the identifier until we write the file!
     fileid = self.depot.create(
         FileIntent('', ld.create_id(), 'application/trig'))
     return fileid
コード例 #2
0
    def new(self):
        nanopub = Nanopublication(identifier=self.prefix[ld.create_id()])
        nanopub.nanopub_resource
        nanopub.assertion
        nanopub.provenance
        nanopub.pubinfo

        return nanopub
コード例 #3
0
    def prepare(self, graph):
        mappings = collections.defaultdict(lambda: self.prefix[ld.create_id()])
        for nanopub in graph.subjects(rdflib.RDF.type, np.Nanopublication):
            if self.prefix not in nanopub:
                new_uri = mappings[nanopub]
                for assertion in graph.objects(nanopub, np.hasAssertion):
                    mappings[assertion] = rdflib.URIRef(new_uri + "_assertion")
                for pubinfo in graph.objects(nanopub, np.hasPublicationInfo):
                    mappings[pubinfo] = rdflib.URIRef(new_uri + "_pubinfo")
                for provenance in graph.objects(nanopub, np.hasProvenance):
                    mappings[provenance] = rdflib.URIRef(new_uri +
                                                         "_provenance")
        #print mappings

        output_graph = rdflib.ConjunctiveGraph()

        for s, p, o, g in graph.quads():
            quad = (mappings.get(s, s), mappings.get(p, p), mappings.get(o, o),
                    mappings.get(g.identifier, g.identifier))
            #print s, p, o, g
            #print quad
            output_graph.add(quad)

        for nanopub_uri in output_graph.subjects(rdflib.RDF.type,
                                                 np.Nanopublication):
            nanopub = Nanopublication(identifier=nanopub_uri)
            nanopub += output_graph.get_context(nanopub.identifier)
            print "Nanopub", len(nanopub), len(
                output_graph.get_context(identifier=nanopub_uri))
            for assertion in output_graph.objects(nanopub.identifier,
                                                  np.hasAssertion):
                a = nanopub.assertion
                a += output_graph.get_context(identifier=assertion)
                print "Assertion", len(a), len(
                    output_graph.get_context(identifier=assertion))
            for pubinfo in output_graph.objects(nanopub.identifier,
                                                np.hasPublicationInfo):
                p = nanopub.pubinfo
                p += output_graph.get_context(identifier=pubinfo)
                print "PubInfo", len(p), len(
                    output_graph.get_context(identifier=pubinfo))
            for provenance in output_graph.objects(nanopub.identifier,
                                                   np.hasProvenance):
                p = nanopub.provenance
                p += output_graph.get_context(identifier=provenance)
                print "Provenance", len(p), len(
                    output_graph.get_context(identifier=provenance))
            print "Total", len(output_graph)
            print "Contexts", [g.identifier for g in output_graph.contexts()]
            yield nanopub
コード例 #4
0
    def prepare(self, graph, mappings=None):
        if mappings is None:
            mappings = {}
        new_nps = list(graph.subjects(rdflib.RDF.type, np.Nanopublication))
        if len(new_nps) == 0:
            new_np = Nanopublication(store=graph.store)
            new_np.add(
                (new_np.identifier, rdflib.RDF.type, np.Nanopublication))
            new_np.add((new_np.identifier, np.hasAssertion, graph.identifier))
            new_np.add((graph.identifier, rdflib.RDF.type, np.Assertion))
            new_nps = [new_np.identifier]
        for nanopub in new_nps:
            if self.prefix not in nanopub:
                new_uri = self.prefix[ld.create_id()]
                mappings[nanopub] = new_uri
                for assertion in graph.objects(nanopub, np.hasAssertion):
                    mappings[assertion] = rdflib.URIRef(new_uri + "_assertion")
                for pubinfo in graph.objects(nanopub, np.hasPublicationInfo):
                    mappings[pubinfo] = rdflib.URIRef(new_uri + "_pubinfo")
                for provenance in graph.objects(nanopub, np.hasProvenance):
                    mappings[provenance] = rdflib.URIRef(new_uri +
                                                         "_provenance")

        #print mappings

        output_graph = rdflib.ConjunctiveGraph()

        bnode_cache = {}
        for s, p, o, g in graph.quads():

            def skolemize(x):
                if isinstance(x, rdflib.BNode):
                    if x not in bnode_cache:
                        bnode_cache[x] = rdflib.URIRef('bnode:' + uuid4().hex)
                    return bnode_cache[x]
                return x

            quad = (skolemize(mappings.get(s, s)), mappings.get(p, p),
                    skolemize(mappings.get(o, o)),
                    mappings.get(g.identifier, g.identifier))
            #print s, p, o, g
            #print quad
            output_graph.add(quad)

        for nanopub_uri in output_graph.subjects(rdflib.RDF.type,
                                                 np.Nanopublication):
            nanopub = Nanopublication(identifier=nanopub_uri)
            nanopub += output_graph.get_context(nanopub.identifier)
            #print "Nanopub", len(nanopub), len(output_graph.get_context(identifier=nanopub_uri))
            for assertion in output_graph.objects(nanopub.identifier,
                                                  np.hasAssertion):
                a = nanopub.assertion
                a += output_graph.get_context(identifier=assertion)
                #print "Assertion", len(a), len(output_graph.get_context(identifier=assertion))
            for pubinfo in output_graph.objects(nanopub.identifier,
                                                np.hasPublicationInfo):
                p = nanopub.pubinfo
                p += output_graph.get_context(identifier=pubinfo)
                #print "PubInfo", len(p), len(output_graph.get_context(identifier=pubinfo))
            for provenance in output_graph.objects(nanopub.identifier,
                                                   np.hasProvenance):
                p = nanopub.provenance
                p += output_graph.get_context(identifier=provenance)
                #print "Provenance", len(p), len(output_graph.get_context(identifier=provenance))
            if nanopub.pubinfo.value(nanopub.identifier,
                                     frbr.realizationOf) is None:
                work = self.prefix[ld.create_id()]
                nanopub.pubinfo.add(
                    (nanopub.identifier, frbr.realizationOf, work))
                nanopub.pubinfo.add((work, rdflib.RDF.type, frbr.Work))
                nanopub.pubinfo.add(
                    (nanopub.identifier, rdflib.RDF.type, frbr.Expression))
            #print "Total", len(output_graph)
            #print "Contexts", [g.identifier for g in output_graph.contexts()]
            yield nanopub
コード例 #5
0
    def process_nanopub(self, i, o, new_np):
        print i.identifier
        p = self.app.NS.prov.used
        for script, np, parameterized_type, type_assertion in self.app.db.query(
                '''
prefix setl: <http://purl.org/twc/vocab/setl/>
select distinct ?setl_script ?np ?parameterized_type ?type_assertion where {
    graph ?assertion {
      ?setl_script rdfs:subClassOf setl:SemanticETLScript;
        rdfs:subClassOf [ a owl:Restriction;
            owl:onProperty prov:used;
            owl:someValuesFrom ?parameterized_type
      ].
    }
    graph ?type_assertion { ?resource rdf:type/rdfs:subClassOf* ?parameterized_type. }
    ?type_np a np:Nanopublication; np:hasAssertion ?type_assertion.
    ?np a np:Nanopublication; np:hasAssertion ?assertion.
    filter not exists {
        ?type_assertion prov:wasGeneratedBy [ a setl:Planner].
    }
    filter not exists {
        ?assertion prov:wasGeneratedBy [ a setl:Planner].
    }
    filter not exists {
        ?planned_assertion prov:wasDerivedFrom* ?assertion;
           prov:wasGeneratedBy [ a setl:Planner].
        graph ?planned_assertion {
            ?setl_run a ?setl_script.
            ?extract prov:used ?resource.
        }
    }
}''',
                initBindings=dict(resource=i.identifier),
                initNs=self.app.NS.prefixes):
            nanopub = self.app.nanopub_manager.get(np)
            print "Template NP", nanopub.identifier, len(nanopub)
            template_prefix = nanopub.assertion.value(script,
                                                      setl.hasTemplatePrefix)
            replacement_prefix = self.app.NS.local['setl/' + ld.create_id() +
                                                   "/"]

            mappings = {}
            for x, in nanopub.assertion.query(
                    "select ?x where {?x a ?t}",
                    initBindings={'t': parameterized_type},
                    initNs=self.app.NS.prefixes):
                mappings[x] = i.identifier

            script_run = rdflib.URIRef(
                script.replace(template_prefix, replacement_prefix, 1))
            for x, in nanopub.assertion.query("select ?x where {?x a ?t}",
                                              initBindings={'t': script},
                                              initNs=self.app.NS.prefixes):
                mappings[x] = script_run

            def replace(x):
                if x in mappings:
                    return mappings[x]
                if isinstance(x, rdflib.URIRef) and x.startswith("bnode:"):
                    return rdflib.BNode(x.replace("bnode:", "", 1))
                if isinstance(x, rdflib.URIRef) and x == script:
                    return script
                if isinstance(x,
                              rdflib.URIRef) and x.startswith(template_prefix):
                    return rdflib.URIRef(
                        x.replace(template_prefix, replacement_prefix, 1))
                return x

            for s, p, o in nanopub.assertion:
                new_np.assertion.add((replace(s), replace(p), replace(o)))
            new_np.assertion.add((script_run, rdflib.RDF.type, script))
            new_np.assertion.add(
                (script_run, rdflib.RDF.type, setl.SemanticETLScript))
            new_np.provenance.add(
                (new_np.assertion.identifier, prov.wasDerivedFrom,
                 nanopub.assertion.identifier))
            new_np.provenance.add((new_np.assertion.identifier,
                                   prov.wasDerivedFrom, type_assertion))
            print "Instance NP", new_np.identifier, len(new_np)