コード例 #1
0
ファイル: testGraphDB.py プロジェクト: rdmpage/hello-scigraph
def main():

    store = SPARQLStore(SPARQL_ENDPOINT)
    g = ConjunctiveGraph(store=store)
    # g.bind("sg", "http://www.springernature.com/scigraph/ontologies/core/")

    # get a few articles
    q1 = g.query(ALL_ARTICLES_IDS_SAMPLE)
    for row in q1:
        print("Article URI:", str(row[0]))

    # extract more article info
    for row in q1:
        try:
            with time_limit(MAX_TIMEOUT):
                raw = g.query(ARTICLE_INFO_QUERY % str(row[0]))
                g1 = ConjunctiveGraph()
                g1.parse(data=raw.serialize())

                # create JSON-LD
                context = {
                    "@vocab": "http://elastic-index.scigraph.com/",
                    "@language": "en"
                }
                print(g1.serialize(format='json-ld', context=context,
                                   indent=4))
                print("======")
        except TimeoutException, msg:
            error = "Timed out!"
            print(error)
        except Exception, e:
            error = "Exception: %s" % e
            print(error)
コード例 #2
0
    def test_exclude_xhtml(self):
        ns = "http://www.w3.org/1999/xhtml/vocab#"
        kg = ConjunctiveGraph()
        kg.add((
            BNode(),
            URIRef("http://www.w3.org/1999/xhtml/vocab#role"),
            URIRef("http://www.w3.org/1999/xhtml/vocab#button"),
        ))
        print(kg.serialize(format="turtle"))

        q_xhtml = ('SELECT * WHERE { ?s ?p ?o . FILTER (strstarts(str(?p), "' +
                   ns + '"))}')
        print(q_xhtml)

        res = kg.query(q_xhtml)
        self.assertEquals(len(res), 1)

        q_del = (
            'DELETE {?s ?p ?o} WHERE { ?s ?p ?o . FILTER (strstarts(str(?p), "'
            + ns + '"))}')
        kg.update(q_del)
        print(kg.serialize(format="turtle"))

        res = kg.query(q_xhtml)
        self.assertEquals(len(res), 0)
コード例 #3
0
ファイル: forms.py プロジェクト: memaldi/linked-tag-world
def validate_sparql_endpoint(form, field):
    try:
        g = ConjunctiveGraph('SPARQLStore')
        g.open(field.data)
        g.query('SELECT * WHERE { ?s ?p ?o } LIMIT 1')
    except:
        raise ValidationError('This is not a valid SPARQL endpoint.')
コード例 #4
0
    def test_OLS(self):
        print(util.cache_OLS.values)

        turtle_edam = self.turtle_edam
        kg = ConjunctiveGraph()
        kg.parse(data=turtle_edam, format="turtle")

        query_classes = self.query_classes
        query_properties = self.query_properties

        table_content = {"classes": [], "properties": []}
        qres = kg.query(query_classes)

        for row in qres:
            table_content["classes"].append({"name": row["class"], "tag": []})

        qres = kg.query(query_properties)
        for row in qres:
            table_content["properties"].append({
                "name": row["prop"],
                "tag": []
            })

        start = datetime.now().timestamp()
        class_or_property_found = False
        for c in table_content["classes"]:
            if util.ask_OLS(c["name"]):
                c["tag"].append("OLS")
                class_or_property_found = True
            print(c)
        for p in table_content["properties"]:
            if util.ask_OLS(p["name"]):
                p["tag"].append("OLS")
                class_or_property_found = True
            print(p)
        end = datetime.now().timestamp()
        delta = end - start
        print(f"OLS check done in {delta}")

        self.assertTrue(class_or_property_found, True)
        self.assertGreaterEqual(delta, 2)
        print(util.cache_OLS.values)

        # check that cache is working --> fast answers
        start = datetime.now().timestamp()
        for c in table_content["classes"]:
            if util.ask_OLS(c["name"]):
                c["tag"].append("OLS")
                class_or_property_found = True
            print(c)
        for p in table_content["properties"]:
            if util.ask_OLS(p["name"]):
                p["tag"].append("OLS")
                class_or_property_found = True
            print(p)
        end = datetime.now().timestamp()
        delta = end - start
        print(f"OLS check done in {delta}")
        self.assertLessEqual(delta, 0.1)
        print(util.cache_OLS.values)
コード例 #5
0
    def test_LOV(self):
        turtle_edam = self.turtle_edam
        kg = ConjunctiveGraph()
        kg.parse(data=turtle_edam, format="turtle")

        query_classes = self.query_classes
        query_properties = self.query_properties

        table_content = {"classes": [], "properties": []}
        qres = kg.query(query_classes)

        for row in qres:
            table_content["classes"].append({"name": row["class"], "tag": []})
            print(f'{row["class"]}')

        qres = kg.query(query_properties)
        print("Prop")
        for row in qres:
            table_content["properties"].append({
                "name": row["prop"],
                "tag": []
            })
            print(f'{row["prop"]}')

        class_or_property_found = False
        for c in table_content["classes"]:
            if util.ask_LOV(c["name"]):
                c["tag"].append("LOV")
                class_or_property_found = True
        for p in table_content["properties"]:
            if util.ask_LOV(p["name"]):
                p["tag"].append("LOV")
                class_or_property_found = True

        self.assertTrue(class_or_property_found, True)
コード例 #6
0
    def test_BioPortal(self):
        turtle_edam = self.turtle_edam
        kg = ConjunctiveGraph()
        kg.parse(data=turtle_edam, format="turtle")

        query_classes = self.query_classes
        query_properties = self.query_properties

        table_content = {"classes": [], "properties": []}
        qres = kg.query(query_classes)
        print("Class")
        for row in qres:
            table_content["classes"].append({"name": row["class"], "tag": []})
            print(f'{row["class"]}')

        qres = kg.query(query_properties)
        print("Prop")
        for row in qres:
            table_content["properties"].append({
                "name": row["prop"],
                "tag": []
            })
            print(f'{row["prop"]}')

        for c in table_content["classes"]:
            if util.ask_BioPortal(c["name"], "class"):
                c["tag"].append("BioPortal")

        for p in table_content["properties"]:
            if util.ask_BioPortal(c["name"], "property"):
                c["tag"].append("BioPortal")
