def swap(self, oldlit, newlit): """Swap a literal in the equation for another. Note that this may change the root and the operation interface """ newroot = swap(self.root, oldlit, newlit) self.setRoot(newroot) return
def testSimpleFunction(self): """Test a simple function.""" # Make some variables v1, v2, v3, v4, v5 = _makeArgs(5) # Make some operations mult = literals.MultiplicationOperator() plus = literals.AdditionOperator() minus = literals.SubtractionOperator() # Create the equation (v1+v3)*(v4-v2) plus.addLiteral(v1) plus.addLiteral(v3) minus.addLiteral(v4) minus.addLiteral(v2) mult.addLiteral(plus) mult.addLiteral(minus) # Set the values of the variables. # The equation should evaluate to (1+3)*(4-2) = 8 v1.setValue(1) v2.setValue(2) v3.setValue(3) v4.setValue(4) v5.setValue(5) # Evaluate self.assertEquals(8, mult.value) # Now swap an argument visitors.swap(mult, v2, v5) # Check that the operator value is invalidated self.assertTrue(mult._value is None) self.assertTrue(minus._flush not in v2._observers) self.assertTrue(minus._flush in v5._observers) # now get the args args = visitors.getArgs(mult) self.assertEqual(4, len(args)) self.assertTrue(v1 in args) self.assertTrue(v2 not in args) self.assertTrue(v3 in args) self.assertTrue(v4 in args) self.assertTrue(v5 in args) # Re-evaluate (1+3)*(4-5) = -4 self.assertEquals(-4, mult.value) # Swap out the "-" operator plus2 = literals.AdditionOperator() visitors.swap(mult, minus, plus2) self.assertTrue(mult._value is None) self.assertTrue(mult._flush not in minus._observers) self.assertTrue(mult._flush in plus2._observers) # plus2 has no arguments yet. Verify this. self.assertRaises(ValueError, mult.getValue) # Add the arguments to plus2. plus2.addLiteral(v4) plus2.addLiteral(v5) # Re-evaluate (1+3)*(4+5) = 36 self.assertEquals(36, mult.value) return
def testSimpleFunction(self): """Test a simple function.""" # Make some variables v1, v2, v3, v4, v5 = _makeArgs(5) # Make some operations mult = literals.MultiplicationOperator() plus = literals.AdditionOperator() minus = literals.SubtractionOperator() # Create the equation (v1+v3)*(v4-v2) plus.addLiteral(v1) plus.addLiteral(v3) minus.addLiteral(v4) minus.addLiteral(v2) mult.addLiteral(plus) mult.addLiteral(minus) # Set the values of the variables. # The equation should evaluate to (1+3)*(4-2) = 8 v1.setValue(1) v2.setValue(2) v3.setValue(3) v4.setValue(4) v5.setValue(5) # Evaluate self.assertEqual(8, mult.value) # Now swap an argument visitors.swap(mult, v2, v5) # Check that the operator value is invalidated self.assertTrue(mult._value is None) self.assertFalse(v2.hasObserver(minus._flush)) self.assertTrue(v5.hasObserver(minus._flush)) # now get the args args = visitors.getArgs(mult) self.assertEqual(4, len(args)) self.assertTrue(v1 in args) self.assertTrue(v2 not in args) self.assertTrue(v3 in args) self.assertTrue(v4 in args) self.assertTrue(v5 in args) # Re-evaluate (1+3)*(4-5) = -4 self.assertEqual(-4, mult.value) # Swap out the "-" operator plus2 = literals.AdditionOperator() visitors.swap(mult, minus, plus2) self.assertTrue(mult._value is None) self.assertFalse(minus.hasObserver(mult._flush)) self.assertTrue(plus2.hasObserver(mult._flush)) # plus2 has no arguments yet. Verify this. self.assertRaises(ValueError, mult.getValue) # Add the arguments to plus2. plus2.addLiteral(v4) plus2.addLiteral(v5) # Re-evaluate (1+3)*(4+5) = 36 self.assertEqual(36, mult.value) return