示例#1
0
 /// <summary>
 /// Creates a SPARQL Result Set for the Results of a Query with the Leviathan Engine.
 /// </summary>
 /// <param name="context">SPARQL Evaluation Context.</param>
 public SparqlResultSet(SparqlEvaluationContext context)
 {
     _type = (context.Query.QueryType == SparqlQueryType.Ask) ? SparqlResultsType.Boolean : SparqlResultsType.VariableBindings;
     if (context.OutputMultiset is NullMultiset)
     {
         _result = false;
     }
     else if (context.OutputMultiset is IdentityMultiset)
     {
         _result = true;
         foreach (String var in context.OutputMultiset.Variables)
         {
             _variables.Add(var);
         }
     }
     else
     {
         _result = true;
         foreach (String var in context.OutputMultiset.Variables)
         {
             _variables.Add(var);
         }
         foreach (ISet s in context.OutputMultiset.Sets)
         {
             AddResult(new SparqlResult(s, context.OutputMultiset.Variables));
         }
     }
 }
示例#2
0
 /// <summary>
 /// Adds a Variable to the Result Set.
 /// </summary>
 /// <param name="var">Variable Name.</param>
 internal void AddVariable(String var)
 {
     if (!_variables.Contains(var))
     {
         _variables.Add(var);
     }
     _type = SparqlResultsType.VariableBindings;
 }
 /// <summary>
 /// Adds a Variable to the Result Set
 /// </summary>
 /// <param name="var">Variable Name</param>
 protected internal void AddVariable(String var)
 {
     if (!this._variables.Contains(var))
     {
         this._variables.Add(var);
     }
     this._type = SparqlResultsType.VariableBindings;
 }
示例#4
0
        /// <summary>
        /// Starts writing results
        /// </summary>
        protected override void StartResultsInternal()
        {
            if (this._closeOnEnd && this._writer == null)
            {
                throw new RdfParseException("Cannot use this ResultWriteThroughHandler as an Results Handler for parsing as you set closeOnEnd to true and you have already used this Handler and so the provided TextWriter was closed");
            }
            this._currentType = SparqlResultsType.Unknown;
            this._currVariables.Clear();
            this._headerWritten = false;

            if (this._formatterType != null)
            {
                this._formatter        = null;
                this._formattingMapper = new QNameOutputMapper();

                // Instantiate a new Formatter
                ConstructorInfo[] cs = this._formatterType.GetConstructors();
                Type qnameMapperType = typeof(QNameOutputMapper);
                Type nsMapperType    = typeof(INamespaceMapper);
                foreach (ConstructorInfo c in cs.OrderByDescending(c => c.GetParameters().Count()))
                {
                    ParameterInfo[] ps = c.GetParameters();
                    try
                    {
                        if (ps.Length == 1)
                        {
                            if (ps[0].ParameterType.Equals(qnameMapperType))
                            {
                                this._formatter = Activator.CreateInstance(this._formatterType, new Object[] { this._formattingMapper }) as IResultFormatter;
                            }
                            else if (ps[0].ParameterType.Equals(nsMapperType))
                            {
                                this._formatter = Activator.CreateInstance(this._formatterType, new Object[] { this._formattingMapper }) as IResultFormatter;
                            }
                        }
                        else if (ps.Length == 0)
                        {
                            this._formatter = Activator.CreateInstance(this._formatterType) as IResultFormatter;
                        }

                        if (this._formatter != null)
                        {
                            break;
                        }
                    }
                    catch
                    {
                        // Suppress errors since we'll throw later if necessary
                    }
                }

                // If we get out here and the formatter is null then we throw an error
                if (this._formatter == null)
                {
                    throw new RdfParseException("Unable to instantiate a IResultFormatter from the given Formatter Type " + this._formatterType.FullName);
                }
            }
        }
