Exemplo n.º 1
0
        /// <summary>
        /// Gets the Numeric Value of the function as evaluated in the given Context for the given Binding ID
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        /// <param name="bindingID">Binding ID</param>
        /// <returns></returns>
        public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID)
        {
            IValuedNode a = this._expr.Evaluate(context, bindingID);

            if (a == null)
            {
                throw new RdfQueryException("Cannot calculate an arithmetic expression on a null");
            }

            switch (a.NumericType)
            {
#if !SILVERLIGHT
            case SparqlNumericType.Integer:
                try
                {
                    return(new LongNode(null, Convert.ToInt64(Math.Floor(a.AsDecimal()))));
                }
                catch (RdfQueryException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw new RdfQueryException("Unable to cast floor value of integer to an integer", ex);
                }

            case SparqlNumericType.Decimal:
                return(new DecimalNode(null, Math.Floor(a.AsDecimal())));
#else
            case SparqlNumericType.Integer:
            case SparqlNumericType.Decimal:
#endif

            case SparqlNumericType.Float:
                try
                {
                    return(new FloatNode(null, Convert.ToSingle(Math.Floor(a.AsDouble()))));
                }
                catch (RdfQueryException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw new RdfQueryException("Unable to cast floor value of float to a float", ex);
                }

            case SparqlNumericType.Double:
                return(new DoubleNode(null, Math.Floor(a.AsDouble())));

            default:
                throw new RdfQueryException("Cannot evalute an Arithmetic Expression when the Numeric Type of the expression cannot be determined");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the Numeric Value of the function as evaluated in the given Context for the given Binding ID
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        /// <param name="bindingID">Binding ID</param>
        /// <returns></returns>
        public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID)
        {
            IValuedNode a = this._expr.Evaluate(context, bindingID);

            if (a == null)
            {
                throw new RdfQueryException("Cannot calculate an arithmetic expression on a null");
            }

            switch (a.NumericType)
            {
            case SparqlNumericType.Integer:
                //Rounding an Integer has no effect
                return(a);

            case SparqlNumericType.Decimal:
#if !SILVERLIGHT
                return(new DecimalNode(null, Math.Round(a.AsDecimal(), MidpointRounding.AwayFromZero)));
#else
                return(new DecimalNode(null, Math.Round(a.AsDecimal())));
#endif

            case SparqlNumericType.Float:
                try
                {
#if !SILVERLIGHT
                    return(new FloatNode(null, Convert.ToSingle(Math.Round(a.AsDouble(), MidpointRounding.AwayFromZero), CultureInfo.InvariantCulture)));
#else
                    return(new FloatNode(null, Convert.ToSingle(Math.Round(a.AsDouble()), CultureInfo.InvariantCulture)));
#endif
                }
                catch (RdfQueryException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw new RdfQueryException("Unable to cast the float value of a round to a float", ex);
                }

            case SparqlNumericType.Double:
#if !SILVERLIGHT
                return(new DoubleNode(null, Math.Round(a.AsDouble(), MidpointRounding.AwayFromZero)));
#else
                return(new DoubleNode(null, Math.Round(a.AsDouble())));
#endif

            default:
                throw new RdfQueryException("Cannot evalute an Arithmetic Expression when the Numeric Type of the expression cannot be determined");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the numeric value of the function in the given Evaluation Context for the given Binding ID
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        /// <param name="bindingID">Binding ID</param>
        /// <returns></returns>
        public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID)
        {
            IValuedNode a = _leftExpr.Evaluate(context, bindingID);
            IValuedNode b = _rightExpr.Evaluate(context, bindingID);

            SparqlNumericType type = (SparqlNumericType)Math.Max((int)a.NumericType, (int)b.NumericType);

            switch (type)
            {
            case SparqlNumericType.Integer:
                return(new LongNode(null, Math.Max(a.AsInteger(), b.AsInteger())));

            case SparqlNumericType.Decimal:
                return(new DecimalNode(null, Math.Max(a.AsDecimal(), b.AsDecimal())));

            case SparqlNumericType.Float:
                return(new FloatNode(null, Math.Max(a.AsFloat(), b.AsFloat())));

            case SparqlNumericType.Double:
                return(new DoubleNode(null, Math.Max(a.AsDouble(), b.AsDouble())));

            default:
                throw new RdfQueryException("Cannot evalute an Arithmetic Expression when the Numeric Type of the expression cannot be determined");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Evaluates the expression
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        /// <param name="bindingID">Binding ID</param>
        /// <returns></returns>
        public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID)
        {
            IValuedNode temp = _expr.Evaluate(context, bindingID);

            if (temp == null)
            {
                throw new RdfQueryException("Cannot square a null");
            }

            switch (temp.NumericType)
            {
            case SparqlNumericType.Integer:
                long l = temp.AsInteger();
                return(new LongNode(null, l * l * l));

            case SparqlNumericType.Decimal:
                decimal d = temp.AsDecimal();
                return(new DecimalNode(null, d * d * d));

            case SparqlNumericType.Float:
                float f = temp.AsFloat();
                return(new FloatNode(null, f * f * f));

            case SparqlNumericType.Double:
                double dbl = temp.AsDouble();
                return(new DoubleNode(null, Math.Pow(dbl, 3)));

            case SparqlNumericType.NaN:
            default:
                throw new RdfQueryException("Cannot square a non-numeric argument");
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Compares two Nodes for Numeric Ordering
        /// </summary>
        /// <param name="x">Node</param>
        /// <param name="y">Node</param>
        /// <param name="type">Numeric Type</param>
        /// <returns></returns>
        protected virtual int NumericCompare(IValuedNode x, IValuedNode y, SparqlNumericType type)
        {
            if (x == null || y == null)
            {
                throw new RdfQueryException("Cannot evaluate numeric ordering when one or both arguments are Null");
            }
            if (type == SparqlNumericType.NaN)
            {
                throw new RdfQueryException("Cannot evaluate numeric ordering when the Numeric Type is NaN");
            }

            switch (type)
            {
            case SparqlNumericType.Decimal:
                return(x.AsDecimal().CompareTo(y.AsDecimal()));

            case SparqlNumericType.Double:
                return(x.AsDouble().CompareTo(y.AsDouble()));

            case SparqlNumericType.Float:
                return(x.AsFloat().CompareTo(y.AsFloat()));

            case SparqlNumericType.Integer:
                return(x.AsInteger().CompareTo(y.AsInteger()));

            default:
                throw new RdfQueryException("Cannot evaluate numeric equality since of the arguments is not numeric");
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the Numeric Value of the function as evaluated in the given Context for the given Binding ID
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        /// <param name="bindingID">Binding ID</param>
        /// <returns></returns>
        public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID)
        {
            IValuedNode a = this._expr.Evaluate(context, bindingID);

            if (a == null)
            {
                throw new RdfQueryException("Cannot calculate an arithmetic expression on a null");
            }

            int p = 0;

            if (this._precision != null)
            {
                IValuedNode precision = this._precision.Evaluate(context, bindingID);
                if (precision == null)
                {
                    throw new RdfQueryException("Cannot use a null precision for rounding");
                }
                try
                {
                    p = Convert.ToInt32(precision.AsInteger());
                }
                catch
                {
                    throw new RdfQueryException("Unable to cast precision to an integer");
                }
            }

            switch (a.NumericType)
            {
            case SparqlNumericType.Integer:
                //Rounding an Integer has no effect
                return(a);

            case SparqlNumericType.Decimal:
                return(new DecimalNode(null, Math.Round(a.AsDecimal(), p, MidpointRounding.AwayFromZero)));

            case SparqlNumericType.Float:
                try
                {
                    return(new FloatNode(null, Convert.ToSingle(Math.Round(a.AsDouble(), p, MidpointRounding.AwayFromZero))));
                }
                catch (RdfQueryException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw new RdfQueryException("Unable to cast the float value of a round to a float", ex);
                }

            case SparqlNumericType.Double:
                return(new DoubleNode(null, Math.Round(a.AsDouble(), p, MidpointRounding.AwayFromZero)));

            default:
                throw new RdfQueryException("Cannot evalute an Arithmetic Expression when the Numeric Type of the expression cannot be determined");
            }
        }
Exemplo n.º 7
0
        public void ShouldSuccesfullyEvaluateDecimalCastRegardlessOfCulture()
        {
            foreach (var ci in TestedCultureInfos)
            {
                TestTools.ExecuteWithChangedCulture(ci, () =>
                {
                    // given
                    var cast = new DecimalCast(new ConstantTerm(3.4m.ToLiteral(_graph)));

                    // when
                    IValuedNode valuedNode = cast.Evaluate(new SparqlEvaluationContext(new SparqlQuery(), new InMemoryDataset()), 0);

                    // then
                    Assert.AreEqual(3.4m, valuedNode.AsDecimal());
                });
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Calculates the Numeric Value of this Expression as evaluated for a given Binding
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        /// <param name="bindingID">Binding ID</param>
        /// <returns></returns>
        public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID)
        {
            IValuedNode a = this._leftExpr.Evaluate(context, bindingID);
            IValuedNode b = this._rightExpr.Evaluate(context, bindingID);

            if (a == null || b == null)
            {
                throw new RdfQueryException("Cannot apply division when one/both arguments are null");
            }

            SparqlNumericType type = (SparqlNumericType)Math.Max((int)a.NumericType, (int)b.NumericType);

            try
            {
                switch (type)
                {
                case SparqlNumericType.Integer:
                case SparqlNumericType.Decimal:
                    //For Division Integers are treated as decimals
                    decimal d = a.AsDecimal() / b.AsDecimal();
                    if (Decimal.Floor(d).Equals(d) && d >= Int64.MinValue && d <= Int64.MaxValue)
                    {
                        return(new LongNode(null, Convert.ToInt64(d)));
                    }
                    return(new DecimalNode(null, d));

                case SparqlNumericType.Float:
                    return(new FloatNode(null, a.AsFloat() / b.AsFloat()));

                case SparqlNumericType.Double:
                    return(new DoubleNode(null, a.AsDouble() / b.AsDouble()));

                default:
                    throw new RdfQueryException("Cannot evalute an Arithmetic Expression when the Numeric Type of the expression cannot be determined");
                }
            }
            catch (DivideByZeroException)
            {
                throw new RdfQueryException("Cannot evaluate a Division Expression where the divisor is Zero");
            }
        }
Exemplo n.º 9
0
        private static object GetPrimitiveValue(IValuedNode valuedNode, IEdmPrimitiveType targetType, bool asNullable)
        {
            // TODO: Some sort of cast to nullable when necessary
            switch (targetType.PrimitiveKind)
            {
            case EdmPrimitiveTypeKind.Boolean:
                return(valuedNode.AsBoolean());

            case EdmPrimitiveTypeKind.Byte:
                return((byte)valuedNode.AsInteger());

            case EdmPrimitiveTypeKind.DateTime:
                return(valuedNode.AsDateTime());

            case EdmPrimitiveTypeKind.Decimal:
                return(valuedNode.AsDecimal());

            case EdmPrimitiveTypeKind.Double:
                return(valuedNode.AsDouble());

            case EdmPrimitiveTypeKind.Int16:
                return((Int16)valuedNode.AsInteger());

            case EdmPrimitiveTypeKind.Int32:
                return((Int32)valuedNode.AsInteger());

            case EdmPrimitiveTypeKind.Int64:
                return(valuedNode.AsInteger());

            case EdmPrimitiveTypeKind.String:
                return(valuedNode.AsString());

            case EdmPrimitiveTypeKind.DateTimeOffset:
                return(valuedNode.AsDateTime());

            default:
                throw new NotSupportedException(
                          String.Format("Support for primitive type {0} has not been implemented yet",
                                        targetType.PrimitiveKind));
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Calculates the Numeric Value of this Expression as evaluated for a given Binding
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        /// <param name="bindingID">Binding ID</param>
        /// <returns></returns>
        public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID)
        {
            IValuedNode a = this._expr.Evaluate(context, bindingID);

            if (a == null)
            {
                throw new RdfQueryException("Cannot apply unary minus to a null");
            }

            switch (a.NumericType)
            {
            case SparqlNumericType.Integer:
                return(new LongNode(null, -1 * a.AsInteger()));

            case SparqlNumericType.Decimal:
                decimal decvalue = a.AsDecimal();
                if (decvalue == Decimal.Zero)
                {
                    return(new DecimalNode(null, Decimal.Zero));
                }
                else
                {
                    return(new DecimalNode(null, -1 * decvalue));
                }

            case SparqlNumericType.Float:
                float fltvalue = a.AsFloat();
                if (Single.IsNaN(fltvalue))
                {
                    return(new FloatNode(null, Single.NaN));
                }
                else if (Single.IsPositiveInfinity(fltvalue))
                {
                    return(new FloatNode(null, Single.NegativeInfinity));
                }
                else if (Single.IsNegativeInfinity(fltvalue))
                {
                    return(new FloatNode(null, Single.PositiveInfinity));
                }
                else
                {
                    return(new FloatNode(null, -1.0f * fltvalue));
                }

            case SparqlNumericType.Double:
                double dblvalue = a.AsDouble();
                if (Double.IsNaN(dblvalue))
                {
                    return(new DoubleNode(null, Double.NaN));
                }
                else if (Double.IsPositiveInfinity(dblvalue))
                {
                    return(new DoubleNode(null, Double.NegativeInfinity));
                }
                else if (Double.IsNegativeInfinity(dblvalue))
                {
                    return(new DoubleNode(null, Double.PositiveInfinity));
                }
                else
                {
                    return(new DoubleNode(null, -1.0 * dblvalue));
                }

            default:
                throw new RdfQueryException("Cannot evalute an Arithmetic Expression when the Numeric Type of the expression cannot be determined");
            }
        }
Exemplo n.º 11
0
 private static object GetPrimitiveValue(IValuedNode valuedNode, IEdmPrimitiveType targetType, bool asNullable)
 {
     // TODO: Some sort of cast to nullable when necessary
         switch (targetType.PrimitiveKind)
         {
             case EdmPrimitiveTypeKind.Boolean:
                 return valuedNode.AsBoolean();
             case EdmPrimitiveTypeKind.Byte:
                 return (byte) valuedNode.AsInteger();
             case EdmPrimitiveTypeKind.DateTime:
                 return valuedNode.AsDateTime();
             case EdmPrimitiveTypeKind.Decimal:
                 return valuedNode.AsDecimal();
             case EdmPrimitiveTypeKind.Double:
                 return valuedNode.AsDouble();
             case EdmPrimitiveTypeKind.Int16:
                 return (Int16) valuedNode.AsInteger();
             case EdmPrimitiveTypeKind.Int32:
                 return (Int32) valuedNode.AsInteger();
             case EdmPrimitiveTypeKind.Int64:
                 return valuedNode.AsInteger();
             case EdmPrimitiveTypeKind.String:
                 return valuedNode.AsString();
             case EdmPrimitiveTypeKind.DateTimeOffset:
                 return valuedNode.AsDateTime();
             default:
                 throw new NotSupportedException(
                     String.Format("Support for primitive type {0} has not been implemented yet",
                                   targetType.PrimitiveKind));
         }
 }