Example #1
0
    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)
Example #2
0
    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)
    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()
export_table(
    file_or_path=engineers_algebra_json,
    lefts=Set(['?eng', '?name', '?fav']),
    table=engineers_algebra,
    out_format='json')

if print_examples:
    print('Engineers (data algebra API, took {time:.3f} s): {result}'.format(
        time=elapsed_algebra, result=engineers_algebra))
Example #4
0
 def test_triple_match_assert(self):
     self.assertRaises(AssertionError, lambda: triple_match(Atom(1)))
Example #5
0
 def test_triple_match_assert(self):
     self.assertRaises(AssertionError, lambda: triple_match(Atom(1)))
Example #6
0
    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()
export_table(
    file_or_path=engineers_algebra_json,
    lefts=Set(['?eng', '?name', '?fav']),
    table=engineers_algebra,
    out_format='json')

if print_examples:
    print('Engineers (data algebra API, took {time:.3f} s): {result}'.format(
        time=elapsed_algebra, result=engineers_algebra))