Exemple #1
0
    def testSimpleFunction(self):
        """Test a simple function."""

        # Make some variables
        v1, v2, v3, v4 = _makeArgs(4)

        # 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)

        # now get the args
        args = visitors.getArgs(mult)
        self.assertEqual(4, len(args))
        self.assertTrue(v1 in args)
        self.assertTrue(v2 in args)
        self.assertTrue(v3 in args)
        self.assertTrue(v4 in args)

        return
    def setRoot(self, root):
        """Set the root of the Literal tree.

        Raises:
        ValueError if errors are found in the Literal tree.

        """

        # Validate the new root
        validate(root)

        # Stop observing the old root
        if self.root is not None:
            self.root.removeObserver(self._flush)

        # Add the new root
        self.root = root
        self.root.addObserver(self._flush)
        self._flush(self)

        # Get the args
        args = getArgs(root, getconsts=False)
        self.argdict = OrderedDict( [(arg.name, arg) for arg in args] )

        # Set Operator attributes
        self.nin = len(self.args)

        return
    def testSimpleFunction(self):
        """Test a simple function."""

        # Make some variables
        v1, v2, v3, v4 = _makeArgs(4)

        # 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)

        # now get the args
        args = visitors.getArgs(mult)
        self.assertEqual(4, len(args))
        self.assertTrue(v1 in args)
        self.assertTrue(v2 in args)
        self.assertTrue(v3 in args)
        self.assertTrue(v4 in args)

        return
    def setRoot(self, root):
        """Set the root of the Literal tree.

        Raises:
        ValueError if errors are found in the Literal tree.

        """

        # Validate the new root
        validate(root)

        # Stop observing the old root
        if self.root is not None:
            self.root.removeObserver(self._flush)

        # Add the new root
        self.root = root
        self.root.addObserver(self._flush)
        self._flush(other=(self, ))

        # Get the args
        args = getArgs(root, getconsts=False)
        self.argdict = OrderedDict([(arg.name, arg) for arg in args])

        # Set Operator attributes
        self.nin = len(self.args)

        return
Exemple #5
0
    def testArg(self):
        """Test just an Argument equation."""
        # Make some variables
        v1 = _makeArgs(1)[0]

        args = visitors.getArgs(v1)

        self.assertEquals(1, len(args))
        self.assertTrue(args[0] is v1)
        return
    def testArg(self):
        """Test just an Argument equation."""
        # Make some variables
        v1 = _makeArgs(1)[0]

        args = visitors.getArgs(v1)

        self.assertEqual(1, len(args))
        self.assertTrue(args[0] is v1)
        return
Exemple #7
0
    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