Example #1
0
 /// <summary>
 /// Adds a Variable to the Query
 /// </summary>
 /// <param name="var">Variable</param>
 protected internal void AddVariable(SparqlVariable var)
 {
     if (!this._vars.ContainsKey(var.Name))
     {
         this._vars.Add(var.Name, var);
     }
     else
     {
         throw new RdfQueryException("Variable ?" + var.Name + " is already defined in this Query");
     }
 }
Example #2
0
 /// <summary>
 /// Adds a Variable to the Query
 /// </summary>
 /// <param name="var">Variable</param>
 protected internal void AddVariable(SparqlVariable var)
 {
     if (!this._vars.Any(v => v.Name.Equals(var)))
     {
         this._vars.Add(var);
     }
     else
     {
         throw new RdfQueryException("Variable ?" + var.Name + " is already defined in this Query");
     }
 }
Example #3
0
        public void SparqlVarNames()
        {
            List <String> names = new List <String>
            {
                "?var",
                "$var",
                "var"
            };

            foreach (String name in names)
            {
                SparqlVariable var = new SparqlVariable(name);
                Assert.AreEqual("var", var.Name);

                VariablePattern varPat = new VariablePattern(name);
                Assert.AreEqual("var", varPat.VariableName);

                VariableTerm varTerm = new VariableTerm(name);
                Assert.AreEqual("var", varTerm.Variables.First());
            }
        }
        public void SparqlVarNames()
        {
            var names = new List <string>
            {
                "?var",
                "$var",
                "var"
            };

            foreach (var name in names)
            {
                var var = new SparqlVariable(name);
                Assert.Equal("var", var.Name);

                var varPat = new VariablePattern(name);
                Assert.Equal("var", varPat.VariableName);

                var varTerm = new VariableTerm(name);
                Assert.Equal("var", varTerm.Variables.First());
            }
        }
Example #5
0
 /// <summary>
 /// Adds a Variable to the Query
 /// </summary>
 /// <param name="var">Variable</param>
 protected internal void AddVariable(SparqlVariable var)
 {
     if (!this._vars.ContainsKey(var.Name))
     {
         this._vars.Add(var.Name, var);
     }
     else
     {
         throw new RdfQueryException("Variable ?" + var.Name + " is already defined in this Query");
     }
 }
        private SparqlVariable TryParseAggregate(SparqlQueryParserContext context, IToken agg)
        {
            if (context.SyntaxMode == SparqlQuerySyntax.Sparql_1_0) throw ParserHelper.Error("Aggregates are not supported in SPARQL 1.0", agg);

            IToken next;
            SparqlVariable var;
            ISparqlAggregate aggregate;

            //Check that the Token is an Aggregate Keyword Token
            switch (agg.TokenType)
            {
                case Token.AVG:
                case Token.COUNT:
                case Token.GROUPCONCAT:
                case Token.MAX:
                case Token.MEDIAN:
                case Token.MIN:
                case Token.MODE:
                case Token.NMAX:
                case Token.NMIN:
                case Token.SAMPLE:
                case Token.SUM:
                    //OK
                    break;

                default:
                    throw ParserHelper.Error("Cannot parse an Aggregate since '" + agg.GetType().ToString() + "' is not an Aggregate Keyword Token", agg);
            }

            //Gather up the Tokens and call into the Expression Parser to get this parsed
            Queue<IToken> tokens = new Queue<IToken>();
            tokens.Enqueue(agg);
            int openBrackets = 0;
            do
            {
                next = context.Tokens.Dequeue();
                if (next.TokenType == Token.LEFTBRACKET)
                {
                    openBrackets++;
                }
                else if (next.TokenType == Token.RIGHTBRACKET)
                {
                    openBrackets--;
                }

                tokens.Enqueue(next);
            } while (openBrackets > 0);

            context.ExpressionParser.AllowAggregates = true;
            ISparqlExpression aggExpr = context.ExpressionParser.Parse(tokens);
            context.ExpressionParser.AllowAggregates = false;

            if (aggExpr is AggregateExpressionTerm)
            {
                aggregate = ((AggregateExpressionTerm)aggExpr).Aggregate;
            }
            else if (aggExpr is NonNumericAggregateExpressionTerm)
            {
                aggregate = ((NonNumericAggregateExpressionTerm)aggExpr).Aggregate;
            }
            else
            {
                throw new RdfParseException("Unexpected expression was parsed when an Aggregate was expected: " + aggExpr.ToString());
            }

            //See if there is an alias
            String alias = "Result";
            next = context.Tokens.Peek();
            if (next.TokenType == Token.AS)
            {
                context.Tokens.Dequeue();
                next = context.Tokens.Dequeue();
                if (next.TokenType != Token.VARIABLE)
                {
                    throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "', expected a Variable Token after an AS Keyword to act as an aliased name for the Aggregate", next);
                }
                alias = next.Value.Substring(1);
            }
            else
            {
                if (context.SyntaxMode != SparqlQuerySyntax.Extended) throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected an AS keyword after an Aggregate", next);

                int nextID = context.NextAliasID;
                if (nextID > 0) alias += nextID.ToString();
                while (context.Query.Variables.Any(v => v.Name.Equals(alias)))
                {
                    alias = "Result" + context.NextAliasID;
                }
                this.RaiseWarning("No AS ?variable given for the Aggregate " + aggregate.ToString() + " so assigning alias '" + alias + "'");
            }


            var = new SparqlVariable(alias, aggregate);

            return var;
        }