コード例 #7
0
ファイル: forms.py プロジェクト: jonlazaro/linked-tag-world
def validate_sparql_endpoint(form, field):
    try:
        g = ConjunctiveGraph('SPARQLStore')
        g.open(field.data)
        g.query('SELECT * WHERE { ?s ?p ?o } LIMIT 1')
    except:
        raise ValidationError('This is not a valid SPARQL endpoint.')
コード例 #8
0
ファイル: response.py プロジェクト: chouisgiser/pycalais
    def __init__(self, raw_result):
        self._detect_fails(raw_result)
        rdf = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
        c = Namespace('http://s.opencalais.com/1/pred/')
        g = Graph()
        self.graph = g

        g.parse(StringIO(raw_result.decode('utf-8').encode('utf-8')))

        self.categories = [row for row in g.query(CATEGORY_QUERY['SPARQL'])]
        self.entities = [row for row in g.query(ENTITY_QUERY['SPARQL'])]
コード例 #9
0
    def __init__(self, raw_result):

        rdf = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
        c = Namespace("http://s.opencalais.com/1/pred/")
        g = Graph()
        self.graph = g

        g.parse(StringIO(raw_result.decode('utf-8').encode('utf-8')))

        self.categories = [row for row in g.query(CATEGORY_QUERY["SPARQL"])]
        self.entities = [row for row in g.query(ENTITY_QUERY["SPARQL"])]
コード例 #10
0
def test_import_jsonld_into_named_graph(flatten_before_import: bool):
    """Test named graphs we use."""
    graph = ConjunctiveGraph()

    jsonld_document = JSONLD_DOCUMENT

    if flatten_before_import:
        jsonld_document = jsonld.flatten(jsonld_document)

    serialized_document = json.dumps(jsonld_document, indent=4)

    graph.parse(
        data=serialized_document,
        format='json-ld',
        publicID=PUBLIC_ID,
    )

    print(graph.serialize(format='n3').decode('utf-8'))

    # Make sure only one NAMED GRAPH is created on import.
    assert list(map(
        operator.itemgetter(Variable('g')),
        graph.query(
            'SELECT DISTINCT ?g WHERE { GRAPH ?g { ?s ?p ?o . } } ORDER BY ?g',
        ).bindings,
    )) == [
        URIRef('https://myblog.net/rdf/'),
    ]

    # The information in @included section was properly parsed.
    assert graph.query('''
        SELECT * WHERE {
            GRAPH ?g {
                ex:Rover rdfs:subClassOf ex:Robot .
            }
        }
    ''', initNs=NAMESPACES).bindings == [{
        Variable('g'): PUBLIC_ID,
    }]

    # The information in the root was properly parsed.
    assert graph.query('''
        SELECT * WHERE {
            GRAPH ?g {
                blog:JSONLD-and-named-graphs a schema:blogPost .
            }
        }
    ''', initNs=NAMESPACES).bindings == [{
        Variable('g'): PUBLIC_ID,
    }]
コード例 #11
0
def test_import_jsonld_into_named_graph():
    """Test named graphs we use."""
    graph = ConjunctiveGraph()

    serialized_document = json.dumps(JSONLD_DOCUMENT, indent=4)

    graph.parse(
        data=serialized_document,
        format='json-ld',
        # All the semantic data about my blog is stored in a particular
        # named graph.
        publicID=PUBLIC_ID,
    )

    assert list(map(
        operator.itemgetter(Variable('g')),
        graph.query(
            'SELECT DISTINCT ?g WHERE { GRAPH ?g { ?s ?p ?o . } } ORDER BY ?g',
        ).bindings,
    )) == [
        URIRef('https://blog.me/JSONLD-and-named-graphs'),
        URIRef('https://myblog.net/rdf/'),
    ]

    # I wanted **one** named graph but got two of them. That's because, when
    # the system was importing contents of @graph block, it was using @id
    # as name for this graph.
    assert graph.query('''
        SELECT * WHERE {
            GRAPH ?g {
                ex:Rover rdfs:subClassOf ex:Robot .
            }
        }
    ''', initNs=NAMESPACES).bindings == [{
        Variable('g'): URIRef('https://blog.me/JSONLD-and-named-graphs'),
    }]

    # `publicID` was used for the part of data which was on the top level
    # of the document.
    assert graph.query('''
        SELECT * WHERE {
            GRAPH ?g {
                blog:JSONLD-and-named-graphs a schema:blogPost .
            }
        }
    ''', initNs=NAMESPACES).bindings == [{
        Variable('g'): PUBLIC_ID,
    }]
コード例 #12
0
    def query_handler(source_id, query, context):
        # Attempt to cnonicalize this object by round-tripping decoding and re-encoding
        query = json.loads(json.dumps(query, sort_keys=True))

        rdf_store = context["rdf_store"]
        graph = ConjunctiveGraph(rdf_store)
        graph.open(DB_URI)
        results = graph.query(
            "select ?subject ?predicate ?object where {?subject ?predicate ?object}"
        )
        # Inelegant, but works: serialize to JSON string, then re-parse
        statements = json.loads(results.serialize(format="json"))
        graph.close()

        context = {
            "@context": {
                "@vocab": "http://facta.kendra.io/vocab#",
                "kv": "http://facta.kendra.io/vocab#",
                "kendra": "http://kendra.io/types#",
                "kuid": "http://kendra.io/uuid#",
                "schema": "http://schema.org/",
                "xsd": "http://www.w3.org/2001/XMLSchema#"
            }
        }

        compacted_graph, contained = s2j.result_data_to_jsonld(
            statements, context)
        result = s2j.extract_salient_results(
            compacted_graph, contained,
            ["kendra:InclusionRelationship", "kendra:TextSelection"])

        result = s2j.quicksearch(result, query)

        return {"result": result}