示例#5
0
 /// <summary>
 /// Adds a Result to the Result Set.
 /// </summary>
 /// <param name="result">Result.</param>
 internal void AddResult(SparqlResult result)
 {
     if (_type == SparqlResultsType.Boolean)
     {
         throw new RdfException("Cannot add a Variable Binding Result to a Boolean Result Set");
     }
     _results.Add(result);
     _type = SparqlResultsType.VariableBindings;
 }
示例#6
0
 /// <summary>
 /// Writes a Variable declaration to the output
 /// </summary>
 /// <param name="var">Variable Name</param>
 /// <returns></returns>
 protected override bool HandleVariableInternal(string var)
 {
     if (this._currentType == SparqlResultsType.Boolean)
     {
         throw new RdfParseException("Cannot handler a Variable when the handler has already handled a boolean result");
     }
     this._currentType = SparqlResultsType.VariableBindings;
     this._currVariables.Add(var);
     return(true);
 }
示例#7
0
        /// <summary>
        /// Creates a Sparql Result Set for the collection of results.
        /// </summary>
        /// <param name="results">Results.</param>
        public SparqlResultSet(IEnumerable <SparqlResult> results)
        {
            _type    = SparqlResultsType.VariableBindings;
            _results = results.ToList();

            if (_results.Any())
            {
                _variables = _results.First().Variables.ToList();
            }
        }
示例#8
0
 /// <summary>
 /// Sets the Boolean Result for the Result Set.
 /// </summary>
 /// <param name="result">Boolean Result.</param>
 internal void SetResult(bool result)
 {
     if (_type != SparqlResultsType.Unknown)
     {
         throw new RdfException("Cannot set the Boolean Result value for this Result Set as its Result Type has already been set");
     }
     _result = result;
     if (_type == SparqlResultsType.Unknown)
     {
         _type = SparqlResultsType.Boolean;
     }
 }
示例#9
0
 /// <summary>
 /// Writes a Result to the output
 /// </summary>
 /// <param name="result">SPARQL Result</param>
 /// <returns></returns>
 protected override bool HandleResultInternal(SparqlResult result)
 {
     if (this._currentType == SparqlResultsType.Boolean)
     {
         throw new RdfParseException("Cannot handle a Result when the handler has already handled a boolean result");
     }
     this._currentType = SparqlResultsType.VariableBindings;
     if (!this._headerWritten && this._formatter is IResultSetFormatter)
     {
         this._writer.WriteLine(((IResultSetFormatter)this._formatter).FormatResultSetHeader(this._currVariables.Distinct()));
         this._headerWritten = true;
     }
     this._writer.WriteLine(this._formatter.Format(result));
     return(true);
 }
示例#10
0
        /// <summary>
        /// Writes a Boolean Result to the output
        /// </summary>
        /// <param name="result">Boolean Result</param>
        protected override void HandleBooleanResultInternal(bool result)
        {
            if (this._currentType != SparqlResultsType.Unknown)
            {
                throw new RdfParseException("Cannot handle a Boolean Result when the handler has already handled other types of results");
            }
            this._currentType = SparqlResultsType.Boolean;
            if (!this._headerWritten && this._formatter is IResultSetFormatter)
            {
                this._writer.WriteLine(((IResultSetFormatter)this._formatter).FormatResultSetHeader());
                this._headerWritten = true;
            }

            this._writer.WriteLine(this._formatter.FormatBooleanResult(result));
        }
