print(', '.join(engineers_rdflib.vars)) for solution in engineers_rdflib.bindings: print(', '.join([solution[var].n3() for var in engineers_rdflib.vars])) print('(end)') if print_examples: print('Engineers (SPARQL through rdflib, took {time:.3f} s): {result}'.format( time=elapsed_rdflib, result=engineers_rdflib.bindings)) # Present results ---------------------------------------------------------------------------------- if show_results_as_webpage: descriptors = [ HtmlDesc( 'Input Graph', graph_algebra, 'The RDF graph source:' + sample_graph ), HtmlDesc( 'Result', engineers_algebra, 'Equivalent to the SPARQL query:' + query_rdflib + '\nWith the JSON result\n\n' + engineers_algebra_json.getvalue() ) ] html = math_object_as_html('Example 1: Simple Query', descriptors) open_webpage_from_html_str(html)
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.functional_cross_union(names, engineers) if print_examples: print('Engineers and their names:', engs_and_names) # Present results. if show_results_as_webpage: descriptors = [ HtmlDesc('Input Graph', graph_algebra, 'The RDF graph source:' + sample_graph), HtmlDesc('Result', engs_and_names, 'Only engineers with names.') ] html = math_object_as_html('Simple Pattern Match Example', descriptors) open_webpage_from_html_str(html)
# 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) if print_examples: print('Engineers and their names:', engs_and_names) # Present results. if show_results_as_webpage: descriptors = [ HtmlDesc('Input Graph', graph_algebra, 'The RDF graph source:' + sample_graph), HtmlDesc('Result', engs_and_names, 'Only engineers with names.') ] html = math_object_as_html('Simple Pattern Match Example', descriptors) open_webpage_from_html_str(html)