コード例 #13
0
ファイル: piNode.py プロジェクト: drewp/homeauto
class Config(object):
    def __init__(self, masterGraph, hubHost):
        self.etcd = etcd3.client(host=hubHost, port=9022)

        self.masterGraph = masterGraph
        self.hubHost = hubHost
        self.configGraph = ConjunctiveGraph()
        self.boards = []
        self.etcPrefix = 'pi/'

        self.reread()

        deferToThread(self.watchEtcd)

    def watchEtcd(self):
        events, cancel = self.etcd.watch_prefix(self.etcPrefix)
        reactor.addSystemEventTrigger('before', 'shutdown', cancel)
        for ev in events:
            log.info('%s changed', ev.key)
            reactor.callFromThread(self.configChanged)

    def configChanged(self):
        self.cancelRead()
        self.rereadLater = reactor.callLater(.1, self.reread)

    def cancelRead(self):
        if getattr(self, 'rereadLater', None):
            self.rereadLater.cancel()
        self.rereadLater = None

    @STATS.configReread.time()
    def reread(self):
        self.rereadLater = None
        log.info('read config')
        self.configGraph = ConjunctiveGraph()
        for v, md in self.etcd.get_prefix(self.etcPrefix):
            log.info('  read file %r', md.key)
            self.configGraph.parse(StringInputSource(v), format='n3')
        self.configGraph.bind('', ROOM)
        self.configGraph.bind('rdf', RDF)
        # config graph is too noisy; maybe make it a separate resource
        #masterGraph.patch(Patch(addGraph=self.configGraph))
        self.setupBoards()
        
    def setupBoards(self):       
        thisHost = Literal(hostname)
        for row in self.configGraph.query(
                'SELECT ?board WHERE { ?board a :PiBoard; :hostname ?h }',
                initBindings=dict(h=thisHost)):
            thisBoard = row.board
            break
        else:
            log.warn("config had no board for :hostname %s. Waiting for config update." %
                     thisHost)
            self.boards = []
            return

        log.info("found config for board %r" % thisBoard)
        self.boards = [Board(self.configGraph, self.masterGraph, thisBoard, self.hubHost)]
        self.boards[0].startPolling()
コード例 #14
0
 def test_dSet_parsed_as_context_returns_results(self):
     querystr = """SELECT DISTINCT ?s FROM <http://test/> { ?s ?p ?o }"""
     graph = ConjunctiveGraph()
     graph.get_context(URIRef('http://test/')
                         ).parse("http://www.w3.org/People/Berners-Lee/card.rdf")
     r = graph.query(querystr, loadContexts=True)
     self.assert_(len(r.bindings) is not 0)
コード例 #15
0
ファイル: datacompiler.py プロジェクト: libris/lxltools
def _construct(compiler, sources, query=None):
    dataset = ConjunctiveGraph()
    if not isinstance(sources, list):
        sources = [sources]
    for sourcedfn in sources:
        source = sourcedfn['source']
        graph = dataset.get_context(URIRef(sourcedfn.get('dataset') or source))
        if isinstance(source, (dict, list)):
            context_data = sourcedfn['context']
            if not isinstance(context_data, list):
                context_data = compiler.load_json(context_data )['@context']
            context_data = [compiler.load_json(ctx)['@context']
                            if isinstance(ctx, unicode) else ctx
                            for ctx in context_data]
            to_rdf(source, graph, context_data=context_data)
        elif isinstance(source, Graph):
            graph += source
        else:
            graph += compiler.cached_rdf(source)
    if not query:
        return graph
    with compiler.path(query).open() as fp:
        result = dataset.query(fp.read())
    g = Graph()
    for spo in result:
        g.add(spo)
    return g
コード例 #16
0
def get_entities_from_text(text):
    """
    This function extract RDF graph for textual entities, their YAGO type, beginindex and endindex, processed by PIKES tool.
    input:
      text –  Image caption of Flickr30k entities dataset
    
    output:
     sparql query result  – stored textual entities recognized and linked by PIKES

    """

    pikes_answer = pikes_text2rdf(text.lower())
    
    g = ConjunctiveGraph()
    g.parse(data = pikes_answer.content.decode('utf-8'),format="trig")
    #Sparql query for entities information extraction
    sparql_query = """SELECT ?TED ?TEM ?TET ?anchor ?beginindex ?endindex
           WHERE {
           GRAPH ?g1 {?TED <http://groundedannotationframework.org/gaf#denotedBy> ?TEM}
           GRAPH ?g2 {?TED a ?TET}
           GRAPH ?g3 {?TEM nif:anchorOf ?anchor}
           GRAPH ?g4 {?TEM nif:beginIndex ?beginindex}
           GRAPH ?g5 {?TEM nif:endIndex ?endindex}
           }"""

    return g.query(sparql_query)
コード例 #17
0
def calculate_edges((offset_limit, config)):
    g = ConjunctiveGraph(config['graph_store'], config['graph_identifier'])
    g.open(config['db_configstring'], create=False)
    engine = create_engine(config['alchemy_configstring'])

    Session = sessionmaker(bind=engine)
    session = Session()

    results = session.query(Node).filter_by(node_type="subject").order_by(
        Node.node_id.desc()).offset(offset_limit[0]).limit(
            offset_limit[1]).all()

    for result in results:
        query = "SELECT ?p ?o WHERE { <%s> ?p ?o }" % result.node_uri
        items = g.query(query)
        for item in items:
            if str(item[0]
                   ) != "http://www.w3.org/1999/02/22-rdf-syntax-ns#type":
                neighbors = session.query(Node).filter_by(
                    node_uri=item[1]).all()
                if len(neighbors) > 0:
                    for neighbor in neighbors:
                        result.add_neighbors(str(item[0]), neighbor)
        session.commit()
    g.close()
    session.close()
