本文整理汇总了Python中rdflib.graph.ConjunctiveGraph.parse方法的典型用法代码示例。如果您正苦于以下问题:Python ConjunctiveGraph.parse方法的具体用法?Python ConjunctiveGraph.parse怎么用?Python ConjunctiveGraph.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rdflib.graph.ConjunctiveGraph
的用法示例。
在下文中一共展示了ConjunctiveGraph.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_n3
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
def parse_n3(term_n3):
''' Disclaimer: Quick and dirty hack using the n3 parser. '''
prepstr = ("@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n"
"<urn:no_use> <urn:no_use> %s.\n" % term_n3)
g = ConjunctiveGraph()
g.parse(data=prepstr, format='n3')
return [t for t in g.triples((None, None, None))][0][2]
示例2: test_turtle_namespace_prefixes
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
def test_turtle_namespace_prefixes(self):
g = ConjunctiveGraph()
n3 = \
"""
@prefix _9: <http://data.linkedmdb.org/resource/movie/> .
@prefix p_9: <urn:test:> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
p_9:a p_9:b p_9:c .
<http://data.linkedmdb.org/resource/director/1> a
<http://data.linkedmdb.org/resource/movie/director>;
rdfs:label "Cecil B. DeMille (Director)";
_9:director_name "Cecil B. DeMille" ."""
g.parse(data=n3, format='n3')
turtle = g.serialize(format="turtle")
# Check round-tripping, just for kicks.
g = ConjunctiveGraph()
g.parse(data=turtle, format='turtle')
# Shouldn't have got to here
s = g.serialize(format="turtle")
self.assertTrue(b('@prefix _9') not in s)
示例3: main
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
def main():
parser = argparse.ArgumentParser(
description='OMIA integration test',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument(
'--input', '-i', type=str, required=True,
help='Location of input ttl file')
args = parser.parse_args()
graph = ConjunctiveGraph()
graph.parse(args.input, format=rdflib_util.guess_format(args.input))
model_of = URIRef('http://purl.obolibrary.org/obo/RO_0003301')
models = graph.subject_objects(model_of)
model_len = len(list(models))
if model_len < EXPECTED_PAIRS:
logger.error("Not enough model_of predicates in graph:"
" {} expected {} check omia log for"
" warnings".format(model_len, EXPECTED_PAIRS))
exit(1)
omim_diseases = graph.objects(
subject=URIRef('https://monarchinitiative.org/model/OMIA-breed:18'),
predicate=model_of
)
if list(omim_diseases) != [URIRef('http://purl.obolibrary.org/obo/OMIM_275220')]:
logger.error("Missing breed to omim triple for {}".format('OMIA-breed:18'))
exit(1)
logger.info("PASSED")
示例4: has_correct_hash
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
def has_correct_hash(self, resource):
f = RdfUtils.get_format(resource.get_filename())
cg = ConjunctiveGraph()
cg.parse(data=resource.get_content(), format=f)
quads = RdfUtils.get_quads(cg)
h = RdfHasher.make_hash(quads, resource.get_hashstr())
return resource.get_hashstr() == h
示例5: TestSearchAnnotations
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
class TestSearchAnnotations(unittest.TestCase):
def test_search_for_uri(self):
for url in annotation_urls:
g, target, selector = specific_resource(self.canvas,
res=URIRef(uuid.uuid4()),
selector=URIRef(uuid.uuid4()))
g, anno, body, target = annotation(g=g,
anno=URIRef(uuid.uuid4()),
target=target,
body=URIRef(uuid.uuid4()))
response = self.client.post(url, data=g.serialize(),
content_type="text/xml")
self.assertEqual(response.status_code, 201)
for uri in [anno, body, target, selector, self.canvas]:
response = self.client.get(url, {'uri': uri})
self.assertEqual(response.status_code, 200)
validate_return_content(self, response, g)
def tearDown(self):
pass
def setUp(self):
url = reverse('semantic_store_annotations', kwargs=dict())
self.client = Client()
fixture_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
"semantic_store_test_fixture.xml")
self.g = ConjunctiveGraph(rdfstore.rdfstore(),
identifier=rdfstore.default_identifier)
self.g.parse(fixture_filename)
canvases = self.g.subjects(URIRef(NS.rdf['type']), URIRef(NS.dms['Canvas']))
self.canvas = list(canvases)[0]
示例6: testParse
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
def testParse(self):
g = ConjunctiveGraph()
try:
g.parse("http://groups.csail.mit.edu/dig/2005/09/rein/examples/troop42-policy.n3", format="n3")
except URLError:
from nose import SkipTest
raise SkipTest('No network to retrieve the information, skipping test')
示例7: Query
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
class Query(unittest.TestCase):
def setUp(self):
self.graph = ConjunctiveGraph()
self.graph.parse(StringIO(test_data), format="n3")
def test1(self):
r=list(self.graph.query(test_query1))
self.assertEqual(len(r), 1)
def test2(self):
r=list(self.graph.query(test_query2))
self.assertEqual(len(r), 1)
def test3(self):
r=list(self.graph.query(test_query3))
self.assertEqual(len(r), 1)
def test4(self):
r=list(self.graph.query(test_query4))
self.assertEqual(len(r), 1)
def test5(self):
r=list(self.graph.query(test_query5))
self.assertEqual(len(r), 0)
示例8: get_sparql
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
def get_sparql(self, current_ontology=None, destination_ontology=None,
current_version=None, destination_version=None,
origen=None, insert=None):
""" Make sparql statements to be executed """
query_up = ""
query_down = ""
if insert is None:
current_graph = ConjunctiveGraph()
destination_graph = ConjunctiveGraph()
#if insert is None:
try:
if current_ontology is not None:
current_graph.parse(data=current_ontology, format='turtle')
destination_graph.parse(data=destination_ontology,
format='turtle')
except BadSyntax, e:
e._str = e._str.decode('utf-8')
raise MigrationException("Error parsing graph %s" % unicode(e))
forward_migration, backward_migration = (
self._generate_migration_sparql_commands(
destination_graph,
current_graph,
self.__virtuoso_graph))
query_up += forward_migration
query_down += backward_migration
forward_migration, backward_migration = (
self._generate_migration_sparql_commands(
current_graph,
destination_graph,
self.__virtuoso_graph))
query_down += forward_migration
query_up += backward_migration
示例9: test_url
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
def test_url(self):
if self.html5lib_installed():
g = ConjunctiveGraph()
g.parse(location='http://oreilly.com/catalog/9780596516499/',
format='rdfa',
lax=True)
self.assertTrue(len(g) > 0)
示例10: test_file
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
def test_file(self):
if self.html5lib_installed():
g = ConjunctiveGraph()
g.parse(location='test/rdfa/oreilly.html',
format='rdfa',
lax=True)
self.assertEqual(len(g), 77)
示例11: TestSparqlJsonResults
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
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 = json.loads(results.serialize(format='json'))
msg = "Expected:\n %s \n- to contain:\n%s" % (result_json, correct)
self.failUnless(result_json["head"]==correct["head"], msg)
result_bindings = sorted(result_json["results"]["bindings"])
correct_bindings = sorted(correct["results"]["bindings"])
msg = "Expected:\n %s \n- to contain:\n%s" % (result_bindings, correct_bindings)
self.failUnless(result_bindings==correct_bindings, msg)
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')
示例12: test4_DAWG_DATASET_COMPLIANCE_is_True
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
def test4_DAWG_DATASET_COMPLIANCE_is_True(self):
raise SkipTest("known DAWG_DATATSET_COMPLIANCE SPARQL issue")
graph = Graph()
graph.parse(data=test4data, format='n3')
res = graph.query(test4query, dSCompliance=True)
# print("json", res.serialize(format='json'))
assert len(res) == 2
示例13: TestIssue06
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
class TestIssue06(unittest.TestCase):
debug = False
sparql = True
def setUp(self):
self.graph = ConjunctiveGraph()
self.graph.parse(data=testgraph, publicID="testgraph")
def test_issue_6(self):
query = """
PREFIX ex: <http://temp.example.org/terms/>
PREFIX loc: <http://simile.mit.edu/2005/05/ontologies/location#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT *
WHERE {
{?event ex:date ?date .
FILTER (xsd:date(?date) >= xsd:date("2007-12-31") && xsd:date(?date) <= xsd:date("2008-01-11"))}
UNION
{?event ex:starts ?start; ex:finishes ?end .
FILTER (xsd:date(?start) >= xsd:date("2008-01-02") && xsd:date(?end) <= xsd:date("2008-01-10"))}
}
ORDER BY ?event
"""
self.graph.query(query, DEBUG=False)
示例14: TestSparqlJsonResults
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
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 = json.loads(results.serialize(format='json').decode('utf-8'))
msg = "Expected:\n %s \n- to contain:\n%s" % (result_json, correct)
self.assertEqual(sorted(result_json["head"], key=repr),
sorted(correct["head"], key=repr), msg)
# Sort by repr - rather a hack, but currently the best way I can think
# of to ensure the results are in the same order.
result_bindings = sorted(result_json["results"]["bindings"], key=repr)
correct_bindings = sorted(correct["results"]["bindings"], key=repr)
msg = "Expected:\n %s \n- to contain:\n%s" % (result_bindings, correct_bindings)
self.failUnless(result_bindings==correct_bindings, msg)
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')
示例15: parse_and_serialize
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import parse [as 别名]
def parse_and_serialize(input_files, input_format, guess,
outfile, output_format, ns_bindings,
store_conn="", store_type=None):
if store_type:
store = plugin.get(store_type, Store)()
store.open(store_conn)
graph = ConjunctiveGraph(store)
else:
store = None
graph = ConjunctiveGraph()
for prefix, uri in list(ns_bindings.items()):
graph.namespace_manager.bind(prefix, uri, override=False)
for fpath in input_files:
use_format, kws = _format_and_kws(input_format)
if fpath == '-':
fpath = sys.stdin
elif not input_format and guess:
use_format = guess_format(fpath) or DEFAULT_INPUT_FORMAT
graph.parse(fpath, format=use_format, **kws)
if outfile:
output_format, kws = _format_and_kws(output_format)
kws.setdefault('base', None)
graph.serialize(destination=outfile, format=output_format, **kws)
if store:
store.rollback()