当前位置: 首页>>代码示例>>Python>>正文


Python ConjunctiveGraph.query方法代码示例

本文整理汇总了Python中rdflib.Graph.ConjunctiveGraph.query方法的典型用法代码示例。如果您正苦于以下问题:Python ConjunctiveGraph.query方法的具体用法?Python ConjunctiveGraph.query怎么用?Python ConjunctiveGraph.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rdflib.Graph.ConjunctiveGraph的用法示例。


在下文中一共展示了ConjunctiveGraph.query方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testAggregateSPARQL

# 需要导入模块: from rdflib.Graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.Graph.ConjunctiveGraph import query [as 别名]
def testAggregateSPARQL():
    memStore = plugin.get('IOMemory',Store)()
    graph1 = Graph(memStore,URIRef("graph1"))
    graph2 = Graph(memStore,URIRef("graph2"))
    graph3 = Graph(memStore,URIRef("graph3"))

    for n3Str,graph in [(testGraph1N3,graph1),
                        (testGraph2N3,graph2),
                        (testGraph3N3,graph3)]:
        graph.parse(StringIO(n3Str),format='n3')

    graph4 = Graph(memStore,RDFS.RDFSNS)
    graph4.parse(RDFS.RDFSNS)
    G = ConjunctiveGraph(memStore)
    rt =  G.query(sparqlQ)
    assert len(rt) > 1
    #print rt.serialize(format='xml')
    LOG_NS = Namespace(u'http://www.w3.org/2000/10/swap/log#')
    rt=G.query(sparqlQ2,initBindings={u'?graph' : URIRef("graph3")})
    #print rt.serialize(format='json')
    assert rt.serialize('python')[0] == LOG_NS.N3Document,str(rt)
开发者ID:AuroraSkywalker,项目名称:watchdog,代码行数:23,代码来源:aggregate_graphs.py

示例2: testDefaultGraph

# 需要导入模块: from rdflib.Graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.Graph.ConjunctiveGraph import query [as 别名]
def testDefaultGraph():
    memStore = plugin.get('IOMemory',Store)()
    graph1 = Graph(memStore,URIRef("graph1"))
    graph2 = Graph(memStore,URIRef("graph2"))
    graph3 = Graph(memStore,URIRef("graph3"))
    
    for n3Str,graph in [(testGraph1N3,graph1),
                        (testGraph2N3,graph2),
                        (testGraph3N3,graph3)]:
        graph.parse(StringIO(n3Str),format='n3')
    G = ConjunctiveGraph(memStore)
    #test that CG includes triples from all 3
    assert G.query(sparqlQ3),"CG as default graph should *all* triples"
    assert not graph2.query(sparqlQ3),"Graph as default graph should *not* include triples from other graphs"
开发者ID:AuroraSkywalker,项目名称:watchdog,代码行数:16,代码来源:aggregate_graphs.py

示例3: testQueryingMore

# 需要导入模块: from rdflib.Graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.Graph.ConjunctiveGraph import query [as 别名]
 def testQueryingMore(self):
     for result in self.results:
         uri = result[0]
         g = ConjunctiveGraph()
         g.parse(uri)
         query = Parse("""
                             SELECT ?person
                             WHERE {
                                      <%s> foaf:primaryTopic ?person .
                                      ?person rdf:type foaf:Person . 
                                   }
                       """ % uri )
         queryResults = g.query(query, initNs=NSbindings).serialize('python')
         if (len(queryResults)>0):
             self.assertEquals(str(queryResults[0]), "http://www.wikier.org/foaf#wikier")
开发者ID:BackupTheBerlios,项目名称:swaml-svn,代码行数:17,代码来源:sindice.py

示例4: TestSPARQLToldBNodes

# 需要导入模块: from rdflib.Graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.Graph.ConjunctiveGraph import query [as 别名]
class TestSPARQLToldBNodes(unittest.TestCase):
    def setUp(self):
        NS = u"http://example.org/"
        self.graph = ConjunctiveGraph()
        self.graph.parse(StringInputSource("""
           @prefix    : <http://example.org/> .
           @prefix rdf: <%s> .
           @prefix rdfs: <%s> .
           [ :prop :val ].
           [ a rdfs:Class ]."""%(RDF.RDFNS,RDFS.RDFSNS)), format="n3")
    def testToldBNode(self):
        for s,p,o in self.graph.triples((None,RDF.type,None)):
            pass
        query = """SELECT ?obj WHERE { %s ?prop ?obj }"""%s.n3()
        print query
        rt = self.graph.query(query)
        self.failUnless(len(rt) == 1,"BGP should only match the 'told' BNode by name (result set size: %s)"%len(rt))