コード例 #18
0
    def test_flowcells_index_rdfa(self):
        model = ConjunctiveGraph()

        response = self.client.get(reverse('flowcell_index'))
        self.assertEqual(response.status_code, 200)
        model.parse(data=smart_text(response.content), format='rdfa')

        add_default_schemas(model)
        inference = Infer(model)
        errmsgs = list(inference.run_validation())
        self.assertEqual(len(errmsgs), 0, errmsgs)

        body =  """prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#>

        select ?flowcell
        where {
           ?flowcell a libns:IlluminaFlowcell .
        }"""
        bindings = set(['flowcell'])
        count = 0
        for r in model.query(body):
            count += 1

        self.assertEqual(count, len(FlowCell.objects.all()))
コード例 #19
0
ファイル: plugin.py プロジェクト: yo-bj/supybot-plugins
class FOAF(callbacks.Privmsg):

    DATAFILE = '/var/www/rc98.net/zoia.rdf'

    def __init__(self, irc):
        self.g = Graph()
        #      self.g.parse('http://rc98.net/zoia.rdf')
        self.g.parse(self.DATAFILE, format="xml")
        self.uri = rdflib.URIRef('http://www.code4lib.org/id/zoia')
        self.FOAF = Namespace('http://xmlns.com/foaf/0.1/')
        super(callbacks.Plugin, self).__init__(irc)

    def _uri_of_user(self, nick):
        result = self.g.query("""
          PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
          SELECT ?uri WHERE 
          {<http://www.code4lib.org/id/zoia> foaf:knows ?uri . ?uri foaf:nick ?nick .}
          """,
                              initBindings={'nick': nick})
        if len(result) > 0:
            userURI = list(result)[0][0]
            return (userURI)
        else:
            return (None)

    def _user_graph(self, uri):
        userGraph = Graph()
        try:
            userGraph.parse(uri)
        except Exception, e:
            u = "http://www.w3.org/2007/08/pyRdfa/extract?space-preserve=true&uri=" + uri
            userGraph.parse(u, identifier=uri)
        return userGraph
コード例 #20
0
ファイル: plugin.py プロジェクト: sylvar/supybot-plugins
class FOAF(callbacks.Privmsg):

    DATAFILE = "/var/www/rc98.net/zoia.rdf"

    def __init__(self, irc):
        self.g = Graph()
        #      self.g.parse('http://rc98.net/zoia.rdf')
        self.g.parse(self.DATAFILE, format="xml")
        self.uri = rdflib.URIRef("http://www.code4lib.org/id/zoia")
        self.FOAF = Namespace("http://xmlns.com/foaf/0.1/")
        super(callbacks.Plugin, self).__init__(irc)

    def _uri_of_user(self, nick):
        result = self.g.query(
            """
          PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
          SELECT ?uri WHERE 
          {<http://www.code4lib.org/id/zoia> foaf:knows ?uri . ?uri foaf:nick ?nick .}
          """,
            initBindings={"nick": nick},
        )
        if len(result) > 0:
            userURI = list(result)[0][0]
            return userURI
        else:
            return None

    def _user_graph(self, uri):
        userGraph = Graph()
        try:
            userGraph.parse(uri)
        except Exception, e:
            u = "http://www.w3.org/2007/08/pyRdfa/extract?space-preserve=true&uri=" + uri
            userGraph.parse(u, identifier=uri)
        return userGraph
コード例 #21
0
def get_all_measurement_types(ontology_file):
    graph = ConjunctiveGraph()
    graph.load(ontology_file, format="n3")
    query_str = '''SELECT DISTINCT ?mt ?label ?comment ?defn
        WHERE {
          ?mt rdfs:label ?label .
          ?mt rdfs:subClassOf <%s> .
          ?mt rdfs:subClassOf ?r1 .
          ?r1 owl:onProperty oboe:measuresEntity ; owl:someValuesFrom ?ent .
          ?mt rdfs:subClassOf ?r2 .
          ?r2 owl:onProperty oboe:measuresCharacteristic ; owl:someValuesFrom ?char .
          OPTIONAL { ?mt rdfs:comment ?comment }
          OPTIONAL { ?mt skos:definition ?defn }
        }''' % (MeasurementType)
    qres = list(graph.query(query_str, initNs=dict(oboe=URIRef("http://ecoinformatics.org/oboe/oboe.1.2/oboe-core.owl#"),
                                                   owl=OWL,rdfs=RDFS,skos=SKOS)))
    if len(qres) > 0:
        qres.sort(key=lambda x: x[0], reverse=True)
        result = dict()
        i = 0
        for row in qres:
            result[i] = {'uri' : row[0], 'label' : row[1], 'comment' : row[2], 'defn' : row[3]}
            i = i + 1
        print "Sparql query finished!"
        return result
    return None
コード例 #22
0
ファイル: query.py プロジェクト: draketuija/definitions
def run_query(source, query_file, context=None):
    graph = ConjunctiveGraph()
    fmt = ('json-ld' if source.endswith('.jsonld') else guess_format(source))
    graph.parse(source, format=fmt, context=context)
    with open(query_file) as fp:
        query_text = fp.read().decode('utf-8')
    result = graph.query(query_text)
    for row in result:
        print('\t'.join(t.n3() if t else 'UNDEF' for t in row).encode('utf-8'))
コード例 #23
0
class TestSparqlJsonResults(unittest.TestCase):
    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringIO(test_data), format="n3")

    def test_base_ref(self):
        rt = self.graph.query(test_query).serialize("python")
        self.failUnless(rt[0] == Literal("Alice"),
                        "Expected:\n 'Alice' \nGot:\n %s" % rt)
コード例 #24
0
class TestSparqlJsonResults(unittest.TestCase):

    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringIO(test_data), format="n3")

    def test_base_ref(self):
        rt=self.graph.query(test_query).serialize("python")
        self.failUnless(rt[0] == Literal("Alice"),"Expected:\n 'Alice' \nGot:\n %s" % rt)
コード例 #25
0
    def testOrderBy(self):
        graph = ConjunctiveGraph(plugin.get('IOMemory', Store)())
        graph.parse(StringIO(test_data), format="n3")
        results = graph.query(test_query)

        self.failUnless(False not in [
            r[0] == a
            for r, a in zip(results, ['Alice', 'Bob', 'Charlie', 'Dave'])
        ])
