def test_build_context_for_1_hop(self): starting_thing = neighbour.Thing("0", "person", "entity") samplers = [samp.Sampler(2, ordered.ordered_sample, limit=2)] context_builder = builder.ContextBuilder( samplers, neighbour_finder=mocks.MockNeighbourFinder()) thing_context = context_builder.build( mock.Mock(grakn.client.Transaction), starting_thing) expected_context = { 0: [ builder.Node((0, ), neighbour.Thing("1", "name", "attribute", data_type='string', value='Sundar Pichai'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, ), neighbour.Thing("2", "employment", "relation"), "employee", neighbour.TARGET_PLAYS), ], 1: [builder.Node((), neighbour.Thing("0", "person", "entity"))], } self.assertEqual(expected_context, thing_context)
def test_build_context_for_0_hop(self): starting_thing = neighbour.Thing("0", "person", "entity") samplers = [] context_builder = builder.ContextBuilder(samplers, neighbour_finder=mocks.MockNeighbourFinder()) thing_context = context_builder.build(mock.Mock(grakn.client.Transaction), starting_thing) expected_context = { 0: [builder.Node((), neighbour.Thing("0", "person", "entity"))], } self.assertEqual(expected_context, thing_context)
def test_input_output(self): neighbour_sample_sizes = (2, 3) samplers = [lambda x: x for sample_size in neighbour_sample_sizes] starting_thing = neighbour.Thing("0", "person", "entity") context_builder = builder.ContextBuilder(samplers, neighbour_finder=mocks.MockNeighbourFinder()) thing_context = context_builder.build(self._tx, starting_thing) self.assertEqual(thing_context, mocks.mock_traversal_output())
def test_build_context_batch(self): batch = [neighbour.Thing("0", "person", "entity"), neighbour.Thing("0", "person", "entity")] samplers = [] context_builder = builder.ContextBuilder(samplers, neighbour_finder=mocks.MockNeighbourFinder()) thing_context = context_builder.build_batch(mock.Mock(grakn.client.Session), batch) expected_context_batch = [{ 0: [builder.Node((), neighbour.Thing("0", "person", "entity"))], }, { 0: [builder.Node((), neighbour.Thing("0", "person", "entity"))], } ] self.assertEqual(expected_context_batch, thing_context)
def setUp(self): self._tx = self.session.transaction(grakn.TxType.WRITE) neighbour_sample_sizes = (2, 3) samplers = [lambda x: x for sample_size in neighbour_sample_sizes] starting_thing = neighbour.Thing("0", "person", "entity") things = [starting_thing] context_builder = builder.ContextBuilder(samplers, neighbour_finder=mocks.MockNeighbourFinder()) self._neighbourhood_depths = [context_builder.build(self._tx, thing) for thing in things] self._neighbour_roles = builder.convert_thing_contexts_to_neighbours(self._neighbourhood_depths) self._flattened = flatten_tree(self._neighbour_roles)
def test_input_output_integration(self): """ Runs using real samplers :return: """ sampling_method = ordered.ordered_sample samplers = [samp.Sampler(2, sampling_method, limit=2), samp.Sampler(3, sampling_method, limit=1)] starting_thing = neighbour.Thing("0", "person", "entity") context_builder = builder.ContextBuilder(samplers, neighbour_finder=mocks.MockNeighbourFinder()) thing_context = context_builder.build(self._tx, starting_thing) self.assertEqual(thing_context, mocks.mock_traversal_output())
def test_build_context_for_3_hop(self): starting_thing = neighbour.Thing("0", "person", "entity") samplers = [samp.Sampler(2, ordered.ordered_sample, limit=2), samp.Sampler(2, ordered.ordered_sample, limit=2), samp.Sampler(3, ordered.ordered_sample, limit=3)] context_builder = builder.ContextBuilder(samplers, neighbour_finder=mocks.MockNeighbourFinder()) thing_context = context_builder.build(mock.Mock(grakn.client.Transaction), starting_thing) expected_context = { 3: [builder.Node((), neighbour.Thing("0", "person", "entity"))], 2: [builder.Node((0,), neighbour.Thing("1", "name", "attribute", data_type='string', value='Sundar Pichai'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((1,), neighbour.Thing("2", "employment", "relation"), "employee", neighbour.TARGET_PLAYS), ], 1: [builder.Node((0, 0), neighbour.Thing("0", "person", "entity"), "has", neighbour.TARGET_PLAYS), # Note that (0, 1) is reversed compared to the natural expectation builder.Node((0, 1), neighbour.Thing("3", "company", "entity"), "employer", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, 1), neighbour.Thing("0", "person", "entity"), "employee", neighbour.NEIGHBOUR_PLAYS), ], 0: [builder.Node((0, 0, 0), neighbour.Thing("1", "name", "attribute", data_type='string', value='Sundar Pichai'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, 0, 0), neighbour.Thing("2", "employment", "relation"), "employee", neighbour.TARGET_PLAYS), builder.Node((0, 0, 1), neighbour.Thing("4", "name", "attribute", data_type='string', value='Google'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, 0, 1), neighbour.Thing("4", "name", "attribute", data_type='string', value='Google'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((2, 0, 1), neighbour.Thing("4", "name", "attribute", data_type='string', value='Google'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((0, 1, 1), neighbour.Thing("1", "name", "attribute", data_type='string', value='Sundar Pichai'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, 1, 1), neighbour.Thing("2", "employment", "relation"), "employee", neighbour.TARGET_PLAYS), ] } self.assertEqual(expected_context, thing_context)