def test___ne__(): """Test != operator for ConstantAssignment object.""" vocabulary1 = Vocabulary(['C'], [RelationSymbol('R', 1)], ['V']) vocabulary2 = Vocabulary(['C\''], [RelationSymbol('R\'', 1)], ['V\'']) a = Attribute("a", []) b = Attribute("b", []) astr1 = AttributeStructure(a, b) astr2 = AttributeStructure(b) objs1 = ['a', 'b', 'c'] objs2 = ['a'] attribute_system1 = AttributeSystem(astr1, objs1) attribute_system2 = AttributeSystem(astr2, objs2) mapping1 = {'C': 'a'} mapping2 = {'C': 'b'} A1 = ConstantAssignment(vocabulary1, attribute_system1, mapping1) A2 = ConstantAssignment(vocabulary1, attribute_system1, mapping1) A3 = ConstantAssignment(vocabulary2, attribute_system1, {}) A4 = ConstantAssignment(vocabulary1, attribute_system2, {}) A5 = ConstantAssignment(vocabulary1, attribute_system1, mapping2) assert not A1 != A1 assert not A1 != A2 assert A1 != A3 assert A1 != A4 assert A1 != A5
def test___le__(): """Test overloaded <= subset operator for ConstantAssignment.""" vocabulary1 = Vocabulary(['C1', 'C2', 'C3'], [RelationSymbol('R', 1)], ['V']) vocabulary2 = Vocabulary(['C\''], [RelationSymbol('R\'', 1)], ['V\'']) a = Attribute("a", []) b = Attribute("b", []) astr1 = AttributeStructure(a, b) astr2 = AttributeStructure(b) objs1 = ['a', 'b', 'c'] objs2 = ['a'] attribute_system1 = AttributeSystem(astr1, objs1) attribute_system2 = AttributeSystem(astr2, objs2) mapping1 = {'C1': 'a'} mapping2 = {'C1': 'a', 'C2': 'b'} mapping3 = {'C1': 'a', 'C2': 'b', 'C3': 'c'} mapping4 = {'C\'': 'a'} mapping5 = {'C1': 'b'} CA1 = ConstantAssignment(vocabulary1, attribute_system1, mapping1) CA2 = ConstantAssignment(vocabulary1, attribute_system1, mapping2) CA3 = ConstantAssignment(vocabulary1, attribute_system1, mapping3) CA4 = ConstantAssignment(vocabulary2, attribute_system2, mapping4) CA5 = ConstantAssignment(vocabulary1, attribute_system1, mapping5) assert CA1 <= CA1 assert CA1 <= CA2 assert CA1 <= CA2 <= CA3 assert not CA1 <= CA4 assert not CA4 <= CA1 assert not CA5 <= CA2
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___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_is_alternate_extension(): """Test is_alternate_extension function.""" from copy import deepcopy color, size = Attribute("color", ['R', 'G', 'B']), Attribute( "size", ['S', 'M', 'L']) a = AttributeStructure(color, size) o = ['s1', 's2'] asys = AttributeSystem(a, o) s = State(asys) s.set_ascription(('color', 's1'), ['R', 'B']) s.set_ascription(('size', 's2'), ['M', 'L']) s1 = deepcopy(s) s1.set_ascription(('color', 's1'), ['B']) s1.set_ascription(('size', 's1'), ['S', 'M']) s1.set_ascription(('color', 's2'), ['B', 'G']) s2 = deepcopy(s) s2.set_ascription(('size', 's1'), ['L']) s2.set_ascription(('size', 's2'), ['L']) s3 = deepcopy(s) s3.set_ascription(('color', 's1'), ['R']) aes = s.get_alternate_extensions(s1, s2, s3) ae_s5, ae_s6, ae_s4 = aes for ae in aes: print s.is_alternate_extension(ae, s1, s2, s3) color, size = Attribute("color", ['R', 'G', 'B']), Attribute( "size", ['S', 'M', 'L']) a = AttributeStructure(color, size) o = ['s'] asys = AttributeSystem(a, o) s = State(asys) s1 = deepcopy(s) s1.set_ascription(('color', 's'), ['B', 'G']) s1.set_ascription(('size', 's'), ['S']) aes = s.get_alternate_extensions(s1) ae_s2, ae_s3 = aes for ae in aes: print s.is_alternate_extension(ae, s1)
def test___init__(): """Test VariableAssignment constructor.""" def test_TypeError(vocabulary, attribute_system, mapping): """Test constructor for TypeErrors with given params.""" with pytest.raises(TypeError) as excinfo: VariableAssignment(vocabulary, attribute_system, mapping) def test_ValueError(vocabulary, attribute_system, mapping): """Test constructor for ValueErrors with given params.""" with pytest.raises(ValueError) as excinfo: VariableAssignment(vocabulary, attribute_system, mapping) vocabulary = Vocabulary(['C'], [RelationSymbol('R', 1)], ['V']) a = Attribute("a", []) b = Attribute("b", []) astr = AttributeStructure(a, b) objs = ['a', 'b', 'c'] attribute_system = AttributeSystem(astr, objs) test_TypeError(vocabulary, attribute_system, {'V': 1}) test_TypeError(vocabulary, attribute_system, {1: 'a'}) test_ValueError(vocabulary, attribute_system, {'V': 'bad'}) test_ValueError(vocabulary, attribute_system, {'bad': 'a'}) VA = VariableAssignment(vocabulary, attribute_system, {'V': '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___deepcopy__(): """Test deepcopy""" color = Attribute("color", ['R', 'G', 'B']) size = Attribute("size", ['S', 'M', 'L']) a = AttributeStructure(color, size) o = ['s1', 's2'] asys = AttributeSystem(a, o) ascr = { ('color', 's1'): ['R'], ('color', 's2'): ['B', 'G'], ('size', 's1'): ['M'], ('size', 's2'): ['L', 'S'] } s = State(asys, ascr) from copy import deepcopy s_copy = deepcopy(s) assert s == s_copy assert s is not s_copy assert s._attribute_system == s_copy._attribute_system assert s._attribute_system is not s_copy._attribute_system assert s._ascriptions == s_copy._ascriptions assert s._ascriptions is not s_copy._ascriptions
def test___getitem__(): """Test indexing for VariableAssignment object.""" def test_TypeError(variable_assignment, key): """Test constructor for TypeErrors with given params.""" with pytest.raises(TypeError) as excinfo: variable_assignment[key] def test_KeyError(variable_assignment, key): """Test constructor for KeyErrors with given params.""" with pytest.raises(KeyError) as excinfo: variable_assignment[key] vocabulary = Vocabulary(['C'], [RelationSymbol('R', 1)], ['V']) a = Attribute("a", []) b = Attribute("b", []) astr = AttributeStructure(a, b) objs = ['a', 'b', 'c'] attribute_system = AttributeSystem(astr, objs) mapping = {'V': 'a'} VA = VariableAssignment(vocabulary, attribute_system, mapping) test_TypeError(VA, 1) test_TypeError(VA, None) test_TypeError(VA, object) test_KeyError(VA, '') assert VA['V'] == 'a'
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_join(): """Test join function for States.""" def test_ValueError(s1, s2): """Test constructor for ValueErrors with given params.""" with pytest.raises(ValueError) as excinfo: State.join(s1, s2) color = Attribute("color", ['R', 'G', 'B']) size = Attribute("size", ['S', 'M', 'L']) a = AttributeStructure(color, size) o = ['s1', 's2'] asys = AttributeSystem(a, o) ascr1 = { ('color', 's1'): ['R'], ('color', 's2'): ['B'], ('size', 's1'): ['M'], ('size', 's2'): ['L', 'S'] } ascr2 = { ('color', 's1'): ['G'], ('color', 's2'): ['G'], ('size', 's1'): ['L'], ('size', 's2'): ['M', 'S'] } ascr3 = { ('color', 's1'): ['R', 'G'], ('color', 's2'): ['B', 'G'], ('size', 's1'): ['L', 'M'], ('size', 's2'): ['M', 'S', 'L'] } s1 = State(asys, ascr1) s2 = State(asys, ascr2) s3 = State(asys, ascr3) assert s3 == State.join(s1, s2) length = Attribute("length", [1, 3, 5]) shape = Attribute("shape", ['circle', 'triangle', 'rectangle']) a = AttributeStructure(length, shape) o = ['s1', 's2'] bad_asys = AttributeSystem(a, o) bad_state = State(bad_asys) test_ValueError(s1, bad_state)
def test_set_ascription(): """Test set_ascription function.""" def test_TypeError(state, ascription, valueset): """Test set_ascription for TypeErrors with given params.""" with pytest.raises(TypeError) as excinfo: state.set_ascription(ascription, valueset) def test_ValueError(state, ascription, valueset): """Test set_ascription for ValueErrors with given params.""" with pytest.raises(ValueError) as excinfo: state.set_ascription(ascription, valueset) def test_KeyError(state, ascription, valueset): """Test set_ascription for KeyErrors with given params.""" with pytest.raises(KeyError) as excinfo: state.set_ascription(ascription, valueset) color = Attribute("color", ['R', 'G', 'B']) size = Attribute("size", ['S', 'M', 'L']) a = AttributeStructure(color, size) o = ['s1', 's2'] asys = AttributeSystem(a, o) s = State(asys) # test bad ao_pair types/values test_TypeError(s, [], ['R']) test_ValueError(s, (), ['R']) test_ValueError(s, (1, 2, 3), ['R']) test_ValueError(s, (1, 2), ['R']) test_ValueError(s, (1, ''), ['R']) test_ValueError(s, ('', 1), ['R']) # test bad types for ValueSet test_TypeError(s, ('color', 's1'), None) test_TypeError(s, ('color', 's1'), ()) test_TypeError(s, ('color', 's1'), 'a') test_TypeError(s, ('color', 's1'), object) # test empty ValueSet catching test_ValueError(s, ('color', 's1'), []) test_ValueError(s, ('color', 's1'), set([])) test_ValueError(s, ('color', 's1'), ValueSet([])) # test bad ao-pair keys test_KeyError(s, ('color', 'bad object'), ['R']) test_KeyError(s, ('bad label', 's2'), ['R']) # test nonsubset valuesets test_ValueError(s, ('color', 's2'), ['a']) test_ValueError(s, ('color', 's2'), [1]) s.set_ascription(('color', 's2'), ['R']) assert s[('color', 's2')] == ValueSet(['R']) # check reversion to superset is possible s.set_ascription(('color', 's2'), ['R', 'G']) assert s[('color', 's2')] == ValueSet(['R', 'G']) s.set_ascription(('size', 's1'), ['M', 'S']) assert s[('size', 's1')] == ValueSet(['S', 'M'])
def test_get_worlds(): """Test get_worlds function.""" color = Attribute("color", ['R', 'G', 'B']) size = Attribute("size", ['S', 'M', 'L']) a = AttributeStructure(color, size) o = ['s1', 's2'] asys = AttributeSystem(a, o) ascr = { ('color', 's1'): ['R'], ('color', 's2'): ['B', 'G'], ('size', 's1'): ['M'], ('size', 's2'): ['L', 'S'] } ascr1 = { ('color', 's1'): ['R'], ('color', 's2'): ['G'], ('size', 's1'): ['M'], ('size', 's2'): ['L'] } ascr2 = { ('color', 's1'): ['R'], ('color', 's2'): ['G'], ('size', 's1'): ['M'], ('size', 's2'): ['S'] } ascr3 = { ('color', 's1'): ['R'], ('color', 's2'): ['B'], ('size', 's1'): ['M'], ('size', 's2'): ['L'] } ascr4 = { ('color', 's1'): ['R'], ('color', 's2'): ['B'], ('size', 's1'): ['M'], ('size', 's2'): ['S'] } s = State(asys, ascr) w1 = State(asys, ascr1) w2 = State(asys, ascr2) w3 = State(asys, ascr3) w4 = State(asys, ascr4) worlds = [w1, w2, w3, w4] for w in s.get_worlds(): assert w in worlds assert len(s.get_worlds()) == len(worlds)
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___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___repr__(): """Test repr(State).""" color = Attribute("color", ['R', 'G', 'B']) size = Attribute("size", ['S', 'M', 'L']) a = AttributeStructure(color, size) o = ['s1', 's2'] asys = AttributeSystem(a, o) s = State(asys) s1 = State( asys, { ('color', 's1'): ['R'], ('color', 's2'): ['B', 'G'], ('size', 's1'): ['M'], ('size', 's2'): ['L', 'S'] }) s_empty = State(AttributeSystem(AttributeStructure(), [])) assert s_empty.__repr__() == "" assert s.__repr__( ) == "color(s1): {V(B, G, R)}\ncolor(s2): {V(B, G, R)}\nsize(s1): {V(L, M, S)}\nsize(s2): {V(L, M, S)}" assert s1.__repr__( ) == "color(s1): {V(R)}\ncolor(s2): {V(B, G)}\nsize(s1): {V(M)}\nsize(s2): {V(L, S)}"
def test_is_disjoint(): """Test is_disjoint function.""" color = Attribute("color", ['R', 'G', 'B']) size = Attribute("size", ['S', 'M', 'L']) a = AttributeStructure(color, size) o = ['s1', 's2'] asys = AttributeSystem(a, o) ascr = { ('color', 's1'): ['R'], ('color', 's2'): ['B', 'G'], ('size', 's1'): ['M'], ('size', 's2'): ['L', 'S'] } s1 = State(asys, ascr) s3 = State(asys, ascr) length = Attribute("length", [1, 3, 5]) shape = Attribute("shape", ['circle', 'triangle', 'rectangle']) a = AttributeStructure(length, shape) o = ['s1', 's2'] asys = AttributeSystem(a, o) ascr = { ('length', 's1'): [5], ('length', 's2'): [1, 3], ('shape', 's1'): ['circle'], ('shape', 's2'): ['triangle', 'rectangle'] } s2 = State(asys, ascr) assert s1.is_disjoint(s2) assert not s1.is_disjoint(s1) assert not s1.is_disjoint(s3)
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 repr(VariableAssignment).""" vocabulary = Vocabulary(['C', 'C\''], [RelationSymbol('R', 1)], ['V']) a = Attribute("a", []) b = Attribute("b", []) astr = AttributeStructure(a, b) objs = ['a', 'b', 'c'] attribute_system = AttributeSystem(astr, objs) mapping = {'V': 'a'} VA = VariableAssignment(vocabulary, attribute_system, mapping) assert VA.__repr__() == "VA{'V': 'a'}"
def test___eq__(): """Test == operator for VariableAssignment object.""" vocabulary1 = Vocabulary(['C'], [RelationSymbol('R', 1)], ['V']) vocabulary2 = Vocabulary(['C\''], [RelationSymbol('R\'', 1)], ['V\'']) a = Attribute("a", []) b = Attribute("b", []) astr1 = AttributeStructure(a, b) astr2 = AttributeStructure(b) objs1 = ['a', 'b', 'c'] objs2 = ['a'] attribute_system1 = AttributeSystem(astr1, objs1) attribute_system2 = AttributeSystem(astr2, objs2) mapping1 = {'V': 'a'} mapping2 = {'V': 'b'} A1 = VariableAssignment(vocabulary1, attribute_system1, mapping1) A2 = VariableAssignment(vocabulary1, attribute_system1, mapping1) A3 = VariableAssignment(vocabulary1, attribute_system1, mapping2) assert A1 == A1 assert A1 == A2 assert not A1 == A3
def test_add_object(): """Test add object function to state.""" def test_TypeError(state, obj, ascriptions=None): """Test constructor for TypeErrors with given params.""" with pytest.raises(TypeError) as excinfo: state.add_object(obj, ascriptions) def test_ValueError(state, obj, ascriptions=None): """Test constructor for ValueErrors with given params.""" with pytest.raises(ValueError) as excinfo: state.add_object(obj, ascriptions) color = Attribute("color", ['R', 'G', 'B']) a = AttributeStructure(color) o = ['s1'] asys = AttributeSystem(a, o) s = State(asys) test_TypeError(s, None) test_TypeError(s, 1) test_TypeError(s, object) test_TypeError(s, "") test_TypeError(s, "a", 1) test_TypeError(s, "a", object) test_ValueError(s, "s1") test_ValueError(s, "a", {"s1": 1}) test_ValueError(s, "a", {("s1"): 1}) test_ValueError(s, "a", {("s1", 's1', 's1'): 1}) test_ValueError(s, "a", {("color", "s1"): 1}) test_ValueError(s, "a", {("s", "a"): 1}) s.add_object("a") ascr = { ("color", "s1"): ValueSet(['R', 'G', 'B']), ("color", "a"): ValueSet(['R', 'G', 'B']) } assert s._ascriptions == ascr s = State(asys) s.add_object("a", {("color", "a"): ['R']}) ascr = { ("color", "s1"): ValueSet(['R', 'G', 'B']), ("color", "a"): ValueSet(['R']) } assert s._ascriptions == ascr
def test___repr__(): """Test repr(ConstantAssignment).""" vocabulary = Vocabulary(['C', 'C\''], [RelationSymbol('R', 1)], ['V']) a = Attribute("a", []) b = Attribute("b", []) astr = AttributeStructure(a, b) objs = ['a', 'b', 'c'] attribute_system = AttributeSystem(astr, objs) mapping = {'C': 'a'} mapping2 = {'C': 'a', 'C\'': 'b'} CA = ConstantAssignment(vocabulary, attribute_system, mapping) CA2 = ConstantAssignment(vocabulary, attribute_system, mapping2) assert CA.__repr__() == "CA{'C': 'a'}" assert CA2.__repr__() == "CA{'C': 'a', \"C\'\": 'b'}"
def test_get_domain(): """Test get_domain function for ConstantAssignment object.""" vocabulary = Vocabulary(['C', 'C\''], [RelationSymbol('R', 1)], ['V']) a = Attribute("a", []) b = Attribute("b", []) astr = AttributeStructure(a, b) objs = ['a', 'b', 'c'] attribute_system = AttributeSystem(astr, objs) mapping = {'C': 'a'} mapping2 = {'C': 'a', 'C\'': 'b'} CA = ConstantAssignment(vocabulary, attribute_system, mapping) CA2 = ConstantAssignment(vocabulary, attribute_system, mapping2) assert CA.get_domain() == ['C'] assert CA2.get_domain() == ['C', "C'"]
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_add_mapping(): """Test add_mapping function for ConstantAssignment.""" def test_TypeError(constant_assignment, constant_symbol, obj): """Test constructor for TypeErrors with given params.""" with pytest.raises(TypeError) as excinfo: constant_assignment.add_mapping(constant_symbol, obj) def test_ValueError(constant_assignment, constant_symbol, obj): """Test constructor for ValueErrors with given params.""" with pytest.raises(ValueError) as excinfo: constant_assignment.add_mapping(constant_symbol, obj) vocabulary = Vocabulary(['C1', 'C2'], [], []) a = Attribute("a", []) b = Attribute("b", []) astr = AttributeStructure(a, b) objs = ['a', 'b', 'c'] attribute_system = AttributeSystem(astr, objs) C = ConstantAssignment(vocabulary, attribute_system, {'C2': 'b'}) test_TypeError(C, None, 'a') test_TypeError(C, object, 'a') test_TypeError(C, 'C1', None) test_TypeError(C, 'C1', object) test_ValueError(C, 'bad_C', 'a') test_ValueError(C, 'C1', 'bad_obj') test_ValueError(C, 'C2', 'a') test_ValueError(C, 'C1', 'b') C.add_mapping('C1', 'a') C2 = ConstantAssignment(vocabulary, attribute_system, { 'C2': 'b', 'C1': 'a' }) C3 = ConstantAssignment(vocabulary, attribute_system, { 'C1': 'a', 'C2': 'b' }) assert C == C2 assert C == C3
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_is_total(): """Test is_total function for ConstantAssignment object.""" vocabulary = Vocabulary(['C', 'C\''], [RelationSymbol('R', 1)], ['V']) vocabulary_total = Vocabulary(['C1', 'C2', 'C3'], [RelationSymbol('R', 1)], ['V']) a = Attribute("a", []) b = Attribute("b", []) astr = AttributeStructure(a, b) objs = ['a', 'b', 'c'] attribute_system = AttributeSystem(astr, objs) mapping = {'C': 'a'} total_mapping = {'C1': 'a', 'C2': 'b', 'C3': 'c'} CA = ConstantAssignment(vocabulary, attribute_system, mapping) CA_total = ConstantAssignment(vocabulary_total, attribute_system, total_mapping) assert not CA.is_total() assert CA_total.is_total()
def test_in_conflict(): """Test in_conflict function for ConstantAssignment object.""" vocabulary = Vocabulary(['C', 'C\''], [RelationSymbol('R', 1)], ['V']) a = Attribute("a", []) b = Attribute("b", []) astr = AttributeStructure(a, b) objs = ['a', 'b', 'c'] attribute_system = AttributeSystem(astr, objs) mapping = {'C': 'a'} mapping2 = {'C': 'a', 'C\'': 'b'} mapping3 = {'C': 'b'} CA = ConstantAssignment(vocabulary, attribute_system, mapping) CA2 = ConstantAssignment(vocabulary, attribute_system, mapping2) CA3 = ConstantAssignment(vocabulary, attribute_system, mapping3) assert not CA.in_conflict(CA) assert not CA.in_conflict(CA2) assert CA.in_conflict(CA3)
def test_is_world(): """Test is_world function.""" color = Attribute("color", ['R', 'G', 'B']) size = Attribute("size", ['S', 'M', 'L']) a = AttributeStructure(color, size) o = ['s1', 's2'] asys = AttributeSystem(a, o) ascr = { ('color', 's1'): ['R'], ('color', 's2'): ['B', 'G'], ('size', 's1'): ['M'], ('size', 's2'): ['L', 'S'] } s = State(asys, ascr) assert not s.is_world() s.set_ascription(('color', 's2'), ['B']) s.set_ascription(('size', 's2'), ['L']) assert s.is_world()
def test___deepcopy__(): """Test copy.deepcopy for VariableAssignment object.""" from copy import deepcopy vocabulary = Vocabulary(['C'], [RelationSymbol('R', 1)], ['V']) a = Attribute("a", []) b = Attribute("b", []) astr = AttributeStructure(a, b) objs = ['a', 'b', 'c'] attribute_system = AttributeSystem(astr, objs) mapping = {'V': 'a'} VA = VariableAssignment(vocabulary, attribute_system, mapping) VA_copy = deepcopy(VA) assert VA == VA_copy assert VA is not VA_copy assert VA._vocabulary is VA_copy._vocabulary assert VA._attribute_system is not VA_copy._attribute_system assert VA._mapping is not VA_copy._mapping
def test___le__(): """Test <= operator overloaded for extension.""" color = Attribute("color", ['R', 'G', 'B']) size = Attribute("size", ['S', 'M', 'L']) a = AttributeStructure(color, size) o = ['s1', 's2'] asys = AttributeSystem(a, o) s = State(asys) ascr = { ('color', 's1'): ['R'], ('color', 's2'): ['B', 'G'], ('size', 's1'): ['M'], ('size', 's2'): ['L', 'S'] } s1 = State(asys, ascr) assert s <= s assert s1 <= s assert not s <= s1