コード例 #26
0
ファイル: test_taskforce.py プロジェクト: rajat2502/rdfadict
    def runTest(self):
        """Run the specified task force test."""
        import rdfadict
        import rdfadict.sink.graph

        print self._uri

        # set up our target sink
        g = Graph()
        sink = rdfadict.sink.graph.GraphSink(g)

        # run rdfadict over the input
        parser = rdfadict.RdfaParser()
        parser.parseurl(self._source, sink)

        # execute the test SPARQL
        g.query(self._result)

        print g.selected
コード例 #27
0
ファイル: test_taskforce.py プロジェクト: cc-archive/rdfadict
    def runTest(self):
        """Run the specified task force test."""
        import rdfadict
        import rdfadict.sink.graph

        print self._uri

        # set up our target sink
        g = Graph()
        sink = rdfadict.sink.graph.GraphSink(g)

        # run rdfadict over the input
        parser = rdfadict.RdfaParser()
        parser.parseurl(self._source, sink)
        
        # execute the test SPARQL
        g.query(self._result)

        print g.selected
コード例 #28
0
class TestSparqlOPT_FILTER2(unittest.TestCase):
    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.load(StringIO(testContent), format='n3')
    def test_OPT_FILTER(self):
        results = self.graph.query(QUERY,
                                   DEBUG=False)
        results = list(results)
        self.failUnless(
            results == [(doc1,)],
            "expecting : %s .  Got: %s"%([(doc1,)],repr(results)))
コード例 #29
0
ファイル: macros.py プロジェクト: octadocs/octadocs
def sparql(
    instance: rdflib.ConjunctiveGraph,
    query: str,
    **kwargs: str,
) -> SPARQLResult:
    bindings = {
        argument_name: argument_value
        for argument_name, argument_value in kwargs.items()
    }

    return instance.query(query, initBindings=bindings)
コード例 #30
0
class TestSparqlOPT_FILTER2(unittest.TestCase):
    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.load(StringIO(testContent), format='n3')

    def test_OPT_FILTER(self):
        results = self.graph.query(QUERY, DEBUG=False)
        results = list(results)
        self.failUnless(
            results == [(doc1, )],
            "expecting : %s .  Got: %s" % ([(doc1, )], repr(results)))
コード例 #31
0
 def test_simple_recursion(self):
     graph = ConjunctiveGraph()
     graph.load(StringIO(BASIC_KNOWS_DATA), format='n3')
     results = graph.query(KNOWS_QUERY, processor="sparql", DEBUG=False)
     results = set(results)
     person1 = URIRef('ex:person.1')
     person2 = URIRef('ex:person.2')
     nose.tools.assert_equal(
         results,
         set([(person1, None), (person1, Literal('person 3')),
              (person2, Literal('person 3'))]))
コード例 #32
0
 def test_secondary_recursion(self):
     graph = ConjunctiveGraph()
     graph.load(StringIO(SUBCLASS_DATA), format='n3')
     results = graph.query(SUBCLASS_QUERY, processor="sparql", DEBUG=False)
     results = set(results)
     ob = URIRef('ex:ob')
     class1 = URIRef('ex:class.1')
     class2 = URIRef('ex:class.2')
     class3 = URIRef('ex:class.3')
     nose.tools.assert_equal(
         results, set([(ob, class1), (ob, class2), (ob, class3)]))
コード例 #33
0
ファイル: JSON.py プロジェクト: trishnaguha/rdfextras
class JSON(unittest.TestCase):
    def setUp(self):
        self.graph = ConjunctiveGraph(plugin.get('IOMemory', Store)())
        self.graph.parse(StringIO(test_data), format="n3")

    def testComma(self):
        """
        Verify the serialisation of the data as json contains an exact
        substring, with the comma in the correct place.
        """
        results = self.graph.query(test_query)
        result_json = results.serialize(format='json')
        self.failUnless(result_json.find(correct) > 0)

    def testHeader(self):
        """
        Verify that the "x", substring is omitted from the serialised output.
        """
        results = self.graph.query(test_header_query)
        result_json = results.serialize(format='json')
        self.failUnless(result_json.find('"x",') == -1)
コード例 #34
0
ファイル: test_sparql.py プロジェクト: tgbugs/rdflib
def test_named_filter_graph_query():
    g = ConjunctiveGraph()
    g.namespace_manager.bind('rdf', RDF)
    g.namespace_manager.bind('rdfs', RDFS)
    ex = Namespace('https://ex.com/')
    g.namespace_manager.bind('ex', ex)
    g.get_context(ex.g1).parse(format="turtle", data=f"""
    PREFIX ex: <{str(ex)}>
    PREFIX rdfs: <{str(RDFS)}>
    ex:Boris rdfs:label "Boris" .
    ex:Susan rdfs:label "Susan" .
    """)
    g.get_context(ex.g2).parse(format="turtle", data=f"""
    PREFIX ex: <{str(ex)}>
    ex:Boris a ex:Person .
    """)

    assert list(g.query("SELECT ?l WHERE { GRAPH ex:g1 { ?a rdfs:label ?l } ?a a ?type }",
                        initNs={'ex': ex})) == [(Literal('Boris'),)]
    assert list(g.query("SELECT ?l WHERE { GRAPH ex:g1 { ?a rdfs:label ?l } FILTER EXISTS { ?a a ?type }}",
                        initNs={'ex': ex})) == [(Literal('Boris'),)]
    assert list(g.query("SELECT ?l WHERE { GRAPH ex:g1 { ?a rdfs:label ?l } FILTER NOT EXISTS { ?a a ?type }}",
                        initNs={'ex': ex})) == [(Literal('Susan'),)]
    assert list(g.query("SELECT ?l WHERE { GRAPH ?g { ?a rdfs:label ?l } ?a a ?type }",
                        initNs={'ex': ex})) == [(Literal('Boris'),)]
    assert list(g.query("SELECT ?l WHERE { GRAPH ?g { ?a rdfs:label ?l } FILTER EXISTS { ?a a ?type }}",
                        initNs={'ex': ex})) == [(Literal('Boris'),)]
    assert list(g.query("SELECT ?l WHERE { GRAPH ?g { ?a rdfs:label ?l } FILTER NOT EXISTS { ?a a ?type }}",
                        initNs={'ex': ex})) == [(Literal('Susan'),)]