开发者ID:Ju2ender,项目名称:watchdog,代码行数:19,代码来源:test_sparql_told_bnodes.py

示例5: testSPARQLNotEquals

# 需要导入模块: from rdflib.Graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.Graph.ConjunctiveGraph import query [as 别名]
def testSPARQLNotEquals():
    NS = u"http://example.org/"
    graph = ConjunctiveGraph()
    graph.parse(StringInputSource("""
       @prefix    : <http://example.org/> .
       @prefix rdf: <%s> .
       :foo rdf:value 1.
       :bar rdf:value 2."""%RDF.RDFNS), format="n3")
    rt = graph.query("""SELECT ?node 
                        WHERE {
                                ?node rdf:value ?val.
                                FILTER (?val != 1)
                               }""",
                           initNs={'rdf':RDF.RDFNS},                           
                           DEBUG=False)
    for row in rt:        
        item = row[0]
        assert item == URIRef("http://example.org/bar")
开发者ID:Ju2ender,项目名称:watchdog,代码行数:20,代码来源:test_not_equals.py

示例6: TriplifierTest

# 需要导入模块: from rdflib.Graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.Graph.ConjunctiveGraph import query [as 别名]
class TriplifierTest(unittest.TestCase):
  def setUp(self):
    self.values = optparse.Values()
    self.values.ensure_value("baseURI", "b")
    self.graph = ConjunctiveGraph()
    self.t = Triplifier(self.graph, self.values)
    self.mox = mox.Mox()

  def compareGeneratedTriples(self, expected):
    for triple in self.graph.query(ALLTRIPLES):
      self.assertTrue(triple in expected, "%s is not in" % str(triple))

  def testPushInitialTriples(self):
    self.values.ensure_value("distribution", "http://example.com/d")
    self.values.ensure_value("distdate", "Not used")
    self.values.ensure_value("parsedDistDate", date(1985, 07, 01))
    self.t.pushInitialTriples()
    self.assertEqual(2, len(self.graph))
    uriref = URIRef("http://example.com/d")
    expected = [(uriref, RDF.type, DEB['Distribution']),\
                (uriref, DEB['releaseDate'], Literal(date(1985, 07, 01)))]
    self.compareGeneratedTriples(expected)

  def testTriplifyArchitecture(self):
    arch = Architecture("testArch")
    uriref = URIRef("b/arch/testArch")
    self.assertEqual(uriref, self.t.triplifyArchitecture(arch))
    self.assertEqual(2, len(self.graph))
    expected = [(uriref, RDF.type, DEB['Architecture']),\
                (uriref, RDFS.label, Literal("Architecture: testArch", lang='en'))]
    self.compareGeneratedTriples(expected)

  def testTriplifyArchitectureOntologyInstance(self):
    arch = Architecture("all")
    self.assertEqual(DEB['all'], self.t.triplifyArchitecture(arch))

  def testTriplifyVersionNumberSimple(self):
    version = VersionNumber("1.0-1")
    uriref = URIRef("b/version/1.0-1")
    self.assertEqual(uriref, self.t.triplifyVersionNumber(version))
    self.assertEqual(5, len(self.graph))
    expected = [(uriref, RDF.type, DEB['VersionNumber']),\
                (uriref, RDFS.label, Literal("Version: 1.0-1", lang='en')),\
                (uriref, DEB['fullVersion'], Literal("1.0-1")),\
                (uriref, DEB['upstreamVersion'], Literal("1.0")),\
                (uriref, DEB['debianRevision'], Literal("1"))]
    self.compareGeneratedTriples(expected)

  def testTriplifyConstraintSimple(self):
    constraint = Constraint()
    constraint.package = UnversionedBinaryPackage("pkg")
    constraint.operator = ">>"
    constraint.version = VersionNumber("1.0-1")
    self.t.triplifyVersionNumber =\
        self.mockTriplifyVersionNumber(constraint.version)
    self.t.triplifyUnversionedBinaryPackage =\
        self.mockUnversionedBinaryPackage(constraint.package)
    uriref = URIRef("b/constraint/pkg StrictlyLater 1.0-1")
    self.assertEqual(uriref, self.t.triplifyConstraint(constraint))
    self.mox.VerifyAll()
    self.assertEqual(5, len(self.graph))
    expected = [(uriref, RDF.type, DEB['SimplePackageConstraint']),\
                (uriref, RDFS.label, Literal("Constraint: pkg (>> 1.0-1)", lang='en')),\
                (uriref, DEB['package'], URIRef("b/binary/pkg")),\
                (uriref, DEB['constraintOperator'], Literal(">>")),\
                (uriref, DEB['versionNumber'], URIRef("b/version/1.0-1"))]
    self.compareGeneratedTriples(expected)

  def testTriplifyOrConstraint(self):
    orconstraint = OrConstraint()
    constraint1 = Constraint()
    constraint1.package = UnversionedBinaryPackage("pkg1")
    constraint2 = Constraint()
    constraint2.package = UnversionedBinaryPackage("pkg2")
    orconstraint.add(constraint1)
    orconstraint.add(constraint2)
    self.t.triplifyConstraint = self.mockTriplifyConstraint([constraint1, constraint2])
    self.assertEqual(BNode, self.t.triplifyOrConstraint(orconstraint).__class__)
    self.mox.VerifyAll()
    self.assertEqual(3, len(self.graph))

  def testTriplifyBinaryPackageBuild(self):
    bs = BinaryPackage("pkg1", "6.7")
    b = BinaryPackageBuild(bs)
    b.architecture = Architecture("arch")
    b.installedSize = "12345"
    self.t.triplifyArchitecture = self.mockTriplifyArchitecture(b.architecture)
    uriref = URIRef("b/binary/pkg1/6.7/arch")
    self.assertEqual(uriref, self.t.triplifyBinaryPackageBuild(b))
    self.assertEqual(4, len(self.graph))
    expected = [(uriref, RDF.type, DEB['BinaryBuild']),\
                (uriref, RDFS.label, Literal("BinaryBuild: pkg1 (6.7) [arch]", lang='en')),\
                (uriref, DEB['installed-size'], Literal(int("12345"))),\
                (uriref, DEB['architecture'], URIRef("b/arch/arch"))]
    self.compareGeneratedTriples(expected)

  def testTriplifyFile(self):
    f = File("testname", "hash", "1234", Directory("test/path"))
    uriref = URIRef("b/path/test/path/testname")
    self.assertEqual(uriref, self.t.triplifyFile(f))
