本文整理汇总了Python中rdflib.Graph.Graph类的典型用法代码示例。如果您正苦于以下问题:Python Graph类的具体用法?Python Graph怎么用?Python Graph使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Graph类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: process_file
def process_file (self, dirname, basename, **kw):
if not basename.endswith('.doap'):
return
store = Graph()
g = store.parse(os.path.join(dirname, basename))
query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX doap: <http://usefulinc.com/ns/doap#>
SELECT $bug
WHERE {
$project rdf:type doap:Project .
$project doap:bug-database $bug
}"""
results = list(g.query(query))
if len(results) == 1:
bug_database = URL.from_str(results[0][0])
self.scanner.branch.bug_database = unicode(bug_database)
if bug_database.netloc == 'bugzilla.gnome.org': # TODO
product = bug_database['product'][0]
components = pulse.db.Component.select(
pulse.db.Component.ident.like('comp/bugzilla.gnome.org/%s/%%' % product))
for comp in components:
pulse.db.ModuleComponents.set_related (self.scanner.branch, comp)
开发者ID:shaunix,项目名称:blip,代码行数:27,代码来源:bugs.py
示例2: parse_from_soup
def parse_from_soup(self,soup,basefile):
g = Graph()
self.log.info("%s: Parsing" % basefile)
if basefile == "teu":
# FIXME: Use a better base URI?
uri = 'http://rinfo.lagrummet.se/extern/celex/12008M'
startnode = soup.findAll(text="-"*50)[1].parent
g.add((URIRef(uri),DCT['title'],Literal("Treaty on European Union")))
elif basefile == "tfeu":
uri = 'http://rinfo.lagrummet.se/extern/celex/12008E'
startnode = soup.findAll(text="-"*50)[2].parent
g.add((URIRef(uri),DCT['title'],Literal("Treaty on the Functioning of the European Union")))
lines = deque()
for p in startnode.findNextSiblings("p"):
if p.string == "-" * 50:
self.log.info("found the end")
break
else:
if p.string:
lines.append(unicode(p.string))
self.log.info("%s: Found %d lines" % (basefile,len(lines)))
body = self.make_body(lines)
self.process_body(body, '', uri)
# print serialize(body)
return {'meta':g,
'body':body,
'lang':'en',
'uri':uri}
开发者ID:staffanm,项目名称:legacy.lagen.nu,代码行数:30,代码来源:EurlexTreaties.py
示例3: _getDataGraph
def _getDataGraph(self):
g = Graph()
try:
g.parse(self.storeUri, format="n3")
except urllib2.URLError:
print "%s file missing- starting a new one" % self.storeUri
return g
开发者ID:drewp,项目名称:diarybot,代码行数:7,代码来源:diarybot.py
示例4: testConjunction
def testConjunction(self):
self.addStuffInMultipleContexts()
triple = (self.pizza, self.likes, self.pizza)
# add to context 1
graph = Graph(self.graph.store, self.c1)
graph.add(triple)
self.assertEquals(len(self.graph), len(graph))
开发者ID:AuroraSkywalker,项目名称:watchdog,代码行数:7,代码来源:context.py
示例5: __init__
def __init__(self, connection, ontology):
self._connection = connection
self._ontology = ontology
self._rdfObjects = {}
self._graph = Graph()
self._added = Graph()
self._removed = Graph()
开发者ID:abhik1368,项目名称:study-semantic-web,代码行数:7,代码来源:rdfobject.py
示例6: retrieveTestCases
def retrieveTestCases(base_uri):
# query the master test manifest
q = """
PREFIX test: <http://www.w3.org/2006/03/test-description#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?t ?title ?classification ?expected_results
FROM <%s>
WHERE
{
?t dc:title ?title .
?t test:classification ?classification .
OPTIONAL
{
?t test:expectedResults ?expected_results .
}
}""" % (base_uri + "manifest.ttl")
# Construct the graph from the given RDF and apply the SPARQL filter above
g = Graph()
unittests = []
for tc, title, classification_url, expected_results in g.query(q):
classification = classification_url.split("#")[-1]
matches = search(r'(\d+)', tc)
num = matches.groups(1)[0]
if(expected_results == None):
expected_results = 'true'
# Generate the input document URLs
suffix = "xml"
if hostLanguage in ["xhtml1", "xhtml5"]:
suffix = "xhtml"
elif hostLanguage in ["html4", "html5"]:
suffix = "xhtml"
elif hostLanguage in ["svgtiny1.2", "svg"]:
suffix = "svg"
doc_uri = "%stest-cases/%s." % \
(base_uri, num)
unittests.append((int(num),
str(title),
str(doc_uri + suffix),
str(doc_uri + "sparql"),
str(classification),
str(expected_results)))
# Sorts the unit tests in unit test number order.
def sorttests(a, b):
if(a[0] < b[0]):
return -1
elif(a[0] == b[0]):
return 0
else:
return 1
unittests.sort(sorttests)
return unittests
开发者ID:apassant,项目名称:json-ld.org,代码行数:60,代码来源:crazyivan.py
示例7: main
def main(inputFileName, outputFileName=None):
"""
Given an inputfile and optionally outputfile, create a GraphViz file of the NDL inputfile.
If no outputfile is given, default to inputfilename with rdf replaced with dot.
If the file exists, ask for user confirmation to overwrite it.
"""
graph = Graph()
graph.parse(inputFileName)
internal, external, locations = getConnections(graph)
dotStr = dotString(internal, external, locations)
if not outputFileName:
outputFileName = inputFileName.replace(".rdf",".dot")
if os.path.exists(outputFileName):
while True:
arg = raw_input("%s already exists. To replace type 'y' or provide different filename: " % outputFileName)
# Some input should be given, otherwise repeat the question.
if arg:
if arg in "yY":
# Overwrite the file
break
# A new name was given, store it
outputFileName = arg
# Check if new file exists, if so, repeat the question, if not, write the file
if not os.path.exists(arg):
break
f = file(outputFileName,'w')
f.write(dotStr)
f.close()
开发者ID:jeroenh,项目名称:Pynt,代码行数:30,代码来源:ndl2dot.py
示例8: _parse_rdf
def _parse_rdf(self, file):
""" Returns a case from the given file.
"""
store = Graph()
store.parse(file)
print len(store)
开发者ID:Waqquas,项目名称:pylon,代码行数:7,代码来源:rdf.py
示例9: ParserTestCase
class ParserTestCase(unittest.TestCase):
backend = 'default'
path = 'store'
def setUp(self):
self.graph = Graph(store=self.backend)
self.graph.open(self.path)
def tearDown(self):
self.graph.close()
def testNoPathWithHash(self):
g = self.graph
g.parse(StringInputSource("""\
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
>
<rdfs:Class rdf:about="http://example.org#">
<rdfs:label>testing</rdfs:label>
</rdfs:Class>
</rdf:RDF>
"""), publicID="http://example.org")
subject = URIRef("http://example.org#")
label = g.value(subject, RDFS.label)
self.assertEquals(label, Literal("testing"))
type = g.value(subject, RDF.type)
self.assertEquals(type, RDFS.Class)
开发者ID:AuroraSkywalker,项目名称:watchdog,代码行数:32,代码来源:parser.py
示例10: _testPositive
def _testPositive(uri, manifest):
if verbose: write(u"TESTING: %s" % uri)
result = 0 # 1=failed, 0=passed
inDoc = first(manifest.objects(uri, TEST["inputDocument"]))
outDoc = first(manifest.objects(uri, TEST["outputDocument"]))
expected = Graph()
if outDoc[-3:]==".nt":
format = "nt"
else:
format = "xml"
expected.load(outDoc, format=format)
store = TestStore(expected)
if inDoc[-3:]==".nt":
format = "nt"
else:
format = "xml"
try:
store.load(inDoc, format=format)
except ParserError, pe:
write("Failed '")
write(inDoc)
write("' failed with")
raise pe
try:
write(type(pe))
except:
write("sorry could not dump out error.")
result = 1
开发者ID:AuroraSkywalker,项目名称:watchdog,代码行数:29,代码来源:parser_rdfcore.py
示例11: NegationOfAtomicConcept
class NegationOfAtomicConcept(unittest.TestCase):
def setUp(self):
self.ontGraph = Graph()
self.ontGraph.bind("ex", EX_NS)
self.ontGraph.bind("owl", OWL_NS)
Individual.factoryGraph = self.ontGraph
def testAtomicNegation(self):
bar = EX.Bar
baz = ~bar
baz.identifier = EX_NS.Baz
ruleStore, ruleGraph, network = SetupRuleStore(makeNetwork=True)
individual = BNode()
individual2 = BNode()
(EX.OtherClass).extent = [individual]
bar.extent = [individual2]
NormalFormReduction(self.ontGraph)
self.assertEqual(repr(baz), "Class: ex:Baz DisjointWith ex:Bar\n")
posRules, negRules = CalculateStratifiedModel(network, self.ontGraph, [EX_NS.Foo])
self.failUnless(not posRules, "There should be no rules in the 0 strata!")
self.failUnless(len(negRules) == 1, "There should only be one negative rule in a higher strata")
self.assertEqual(repr(negRules[0]), "Forall ?X ( ex:Baz(?X) :- not ex:Bar(?X) )")
baz.graph = network.inferredFacts
self.failUnless(individual in baz.extent, "%s should be a member of ex:Baz" % individual)
self.failUnless(individual2 not in baz.extent, "%s should *not* be a member of ex:Baz" % individual2)
开发者ID:Bazmundi,项目名称:fuxi,代码行数:25,代码来源:Negation.py
示例12: NegatedDisjunctTest
class NegatedDisjunctTest(unittest.TestCase):
def setUp(self):
self.ontGraph = Graph()
self.ontGraph.bind("ex", EX_NS)
self.ontGraph.bind("owl", OWL_NS)
Individual.factoryGraph = self.ontGraph
def testStratified(self):
bar = EX.Bar
baz = EX.Baz
noBarOrBaz = ~(bar | baz)
omega = EX.Omega
foo = omega & noBarOrBaz
foo.identifier = EX_NS.Foo
ruleStore, ruleGraph, network = SetupRuleStore(makeNetwork=True)
individual = BNode()
omega.extent = [individual]
NormalFormReduction(self.ontGraph)
self.assertEqual(repr(foo), "ex:Omega that ( not ex:Bar ) and ( not ex:Baz )")
posRules, negRules = CalculateStratifiedModel(network, self.ontGraph, [EX_NS.Foo])
foo.graph = network.inferredFacts
self.failUnless(not posRules, "There should be no rules in the 0 strata!")
self.assertEqual(
repr(negRules[0]), "Forall ?X ( ex:Foo(?X) :- And( ex:Omega(?X) not ex:Bar(?X) not ex:Baz(?X) ) )"
)
self.failUnless(len(negRules) == 1, "There should only be one negative rule in a higher strata")
self.failUnless(individual in foo.extent, "%s should be a member of ex:Foo" % individual)
开发者ID:Bazmundi,项目名称:fuxi,代码行数:27,代码来源:Negation.py
示例13: NonEqualityPredicatesTestSuite
class NonEqualityPredicatesTestSuite(unittest.TestCase):
def setUp(self):
from FuXi.Rete.RuleStore import N3RuleStore
from FuXi.Rete import ReteNetwork
from FuXi.Rete.Util import generateTokenSet
self.testGraph = Graph()
self.ruleStore=N3RuleStore()
self.ruleGraph = Graph(self.ruleStore)
self.ruleGraph.parse(StringIO(testN3),format='n3')
self.testGraph.parse(StringIO(testN3),format='n3')
self.closureDeltaGraph = Graph()
self.network = ReteNetwork(self.ruleStore,
initialWorkingMemory=generateTokenSet(self.testGraph),
inferredTarget = self.closureDeltaGraph,
nsMap = {})
def testParseBuiltIns(self):
from FuXi.Rete.RuleStore import N3Builtin
from FuXi.Rete.AlphaNode import BuiltInAlphaNode
self.failUnless(self.ruleStore.rules>0, "No rules parsed out form N3!")
for alphaNode in self.network.alphaNodes:
if isinstance(alphaNode, BuiltInAlphaNode):
self.failUnless(alphaNode.n3builtin.uri == MATH_NS.greaterThan,
"Unable to find math:greaterThan func")
def testEvaluateBuiltIns(self):
from FuXi.Rete.RuleStore import N3Builtin
from FuXi.Rete.AlphaNode import BuiltInAlphaNode
self.failUnless(first(self.closureDeltaGraph.triples((None,URIRef('http://test/pred1'),Literal(3)))),
"Missing inferred :pred1 assertions")
开发者ID:KiranAjayakumar,项目名称:python-dlp,代码行数:29,代码来源:BuiltinPredicates.py
示例14: routerEndpoints
def routerEndpoints():
# ideally this would all be in the same rdf store, with int and
# ext versions of urls
txt = open("/my/site/magma/tomato_config.js").read().replace('\n', '')
knownMacAddr = jsValue(txt, 'knownMacAddr')
tomatoUrl = jsValue(txt, 'tomatoUrl')
from rdflib.Graph import Graph
g = Graph()
g.parse("/my/proj/openid_proxy/access.n3", format="n3")
repl = {'/tomato1/' : None, '/tomato2/' : None}
for k in repl:
rows = list(g.query('''
PREFIX p: <http://bigasterisk.com/openid_proxy#>
SELECT ?prefix WHERE {
[
p:requestPrefix ?public;
p:proxyUrlPrefix ?prefix
]
}''', initBindings={Variable("public") : Literal(k)}))
repl[k] = str(rows[0][0])
routers = []
for url in tomatoUrl:
for k, v in repl.items():
url = url.replace(k, v)
routers.append(restkit.Resource(url, timeout=2))
return routers, knownMacAddr
开发者ID:drewp,项目名称:entrancemusic,代码行数:30,代码来源:entrancemusic.py
示例15: DBPediaAbstract
def DBPediaAbstract(uri):
from rdflib.Graph import Graph
g = Graph()
g.parse("http://bigasterisk.com/foaf.rdf")
import pdb
pdb.set_trace()
return uri
开发者ID:bh0085,项目名称:scatterbrainz,代码行数:7,代码来源:getDBRDF.py
示例16: createTestOntGraph
def createTestOntGraph():
graph = Graph()
graph.bind('ex',EX_NS,True)
Individual.factoryGraph = graph
kneeJoint = EX_CL.KneeJoint
joint = EX_CL.Joint
knee = EX_CL.Knee
isPartOf = Property(EX_NS.isPartOf)
graph.add((isPartOf.identifier,RDF.type,OWL_NS.TransitiveProperty))
structure = EX_CL.Structure
leg = EX_CL.Leg
hasLocation = Property(EX_NS.hasLocation,subPropertyOf=[isPartOf])
# graph.add((hasLocation.identifier,RDFS.subPropertyOf,isPartOf.identifier))
kneeJoint.equivalentClass = [joint & (isPartOf|some|knee)]
legStructure = EX_CL.LegStructure
legStructure.equivalentClass = [structure & (isPartOf|some|leg)]
structure += leg
structure += joint
locatedInLeg = hasLocation|some|leg
locatedInLeg += knee
# print graph.serialize(format='n3')
# newGraph = Graph()
# newGraph.bind('ex',EX_NS,True)
# newGraph,conceptMap = StructuralTransformation(graph,newGraph)
# revDict = dict([(v,k) for k,v in conceptMap.items()])
# Individual.factoryGraph = newGraph
# for oldConceptId ,newConceptId in conceptMap.items():
# if isinstance(oldConceptId,BNode):
# oldConceptRepr = repr(Class(oldConceptId,graph=graph))
# if oldConceptRepr.strip() == 'Some Class':
# oldConceptRepr = manchesterSyntax(
# oldConceptId,
# graph)
# print "%s -> %s"%(
# oldConceptRepr,
# newConceptId
# )
#
# else:
# print "%s -> %s"%(
# oldConceptId,
# newConceptId
# )
#
# for c in AllClasses(newGraph):
# if isinstance(c.identifier,BNode) and c.identifier in conceptMap.values():
# print "## %s ##"%c.identifier
# else:
# print "##" * 10
# print c.__repr__(True)
# print "################################"
return graph
开发者ID:Bazmundi,项目名称:fuxi,代码行数:59,代码来源:CompletionReasoning.py
示例17: getURILabel
def getURILabel(uri):
rdfs="http://www.w3.org/2000/01/rdf-schema#"
g = Graph()
g.parse(uri)
for s, p, o in g.triples((None,None,None)):
if re.search(re.compile(rdfs+".*label",re.I),p):
return o
return "No URILABEL Found"
开发者ID:bh0085,项目名称:scatterbrainz,代码行数:8,代码来源:my_rdf.py
示例18: loadAuthRec
def loadAuthRec(self, n3File):
"""Load a RDF graph with authority posts in n3-format"""
g = Graph()
n3File = Util.relpath(n3File)
g.load(n3File, format='n3')
d = {}
for uri, label in g.subject_objects(RDFS.label):
d[unicode(label)] = unicode(uri)
return d
开发者ID:Sup3rgnu,项目名称:lawParse,代码行数:9,代码来源:Source.py
示例19: commit
def commit(self):
"""
Commits changes to the remote graph and flushes local caches.
"""
self._connection.update(add=self._added, remove=self._removed)
self._rdfObjects = {}
self._graph = Graph()
self._added = Graph()
self._removed = Graph()
开发者ID:abhik1368,项目名称:study-semantic-web,代码行数:9,代码来源:rdfobject.py
示例20: makeOutputGraph
def makeOutputGraph():
graph = Graph()
graph.bind('pre', 'http://bigasterisk.com/pre/general/')
graph.bind('local', 'http://bigasterisk.com/pre/drew/') # todo
graph.bind('ad', 'http://bigasterisk.com/pre/general/accountDataType/')
graph.bind('mt', 'http://bigasterisk.com/pre/general/messageType/')
return graph
开发者ID:drewp,项目名称:palmpre2rdf,代码行数:7,代码来源:common.py
注:本文中的rdflib.Graph.Graph类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论