コード例 #35
0
 def test_simple_recursion(self):
     graph = ConjunctiveGraph()
     graph.load(StringIO(BASIC_KNOWS_DATA), format='n3')
     results = graph.query(KNOWS_QUERY,
                           processor="sparql", 
                           DEBUG=False)
     results = set(results)
     person1 = URIRef('ex:person.1')
     person2 = URIRef('ex:person.2')
     nose.tools.assert_equal(
       results,
       set([(person1, None), (person1, Literal('person 3')),
            (person2, Literal('person 3'))]))
コード例 #36
0
ファイル: JSON.py プロジェクト: RDFLib/rdfextras
class JSON(unittest.TestCase):

    def setUp(self):
        self.graph = ConjunctiveGraph(plugin.get('IOMemory',Store)())
        self.graph.parse(StringIO(test_data), format="n3")
        
    def testComma(self):
        """
        Verify the serialisation of the data as json contains an exact
        substring, with the comma in the correct place.
        """
        results = self.graph.query(test_query)
        result_json = results.serialize(format='json')
        self.failUnless(result_json.find(correct) > 0)

    def testHeader(self):
        """
        Verify that the "x", substring is omitted from the serialised output.
        """
        results = self.graph.query(test_header_query)
        result_json = results.serialize(format='json')
        self.failUnless(result_json.find('"x",') == -1)
コード例 #37
0
ファイル: test_not_equals.py プロジェクト: RDFLib/rdfextras
class TestSPARQLFilters(unittest.TestCase):
    debug = False
    sparql = True
    
    def setUp(self):
        NS = u"http://example.org/"
        self.graph = ConjunctiveGraph()
        self.graph.parse(data=testgraph, format="n3", publicID=NS)

    def testSPARQLNotEquals(self):
        rt = self.graph.query(testquery, initNs={'rdf': RDF.uri}, DEBUG=False)
        for row in rt:
            assert str(row[0]) == "http://example.org/bar" #, "unexpected item of '%s'" % repr(row[0])
コード例 #38
0
class _DBPediaMixin(object):
    def __init__(self):
        self.graph = ConjunctiveGraph('SPARQLStore')

    def _get_media_api_host(self, language):
        return language + u'.wikipedia.org'

    def _get_dbpedia_url(self, language):
        return u'http://' +('' if language == u'en' else language + u'.') + u'dbpedia.org'

    def _fetch_dbpedia_query_result(self, query):
        result = self.graph.query( unicode(query) )
        return_fetch_results = [ [ unicode(e.n3()) if (not e is None) else ''  for e in fetch_result] for fetch_result in result ]
        return return_fetch_results
コード例 #39
0
 def test_secondary_recursion(self):
     graph = ConjunctiveGraph()
     graph.load(StringIO(SUBCLASS_DATA), format='n3')
     results = graph.query(SUBCLASS_QUERY,
                           processor="sparql", 
                           DEBUG=False)
     results = set(results)
     ob = URIRef('ex:ob')
     class1 = URIRef('ex:class.1')
     class2 = URIRef('ex:class.2')
     class3 = URIRef('ex:class.3')
     nose.tools.assert_equal(
       results,
       set([(ob, class1), (ob, class2), (ob, class3)]))
コード例 #40
0
class TestSPARQLFilters(unittest.TestCase):
    debug = False
    sparql = True

    def setUp(self):
        NS = u"http://example.org/"
        self.graph = ConjunctiveGraph()
        self.graph.parse(data=testgraph, format="n3", publicID=NS)

    def testSPARQLNotEquals(self):
        rt = self.graph.query(testquery, initNs={'rdf': RDF.uri}, DEBUG=False)
        for row in rt:
            assert str(
                row[0]
            ) == "http://example.org/bar"  #, "unexpected item of '%s'" % repr(row[0])
コード例 #41
0
ファイル: macros.py プロジェクト: octadocs/octadocs
def url(
    resource: rdflib.URIRef,
    graph: rdflib.ConjunctiveGraph
) -> Optional[str]:
    """Convert a URIRef to a clickable URL."""
    bindings = graph.query(
        'SELECT ?url WHERE { ?resource octa:subjectOf/octa:url ?url . } ',
        initBindings={
            'resource': resource,
        }
    ).bindings

    if not bindings:
        return None

    return '/' + bindings[0][rdflib.Variable('url')].value
コード例 #42
0
class TestSparqlXmlResults(unittest.TestCase):
    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringIO(test_data), format="n3")

    def testSimple(self):
        self._query_result_contains(query, expected_fragments)

    def _query_result_contains(self, query, fragments):
        results = self.graph.query(query)
        result_xml = results.serialize(format='xml')
        result_xml = normalize(result_xml)  # TODO: poor mans c14n..
        print result_xml
        for frag in fragments:
            print frag
            self.failUnless(frag in result_xml)
コード例 #43
0
ファイル: macros.py プロジェクト: octadocs/octadocs
def label(
    resource: rdflib.URIRef,
    graph: rdflib.ConjunctiveGraph
) -> Optional[str]:
    """Convert a URIRef to a clickable URL."""
    bindings = graph.query(
        'SELECT ?label WHERE { ?resource rdfs:label ?label . } ',
        initBindings={
            'resource': resource,
        }
    ).bindings

    if not bindings:
        return None

    return bindings[0][rdflib.Variable('label')].value