#.........这里部分代码省略.........
开发者ID:berrueta,项目名称:steamy,代码行数:103,代码来源:exportTest.py

示例7: testBasic

# 需要导入模块: from rdflib.Graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.Graph.ConjunctiveGraph import query [as 别名]
def testBasic(DEBUG = False):
    from glob import glob
    from sre import sub
    for testFile in glob('data/examples/*.rq'):#glob('data/*/*.rq'):
        store = plugin.get(STORE,Store)()
        bootStrapStore(store)
        store.commit()

        prefix = testFile.split('.rq')[-1]
        manifestPath = '/'.join(testFile.split('/')[:-1]+['manifest.n3'])
        manifestPath2 = '/'.join(testFile.split('/')[:-1]+['manifest.ttl'])
        queryFileName = testFile.split('/')[-1]
        store = plugin.get(STORE,Store)()
        store.open(configString,create=False)
        assert len(store) == 0
        manifestG=ConjunctiveGraph(store)
        if not os.path.exists(manifestPath):
            assert os.path.exists(manifestPath2)
            manifestPath = manifestPath2
        manifestG.default_context.parse(open(manifestPath),publicID=TEST_BASE,format='n3')
        manifestData = \
           manifestG.query(
                                  PARSED_MANIFEST_QUERY,
                                  initBindings={'?query' : TEST_BASE[queryFileName]},
                                  initNs=manifestNS,
                                  DEBUG = False)
        store.rollback()
        store.close()
        for source,testCaseName,testCaseComment,expectedRT in manifestData:

            if expectedRT:
                expectedRT = '/'.join(testFile.split('/')[:-1]+[expectedRT.replace(TEST_BASE,'')])
            if source:
                source = '/'.join(testFile.split('/')[:-1]+[source.replace(TEST_BASE,'')])

            testCaseName = testCaseComment and testCaseComment or testCaseName
            print "## Source: %s ##"%source
            print "## Test: %s ##"%testCaseName
            print "## Result: %s ##"%expectedRT

            #Expected results
            if expectedRT:
                store = plugin.get(STORE,Store)()
                store.open(configString,create=False)
                resultG=ConjunctiveGraph(store).default_context
