Ejemplo n.º 1
0
 /// <summary>
 /// Adds inline data to a Graph Pattern respecting any BGP breaks
 /// </summary>
 /// <param name="data"></param>
 internal void AddInlineData(BindingsPattern data)
 {
     if (_break)
     {
         if (_broken)
         {
             _graphPatterns.Last().AddInlineData(data);
         }
         else if (_data == null && _graphPatterns.Count == 0)
         {
             _data = data;
         }
         else
         {
             GraphPattern p = new GraphPattern();
             p.AddInlineData(data);
             _graphPatterns.Add(p);
         }
     }
     else if (_isUnion)
     {
         BreakBGP();
         AddInlineData(data);
     }
     else
     {
         _data = data;
         BreakBGP();
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Adds inline data to a Graph Pattern respecting any BGP breaks
 /// </summary>
 /// <param name="data"></param>
 internal void AddInlineData(BindingsPattern data)
 {
     if (this._break)
     {
         if (this._broken)
         {
             this._graphPatterns.Last().AddInlineData(data);
         }
         else if (this._data == null && this._graphPatterns.Count == 0)
         {
             this._data = data;
         }
         else
         {
             GraphPattern p = new GraphPattern();
             p.AddInlineData(data);
             this._graphPatterns.Add(p);
         }
     }
     else if (this._isUnion)
     {
         this.BreakBGP();
         this.AddInlineData(data);
     }
     else
     {
         this._data = data;
         this.BreakBGP();
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Creates a new BINDINGS modifier
 /// </summary>
 /// <param name="bindings">Bindings</param>
 /// <param name="pattern">Pattern</param>
 public Bindings(BindingsPattern bindings, ISparqlAlgebra pattern)
 {
     this._bindings = bindings;
     this._pattern = pattern;
 }
Ejemplo n.º 4
0
        private void TryParseBindingsClause(SparqlQueryParserContext context)
        {
            //First expect one/more variables
            IToken next = context.Tokens.Peek();
            List<String> vars = new List<String>();
            while (next.TokenType == Token.VARIABLE)
            {
                vars.Add(next.Value.Substring(1));
                context.Tokens.Dequeue();
                next = context.Tokens.Peek();
            }
            if (vars.Count == 0)
            {
                //If No Variables then expect an empty BINDINGS { }
                next = context.Tokens.Peek();
                if (next.TokenType != Token.LEFTCURLYBRACKET)
                {
                    throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected either one/more Variable Tokens or the empty set { } after a BINDINGS keyword", next);
                }
            }

            //Then expect a Left Curly Bracket
            if (next.TokenType == Token.LEFTCURLYBRACKET)
            {
                context.Tokens.Dequeue();
                BindingsPattern bindings = new BindingsPattern(vars);

                //Each Binding tuple must start with a (
                next = context.Tokens.Peek();
                while (next.TokenType == Token.LEFTBRACKET)
                {
                    //Discard the ( and peek the next token
                    context.Tokens.Dequeue();
                    next = context.Tokens.Peek();

                    //Expect a sequence of values in the tuple
                    List<PatternItem> values = new List<PatternItem>();
                    while (next.TokenType != Token.RIGHTBRACKET)
                    {
                        next = context.Tokens.Dequeue();

                        //Get the value
                        switch (next.TokenType)
                        {
                            case Token.URI:
                            case Token.QNAME:
                            case Token.LITERALWITHDT:
                            case Token.LITERALWITHLANG:
                            case Token.PLAINLITERAL:
                                values.Add(this.TryCreatePatternItem(context, next));
                                break;

                            case Token.LONGLITERAL:
                            case Token.LITERAL:
                                //Need to check for subsequent datatype or language declaration
                                IToken lit = next;
                                next = context.Tokens.Peek();
                                if (next.TokenType == Token.HATHAT)
                                {
                                    context.Tokens.Dequeue();
                                    next = context.Tokens.Dequeue();
                                    if (next.TokenType == Token.DATATYPE)
                                    {
                                        LiteralWithDataTypeToken dtlit = new LiteralWithDataTypeToken(lit, (DataTypeToken)next);
                                        values.Add(this.TryCreatePatternItem(context, dtlit));
                                    }
                                    else
                                    {
                                        throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Datatype Token to specify the datatype for a Literal", next);
                                    }
                                }
                                else if (next.TokenType == Token.LANGSPEC)
                                {
                                    context.Tokens.Dequeue();
                                    LiteralWithLanguageSpecifierToken langlit = new LiteralWithLanguageSpecifierToken(lit, (LanguageSpecifierToken)next);
                                    values.Add(this.TryCreatePatternItem(context, langlit));
                                }
                                else
                                {
                                    values.Add(this.TryCreatePatternItem(context, lit));
                                }
                                break;

                            case Token.UNDEF:
                                //UNDEF indicates an unbound variable which equates to a null
                                values.Add(null);
                                break;

                            default:
                                throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Token for a URI/Literal or an UNDEF keyword as part of a tuple in a BINDINGS clause", next);
                        }

                        next = context.Tokens.Peek();
                    }

                    if (vars.Count != values.Count)
                    {
                        throw new RdfParseException("Invalid tuple in the BINDINGS clause, each Binding should contain " + vars.Count + " values but got a tuple containing " + values.Count + " values");
                    }

                    //Generate a representation of this possible solution and add it to our Bindings object
                    bindings.AddTuple(new BindingTuple(vars, values));

                    //Discard the ) and peek the next token
                    context.Tokens.Dequeue();
                    next = context.Tokens.Peek();
                }

                //Set the Query's BINDINGS clause
                context.Query.Bindings = bindings;

                //Finally we need to see a Right Curly Bracket
                if (next.TokenType != Token.RIGHTCURLYBRACKET)
                {
                    throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Right Curly Bracket to terminate the BINDINGS clause", next);
                }
                context.Tokens.Dequeue();
            }
            else
            {
                throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Left Curly Bracket after the list of variables as part of a BINDINGS clause", next);
            }
        }