コード例 #44
0
ファイル: resources.py プロジェクト: neontribe/PlingBack
class TripleStore(object):
    """ A root object to supply store connectivity to view code """
    def __init__(self, request):
        self.request = request

        q = self.request.registry.queryUtility
        self.logger = q(IDebugLogger)

        if self.request.registry.settings:
            if (self.request.registry.settings.get('store_type',None) == 'talis'):
                # Configure a connection to the talis store
                self.store = Talis(self.request.registry.settings['talis_store'],
                               self.request.registry.settings['talis_user'],
                               self.request.registry.settings['talis_password'])
                self.debug_sparql = self.request.registry.settings['debug_sparql']
            else:
                self.store = Graph()
                self.debug_sparql = False
        else:
            self.store = Graph()
            self.debug_sparql = False
              
    def query(self, sparql, format='json'):
        
        if self.debug_sparql:
            msg = (sparql)
            self.logger and self.logger.debug(msg)
        
        #if isinstance(self.store, Graph) and "DESCRIBE" in sparql:
        #    """ rdflib currently requires a hack for describe queries involving literals """
        #    res = [x for x in self.store if not 'http://plings.net/' in str(x[0])]
        ##    import pdb
        #    pdb.set_trace()
        #else:
        res = self.store.query(sparql)
        
        # If we're working with a local rdflib graph we'll need to delve to get the results
        if isinstance(res, SPARQLQueryResult):
            return res.result
        else:
            return res
        
    def sync(self):
        if self.request.registry.settings['store_type'] == 'talis':
            self.store.sync()
        else:
            pass
コード例 #45
0
class TestSparqlXmlResults(unittest.TestCase):

    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringIO(test_data), format="n3")

    def testSimple(self):
        self._query_result_contains(query, expected_fragments)

    def _query_result_contains(self, query, fragments):
        results = self.graph.query(query)
        result_xml = results.serialize(format='xml')
        result_xml = normalize(result_xml) # TODO: poor mans c14n..
        print result_xml
        for frag in fragments:
            print frag
            self.failUnless(frag in result_xml)
コード例 #46
0
class DateFilterTest(unittest.TestCase):
    # debug = True
    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.load(StringIO(testContent), format='n3')
    def test_DATE_FILTER1(self):
        for query in [QUERY1,QUERY2,QUERY3]:
            # print query
            #pQuery = Parse(query)
            #print RenderSPARQLAlgebra(pQuery)
            results = self.graph.query(query,
                                       processor="sparql",
                                       DEBUG=False)
            results = list(results)
            self.failUnless(
                len(results) and results == [(ANSWER1,)],
                "expecting : %s .  Got: %s"%([(ANSWER1,)],repr(results)))
コード例 #47
0
ファイル: test_samples.py プロジェクト: detrout/htsworkflow
    def test_library_rdf(self):
        library = LibraryFactory.create()

        model = ConjunctiveGraph()

        response = self.client.get(library.get_absolute_url())
        self.assertEqual(response.status_code, 200)
        model.parse(data=smart_text(response.content), format='rdfa')

        body = """prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#>

        select ?library ?name ?library_id ?gel_cut ?made_by
        where {
           ?library a libns:Library ;
                    libns:name ?name ;
                    libns:library_id ?library_id ;
                    libns:gel_cut ?gel_cut ;
                    libns:made_by ?made_by
        }"""

        count = 0
        for r in model.query(body):
            self.assertEqual(r['library_id'].toPython(),
                             library.id)
            self.assertEqual(r['name'].toPython(),
                             library.library_name)
            self.assertEqual(r['gel_cut'].toPython(),
                             library.gel_cut_size)
            self.assertEqual(r['made_by'].toPython(),
                             library.made_by)
            count += 1

        # make sure there was actually data
        self.assertGreater(count, 0)

        state = validate_xhtml(smart_bytes(response.content))
        if state is not None:
            self.assertTrue(state)

        # validate a library page.
        add_default_schemas(model)
        inference = Infer(model)
        errmsgs = list(inference.run_validation())
        self.assertEqual(len(errmsgs), 0)
コード例 #48
0
def parse_lastfm(username):
    ''' return an array of zitgist uris for scrobbled artists '''
    graph = ConjunctiveGraph()
    print 'parsing last.fm data from dbtune for %s' % username
    graph.parse('http://dbtune.org/last-fm/'+username)
    query = '''
            PREFIX mo: <http://purl.org/ontology/mo/>
            PREFIX owl: <http://www.w3.org/2002/07/owl#>

            SELECT DISTINCT ?same WHERE {
                ?artist a mo:MusicArtist ;
                    owl:sameAs ?same .
                }
            '''
    same = []
    for row in graph.query(query):
        same.append(row[0])
    return same
コード例 #49
0
ファイル: test_taskforce.py プロジェクト: rajat2502/rdfadict
def get_test(uri):
    """Return a single test case for the specified URI."""

    for manifest in MANIFESTS:

        LOG.debug("Retrieving manifest %s" % manifest)

        test_manifest = Graph()
        test_manifest.parse(manifest)

        # find all the approved tests'
        for result in test_manifest.query(APPROVED_TESTS):

            if str(result[0]) == uri:
                return new.classobj(py_name(str(result[3])), (TaskForceTest, ),
                                    {})(*result)

    return None
コード例 #50
0
class Config(object):
    def __init__(self, masterGraph):
        self.graph = ConjunctiveGraph()
        log.info('read config')
        for f in os.listdir('config'):
            if f.startswith('.'): continue
            self.graph.parse('config/%s' % f, format='n3')
        self.graph.bind('', ROOM) # not working
        self.graph.bind('rdf', RDF)
        # config graph is too noisy; maybe make it a separate resource
        #masterGraph.patch(Patch(addGraph=self.graph))

    def serialDevices(self):
        return dict([(row.dev, row.board) for row in self.graph.query(
            """SELECT ?board ?dev WHERE {
                 ?board :device ?dev;
                 a :ArduinoBoard .
               }""", initNs={'': ROOM})])