#                if DEBUG:
#                    print "###"*10
#                    print "parsing: ", open(expectedRT).read()
#                    print "###"*10
                assert len(store) == 0
                print "## Parsing (%s) ##"%(expectedRT)
                if not trialAndErrorRTParse(resultG,expectedRT,DEBUG):
                    if DEBUG:
                        print "Unexpected result format (for %s), skipping"%(expectedRT)
                    store.rollback()
                    store.close()
                    continue
                if DEBUG:
                    print "## Done .. ##"

                rtVars = [rtVar for rtVar in resultG.objects(None,RESULT_NS.resultVariable)]
                bindings = []
                resultSetNode = resultG.value(predicate=RESULT_NS.value,object=RESULT_NS.ResultSet)
                for solutionNode in resultG.objects(resultSetNode,RESULT_NS.solution):
                    bindingDict = dict([(key,None) for key in rtVars])
                    for bindingNode in resultG.objects(solutionNode,RESULT_NS.binding):
                        value = resultG.value(subject=bindingNode,predicate=RESULT_NS.value)
                        name  = resultG.value(subject=bindingNode,predicate=RESULT_NS.variable)
                        bindingDict[name] = value
                    bindings.append(tuple([bindingDict[vName] for vName in rtVars]))
                if DEBUG:
                    print "Expected bindings: ", bindings
                    print open(expectedRT).read()
                store.rollback()
                store.close()

            if testFile.startswith('data/NegativeSyntax'):
                try:
                    query = open(testFile).read()
                    p = Parse(query,DEBUG)
                except:
                    continue
                else:
                    raise Exception("Test %s should have failed!"%testFile)
            if testFile in tests2Skip:
                print "Skipping test (%s)"%testCaseName
                continue
            query = open(testFile).read()
            print "### %s (%s) ###"%(testCaseName,testFile)
            print query
            p = Parse(query,DEBUG_PARSE)
            if DEBUG:
                print p
            if EVALUATE and source:
                if DEBUG:
                    print "### Source Graph: ###"
                    print open(source).read()
                store = plugin.get(STORE,Store)()
                store.open(configString,create=False)
                g=ConjunctiveGraph(store)
#.........这里部分代码省略.........
开发者ID:AuroraSkywalker,项目名称:watchdog,代码行数:103,代码来源:test.py

示例8: TriplesDatabase

# 需要导入模块: from rdflib.Graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.Graph.ConjunctiveGraph import query [as 别名]
class TriplesDatabase(object):
    """A database from the defined triples"""
    def __init__(self):
        self._open = False

    def open(self, filename, graphClass=None):
        """
        Load existing database at 'filename'.
        """
        if filename is None:
            if graphClass is None:
                self.graph = Graph()
            else:
                self.graph = graphClass()
        else:
            assert os.path.exists(filename), (
                    "%s must be an existing database" % (filename,))

            path, filename = os.path.split(filename)
            self.graph = sqliteBackedGraph(path, filename)

        self._open = True

    def query(self, rest, initNs=None, initBindings=None):
        """
        Execute a SPARQL query and get the results as a SPARQLResult

        {rest} is a string that should begin with "SELECT ", usually
        """
        assert self._open

        if initNs is None:
            initNs = dict(self.graph.namespaces()) 
        if initBindings is None: initBindings = {}

        sel = select(self.getBase(), rest)
        ret = self.graph.query(sel, initNs=initNs, initBindings=initBindings,
                DEBUG=False)
        return ret

    def getBase(self):
        d = dict(self.graph.namespaces())
        return d.get('', RDFSNS)

    def addTriple(self, s, v, *objects):
        """
        Make a statement/arc/triple in the database.

        Strings, ints and floats as s or o will automatically be coerced to
        RDFLiteral().  It is an error to give a RDFLiteral as v, so no
        coercion will be done in that position.

        2-tuples will be coerced to bnodes.
        
        If more than one object is given, i.e.
            addTriple(a, b, c1, c2, c3) 
        this is equivalent to:
            addTriple(a,b,c1); addTriple(a,b,c2); addTriple(a,b,c3)
        """
        assert self._open
        assert len(objects) >= 1, "You must provide at least one object"
        if canBeLiteral(s):
            s = RDFLiteral(s)

        bnode = None
        for o in objects:
            if canBeLiteral(o):
                o = RDFLiteral(o)
            elif isinstance(o, tuple) and len(o) == 2:
                if bnode is None:
                    bnode = BNode()
                self.addTriple(bnode, *o)
                o = bnode

            assert None not in [s,v,o]
            self.graph.add((s, v, o))

    def dump(self):
        assert self._open
        io = StringIO()
        try:
            self.graph.serialize(destination=io, format='n3')
        except Exception, e:
            import sys, pdb; pdb.post_mortem(sys.exc_info()[2])
        return io.getvalue()