示例#11
0
 /// <summary>
 /// Ends the writing of results closing the <see cref="TextWriter">TextWriter</see> depending on the option set when this instance was instantiated
 /// </summary>
 /// <param name="ok"></param>
 protected override void EndResultsInternal(bool ok)
 {
     if (this._formatter is IResultSetFormatter)
     {
         this._writer.WriteLine(((IResultSetFormatter)this._formatter).FormatResultSetFooter());
     }
     if (this._closeOnEnd)
     {
         this._writer.Close();
         this._writer = null;
     }
     this._currentType = SparqlResultsType.Unknown;
     this._currVariables.Clear();
     this._headerWritten = false;
 }
 public ResultSetDeserializationInfo(SerializationInfo info, StreamingContext context)
 {
     this._type = (SparqlResultsType)info.GetValue("type", typeof(SparqlResultsType));
     switch (this._type)
     {
         case SparqlResultsType.Boolean:
             this._r = info.GetBoolean("result");
             break;
         case SparqlResultsType.VariableBindings:
             this._vars = (List<String>)info.GetValue("variables", typeof(List<String>));
             this._results = (List<SparqlResult>)info.GetValue("results", typeof(List<SparqlResult>));
             break;
         default:
             throw new RdfParseException("The type property of a serialized SparqlResultSet did not contain a valid value");
     }
 }
        public ResultSetDeserializationInfo(SerializationInfo info, StreamingContext context)
        {
            _type = (SparqlResultsType)info.GetValue("type", typeof(SparqlResultsType));
            switch (_type)
            {
            case SparqlResultsType.Boolean:
                _r = info.GetBoolean("result");
                break;

            case SparqlResultsType.VariableBindings:
                _vars    = (List <String>)info.GetValue("variables", typeof(List <String>));
                _results = (List <SparqlResult>)info.GetValue("results", typeof(List <SparqlResult>));
                break;

            default:
                throw new RdfParseException("The type property of a serialized SparqlResultSet did not contain a valid value");
            }
        }
示例#14
0
        /// <summary>
        /// Reads the data for XML deserialization (.Net serialization not the official SPARQL results serialization).
        /// </summary>
        /// <param name="reader">XML Reader.</param>
        public void ReadXml(XmlReader reader)
        {
            reader.Read();
            switch (reader.Name)
            {
            case "boolean":
                _type   = SparqlResultsType.Boolean;
                _result = reader.ReadElementContentAsBoolean();
                break;

            case "variables":
                _type   = SparqlResultsType.VariableBindings;
                _result = true;
                reader.Read();
                while (reader.Name.Equals("variable"))
                {
                    _variables.Add(reader.ReadElementContentAsString());
                }
                reader.Read();
                if (reader.Name.Equals("results"))
                {
                    reader.Read();
                    while (reader.Name.Equals("result"))
                    {
                        _results.Add(reader.DeserializeResult());
                    }
                }
                else
                {
                    throw new RdfParseException("Unable to deserialize a SparqlResultSet as did not get the expected <results> element after the <variables> element");
                }
                break;

            default:
                throw new RdfParseException("Unable to deserialize a SparqlResultSet, expected a <boolean> or <results> element after the <resultSet> element");
            }
        }
示例#15
0
 /// <summary>
 /// Ends the writing of results closing the <see cref="TextWriter">TextWriter</see> depending on the option set when this instance was instantiated
 /// </summary>
 /// <param name="ok"></param>
 protected override void EndResultsInternal(bool ok)
 {
     if (this._formatter is IResultSetFormatter)
     {
         this._writer.WriteLine(((IResultSetFormatter)this._formatter).FormatResultSetFooter());
     }
     if (this._closeOnEnd)
     {
         this._writer.Close();
         this._writer = null;
     }
     this._currentType = SparqlResultsType.Unknown;
     this._currVariables.Clear();
     this._headerWritten = false;
 }
示例#16
0
 /// <summary>
 /// Writes a Result to the output
 /// </summary>
 /// <param name="result">SPARQL Result</param>
 /// <returns></returns>
 protected override bool HandleResultInternal(SparqlResult result)
 {
     if (this._currentType == SparqlResultsType.Boolean) throw new RdfParseException("Cannot handle a Result when the handler has already handled a boolean result");
     this._currentType = SparqlResultsType.VariableBindings;
     if (!this._headerWritten && this._formatter is IResultSetFormatter)
     {
         this._writer.WriteLine(((IResultSetFormatter)this._formatter).FormatResultSetHeader(this._currVariables.Distinct()));
         this._headerWritten = true;
     }
     this._writer.WriteLine(this._formatter.Format(result));
     return true;
 }
