示例#1
0
    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)
示例#2
0
    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)
示例#3
0
    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)
示例#4
0
def mock_context():
    return {
        2: [builder.Node((), neighbour.Thing("0", "person", "entity"))],
        1: [
            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),
        ],
        0: [
            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),
        ]
    }
示例#5
0
    def test_outcome_is_as_expected(self):
        context_batch = [{
            1: [builder.Node((), neighbour.Thing("0", "person", "entity"))],
            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),
            ]
        }]

        max_hops_shape = (1, 2, 1)

        filled_arrays = array.convert_context_batch_to_arrays(
            context_batch,
            max_hops_shape,
            role_type=(np.dtype('U50'), ''),
            role_direction=(np.int, -1),
            neighbour_type=(np.dtype('U50'), ''),
            neighbour_data_type=(np.dtype('U10'), ''),
            neighbour_value_long=(np.int, 0),
            neighbour_value_double=(np.float, 0.0),
            neighbour_value_boolean=(np.int, -1),
            neighbour_value_date=('datetime64[s]', ''),
            neighbour_value_string=(np.dtype('U50'), ''))

        expected_filled_arrays = [{
            'role_type':
            np.array([[['has'], ['employee']]], dtype=np.dtype('U50')),
            'role_direction':
            np.array([[[neighbour.NEIGHBOUR_PLAYS], [neighbour.TARGET_PLAYS]]],
                     dtype=np.int),
            'neighbour_type':
            np.array([[['name'], ['employment']]], dtype=np.dtype('U50')),
            'neighbour_data_type':
            np.array([[['string'], ['']]], dtype=np.dtype('U10')),
            'neighbour_value_long':
            np.array([[[0], [0]]], dtype=np.int),
            'neighbour_value_double':
            np.array([[[0.0], [0.0]]], dtype=np.int),
            'neighbour_value_boolean':
            np.array([[[-1], [-1]]], dtype=np.int),
            'neighbour_value_date':
            np.array([[[''], ['']]], dtype='datetime64[s]'),
            'neighbour_value_string':
            np.array([[['Sundar Pichai'], ['']]], dtype=np.dtype('U50'))
        }, {
            'neighbour_type':
            np.array([['person']], dtype=np.dtype('U50')),
            'neighbour_data_type':
            np.array([['']], dtype=np.dtype('U10')),
            'neighbour_value_long':
            np.array([[0]], dtype=np.int),
            'neighbour_value_double':
            np.array([[0.0]], dtype=np.int),
            'neighbour_value_boolean':
            np.array([[-1]], dtype=np.int),
            'neighbour_value_date':
            np.array([['']], dtype='datetime64[s]'),
            'neighbour_value_string':
            np.array([['']], dtype=np.dtype('U50'))
        }]

        np.testing.assert_equal(filled_arrays, expected_filled_arrays)
示例#6
0
    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)