Пример #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");
            }

            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");
            }
        }
Пример #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)
            {
#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");
            }
        }
Пример #3
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");
            }
        }
Пример #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 min = _leftExpr.Evaluate(context, bindingID);

            if (min == null)
            {
                throw new RdfQueryException("Cannot randomize with a null minimum");
            }
            IValuedNode max = _rightExpr.Evaluate(context, bindingID);

            if (max == null)
            {
                throw new RdfQueryException("Cannot randomize with a null maximum");
            }

            if (min.NumericType == SparqlNumericType.NaN || max.NumericType == SparqlNumericType.NaN)
            {
                throw new RdfQueryException("Cannot randomize when one/both arguments are non-numeric");
            }

            double x = min.AsDouble();
            double y = max.AsDouble();

            if (x > y)
            {
                throw new RdfQueryException("Cannot generate a random number in the given range since the minumum is greater than the maximum");
            }
            double range = y - x;
            double rnd   = _rnd.NextDouble() * range;

            rnd += x;
            return(new DoubleNode(null, rnd));
        }
Пример #5
0
        /// <summary>
        /// Internal helper for calculating 2D Cartesian Distance.
        /// </summary>
        /// <param name="context">Evaluation Context.</param>
        /// <param name="bindingID">Binding ID.</param>
        /// <returns></returns>
        private IValuedNode CartesianDistance2D(SparqlEvaluationContext context, int bindingID)
        {
            IValuedNode x1 = _x1.Evaluate(context, bindingID);

            if (x1 == null)
            {
                throw new RdfQueryException("Cannot calculate cartesian distance when a argument is null");
            }
            IValuedNode y1 = _y1.Evaluate(context, bindingID);

            if (y1 == null)
            {
                throw new RdfQueryException("Cannot calculate cartesian distance when a argument is null");
            }
            IValuedNode x2 = _x2.Evaluate(context, bindingID);

            if (x2 == null)
            {
                throw new RdfQueryException("Cannot calculate cartesian distance when a argument is null");
            }
            IValuedNode y2 = _y2.Evaluate(context, bindingID);

            if (y2 == null)
            {
                throw new RdfQueryException("Cannot calculate cartesian distance when a argument is null");
            }

            double dX = x2.AsDouble() - x1.AsDouble();
            double dY = y2.AsDouble() - y1.AsDouble();

            return(new DoubleNode(null, Math.Sqrt(Math.Pow(dX, 2) + Math.Pow(dY, 2))));
        }
Пример #6
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");
            }
        }
Пример #7
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");
            }
        }
Пример #8
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");
            }
        }
Пример #9
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 = this._expr.Evaluate(context, bindingID);
            if (temp == null) throw new RdfQueryException("Cannot evaluate reciprocal of a null");
            double d = temp.AsDouble();
            if (d == 0) throw new RdfQueryException("Cannot evaluate reciprocal of zero");

            return new DoubleNode(null, 1d / d);
        }
Пример #10
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 apply a numeric function to a null");
            }

            if (temp.NumericType == SparqlNumericType.NaN)
            {
                throw new RdfQueryException("Cannot apply a numeric function to a non-numeric argument");
            }

            return(new DoubleNode(null, Math.PI * (temp.AsDouble() / 180d)));
        }
Пример #11
0
        public void ShouldSuccesfullyEvaluateDoubleCastRegardlessOfCulture()
        {
            foreach (var ci in TestedCultureInfos)
            {
                TestTools.ExecuteWithChangedCulture(ci, () =>
                {
                    // given
                    var cast = new DoubleCast(new ConstantTerm(3.4d.ToLiteral(_graph)));

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

                    // then
                    Assert.AreEqual(3.4d, valuedNode.AsDouble());
                });
            }
        }
Пример #12
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");
            }
        }
Пример #13
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));
            }
        }
Пример #14
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 arg = _leftExpr.Evaluate(context, bindingID);

            if (arg == null)
            {
                throw new RdfQueryException("Cannot raise a null to a power");
            }
            IValuedNode pow = _rightExpr.Evaluate(context, bindingID);

            if (pow == null)
            {
                throw new RdfQueryException("Cannot raise to a null power");
            }

            if (arg.NumericType == SparqlNumericType.NaN || pow.NumericType == SparqlNumericType.NaN)
            {
                throw new RdfQueryException("Cannot raise to a power when one/both arguments are non-numeric");
            }

            return(new DoubleNode(null, Math.Pow(arg.AsDouble(), pow.AsDouble())));
        }
