示例#1
0
    def test_graph_is_built_from_grakn_as_expected(self):

        g1 = nx.MultiDiGraph()
        g1.add_node('x')

        g2 = nx.MultiDiGraph()
        g2.add_node('x')
        g2.add_node('n')
        g2.add_edge('x', 'n', type='has')

        g3 = nx.MultiDiGraph()
        g3.add_node('x')
        g3.add_node('r')
        g3.add_node('y')
        g3.add_edge('r', 'x', type='child')
        g3.add_edge('r', 'y', type='parent')

        query_sampler_variable_graph_tuples = [('match $x isa person;', mock_sampler, g1),
                                               ('match $x isa person, has name $n;', mock_sampler, g2),
                                               ('match $x isa person; $r(child: $x, parent: $y);', mock_sampler, g3),
                                               # TODO Add functionality for loading schema at a later date
                                               # ('match $x sub person; $x sub $type;', g4),
                                               # ('match $x sub $y;', g5),
                                               ]

        with self._client.session(self._database, SessionType.DATA) as session:

            with session.transaction(TransactionType.WRITE) as tx:
                tx.query(ITBuildGraphFromQueriesWithRealGrakn.SCHEMA)
                tx.query(ITBuildGraphFromQueriesWithRealGrakn.DATA)
                tx.commit()

            with session.transaction(TransactionType.READ) as tx:
                combined_graph = build_graph_from_queries(query_sampler_variable_graph_tuples, tx)

                person_exp = build_thing(next(tx.query().match('match $x isa person;')).get('x'))
                name_exp = build_thing(next(tx.query().match('match $x isa name;')).get('x'))
                parentship_exp = build_thing(next(tx.query().match('match $x isa parentship;')).get('x'))

        expected_combined_graph = nx.MultiDiGraph()
        expected_combined_graph.add_node(person_exp, type='person')
        expected_combined_graph.add_node(name_exp, type='name', value_type='string', value='Bob')
        expected_combined_graph.add_node(parentship_exp, type='parentship')
        expected_combined_graph.add_edge(parentship_exp, person_exp, type='child')
        expected_combined_graph.add_edge(parentship_exp, person_exp, type='parent')
        expected_combined_graph.add_edge(person_exp, name_exp, type='has')

        self.assertGraphsEqual(expected_combined_graph, combined_graph)
示例#2
0
def concept_dict_from_concept_map(concept_map):
    """
    Given a concept map, build a dictionary of the variables present and the concepts they refer to, locally storing any
    information required about those concepts.
    :param concept_map: A dict of Concepts provided by Grakn keyed by query variables
    :return: A dictionary of concepts keyed by query variables
    """
    return {variable: build_thing(grakn_concept) for variable, grakn_concept in concept_map.map().items()}