示例#17
0
 /// <summary>
 /// Writes a Variable declaration to the output
 /// </summary>
 /// <param name="var">Variable Name</param>
 /// <returns></returns>
 protected override bool HandleVariableInternal(string var)
 {
     if (this._currentType == SparqlResultsType.Boolean) throw new RdfParseException("Cannot handler a Variable when the handler has already handled a boolean result");
     this._currentType = SparqlResultsType.VariableBindings;
     this._currVariables.Add(var);
     return true;
 }
示例#18
0
        /// <summary>
        /// Starts writing results
        /// </summary>
        protected override void StartResultsInternal()
        {
            if (this._closeOnEnd && this._writer == null) throw new RdfParseException("Cannot use this ResultWriteThroughHandler as an Results Handler for parsing as you set closeOnEnd to true and you have already used this Handler and so the provided TextWriter was closed");
            this._currentType = SparqlResultsType.Unknown;
            this._currVariables.Clear();
            this._headerWritten = false;

            if (this._formatterType != null)
            {
                this._formatter = null;
                this._formattingMapper = new QNameOutputMapper();

                //Instantiate a new Formatter
                ConstructorInfo[] cs = this._formatterType.GetConstructors();
                Type qnameMapperType = typeof(QNameOutputMapper);
                Type nsMapperType = typeof(INamespaceMapper);
                foreach (ConstructorInfo c in cs.OrderByDescending(c => c.GetParameters().Count()))
                {
                    ParameterInfo[] ps = c.GetParameters();
                    try
                    {
                        if (ps.Length == 1)
                        {
                            if (ps[0].ParameterType.Equals(qnameMapperType))
                            {
                                this._formatter = Activator.CreateInstance(this._formatterType, new Object[] { this._formattingMapper }) as IResultFormatter;
                            }
                            else if (ps[0].ParameterType.Equals(nsMapperType))
                            {
                                this._formatter = Activator.CreateInstance(this._formatterType, new Object[] { this._formattingMapper }) as IResultFormatter;
                            }
                        }
                        else if (ps.Length == 0)
                        {
                            this._formatter = Activator.CreateInstance(this._formatterType) as IResultFormatter;
                        }

                        if (this._formatter != null) break;
                    }
                    catch
                    {
                        //Suppress errors since we'll throw later if necessary
                    }
                }

                //If we get out here and the formatter is null then we throw an error
                if (this._formatter == null) throw new RdfParseException("Unable to instantiate a IResultFormatter from the given Formatter Type " + this._formatterType.FullName);
            }
        }
示例#19
0
 /// <summary>
 /// Creates a Sparql Result Set for the Results of an ASK Query with the given Result value
 /// </summary>
 /// <param name="result"></param>
 public SparqlResultSet(bool result)
 {
     this._result = result;
     this._type   = SparqlResultsType.Boolean;
 }
示例#20
0
        /// <summary>
        /// Writes a Boolean Result to the output
        /// </summary>
        /// <param name="result">Boolean Result</param>
        protected override void HandleBooleanResultInternal(bool result)
        {
            if (this._currentType != SparqlResultsType.Unknown) throw new RdfParseException("Cannot handle a Boolean Result when the handler has already handled other types of results");
            this._currentType = SparqlResultsType.Boolean;
            if (!this._headerWritten && this._formatter is IResultSetFormatter)
            {
                this._writer.WriteLine(((IResultSetFormatter)this._formatter).FormatResultSetHeader());
                this._headerWritten = true;
            }

            this._writer.WriteLine(this._formatter.FormatBooleanResult(result));
        }
示例#21
0
 /// <summary>
 /// Creates a Sparql Result Set for the Results of an ASK Query with the given Result value.
 /// </summary>
 /// <param name="result"></param>
 public SparqlResultSet(bool result)
 {
     _result = result;
     _type   = SparqlResultsType.Boolean;
 }