Пример #15
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 = this._expr.Evaluate(context, bindingID);

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

            switch (temp.NumericType)
            {
            case SparqlNumericType.Integer:
            case SparqlNumericType.Decimal:
            case SparqlNumericType.Float:
            case SparqlNumericType.Double:
                return(new DoubleNode(null, Math.Log(temp.AsDouble(), Math.E)));

            case SparqlNumericType.NaN:
            default:
                throw new RdfQueryException("Cannot square a non-numeric argument");
            }
        }
        /// <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 x = _leftExpr.Evaluate(context, bindingID);

            if (x == null)
            {
                throw new RdfQueryException("Cannot calculate distance of a null");
            }
            IValuedNode y = _rightExpr.Evaluate(context, bindingID);

            if (y == null)
            {
                throw new RdfQueryException("Cannot calculate distance of a null");
            }

            if (x.NumericType == SparqlNumericType.NaN || y.NumericType == SparqlNumericType.NaN)
            {
                throw new RdfQueryException("Cannot calculate distance when one/both arguments are non-numeric");
            }

            return(new DoubleNode(null, Math.Sqrt(Math.Pow(x.AsDouble(), 2) + Math.Pow(y.AsDouble(), 2))));
        }
Пример #17
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 arg = _leftExpr.Evaluate(context, bindingID);

            if (arg == null)
            {
                throw new RdfQueryException("Cannot log a null");
            }
            IValuedNode logBase = _rightExpr.Evaluate(context, bindingID);

            if (logBase == null)
            {
                throw new RdfQueryException("Cannot log to a null base");
            }

            if (arg.NumericType == SparqlNumericType.NaN || logBase.NumericType == SparqlNumericType.NaN)
            {
                throw new RdfQueryException("Cannot log when one/both arguments are non-numeric");
            }

            return(new DoubleNode(null, Math.Log(arg.AsDouble(), logBase.AsDouble())));
        }
Пример #18
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 arg = this._leftExpr.Evaluate(context, bindingID);

            if (arg == null)
            {
                throw new RdfQueryException("Cannot root a null");
            }
            IValuedNode root = this._rightExpr.Evaluate(context, bindingID);

            if (root == null)
            {
                throw new RdfQueryException("Cannot root to a null root");
            }

            if (arg.NumericType == SparqlNumericType.NaN || root.NumericType == SparqlNumericType.NaN)
            {
                throw new RdfQueryException("Cannot root when one/both arguments are non-numeric");
            }

            return(new DoubleNode(null, Math.Pow(arg.AsDouble(), (1d / root.AsDouble()))));
        }
Пример #19
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");
            }
        }
Пример #20
0
        /// <summary>
        /// Casts the value of the inner Expression to a Double
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        /// <param name="bindingID">Binding ID</param>
        /// <returns></returns>
        public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID)
        {
            IValuedNode n = _expr.Evaluate(context, bindingID);//.CoerceToDouble();

            if (n == null)
            {
                throw new RdfQueryException("Cannot cast a Null to a xsd:double");
            }

            // New method should be much faster
            // if (n is DoubleNode) return n;
            // return new DoubleNode(null, n.AsDouble());

            switch (n.NodeType)
            {
            case NodeType.Blank:
            case NodeType.GraphLiteral:
            case NodeType.Uri:
                throw new RdfQueryException("Cannot cast a Blank/URI/Graph Literal Node to a xsd:double");

            case NodeType.Literal:
                if (n is DoubleNode)
                {
                    return(n);
                }
                if (n is FloatNode)
                {
                    return(new DoubleNode(n.Graph, n.AsDouble()));
                }
                // See if the value can be cast
                ILiteralNode lit = (ILiteralNode)n;
                if (lit.DataType != null)
                {
                    string dt = lit.DataType.ToString();
                    if (dt.Equals(XmlSpecsHelper.XmlSchemaDataTypeDouble) || dt.Equals(XmlSpecsHelper.XmlSchemaDataTypeFloat))
                    {
                        double d;
                        if (Double.TryParse(lit.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out d))
                        {
                            // Parsed OK
                            return(new DoubleNode(lit.Graph, d));
                        }
                        else
                        {
                            throw new RdfQueryException("Invalid lexical form for xsd:double");
                        }
                    }
                    else if (dt.Equals(XmlSpecsHelper.XmlSchemaDataTypeDateTime))
                    {
                        // DateTime cast forbidden
                        throw new RdfQueryException("Cannot cast a xsd:dateTime to a xsd:double");
                    }
                    else
                    {
                        double d;
                        if (Double.TryParse(lit.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out d))
                        {
                            // Parsed OK
                            return(new DoubleNode(lit.Graph, d));
                        }
                        else
                        {
                            throw new RdfQueryException("Cannot cast the value '" + lit.Value + "' to a xsd:double");
                        }
                    }
                }
                else
                {
                    double d;
                    if (Double.TryParse(lit.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out d))
                    {
                        // Parsed OK
                        return(new DoubleNode(lit.Graph, d));
                    }
                    else
                    {
                        throw new RdfQueryException("Cannot cast the value '" + lit.Value + "' to a xsd:double");
                    }
                }

            default:
                throw new RdfQueryException("Cannot cast an Unknown Node to a xsd:double");
            }
        }