开发者ID:corydodt,项目名称:Playtools,代码行数:87,代码来源:sparqly.py

示例9: sparql_funcs

# 需要导入模块: from rdflib.Graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.Graph.ConjunctiveGraph import query [as 别名]
class sparql_funcs():
    
    def __init__(self):
        self.g = Graph('IOMemory')
        #self.endpoint = "http://www.opencorrespondence.org/data/endpoint/rdf"
        #self.g.bind('geo', geo)


    def find_places(self):
        '''
            Function to get the distinct locations mentioned in the headers of the letters. 
            These are the locations from which Dickens wrote. 
            TODO: Parsing the letters to get the places mentioned in them
        '''
        row = set()
        o = OFS()
        
        for b in o.list_buckets():
            endpoint = o.get_stream(b, "endpoint")

        self.g.parse(endpoint)

        for s,_,n in self.g.triples((None, dublin_core['title'], None)):
            loc_key = urllib.unquote(n.replace("http://www.opencorrespondence.org/place/resource/", "").replace("/rdf",""))
            row.add(self.tidy_location(loc_key))

        return row
    
    def tidy_location (self, location):
        '''
           Function to tidy up some of the places where they refer to the same place
           TODO: prob need some language processing to make this scalable
        '''
        ret_location = '';
        if location == 'Office Of "household Words,':
            ret_location = "Household Words"
        elif location== '"household Words" Office':
            ret_location = "Household Words"
        elif location== '"household Words"':
            ret_location = "Household Words"
        elif location== 'H. W. Office':
            ret_location = "Household Words"
        elif location == '"household Words,':
            ret_location = "Household Words"
        elif location == '"all The Year Round" Office':
            ret_location = "All The Year Round"
        elif location == 'Office Of "all The Year Round,':
            ret_location = "All The Year Round"
        elif location == "Gad's Hill Place":
            ret_location = "Gads Hill"
        elif location == "Gad's Hill":
            ret_location = "Gads Hill"
        elif location == "Gad's Hill Place, Higham":
            ret_location = "Gads Hill"
        elif location == "Tavistock House, Tavistock Square":
            ret_location = "Tavistock House"
        elif location == "London, Tavistock House":
            ret_location = "Tavistock House"
        elif location == "Tavistock House, London":
            ret_location = "Tavistock House"
        else:
            if "U.s." in location:
                location = str(location).replace("U.s", "")
            ret_location = str(location).replace(".", "")    
            
        return ret_location
    
    def find_correspondents(self):
        '''
            Function to get the distinct locations mentioned in the headers of the letters. 
            These are the locations from which Dickens wrote. 
            TODO: Parsing the letters to get the places mentioned in them
        '''
        row = set()
        self.g.parse(self.endpoint)

        for s,_,n in self.g.triples((None, letter['correspondent'], None)):
            loc_key = urllib.unquote(n.replace("http://www.opencorrespondence.org/correspondent/resource/", "").replace("/rdf", ""))
            row.add(loc_key)

        return row
    
    def get_abstract (self, resource_id):
        
        self.g.parse('http://dbpedia.org/resource/'.resource_id)
        q = '''
          SELECT *
                WHERE 
                {
                ?x dbpedia:abstract ?abstract .
                FILTER (lang(?abstract) = 'en')
                }
        '''
        for row in self.g.query(q,
                   initNs=dict(dbpedia=Namespace("http://dbpedia.org/ontology/")),
                   initBindings={}):
            return row[1]

    
    def query_dates(self, author):
#.........这里部分代码省略.........
开发者ID:Spencerx,项目名称:openletters,代码行数:103,代码来源:sparql_funcs.py


注:本文中的rdflib.Graph.ConjunctiveGraph.query方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。