Esempio n. 1
0
        private void RoundTripValuedNodeConversion <T>(dynamic value, Func <IValuedNode, T> convertBack)
        {
            // given
            ILiteralNode literalNode = LiteralExtensions.ToLiteral(value, _graph);

            // when
            IValuedNode valuedNode    = literalNode.AsValuedNode();
            T           convertedBack = convertBack(valuedNode);

            // then
            Assert.AreEqual(value, convertedBack);
        }
Esempio n. 2
0
        private static ILiteralNode CreateLiteralNode(ConstantNode node)
        {
            NodeFactory nodeFactory = new NodeFactory();

            switch ((node.TypeReference.Definition as IEdmPrimitiveType).PrimitiveKind)
            {
            case EdmPrimitiveTypeKind.DateTimeOffset:
                return(LiteralExtensions.ToLiteralDate((node.Value as DateTimeOffset?).GetValueOrDefault(), nodeFactory));

            case EdmPrimitiveTypeKind.Date:
                return(LiteralExtensions.ToLiteralDate((node.Value as Date?).GetValueOrDefault(), nodeFactory));

            case EdmPrimitiveTypeKind.Int32:
                return(LiteralExtensions.ToLiteral((node.Value as Int32?).GetValueOrDefault(), nodeFactory));

            case EdmPrimitiveTypeKind.Double:
                return(LiteralExtensions.ToLiteral((node.Value as Double?).GetValueOrDefault(), nodeFactory));

            case EdmPrimitiveTypeKind.Decimal:
                return(LiteralExtensions.ToLiteral((node.Value as Decimal?).GetValueOrDefault(), nodeFactory));

            case EdmPrimitiveTypeKind.Single:
                return(LiteralExtensions.ToLiteral((node.Value as Single?).GetValueOrDefault(), nodeFactory));

            case EdmPrimitiveTypeKind.Int16:
                return(LiteralExtensions.ToLiteral((node.Value as Int16?).GetValueOrDefault(), nodeFactory));

            case EdmPrimitiveTypeKind.Int64:
                return(LiteralExtensions.ToLiteral((node.Value as Int64?).GetValueOrDefault(), nodeFactory));

            case EdmPrimitiveTypeKind.Boolean:
                return(LiteralExtensions.ToLiteral((node.Value as Boolean?).GetValueOrDefault(), nodeFactory));

            case EdmPrimitiveTypeKind.String:
                return(nodeFactory.CreateLiteralNode(node.LiteralText.Replace("'", "")));

            default:
                return(nodeFactory.CreateLiteralNode(node.LiteralText.Replace("'", "")));
            }
        }
Esempio n. 3
0
        private ISparqlExpression BuildSparqlFilter(SingleValueNode filterNode, string suffix = "")
        {
            NodeFactory nodeFactory = new NodeFactory();

            if (filterNode.GetType() == typeof(SingleValueFunctionCallNode))
            {
                return(BuildFunctionExpression(filterNode as SingleValueFunctionCallNode));
            }
            else if (filterNode.GetType() == typeof(SingleValuePropertyAccessNode))
            {
                return(new VariableTerm($"?{(filterNode as SingleValuePropertyAccessNode).Property.Name}{suffix}"));
            }
            else if (filterNode.GetType() == typeof(ConstantNode))
            {
                return(new ConstantTerm(CreateLiteralNode(filterNode as ConstantNode)));
            }
            else if (filterNode.GetType() == typeof(ConvertNode))
            {
                var convert = filterNode as ConvertNode;
                if (convert.Source is SingleValueFunctionCallNode)
                {
                    return(BuildSparqlFilter(convert.Source as SingleValueFunctionCallNode, suffix));
                }
                else if (convert.Source is ConstantNode)
                {
                    return(new ConstantTerm(CreateLiteralNode(convert.Source as ConstantNode)));
                }
                else if (convert.Source is SingleValuePropertyAccessNode)
                {
                    var varName = (convert.Source as SingleValuePropertyAccessNode).Property.Name;
                    if (varName.ToLower() == "localid")
                    {
                        var node = LiteralExtensions.ToLiteral(NamespaceUri.ToString().Length + 1, nodeFactory);
                        varName = EdmNodeList[EdmNodeList.Count - 1].Name;
                        return((new UnaryExpressionFilter(
                                    new SubStrFunction(new StrFunction(new VariableTerm($"?{varName}{suffix}")),
                                                       (new ConstantTerm(node))))).Expression);
                    }
                    else
                    {
                        return(new VariableTerm($"?{varName}{suffix}"));
                    }
                }
            }
            else if (filterNode.GetType() == typeof(BinaryOperatorNode))
            {
                var binaryOperator = filterNode as BinaryOperatorNode;
                var left           = BuildSparqlFilter(binaryOperator.Left, suffix);
                var right          = BuildSparqlFilter(binaryOperator.Right, suffix);

                if (binaryOperator.OperatorKind == BinaryOperatorKind.And)
                {
                    return(new AndExpression(left, right));
                }
                else if (binaryOperator.OperatorKind == BinaryOperatorKind.Or)
                {
                    return(new OrExpression(left, right));
                }
                else if (binaryOperator.OperatorKind == BinaryOperatorKind.Equal)
                {
                    return(new EqualsExpression(left, right));
                }
                else if (binaryOperator.OperatorKind == BinaryOperatorKind.NotEqual)
                {
                    return(new NotEqualsExpression(left, right));
                }
                else if (binaryOperator.OperatorKind == BinaryOperatorKind.GreaterThan)
                {
                    return(new GreaterThanExpression(left, right));
                }
                else if (binaryOperator.OperatorKind == BinaryOperatorKind.GreaterThanOrEqual)
                {
                    return(new GreaterThanOrEqualToExpression(left, right));
                }
                else if (binaryOperator.OperatorKind == BinaryOperatorKind.LessThan)
                {
                    return(new LessThanExpression(left, right));
                }
                else if (binaryOperator.OperatorKind == BinaryOperatorKind.LessThanOrEqual)
                {
                    return(new LessThanOrEqualToExpression(left, right));
                }
                else if (binaryOperator.OperatorKind == BinaryOperatorKind.Add)
                {
                    return(new AdditionExpression(left, right));
                }
                else if (binaryOperator.OperatorKind == BinaryOperatorKind.Subtract)
                {
                    return(new SubtractionExpression(left, right));
                }
                else if (binaryOperator.OperatorKind == BinaryOperatorKind.Divide)
                {
                    return(new DivisionExpression(left, right));
                }
                else if (binaryOperator.OperatorKind == BinaryOperatorKind.Multiply)
                {
                    return(new MultiplicationExpression(left, right));
                }
            }
            else if (filterNode.GetType() == typeof(UnaryOperatorNode))
            {
                var unaryOperator = filterNode as UnaryOperatorNode;
                if (unaryOperator.OperatorKind == UnaryOperatorKind.Not)
                {
                    return(new NotEqualsExpression(BuildSparqlFilter(unaryOperator.Operand, suffix),
                                                   new ConstantTerm(LiteralExtensions.ToLiteral(true, nodeFactory))));
                }
            }
            return(null);
        }