def get_supplier_solutions(): """Execute the equivalent of the following SPARQL query, querying the Turtle file supplier.ttl: SELECT ?suppkey, ?nationkey FROM supplier WHERE { ?supplier <tpch:suppkey> ?suppkey . ?supplier <tpch:nationkey> ?nationkey . } """ timer = FunctionTimer() short_prints = True suppliers = rdf.import_graph("supplier.ttl") timer.lap("suppliers", short=short_prints) # Find all triples that define a 'suppkey' (as predicate). bgp_suppkey_matches = clans.superstrict(suppliers, clans.from_dict({"p": rdflib.URIRef("tpch:suppkey")})) # Give the subject a name for later joining and object the name we need in the output. bgp_suppkey = clans.compose(bgp_suppkey_matches, clans.from_dict({"supplier": "s", "suppkey": "o"})) # Find all triples that define a 'nationkey' (as predicate) and give the subject a name for # later joining and object the name we need in the output. bgp_nationkey = clans.compose( clans.superstrict(suppliers, clans.from_dict({"p": rdflib.URIRef("tpch:nationkey")})), clans.from_dict({"supplier": "s", "nationkey": "o"}), ) # Join the previous results on 'supplier' and project the columns we need. supplier_solutions = clans.project(clans.functional_cross_union(bgp_suppkey, bgp_nationkey), "nationkey", "suppkey") timer.end("supplier_solutions", short=short_prints) return supplier_solutions
def test_import_graph(self): """Test importing clans from files and strings (function import_graph()).""" def check_graph(graph_mo, graph_ref): self.assertEqual(graph_mo.get_ground_set(), clans.get_absolute_ground_set()) self.assertEqual(graph_mo.get_left_set(), Set('s', 'p', 'o')) self.assertEqual(graph_mo, graph_ref) for graph_id in bg.keys(): graph_data = bg[graph_id] if 'file' in graph_data: graph = import_graph(graph_data['file']()) check_graph(graph, graph_data['mo']()) else: assert 'graph' in graph_data graph1 = import_graph(io.StringIO(graph_data['graph']())) check_graph(graph1, graph_data['mo']())
def test_triple_match(self): # Determine the number of types in the graph data graph_type_cnt = sample_graph.count('rdf:type') # Import the example graph as data algebra MathObject. graph = import_graph(io.StringIO(sample_graph), rdf_format='turtle') # Extract the triples that have a type and include their id triples_matched = triple_match(graph, '?id', URIRef('rdf:type'), '?type') # Verify that the number of sets matched equals the number of types in the graph self.assertEqual(len(triples_matched), graph_type_cnt)
def get_supplier_solutions(): """Execute the equivalent of the following SPARQL query, querying the Turtle file supplier.ttl: SELECT ?suppkey, ?nationkey FROM supplier WHERE { ?supplier <tpch:suppkey> ?suppkey . ?supplier <tpch:nationkey> ?nationkey . } """ timer = FunctionTimer() short_prints = True suppliers = rdf.import_graph('supplier.ttl') timer.lap('suppliers', short=short_prints) # Find all triples that define a 'suppkey' (as predicate). bgp_suppkey_matches = clans.superstrict( suppliers, clans.from_dict({'p': rdflib.URIRef('tpch:suppkey')})) # Give the subject a name for later joining and object the name we need in the output. bgp_suppkey = clans.compose( bgp_suppkey_matches, clans.from_dict({'supplier': 's', 'suppkey': 'o'})) # Find all triples that define a 'nationkey' (as predicate) and give the subject a name for # later joining and object the name we need in the output. bgp_nationkey = clans.compose( clans.superstrict(suppliers, clans.from_dict({'p': rdflib.URIRef('tpch:nationkey')})), clans.from_dict({'supplier': 's', 'nationkey': 'o'})) # Join the previous results on 'supplier' and project the columns we need. supplier_solutions = clans.project( clans.cross_functional_union(bgp_suppkey, bgp_nationkey), 'nationkey', 'suppkey') timer.end('supplier_solutions', short=short_prints) return supplier_solutions
from examples.sampleRdfGraph import sample_graph print_examples = True show_results_as_webpage = True # Print the input graph. if print_examples: print('Input graph:', sample_graph) # Data Algebra ------------------------------------------------------------------------------------- # Import the example graph as data algebra MathObject. graph_algebra = import_graph(io.StringIO(sample_graph), rdf_format='turtle') if print_examples: print('Input graph (as MathObject):', graph_algebra) # Query the MathObject graph, retrieving the id, name and favorite MathObject of all engineers who # have all three. start = time() engineers_algebra = join( triple_match(graph_algebra, '?eng', rdflib.URIRef('rdf:name'), '?name'), triple_match(graph_algebra, '?eng', rdflib.URIRef('rdf:type'), rdflib.URIRef('cat:engineer')), triple_match(graph_algebra, '?eng', rdflib.URIRef('fav:mathobject'), '?fav') ) elapsed_algebra = time() - start engineers_algebra_json = io.StringIO()
from examples.sampleRdfGraph import sample_graph print_examples = True show_results_as_webpage = True # Print the input graph. if print_examples: print('Input graph:', sample_graph) # Data Algebra ------------------------------------------------------------------------------------- # Import the example graph as data algebra MathObject. graph_algebra = import_graph(input_data=sample_graph, rdf_format='turtle') if print_examples: print('Input graph (as MathObject):', graph_algebra) # Query the MathObject graph, retrieving the id, name and favorite MathObject of all engineers who # have all three. start = time() engineers_algebra = join( triple_match(graph_algebra, '?eng', rdflib.URIRef('rdf:name'), '?name'), triple_match(graph_algebra, '?eng', rdflib.URIRef('rdf:type'), rdflib.URIRef('cat:engineer')), triple_match(graph_algebra, '?eng', rdflib.URIRef('fav:mathobject'), '?fav') ) elapsed_algebra = time() - start engineers_algebra_json = io.StringIO()
import io import rdflib import algebraixlib.algebras.clans as clans from algebraixlib.io.rdf import import_graph from algebraixlib.util.html import DataAlgebraHtmlDescriptor as HtmlDesc, math_object_as_html from algebraixlib.util.miscellaneous import open_webpage_from_html_str from algebraixlib.util.rdf import match_and_project from examples.sampleRdfGraph import sample_graph print_examples = True show_results_as_webpage = True # Import and print the input graph. graph_algebra = import_graph(io.StringIO(sample_graph), rdf_format='turtle') if print_examples: print('Input graph:', sample_graph) print('Input graph (as MathObject):', graph_algebra) # Query the imported graph using general pattern matching APIs. names = match_and_project(graph_algebra, {'p': rdflib.URIRef('rdf:name')}, { 's': '?eng', 'o': '?name' }) engineers = match_and_project(graph_algebra, { 'p': rdflib.URIRef('rdf:type'), 'o': rdflib.URIRef('cat:engineer') }, {'s': '?eng'}) engs_and_names = clans.cross_functional_union(names, engineers)