コード例 #51
0
ファイル: test_taskforce.py プロジェクト: cc-archive/rdfadict
def get_test(uri):
    """Return a single test case for the specified URI."""

    for manifest in MANIFESTS:

        LOG.debug("Retrieving manifest %s" % manifest)

        test_manifest = Graph()
        test_manifest.parse(manifest)

        # find all the approved tests'
        for result in test_manifest.query(APPROVED_TESTS):

            if str(result[0]) == uri:
                return new.classobj(py_name(str(result[3])),
                                    (TaskForceTest,),{})(*result)

    return None
コード例 #52
0
def testSPARQLNotEquals():
    NS = u"http://example.org/"
    graph = ConjunctiveGraph()
    graph.parse(StringInputSource(b("""
       @prefix    : <http://example.org/> .
       @prefix rdf: <%s> .
       :foo rdf:value 1.
       :bar rdf:value 2.""") % RDF.uri), format="n3")
    rt = graph.query(b("""SELECT ?node 
                            WHERE {
                                    ?node rdf:value ?val.
                                    FILTER (?val != 1)
                                   }"""),
                           initNs={b('rdf'): RDF.uri},
                           DEBUG=False)
    for row in rt:        
        item = row[0]
        assert item == URIRef("http://example.org/bar"), "unexpected item of '%s'" % repr(item)
コード例 #53
0
def find_long_lat(uri):
    '''return tuple of the longitude and latitude (long,lat) for a geonames uri and the name'''
    graph = ConjunctiveGraph()
    graph.parse(uri)
    query = '''
            PREFIX wgs: <http://www.w3.org/2003/01/geo/wgs84_pos#>
            PREFIX geo: <http://www.geonames.org/ontology#>
            SELECT ?long ?lat ?name WHERE
            { ?x wgs:long ?long;
                 wgs:lat ?lat;
                 geo:name ?name.
            }
            '''
    long_lat_name = None
    print 'finding coordinates for %s ' % uri
    for row in graph.query(query):
        long_lat_name = (row[0], row[1], row[2])
    return long_lat_name
コード例 #54
0
class DeepGraphStore():
    store_name = 'SQLite'

    def __init__(self, create=False, parse=None):
        self.parse = parse
        self.create = create
        self.graph = None

    def setUp(self):
        self.path = "" + random_file_generating()
        self.graph = Graph(store=self.store_name)
        self.graph.open(self.path, create=self.create)

        if self.create:
            if not self.parse:
                self.graph.parse("http://njh.me/foaf.rdf", format='xml')
            else:
                self.graph.parse(self.parse)
            self.graph.commit()

    def open(self, path):
        self.graph = ConjunctiveGraph(self.store_name)
        self.path = path
        self.graph.open(self.path, create=False)

    def query(self, sparql_query):
        return self.graph.query(sparql_query)

    def parse(self, path_to_file_):
        self.graph.parse(path_to_file_)

    def load(self, triples):
        self.graph.load(triples)

    def close(self):
        self.graph.close()

    def size(self):
        size = self.graph.__len__()
        size = len(self.graph)
        # self.close()
        return size
コード例 #55
0
ファイル: rdf2subdue.py プロジェクト: memaldi/rdf2subdue
def calculate_edges((offset_limit, config)):
    g = ConjunctiveGraph(config['graph_store'], config['graph_identifier'])
    g.open(config['db_configstring'], create=False)
    engine = create_engine(config['alchemy_configstring'])
    
    Session = sessionmaker(bind=engine)
    session = Session()
    
    results = session.query(Node).filter_by(node_type="subject").order_by(Node.node_id.desc()).offset(offset_limit[0]).limit(offset_limit[1]).all()
    
    for result in results:
        query = "SELECT ?p ?o WHERE { <%s> ?p ?o }" % result.node_uri
        items = g.query(query)
        for item in items:
            if str(item[0]) != "http://www.w3.org/1999/02/22-rdf-syntax-ns#type":
                neighbors = session.query(Node).filter_by(node_uri=item[1]).all()
                if len(neighbors) > 0:                    
                    for neighbor in neighbors:
                        result.add_neighbors(str(item[0]), neighbor)
        session.commit()
    g.close()
    session.close()
コード例 #56
0
ファイル: test_samples.py プロジェクト: detrout/htsworkflow
    def test_0_not_run(self):
        """Create a library that was run, so it won't show up with not_run filter
        """
        lane = LaneFactory()
        library = lane.library
        url = reverse('library_not_run')
        response = self.client.get(url)

        model = ConjunctiveGraph()
        model.parse(data=smart_text(response.content), format='rdfa')
        body = """prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#>

        select ?library ?library_id
        where {
           ?library a libns:Library ;
                    libns:library_id ?library_id .
        }"""
        results = []
        for r in model.query(body):
            results.append(r['library_id'])

        self.assertEqual(len(results), 0)
コード例 #57
0
class TestSparqlJsonResults(unittest.TestCase):

    def setUp(self):
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringIO(test_data), format="n3")

    def _query_result_contains(self, query, correct):
        results = self.graph.query(query)
        result_json = results.serialize(format='json')
        self.failUnless(result_json.find(correct) >= 0,
                "Expected:\n %s \n- to contain:\n%s" % (result_json, correct))

    testOptional = make_method('optional')

    testWildcard = make_method('wildcard')

    testUnion = make_method('union')

    testUnion3 = make_method('union3')

    testSelectVars = make_method('select_vars')
    
    testWildcardVars = make_method('wildcard_vars')
コード例 #58
0
ファイル: test_taskforce.py プロジェクト: cc-archive/rdfadict
def test_suite():
    """Generate a test suite of all approved task force tests."""

    test_suite = unittest.TestSuite()

    return test_suite

    for manifest in MANIFESTS:

        LOG.debug("Retrieving manifest %s" % manifest)

        test_manifest = Graph()
        test_manifest.parse(manifest)

        # find all the approved tests'
        for result in test_manifest.query(APPROVED_TESTS):
            LOG.debug("Adding test case: %s" % str(result[3]))

            test_suite.addTest(
                new.classobj(py_name(str(result[3])),
                             (TaskForceTest,),{})(*result))

    return test_suite