Пример #21
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));
         }
 }
        /// <summary>
        /// Constructs a Full Text Match property function
        /// </summary>
        /// <param name="info">Property Function information</param>
        public FullTextMatchPropertyFunction(PropertyFunctionInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }
            if (!EqualityHelper.AreUrisEqual(info.FunctionUri, this.FunctionUri))
            {
                throw new ArgumentException("Property Function information is not valid for this function");
            }

            //Get basic arguments
            this._matchVar = info.SubjectArgs[0];
            if (this._matchVar.VariableName != null)
            {
                this._vars.Add(this._matchVar.VariableName);
            }
            if (info.SubjectArgs.Count == 2)
            {
                this._scoreVar = info.SubjectArgs[1];
                if (this._scoreVar.VariableName != null)
                {
                    this._vars.Add(this._scoreVar.VariableName);
                }
            }

            //Check extended arguments
            this._searchVar = info.ObjectArgs[0];
            if (this._searchVar.VariableName != null)
            {
                this._vars.Add(this._searchVar.VariableName);
            }
            switch (info.ObjectArgs.Count)
            {
            case 1:
                break;

            case 2:
                PatternItem arg = info.ObjectArgs[1];
                if (arg.VariableName != null)
                {
                    throw new RdfQueryException("Cannot use a variable as the limit/score threshold for full text queries, must use a numeric constant");
                }
                IValuedNode n = ((NodeMatchPattern)arg).Node.AsValuedNode();
                switch (n.NumericType)
                {
                case SparqlNumericType.Integer:
                    this._limit = (int)n.AsInteger();
                    break;

                case SparqlNumericType.Decimal:
                case SparqlNumericType.Double:
                case SparqlNumericType.Float:
                    this._threshold = n.AsDouble();
                    break;

                default:
                    throw new RdfQueryException("Cannot use a non-numeric constant as the limit/score threshold for full text queries, must use a numeric constant");
                }
                break;

            case 3:
            default:
                PatternItem arg1 = info.ObjectArgs[1];
                PatternItem arg2 = info.ObjectArgs[2];
                if (arg1.VariableName != null || arg2.VariableName != null)
                {
                    throw new RdfQueryException("Cannot use a variable as the limit/score threshold for full text queries, must use a numeric constant");
                }
                IValuedNode n1 = ((NodeMatchPattern)arg1).Node.AsValuedNode();
                switch (n1.NumericType)
                {
                case SparqlNumericType.NaN:
                    throw new RdfQueryException("Cannot use a non-numeric constant as the score threshold for full text queries, must use a numeric constant");

                default:
                    this._threshold = n1.AsDouble();
                    break;
                }
                IValuedNode n2 = ((NodeMatchPattern)arg2).Node.AsValuedNode();
                switch (n2.NumericType)
                {
                case SparqlNumericType.NaN:
                    throw new RdfQueryException("Cannot use a non-numeric constant as the limit for full text queries, must use a numeric constant");

                default:
                    this._limit = (int)n2.AsInteger();
                    break;
                }
                break;
            }
        }