def test_unsat_system_from_paper3(self): """ Test an unsatisfiable system of inequalities from Smaragdakis (Fig 9c). """ sys = IneqSys.InequalitySystem() # Two object types related by two binary fact types obj1 = IneqSys.Variable("obj1", upper=20) obj2 = IneqSys.Variable("obj2", upper=20) rol11 = IneqSys.Variable("rol11", upper=20) rol12 = IneqSys.Variable("rol12", upper=20) rol21 = IneqSys.Variable("rol21", upper=20) rol22 = IneqSys.Variable("rol22", upper=20) fact1 = IneqSys.Variable("fact1", upper=20) fact2 = IneqSys.Variable("fact2", upper=20) # Constant con2 = IneqSys.Variable("con2", lower=2, upper=2) # Semantics of roles sys.add(IneqSys.Inequality(lhs=rol11, rhs=IneqSys.Sum([obj1]))) sys.add(IneqSys.Inequality(lhs=rol11, rhs=IneqSys.Sum([fact1]))) sys.add(IneqSys.Inequality(lhs=rol21, rhs=IneqSys.Sum([fact1]))) sys.add(IneqSys.Inequality(lhs=rol21, rhs=IneqSys.Sum([obj2]))) sys.add(IneqSys.Inequality(lhs=rol12, rhs=IneqSys.Sum([obj1]))) sys.add(IneqSys.Inequality(lhs=rol12, rhs=IneqSys.Sum([fact2]))) sys.add(IneqSys.Inequality(lhs=rol22, rhs=IneqSys.Sum([fact2]))) sys.add(IneqSys.Inequality(lhs=rol22, rhs=IneqSys.Sum([obj2]))) # Uniqueness of fact types sys.add( IneqSys.Inequality(lhs=fact1, rhs=IneqSys.Product([rol11, rol21]))) sys.add( IneqSys.Inequality(lhs=fact2, rhs=IneqSys.Product([rol12, rol22]))) # Disjunctive mandatory sys.add(IneqSys.Inequality(lhs=obj1, rhs=IneqSys.Sum([rol11, rol12]))) sys.add(IneqSys.Inequality(lhs=obj2, rhs=IneqSys.Sum([rol21, rol22]))) # Simple uniqueness sys.add(IneqSys.Inequality(lhs=fact1, rhs=IneqSys.Sum([rol21]))) sys.add(IneqSys.Inequality(lhs=fact2, rhs=IneqSys.Sum([rol12]))) # Simple mandatory sys.add(IneqSys.Inequality(lhs=obj1, rhs=IneqSys.Sum([rol11]))) sys.add(IneqSys.Inequality(lhs=obj2, rhs=IneqSys.Sum([rol22]))) # Frequency sys.add( IneqSys.Inequality(lhs=fact1, rhs=IneqSys.Product([con2, rol11]))) sys.add( IneqSys.Inequality(coeff=2, lhs=rol11, rhs=IneqSys.Sum([fact1]))) solution = sys.solve() self.assertEquals(solution, None) self.assertEquals(rol11.upper, 0)
def test_product(self): """ Test a normal Product class. """ x = IneqSys.Variable('x', upper=5) y = IneqSys.Variable('y', upper=7) prod1 = IneqSys.Product([x, y]) self.assertEquals(prod1.result(), 35) self.assertEquals(prod1.tostring(), "x * y")
def test_simple_unsat_system2(self): """ Test a simple unsatisfiable system #2 (with product). """ sys1 = IneqSys.InequalitySystem() a = IneqSys.Variable('a', upper=20) b = IneqSys.Variable('b', upper=3) c = IneqSys.Variable('c', upper=5) d = IneqSys.Variable('d', upper=2) p = IneqSys.Inequality(lhs=a, rhs=IneqSys.Product([b, c])) q = IneqSys.Inequality(coeff=10, lhs=c, rhs=IneqSys.Product([d, a])) sys1.add(p) sys1.add(q) solution = sys1.solve() self.assertEquals(solution, None)
def test_simple_sat_system3(self): """ Test a simple satisfiable system #3 (with product). """ sys1 = IneqSys.InequalitySystem() a = IneqSys.Variable('a', upper=20) b = IneqSys.Variable('b', upper=3) c = IneqSys.Variable('c', upper=5) d = IneqSys.Variable('d', upper=2) p = IneqSys.Inequality(lhs=a, rhs=IneqSys.Product([b, c])) q = IneqSys.Inequality(coeff=6, lhs=c, rhs=IneqSys.Product([d, a])) sys1.add(p) sys1.add(q) solution = sys1.solve(debug=True) self.assertEquals(solution['a'], 15) self.assertEquals(solution['b'], 3) self.assertEquals(solution['c'], 5) self.assertEquals(solution['d'], 2)
def test_unsat_system_from_paper2(self): """ Test an unsatisfiable system of inequalities from Smaragdakis (Fig 9b). """ sys = IneqSys.InequalitySystem() # Two object types related by a binary fact type obj1 = IneqSys.Variable("obj1", upper=50) obj2 = IneqSys.Variable("obj2", upper=2) rol1 = IneqSys.Variable("rol1", upper=50) rol2 = IneqSys.Variable("rol2", upper=50) fact = IneqSys.Variable("fact", upper=50) # Constant con3 = IneqSys.Variable("con3", lower=3, upper=3) # Semantics of roles sys.add(IneqSys.Inequality(lhs=rol1, rhs=IneqSys.Sum([obj1]))) sys.add(IneqSys.Inequality(lhs=rol1, rhs=IneqSys.Sum([fact]))) sys.add(IneqSys.Inequality(lhs=rol2, rhs=IneqSys.Sum([fact]))) sys.add(IneqSys.Inequality(lhs=rol2, rhs=IneqSys.Sum([obj2]))) # Frequency constraint sys.add(IneqSys.Inequality(lhs=fact, rhs=IneqSys.Product([con3, rol1]))) sys.add(IneqSys.Inequality(coeff=3, lhs=rol1, rhs=IneqSys.Sum([fact]))) # Uniqueness sys.add(IneqSys.Inequality(lhs=fact, rhs=IneqSys.Product([rol1, rol2]))) # Disjunctive mandatory sys.add(IneqSys.Inequality(lhs=obj1, rhs=IneqSys.Sum([rol1]))) sys.add(IneqSys.Inequality(lhs=obj2, rhs=IneqSys.Sum([rol2]))) # Solve the system solution = sys.solve() self.assertEquals(solution, None) self.assertEquals(rol1.upper, 0)
def test_simple_sat_system(self): """ Test a simple satisfiable system. """ sys = IneqSys.InequalitySystem() a = IneqSys.Variable('a', upper=50) b = IneqSys.Variable('b', lower=20) c = IneqSys.Variable('c') p = IneqSys.Inequality(lhs=b, rhs=IneqSys.Sum([a])) q = IneqSys.Inequality(lhs=c, rhs=IneqSys.Product([a, b])) sys.add(p) sys.add(q) solution = sys.solve(debug=True) # Debug on to achieve coverage self.assertEquals(solution['a'], 50) self.assertEquals(solution['b'], 50) self.assertEquals(solution['c'], 2500)
def test_product_empty(self): """ Test an empty Product class. """ prod1 = IneqSys.Product([]) self.assertEquals(prod1.result(), None) self.assertEquals(prod1.tostring(), "")