def test_4variables(self): l1 = Variable('l1', list(range(10))) l2 = Variable('l2', list(range(10))) l3 = Variable('l3', list(range(10))) y1 = Variable('y1', list(range(10))) @AsNAryFunctionRelation(l1, l2, l3, y1) def scene_rel(l1_, l2_, l3_, y1_): if y1_ == round((l1_ + l2_ + l3_) / 3): return 0 return 10000 @AsNAryFunctionRelation(l3) def cost_l3(l3_): return l3_ self.assertEqual(scene_rel(9, 6, 0, 5), 0) self.assertEqual(scene_rel(3, 6, 0, 5), 10000) joined = dpop.join_utils(scene_rel, cost_l3) self.assertEqual(joined(9, 6, 0, 5), 0) self.assertEqual(joined(3, 6, 0, 5), 10000) util = dpop.projection(joined, l3, 'min')
def test_arity_bothsamevar(self): x1 = Variable('x1', ['a', 'b', 'c']) u1 = NAryMatrixRelation([x1]) u2 = NAryMatrixRelation([x1]) u_j = dpop.join_utils(u1, u2) self.assertEqual(u_j.arity, 1)
def test_arity_2diffvar(self): x1 = Variable('x1', ['a', 'b', 'c']) u1 = NAryMatrixRelation([x1]) x2 = Variable('x2', ['1', '2']) u2 = NAryMatrixRelation([x2]) u_j = dpop.join_utils(u1, u2) self.assertEqual(u_j.arity, 2)
def test_join_2diffvar(self): x1 = Variable('x1', ['a', 'b', 'c']) u1 = NAryMatrixRelation([x1], np.array([2, 4, 8], np.int8)) x2 = Variable('x2', ['1', '2']) u2 = NAryMatrixRelation([x2], np.array([1, 3], np.int8)) u_j = dpop.join_utils(u1, u2) self.assertEqual(u_j.arity, 2) self.assertEqual(u_j.get_value_for_assignment(['a', '1']), 3) self.assertEqual(u_j.get_value_for_assignment(['c', '2']), 11) self.assertEqual(u_j.get_value_for_assignment(['b', '1']), 5)
def test_join_bothsamevar(self): x1 = Variable('x1', ['a', 'b', 'c']) u1 = NAryMatrixRelation([x1], np.array([1, 2, 3], np.int8)) u2 = NAryMatrixRelation([x1], np.array([1, 2, 3], np.int8)) # x1 = Variable('x1', ['a', 'b', 'c']) # u1 = dpop.NAryRelation([x1], np.array([1, 2, 3], np.int8)) self.assertEqual(u1.get_value_for_assignment(['b']), 2) u_j = dpop.join_utils(u1, u2) self.assertEqual(u_j.arity, 1) self.assertEqual(u_j.get_value_for_assignment(['a']), 2) self.assertEqual(u_j.get_value_for_assignment(['b']), 4) self.assertEqual(u_j.get_value_for_assignment(['c']), 6)
def test_join_with_no_var_rel(self): # join a relation with a relation with no dimension x1 = Variable('x1', ['a', 'b', 'c']) x2 = Variable('x2', ['1', '2']) u1 = NAryMatrixRelation([x1, x2], np.array([[2, 16], [4, 32], [8, 64]], np.int8)) u2 = NAryMatrixRelation([]) u_j = dpop.join_utils(u1, u2) self.assertEqual(u_j.arity, 2) self.assertEqual(u_j.dimensions, [x1, x2]) self.assertEqual(u_j.get_value_for_assignment(['a', '1']), 2) self.assertEqual(u_j.get_value_for_assignment(['b', '2']), 32)
def test_join_3diffvar(self): x1 = Variable('x1', ['a', 'b', 'c']) x2 = Variable('x2', ['1', '2']) u1 = NAryMatrixRelation([x1, x2], np.array([[2, 16], [4, 32], [8, 64]], np.int8)) x3 = Variable('x3', ['z', 'y']) u2 = NAryMatrixRelation([x2, x3], np.array([[1, 5], [3, 7]], np.int8)) u_j = dpop.join_utils(u1, u2) self.assertEqual(u_j.arity, 3) self.assertEqual(u_j.dimensions, [x1, x2, x3]) self.assertEqual(u_j.get_value_for_assignment(['a', '1', 'z']), 3) self.assertEqual(u_j.get_value_for_assignment(['b', '2', 'y']), 39)
def test_join_different_order(self): # Test joining 2 relations that do not declare their variable in the # same order x1 = Variable('x1', [0, 1, 2]) x2 = Variable('x2', [0, 1, 2]) @AsNAryFunctionRelation(x1, x2) def u1(x, y): return x + y @AsNAryFunctionRelation(x2, x1) def u2(x, y): return x - y j = dpop.join_utils(u1, u2) self.assertEqual(j(1, 1), 2) self.assertEqual(j(1, 2), 4)