def test___le__(): """Test total ordering.""" a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr_empty = AttributeStructure() astr_a = AttributeStructure(a) astr_a_R1 = AttributeStructure(a, R1) astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) # Test subsets and strict subsets assert not astr_empty < astr_empty assert astr_empty <= astr_empty assert astr_empty < astr_a_b_R1_R2 assert astr_empty <= astr_a_b_R1_R2 assert astr_a < astr_a_b_R1_R2 assert astr_a <= astr_a_b_R1_R2 assert astr_a_R1 < astr_a_b_R1_R2 assert astr_a_R1 <= astr_a_b_R1_R2 assert not astr_a_b_R1_R2 < astr_a_b_R1_R2 assert astr_a_b_R1_R2 <= astr_a_b_R1_R2 # Test supersets and strict supersets assert not astr_empty > astr_empty assert astr_empty >= astr_empty assert astr_a_b_R1_R2 > astr_empty assert astr_a_b_R1_R2 >= astr_empty assert astr_a_b_R1_R2 > astr_a assert astr_a_b_R1_R2 >= astr_a assert astr_a_b_R1_R2 > astr_a_R1 assert astr_a_b_R1_R2 >= astr_a_R1 assert not astr_a_b_R1_R2 > astr_a_b_R1_R2 assert astr_a_b_R1_R2 >= astr_a_b_R1_R2
def test___getitem__(): """Test indexing of AttributeStructure.""" def test_TypeError(astr, index): """Test indexing for TypeErrors with given params.""" with pytest.raises(TypeError) as excinfo: astr[index] def test_KeyError(astr, index): """Test indexing for ValueErrors with given params.""" with pytest.raises(KeyError) as excinfo: astr[index] a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(b) <=> ", ["b"], 2) astr = AttributeStructure(a, R1) assert astr[a] == a assert astr[R1] == R1 assert astr["a"] == a assert astr["R1"] == R1 assert astr[1] == R1 test_TypeError(astr, None) test_TypeError(astr, 1.0) test_TypeError(astr, []) test_TypeError(astr, object) test_KeyError(astr, "") test_KeyError(astr, 3) test_KeyError(astr, b) test_KeyError(astr, R2)
def test___eq__(): """Test == operator.""" r1 = Relation("R1(a) <=> ", ["a"], 1) r2 = Relation("R1(a) <=> ", ["a"], 1) # only the same Relation is equal to itself assert r1 == r1 assert r1 == r2
def test___eq__(): """Test == operator for AttributeSystem.""" a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr_empty = AttributeStructure() astr_a_b = AttributeStructure(a, b) astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) objs_empty = [] objs = ['a', 'b', 'c'] asys = AttributeSystem(astr_empty, objs_empty) asys_o = AttributeSystem(astr_empty, objs) asys_o_copy = AttributeSystem(astr_empty, objs) asys_a_b = AttributeSystem(astr_a_b, objs_empty) asys_a_b_copy = AttributeSystem(astr_a_b, objs_empty) asys_a_b_R1_R2 = AttributeSystem(astr_a_b_R1_R2, objs_empty) asys_a_b_R1_R2_copy = AttributeSystem(astr_a_b_R1_R2, objs_empty) asys_a_b_R1_R2_o = AttributeSystem(astr_a_b_R1_R2, objs) asys_a_b_R1_R2_o_copy = AttributeSystem(astr_a_b_R1_R2, objs) # test identity assert asys == asys # components tests assert asys_o == asys_o_copy assert asys_a_b == asys_a_b_copy assert asys_a_b_R1_R2 == asys_a_b_R1_R2_copy assert asys_a_b_R1_R2_o == asys_a_b_R1_R2_o_copy
def test___init__(): """Test AttributeStructure construction.""" def test_TypeError(*ops): """Test constructor for TypeErrors with given params.""" with pytest.raises(TypeError) as excinfo: AttributeStructure(*ops) def test_ValueError(*ops): """Test constructor for TypeErrors with given params.""" with pytest.raises(ValueError) as excinfo: AttributeStructure(*ops) # Test bad optional param test_TypeError("a") test_TypeError(object) a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a1"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) R3 = Relation("R3(a) <=> ", ["a"], 3) # Test no duplicate labels test_ValueError(a, a) # Test no duplicate subscripts test_ValueError(R1, R1) # Test D(R) subset of labels test_ValueError(a, R1) # Test out of order construction AttributeStructure(R2, R3, b, a)
def test___getitem__(): """Test indexing of AttributeSystem.""" a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) objs = ['a', 'b', 'c'] asys_a_b_R1_R2_o = AttributeSystem(astr_a_b_R1_R2, objs) ia = asys_a_b_R1_R2_o[a] assert ia == a assert ia is asys_a_b_R1_R2_o._attribute_structure["a"] iR1 = asys_a_b_R1_R2_o[R1] assert iR1 == R1 assert iR1 is asys_a_b_R1_R2_o._attribute_structure["R1"] # Test object retrieval with string. Note, strings may not index # AttributeStructure members within this AttributeSystem, only objects. # To index objects in AttributeStructure in AttributeSystem, use index # with asys.astr[index] iobj = asys_a_b_R1_R2_o['a'] assert iobj == objs[0] assert iobj is objs[0] # Test retrieval by string for AttributeStructure members # AttributeStructure is cloned so the retrieved objects is not the same # as the one's used to contruct original instance ias = asys_a_b_R1_R2_o._attribute_structure['a'] assert ias == a assert ias is not a iR1s = asys_a_b_R1_R2_o._attribute_structure['R1'] assert iR1s == R1 assert iR1s is not R1
def test___isub__(): """Test -= operator for AttributeSystem.""" a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr_empty = AttributeStructure() astr_a = AttributeStructure(a) astr_a_b = AttributeStructure(a, b) astr_a_R1 = AttributeStructure(a, R1) astr_a_b_R1 = AttributeStructure(a, b, R1) astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) objs = ['a', 'b', 'c'] asys = AttributeSystem(astr_empty, []) asys_a = AttributeSystem(astr_a, []) asys_a_b = AttributeSystem(astr_a_b, []) asys_a_R1 = AttributeSystem(astr_a_R1, []) asys_a_b_R1 = AttributeSystem(astr_a_b_R1, []) asys_a_b_R1_R2 = AttributeSystem(astr_a_b_R1_R2, []) asys_a_b_R1_R2 -= R2 assert asys_a_b_R1_R2 == asys_a_b_R1 assert asys_a_b_R1_R2 is not asys_a_b_R1 asys_a_b_R1_R2 -= R1 assert asys_a_b_R1_R2 == asys_a_b assert asys_a_b_R1_R2 is not asys_a_b asys_a_b_R1_R2 -= b assert asys_a_b_R1_R2 == asys_a assert asys_a_b_R1_R2 is not asys_a asys_a_b_R1_R2 -= a assert asys_a_b_R1_R2 == asys assert asys_a_b_R1_R2 is not asys
def test___repr__(): """Test repr(AttributeInterpretation).""" a = Attribute('hour', ['0,...,23']) a2 = Attribute('minute', ['0,...,59']) r_ahead = Relation('R1(h1,m1,h2,m2) <=> h1 > h2 or (h1 = h2 and m1 > m2)', ['hour', 'minute', 'hour', 'minute'], 1) r_behind = Relation('R2(h1,m1,h2,m2) <=> h1 < h2 or (h1 = h2 and m1 < m2)', ['hour', 'minute', 'hour', 'minute'], 2) r_pm = Relation('R3(h1) <=> h1 > 12', ['hour'], 3) r_am = Relation('R4(h1) <=> h1 < 12', ['hour'], 4) attribute_structure = AttributeStructure( a, a2, r_ahead, r_behind, r_pm, r_am) ahead_rs = RelationSymbol('Ahead', 4) behind_rs = RelationSymbol('Behind', 4) pm_rs = RelationSymbol('PM', 1) vocabulary = Vocabulary( ['C1', 'C2'], [ahead_rs, behind_rs, pm_rs], ['V1', 'V2']) profiles = [ [ahead_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)], [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)], [pm_rs, ('hour', 1)] ] mapping = {ahead_rs: 1, behind_rs: 2, pm_rs: 3} ai = AttributeInterpretation( vocabulary, attribute_structure, mapping, profiles) assert repr(ai) == "[Ahead, 4, 'R1', [('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)]]\n" + \ "[Behind, 4, 'R2', [('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)]]\n" + \ "[PM, 1, 'R3', [('hour', 1)]]"
def test_get_subscripts(): """Test retrieval of subscripts from Relation's in AttributeStructure.""" a = Attribute("a", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["a"], 2) R3 = Relation("R3(a) <=> ", ["a"], 3) R4 = Relation("R4(a) <=> ", ["a"], 4) astr = AttributeStructure(a, R1, R2, R3, R4) assert astr.get_subscripts() == [1, 2, 3, 4]
def test___ne__(): """Test != operator.""" r1 = Relation("R1(a) <=> ", ["a"], 1) # different definitions test r2 = Relation("R2(a) <=> ", ["a"], 2) # different D(R)'s test r3 = Relation("R1(a) <=> ", ["b"], 1) assert r1 != r2 assert r1 != r3
def test___str__(): """Test str(Relation).""" R1 = Relation( 'R1(p1,l1) <=> p1 is_on_line l1', ['position', 'line_positions'], 1) R1_str = "R1 is a subset of position X line_positions, " R1_str += "defined as follows: R1(p1,l1) <=> p1 is_on_line l1" assert str(R1) == R1_str R2 = Relation("R2(a) <=> ", ["a1"], 2) assert str(R2) == "R2 is a subset of a1, defined as follows: R2(a) <=> "
def test___repr__(): """Test repr(AttributeStructure).""" a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr = AttributeStructure(a, b, R1, R2) astr2 = AttributeStructure(R2, R1, b, a) assert str(astr) == "(a: {}, b: {} ; R1,R2)" assert str(astr2) == "(a: {}, b: {} ; R1,R2)"
def test___repr__(): """Test repr().""" a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) objs = ['a', 'b', 'c'] asys_a_b_R1_R2_o = AttributeSystem(astr_a_b_R1_R2, objs) assert repr(asys_a_b_R1_R2_o) == "({a, b, c} ; (a: {}, b: {} ; R1,R2))" assert repr(AttributeSystem(AttributeStructure(), [])) == "({} ; ( ; ))"
def test___isub__(): """Test -= operator for AttributeStructure.""" def test_ValueError(astr, sub): """Test constructor for TypeErrors with given params.""" with pytest.raises(ValueError) as excinfo: astr -= sub def test_KeyError(astr, sub): """Test constructor for TypeErrors with given params.""" with pytest.raises(KeyError) as excinfo: astr -= sub def test_TypeError(astr, sub): """Test constructor for TypeErrors with given params.""" with pytest.raises(TypeError) as excinfo: astr -= sub a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr_empty = AttributeStructure() astr_a = AttributeStructure(a) astr_a_R1 = AttributeStructure(a, R1) astr_a_b = AttributeStructure(a, b) astr_a_b_R1 = AttributeStructure(a, b, R1) astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) # Test invalid subtraction error catching test_KeyError(astr_empty, a) test_KeyError(astr_empty, R1) test_TypeError(astr_empty, None) test_TypeError(astr_empty, "") test_TypeError(astr_empty, object) test_ValueError(astr_empty, astr_a) test_ValueError(astr_empty, astr_a_R1) astr_a_copy = AttributeStructure(a) test_ValueError(astr_a, astr_a_R1) assert astr_a_copy == astr_a # Test attribute removal astr_a_b_R1_R2 -= R2 assert astr_a_b_R1 == astr_a_b_R1_R2 astr_a_b_R1_R2 -= R1 assert astr_a_b == astr_a_b_R1_R2 astr_a_b_R1_R2 -= b assert astr_a == astr_a_b_R1_R2 astr_a_b_R1_R2 -= a assert astr_empty == astr_a_b_R1_R2
def test___deepcopy__(): """Test copy.deepcopy for AttributeStructure object.""" import copy a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) astr_copy = copy.deepcopy(astr_a_b_R1_R2) assert astr_copy == astr_a_b_R1_R2 assert astr_copy._attributes is not astr_a_b_R1_R2._attributes assert astr_copy._relations is not astr_a_b_R1_R2._relations assert astr_copy is not astr_a_b_R1_R2
def test___add__(): """Test + operator.""" from vivid.classes.relation import Relation from vivid.classes.attribute_structure import AttributeStructure a1 = Attribute("a1", []) a2 = Attribute("a2", []) r1 = Relation("R1(a) <=> ", ["a1"], 1) astr = AttributeStructure() astr_a1 = AttributeStructure(a1) astr_a1_a2 = AttributeStructure(a1, a2) astr_a1_r1 = AttributeStructure(a1, r1) astr_a1_a2_r1 = AttributeStructure(a1, a2, r1) # add AttributeStructure and Attribute assert astr_a1 == astr + a1 assert astr_a1 == a1 + astr # add Attribute and Attribute assert astr_a1_a2 == a1 + a2 assert astr_a1_a2 == a2 + a1 # add Attribute and Relation assert astr_a1_r1 == a1 + r1 assert astr_a1_r1 == r1 + a1 assert astr_a1_a2_r1 == a1 + a2 + r1 assert astr_a1_a2_r1 == a2 + a1 + r1 assert astr_a1_a2_r1 == r1 + a1 + a2 # Test for when + fails, i.e. when Relation is added before attributes with # its D(R) labels. with pytest.raises(ValueError) as excinfo: r1 + a2 + a1
def test_is_automorphic(): """Test if system is automorphic.""" a = Attribute("a", []) b = Attribute("b", []) c = Attribute("c", ['a']) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr_c = AttributeStructure(c) astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) objs = ['a', 'b', 'c'] asys_a_b_R1_R2_o = AttributeSystem(astr_a_b_R1_R2, objs) asys_auto = AttributeSystem(astr_c, objs) assert asys_auto.is_automorphic() assert not asys_a_b_R1_R2_o.is_automorphic()
def test___ne__(): """!= operator for AttributeStructure.""" a1 = Attribute("a1", []) a2 = Attribute("a2", []) R1 = Relation("R1(a) <=> ", ["a1"], 1) R2 = Relation("R2(a) <=> ", ["a1"], 2) astr1 = AttributeStructure(a1, a2, R1, R2) astr2 = AttributeStructure(a1, R2) astr3 = AttributeStructure() # strict subset-superset tests assert astr1 != astr2 assert astr2 != astr1 # empty comparison test assert astr1 != astr3 assert astr3 != astr1
def test___deepcopy__(): """.""" """Test copy.deepcopy functionality of Attribute object.""" from copy import deepcopy r = Relation("R1(a) <=> ", ["a1"], 1) r_copy = deepcopy(r) assert r == r_copy assert r is not r_copy
def test___deepcopy__(): """Test copy.deepcopy for AttributeSystem.""" a = Attribute("a", []) b = Attribute("b", []) c = Attribute("c", ['a']) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) objs = ['a', 'b', 'c'] astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) asys_a_b_R1_R2_o = AttributeSystem(astr_a_b_R1_R2, objs) import copy asys_copy = copy.deepcopy(asys_a_b_R1_R2_o) assert asys_copy == asys_a_b_R1_R2_o assert asys_copy is not asys_a_b_R1_R2_o assert asys_copy._attribute_structure is not \ asys_a_b_R1_R2_o._attribute_structure assert asys_copy._objects is not asys_a_b_R1_R2_o._objects
def test___iadd__(): """Test += operator for AttributeStructure.""" a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr_empty = AttributeStructure() astr_a = AttributeStructure(a) astr_a_b = AttributeStructure(a, b) astr_a_b_R1 = AttributeStructure(a, b, R1) astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) astr_empty += a assert astr_a == astr_empty astr_empty += b assert astr_a_b == astr_empty astr_empty += R1 assert astr_a_b_R1 == astr_empty astr_empty += R2 assert astr_a_b_R1_R2 == astr_empty
def test_get_power(): """Test get_power(); power = n * |A|.""" a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr_a_b = AttributeStructure(a, b) astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) objs = ['a', 'b', 'c'] asys_a_b_o = AttributeSystem(astr_a_b, objs) asys_a_b_R1_R2_o = AttributeSystem(astr_a_b_R1_R2, objs) # Test when power = 0 assert AttributeSystem(AttributeStructure(), []).get_power() == 0 assert AttributeSystem(AttributeStructure(a), []).get_power() == 0 assert AttributeSystem(AttributeStructure(), ['o1']).get_power() == 0 # test normal power calculation assert asys_a_b_o.get_power() == 6 assert asys_a_b_R1_R2_o.get_power() == 6
def test___repr__(): """Test str(Context).""" ahead_rs = RelationSymbol('Ahead', 4) behind_rs = RelationSymbol('Behind', 4) pm_rs = RelationSymbol('PM', 1) am_rs = RelationSymbol('AM', 1) vocabulary = Vocabulary(['C1', 'C2'], [ahead_rs, behind_rs, pm_rs, am_rs], ['V1', 'V2']) f = Formula(vocabulary, 'Ahead', 'C1', 'V1') a = Attribute('hour', [Interval(0, 23)]) a2 = Attribute('minute', [Interval(0, 59)]) r_pm = Relation('R1(h1) <=> h1 > 11', ['hour'], 1) r_am = Relation('R2(h1) <=> h1 <= 11', ['hour'], 2) r_ahead = Relation( 'R3(h1,m1,hhh2,mm2) <=> h1 > hhh2 or (h1 = hhh2 and m1 > mm2)', ['hour', 'minute', 'hour', 'minute'], 3) r_behind = Relation( 'R4(h1,m1,h2,m2) <=> h1 <= h2 or (h1 = h2 and m1 < m2)', ['hour', 'minute', 'hour', 'minute'], 4) attribute_structure = AttributeStructure(a, a2, r_ahead, r_behind, r_pm, r_am) objects = ['s1', 's2'] attribute_system = AttributeSystem(attribute_structure, objects) p = ConstantAssignment(vocabulary, attribute_system, { 'C1': 's1', 'C2': 's2' }) assumption_base = AssumptionBase(f) named_state = NamedState(attribute_system, p) C = Context(assumption_base, named_state) assert repr(C) == "hour(s1): {V(I(0, 23))}\n" + \ "hour(s2): {V(I(0, 23))}\n" + \ "minute(s1): {V(I(0, 59))}\n" + \ "minute(s2): {V(I(0, 59))}\n" + \ "CA{'C2': 's2', 'C1': 's1'}\n" + \ "AB(Ahead(C1, V1))"
def test___eq__(): """Test == operator for AttributeStructure.""" a1 = Attribute("a1", []) a2 = Attribute("a2", []) R1 = Relation("R1(a) <=> ", ["a1"], 1) R2 = Relation("R2(a) <=> ", ["a1"], 2) astr1 = AttributeStructure(a1, a2, R1, R2) astr2 = AttributeStructure(a1, a2, R1, R2) astr3 = AttributeStructure(R2, R1, a2, a1) astr4 = AttributeStructure() astr5 = AttributeStructure() # Test identity assert astr1 == astr1 # Test regular equality assert astr1 == astr2 # Test out of order constuctions assert astr1 == astr3 # Test empty equality assert astr4 == astr5
def test___add__(): """Test + operator for AttributeSystem.""" a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr_empty = AttributeStructure() astr_a = AttributeStructure(a) astr_a_b = AttributeStructure(a, b) astr_a_R1 = AttributeStructure(a, R1) astr_a_b_R1 = AttributeStructure(a, b, R1) astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) objs = ['a', 'b', 'c'] asys = AttributeSystem(astr_empty, []) asys_a = AttributeSystem(astr_a, []) asys_a_b = AttributeSystem(astr_a_b, []) asys_a_R1 = AttributeSystem(astr_a_R1, []) asys_a_b_R1 = AttributeSystem(astr_a_b_R1, []) asys_a_b_R1_R2 = AttributeSystem(astr_a_b_R1_R2, []) assert asys_a == asys + a # ensure no implicit type conversion happens assert not asys_a == asys with pytest.raises(AttributeError) as excinfo: asys_a == a assert asys_a_b == asys + a + b assert asys_a_b == a + asys + b assert asys_a_b == a + b + asys assert asys_a_b_R1_R2 == a + b + asys + R1 + R2 assert asys_a_R1 == R1 + asys_a assert asys_a_b_R1 == R1 + asys_a + b assert asys_a_b_R1 == asys + astr_a_b_R1 assert asys_a_b_R1_R2 == asys + astr_a_b_R1 + R2 assert asys_a_b_R1_R2 == asys + astr_a_b + R1 + R2 assert asys_a_b_R1_R2 == asys + astr_a + b + R1 + R2 assert asys_a_b_R1_R2 == asys + b + astr_a + R1 + R2
def test___deepcopy__(): """Test copy.deepcopy for AttributeInterpretation object.""" a = Attribute('hour', ['0,...,23']) a2 = Attribute('minute', ['0,...,59']) r_ahead = Relation('R1(h1,m1,h2,m2) <=> h1 > h2 or (h1 = h2 and m1 > m2)', ['hour', 'minute', 'hour', 'minute'], 1) r_behind = Relation('R2(h1,m1,h2,m2) <=> h1 < h2 or (h1 = h2 and m1 < m2)', ['hour', 'minute', 'hour', 'minute'], 2) r_pm = Relation('R3(h1) <=> h1 > 12', ['hour'], 3) r_am = Relation('R4(h1) <=> h1 < 12', ['hour'], 4) attribute_structure = AttributeStructure( a, a2, r_ahead, r_behind, r_pm, r_am) ahead_rs = RelationSymbol('Ahead', 4) behind_rs = RelationSymbol('Behind', 4) pm_rs = RelationSymbol('PM', 1) vocabulary = Vocabulary( ['C1', 'C2'], [ahead_rs, behind_rs, pm_rs], ['V1', 'V2']) profiles = [ [ahead_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)], [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)], [pm_rs, ('hour', 1)]] mapping = {ahead_rs: 1, behind_rs: 2, pm_rs: 3} ai = AttributeInterpretation( vocabulary, attribute_structure, mapping, profiles) from copy import deepcopy ai_copy = deepcopy(ai) assert ai == ai_copy assert ai is not ai_copy assert ai._vocabulary is ai_copy._vocabulary assert ai._attribute_structure is not ai_copy._attribute_structure assert ai._mapping is not ai_copy._mapping assert ai._profiles is not ai_copy._profiles assert ai._table is not ai_copy._table assert ai._relation_symbols is not ai_copy._relation_symbols
def test___repr__(): """Test str(Relation).""" R1 = Relation( 'R1(p1,l1) <=> p1 is_on_line l1', ['position', 'line_positions'], 1) R2 = Relation("R2(a) <=> ", ["a1"], 2) assert R1.__repr__() == "R1" assert R2.__repr__() == "R2"
def test___sub__(): """Test - operator for AttributeSystem.""" a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr_empty = AttributeStructure() astr_a = AttributeStructure(a) astr_a_b = AttributeStructure(a, b) astr_a_R1 = AttributeStructure(a, R1) astr_a_b_R1 = AttributeStructure(a, b, R1) astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) objs = ['a', 'b', 'c'] asys = AttributeSystem(astr_empty, []) asys_a = AttributeSystem(astr_a, []) asys_a_b = AttributeSystem(astr_a_b, []) asys_a_R1 = AttributeSystem(astr_a_R1, []) asys_a_b_R1 = AttributeSystem(astr_a_b_R1, []) asys_a_b_R1_R2 = AttributeSystem(astr_a_b_R1_R2, []) assert asys_a == asys + a # ensure no implicit type conversion happens assert not asys_a == asys with pytest.raises(AttributeError) as excinfo: asys_a == a assert asys == asys_a_b - a - b # Test D(R) remains in AttributeStructure with pytest.raises(ValueError) as excinfo: asys_a_b_R1_R2 - a assert asys_a == asys_a_R1 - R1 assert asys == asys_a_b_R1_R2 - R1 - R2 - a - b assert asys == asys_a_b_R1_R2 - astr_a_b_R1_R2 assert asys == asys_a_b_R1_R2 - R1 - R2 - astr_a_b assert asys == asys_a_b_R1_R2 - R1 - R2 - astr_a - b
def test___contains__(): """Test "in" operator for AttributeSystem.""" a = Attribute("a", []) b = Attribute("b", []) R1 = Relation("R1(a) <=> ", ["a"], 1) R2 = Relation("R2(a) <=> ", ["b"], 2) astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2) objs = ['a', 'b', 'c'] asys_a_b_R1_R2 = AttributeSystem(astr_a_b_R1_R2, []) asys_a_b_R1_R2_o = AttributeSystem(astr_a_b_R1_R2, objs) assert a in asys_a_b_R1_R2_o assert R1 in asys_a_b_R1_R2_o assert astr_a_b_R1_R2 in asys_a_b_R1_R2 assert astr_a_b_R1_R2 in asys_a_b_R1_R2_o with pytest.raises(TypeError) as excinfo: asys_a_b_R1_R2 in asys_a_b_R1_R2_o # Test strings only checked against objects assert 'a' not in asys_a_b_R1_R2 assert 'a' in asys_a_b_R1_R2._attribute_structure assert 'a' in asys_a_b_R1_R2_o
def test___deepcopy__(): """Test copy.deepcopy for Context object.""" ahead_rs = RelationSymbol('Ahead', 4) behind_rs = RelationSymbol('Behind', 4) pm_rs = RelationSymbol('PM', 1) am_rs = RelationSymbol('AM', 1) vocabulary = Vocabulary(['C1', 'C2'], [ahead_rs, behind_rs, pm_rs, am_rs], ['V1', 'V2']) f = Formula(vocabulary, 'Ahead', 'C1', 'V1') a = Attribute('hour', [Interval(0, 23)]) a2 = Attribute('minute', [Interval(0, 59)]) r_pm = Relation('R1(h1) <=> h1 > 11', ['hour'], 1) r_am = Relation('R2(h1) <=> h1 <= 11', ['hour'], 2) r_ahead = Relation( 'R3(h1,m1,hhh2,mm2) <=> h1 > hhh2 or (h1 = hhh2 and m1 > mm2)', ['hour', 'minute', 'hour', 'minute'], 3) r_behind = Relation( 'R4(h1,m1,h2,m2) <=> h1 <= h2 or (h1 = h2 and m1 < m2)', ['hour', 'minute', 'hour', 'minute'], 4) attribute_structure = AttributeStructure(a, a2, r_ahead, r_behind, r_pm, r_am) objects = ['s1', 's2'] attribute_system = AttributeSystem(attribute_structure, objects) p = ConstantAssignment(vocabulary, attribute_system, { 'C1': 's1', 'C2': 's2' }) assumption_base = AssumptionBase(f) named_state = NamedState(attribute_system, p) C = Context(assumption_base, named_state) from copy import deepcopy C_copy = deepcopy(C)
def test_is_valid_definition(): """Test is_valid_definition function.""" # correct definition form test assert Relation.is_valid_definition("R1(a,b,c) <=>") # whitespace correction test assert Relation.is_valid_definition(" R 1( a, b , c ) < = > ")
def test_get_arity(): """Test get_arity function.""" r = Relation("R1(a,b,c) <=> ", ["a", 'b', 'c'], 1) assert r.get_arity() == 3
def test_get_DR(): """Test get_DR function.""" r = Relation("R1(a,b,c) <=> ", ["a", 'b', 'c'], 1) assert r.get_DR() == ["a", 'b', 'c'] assert r.get_DR(string=True) == "a X b X c"