/// <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 = _leftExpr.Evaluate(context, bindingID);
            IValuedNode b = _rightExpr.Evaluate(context, bindingID);

            IValuedNode[]   inputs = new IValuedNode[] { a, b };
            ISparqlOperator op     = null;

            if (SparqlOperators.TryGetOperator(SparqlOperatorType.Multiply, out op, inputs))
            {
                return(op.Apply(inputs));
            }
            else
            {
                throw new RdfQueryException("Cannot apply multiplication to the given inputs");
            }

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

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

            // switch (type)
            // {
            //    case SparqlNumericType.Integer:
            //        return new LongNode(null, a.AsInteger() * b.AsInteger());
            //    case SparqlNumericType.Decimal:
            //        return new DecimalNode(null, a.AsDecimal() * b.AsDecimal());
            //    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");
            // }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Tries to return the operator which applies for the given inputs
        /// </summary>
        /// <param name="type">Operator Type</param>
        /// <param name="op">Operator</param>
        /// <param name="ns">Inputs</param>
        /// <returns></returns>
        public static bool TryGetOperator(SparqlOperatorType type, out ISparqlOperator op, params IValuedNode[] ns)
        {
            if (!_init)
            {
                Init();
            }

            op = null;
            List <ISparqlOperator> ops;

            lock (_operators)
            {
                if (_operators.TryGetValue(type, out ops))
                {
                    foreach (ISparqlOperator possOp in ops)
                    {
                        if (possOp.IsApplicable(ns))
                        {
                            op = possOp;
                            return(true);
                        }
                    }
                    return(false);
                }
                else
                {
                    return(false);
                }
            }
        }
Exemplo n.º 3
0
        private void TestApplication(SparqlOperatorType opType, IEnumerable <IValuedNode> ns, IValuedNode expected, bool shouldFail)
        {
            ISparqlOperator op = null;

            if (SparqlOperators.TryGetOperator(opType, out op, ns.ToArray()))
            {
                IValuedNode actual;
                try
                {
                    actual = op.Apply(ns.ToArray());
                }
                catch (Exception ex)
                {
                    if (shouldFail)
                    {
                        return;
                    }
                    throw;
                }

                Assert.AreEqual(expected, actual);
            }
            else
            {
                if (!shouldFail)
                {
                    Assert.Fail("Expected to be able to select an operator to apply to the inputs");
                }
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Registers a new operator
 /// </summary>
 /// <param name="op">Operator</param>
 public static void AddOperator(ISparqlOperator op)
 {
     if (!_init)
     {
         Init();
     }
     lock (_operators)
     {
         _operators[op.Operator].Add(op);
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Returns whether the given operator is registered
 /// </summary>
 /// <param name="op">Operator</param>
 /// <returns></returns>
 /// <remarks>
 /// Checking is done both by reference and instance type so you can check if an operator is registered even if you don't have the actual reference to the instance that registered
 /// </remarks>
 public static bool IsRegistered(ISparqlOperator op)
 {
     if (!_init)
     {
         Init();
     }
     lock (_operators)
     {
         return(_operators[op.Operator].Contains(op) || _operators[op.Operator].Any(o => op.GetType().Equals(o.GetType())));
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// Removes the registration of an operator by instance type of the operator
 /// </summary>
 /// <param name="op">Operator</param>
 public static void RemoveOperatorByType(ISparqlOperator op)
 {
     if (!_init)
     {
         Init();
     }
     lock (_operators)
     {
         _operators[op.Operator].RemoveAll(o => op.GetType().Equals(o.GetType()));
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// Removes the registration of an operator by instance reference
 /// </summary>
 /// <param name="op">Operator Reference</param>
 public static void RemoveOperator(ISparqlOperator op)
 {
     if (!_init)
     {
         Init();
     }
     lock (_operators)
     {
         _operators[op.Operator].Remove(op);
     }
 }
Exemplo n.º 8
0
        private void TestLookup(SparqlOperatorType opType, Type returnedOpInstanceType, IEnumerable <IValuedNode> ns, bool opExists)
        {
            ISparqlOperator op = null;

            if (SparqlOperators.TryGetOperator(opType, out op, ns.ToArray()))
            {
                Assert.True(opExists, "Operator returned when no operator was expected for the given inputs");
                Assert.Equal(returnedOpInstanceType, op.GetType());
            }
            else
            {
                Assert.False(opExists, "No Operator returned when an operator was expected for the given inputs");
            }
        }
Exemplo n.º 9
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);

            IValuedNode[]   inputs = new IValuedNode[] { a, b };
            ISparqlOperator op     = null;

            if (SparqlOperators.TryGetOperator(SparqlOperatorType.Divide, out op, inputs))
            {
                return(op.Apply(inputs));
            }
            else
            {
                throw new RdfQueryException("Cannot apply division to the given inputs");
            }
        }
Exemplo n.º 10
0
        public void ConfigurationSerializationOperators()
        {
            List <ISparqlOperator> ops = new List <ISparqlOperator>()
            {
                new AdditionOperator(),
                new DateTimeAddition(),
                new TimeSpanAddition(),
                new DivisionOperator(),
                new MultiplicationOperator(),
                new DivisionOperator(),
                new SubtractionOperator(),
                new DateTimeSubtraction(),
                new TimeSpanSubtraction()
            };

            Graph g = new Graph();
            ConfigurationSerializationContext context = new ConfigurationSerializationContext(g);
            List <INode> nodes = new List <INode>();

            foreach (ISparqlOperator op in ops)
            {
                INode opNode = g.CreateBlankNode();
                context.NextSubject = opNode;
                nodes.Add(opNode);

                ((IConfigurationSerializable)op).SerializeConfiguration(context);
            }

            for (int i = 0; i < ops.Count; i++)
            {
                INode           opNode   = nodes[i];
                ISparqlOperator resultOp = ConfigurationLoader.LoadObject(g, opNode) as ISparqlOperator;
                Assert.IsNotNull(resultOp, "Failed to load serialized operator " + ops[i].GetType().Name);
                Assert.AreEqual(ops[i].GetType(), resultOp.GetType());
            }
        }