/// <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.Subtract, out op, inputs)) { return(op.Apply(inputs)); } else { throw new RdfQueryException("Cannot apply addition to the given inputs"); } // if (a == null || b == null) throw new RdfQueryException("Cannot apply subtraction 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"); // } }
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) { if (shouldFail) { return; } throw; } Assert.Equal(expected, actual); } else { Assert.True(shouldFail, "Expected to be able to select an operator to apply to the inputs"); } }
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"); } }
/// <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"); } }
public void ConfigurationAutoOperators1() { try { String data = @"@prefix dnr: <http://www.dotnetrdf.org/configuration#> . _:a a dnr:SparqlOperator ; dnr:type """ + typeof(MockSparqlOperator).AssemblyQualifiedName + @""" ."; Graph g = new Graph(); g.LoadFromString(data); ConfigurationLoader.AutoConfigureSparqlOperators(g); ISparqlOperator op; SparqlOperators.TryGetOperator(SparqlOperatorType.Add, out op, null); Assert.AreEqual(typeof(MockSparqlOperator), op.GetType()); SparqlOperators.RemoveOperator(op); } finally { SparqlOperators.Reset(); } }