internal SparqlResult(object resultObject, SparqlQueryContext sparqlQueryContext) { if (resultObject == null) { throw new ArgumentNullException(nameof(resultObject)); } if (sparqlQueryContext == null) { throw new ArgumentNullException(nameof(sparqlQueryContext)); } SourceSparqlQueryContext = sparqlQueryContext; ResultGraph = resultObject as IGraph; if (ResultGraph == null) { ResultSet = resultObject as SparqlResultSet; if (ResultSet == null) { throw new ArgumentException( $"Result object must be either a {typeof (IGraph).FullName} or a {typeof (SparqlResultSet).FullName} instance. Got a {resultObject.GetType().FullName}"); } ResultFormat = sparqlQueryContext.SparqlResultsFormat ?? SparqlResultsFormat.Xml; } if (resultObject is IGraph) { ResultGraph = resultObject as IGraph; ResultFormat = sparqlQueryContext.GraphResultsFormat ?? RdfFormat.RdfXml; } }
private IEnumerable <Triple> GetTriplesForDataObject(string identity) { var queryContext = new SparqlQueryContext(string.Format(QueryTemplate, identity)); queryContext.SparqlResultsFormat = SparqlResultsFormat.Xml; queryContext.GraphResultsFormat = RdfFormat.NTriples; var results = Client.ExecuteQuery(queryContext, DataSetGraphUris); foreach (var row in results.ResultSet) { // create new triple var triple = new Triple { Subject = identity, Graph = row["g"].ToString(), Predicate = row["p"].ToString() }; var literal = row["o"] as ILiteralNode; if (literal != null) { var dt = literal.DataType?.ToString(); triple.LangCode = literal.Language; triple.DataType = dt ?? RdfDatatypes.String; triple.Object = literal.Value; triple.IsLiteral = true; } else { triple.Object = row["o"].ToString().Trim(); } yield return(triple); } }
internal SparqlResult(TextReader streamReader, ISerializationFormat resultFormat, SparqlQueryContext sparqlQueryContext) { ResultFormat = resultFormat; ParseResult(streamReader); SourceSparqlQueryContext = sparqlQueryContext; }
public SparqlResult ExecuteQuery(SparqlQueryContext queryContext, IList <string> datasetGraphUris) { ISerializationFormat resultFormat; var resultStream = _client.ExecuteQuery(_storeName, queryContext.SparqlQuery, datasetGraphUris, null, queryContext.SparqlResultsFormat, queryContext.GraphResultsFormat, out resultFormat); return(new SparqlResult(resultStream, resultFormat, queryContext)); }
public SparqlResult ExecuteQuery(SparqlQueryContext queryContext, IList <string> datasetGraphUris) { var parser = new SparqlQueryParser(); var query = parser.ParseFromString(queryContext.SparqlQuery); var sparqlResults = _queryProcessor.ProcessQuery(query); return(new SparqlResult(sparqlResults, queryContext)); }
public override SparqlResult ExecuteSparql(SparqlQueryContext sparqlQueryContext) { var resultStream = new MemoryStream(); _serverCore.Query(_storeName, sparqlQueryContext.SparqlQuery, DataSetGraphUris, null, SparqlResultsFormat.Xml, RdfFormat.RdfXml, resultStream); resultStream.Seek(0, SeekOrigin.Begin); return(new SparqlResult(resultStream, sparqlQueryContext)); }
internal SparqlResult(Stream resultStream, ISerializationFormat resultFormat, SparqlQueryContext sparqlQueryContext) { if (resultStream == null) { throw new ArgumentNullException(nameof(resultStream)); } ResultFormat = resultFormat; SourceSparqlQueryContext = sparqlQueryContext; using (var reader = new StreamReader(resultStream)) { ParseResult(reader); } }
public abstract SparqlResult ExecuteSparql(SparqlQueryContext sparqlQueryContext);
internal SparqlResult(string resultString, ISerializationFormat resultFormat, SparqlQueryContext sparqlQueryContext) : this(new StringReader(resultString), resultFormat, sparqlQueryContext) { }
public override SparqlResult ExecuteSparql(SparqlQueryContext sparqlQueryContext) { return(new SparqlResult(Client.ExecuteQuery(sparqlQueryContext.SparqlQuery, DataSetGraphUris), sparqlQueryContext)); }
/// <summary> /// Method to execute a SPARQL query against the underlying store and bind its results to instances of a class /// </summary> /// <typeparam name="T">The type that the SPARQL query results are to be bound to</typeparam> /// <param name="sparqlQueryContext">The context object that specifies the SPARQL query and constructor and member mappings</param> /// <returns>An enumeration over the bound result objects</returns> public abstract IEnumerable <T> ExecuteQuery <T>(SparqlQueryContext sparqlQueryContext);
internal SparqlResult(string xml, SparqlQueryContext sparqlQueryContext) { _resultString = xml; SourceSparqlQueryContext = sparqlQueryContext; }
internal SparqlResult(Stream resultStream, SparqlQueryContext sparqlQueryContext) { _resultStream = resultStream; SourceSparqlQueryContext = sparqlQueryContext; }
/// <summary> /// Executes a SPARQL query against the underlying store and binds the results to /// domain context objects /// </summary> /// <typeparam name="T">The type of domain context object to bind to</typeparam> /// <param name="sparqlQueryContext">The query to be executed</param> /// <returns>An enumeration over the bound objects</returns> /// <remarks>The SPARQL query should be written to return a single variable binding. /// Results that bind the variable to anything other than a resource URI are ignored.</remarks> public override IEnumerable <T> ExecuteQuery <T>(SparqlQueryContext sparqlQueryContext) { var bindType = GetImplType(typeof(T)); if (typeof(BrightstarEntityObject).IsAssignableFrom(bindType)) { foreach (var dataObject in _store.BindDataObjectsWithSparql(sparqlQueryContext.SparqlQuery)) { yield return(BindDataObject <T>(dataObject, bindType)); } } else if (IsAnonymousType(typeof(T))) { var anonymousConstructorArgs = new List <AnonymousConstructorArg>(); foreach (var tuple in sparqlQueryContext.AnonymousMembersMap) { var propertyInfo = typeof(T).GetProperty(tuple.Item1); if (propertyInfo == null) { throw new EntityFrameworkException("No property named '{0}' on anonymous type", tuple.Item1); } var propertyType = propertyInfo.PropertyType; var converter = GetStringConverter(propertyType); if (converter == null) { throw new EntityFrameworkException("No converter available for type '{0}'", propertyType.FullName); } object defaultValue = propertyType.IsValueType ? Activator.CreateInstance(propertyType) : null; anonymousConstructorArgs.Add(new AnonymousConstructorArg { PropertyName = tuple.Item1, VariableName = tuple.Item2, ValueConverter = converter, DefaultValue = defaultValue }); } var sparqlResult = _store.ExecuteSparql(sparqlQueryContext.SparqlQuery); var resultDoc = sparqlResult.ResultDocument; foreach (var row in resultDoc.SparqlResultRows()) { var args = new object[anonymousConstructorArgs.Count]; for (int i = 0; i < anonymousConstructorArgs.Count; i++) { var argInfo = anonymousConstructorArgs[i]; var colValue = row.GetColumnValue(argInfo.VariableName); var colLang = row.GetLiteralLanguageCode(argInfo.VariableName); args[i] = colValue == null ? argInfo.DefaultValue : argInfo.ValueConverter(colValue.ToString(), colLang); } yield return((T)Activator.CreateInstance(typeof(T), args)); } } else if (sparqlQueryContext.HasMemberInitExpression) { var sparqlResult = _store.ExecuteSparql(sparqlQueryContext.SparqlQuery); var resultDoc = sparqlResult.ResultDocument; foreach (var row in resultDoc.SparqlResultRows()) { var values = new Dictionary <string, object>(); foreach (var c in resultDoc.GetVariableNames()) { values[c] = row.GetColumnValue(c); } var value = sparqlQueryContext.ApplyMemberInitExpression <T>(values, ConvertString); yield return((T)value); } } else { var sparqlResult = _store.ExecuteSparql(sparqlQueryContext.SparqlQuery); var resultDoc = sparqlResult.ResultDocument; var converter = GetStringConverter(typeof(T)); if (converter == null) { throw new EntityFrameworkException("No SPARQL results conversion found from string to type '{0}'", typeof(T).FullName); } foreach (var row in resultDoc.SparqlResultRows()) { var value = row.GetColumnValue(0); if (value.GetType() == typeof(T)) { yield return((T)value); } else { yield return((T)converter(row.GetColumnValue(0).ToString(), row.GetLiteralLanguageCode(0))); } } } }
public override IEnumerable <T> ExecuteInstanceQuery <T>(string instanceIdentifier, string typeIdentifier) { _lastQuery = String.Format("ASK {{ <{0}> a <{1}>. }}", instanceIdentifier, typeIdentifier); _lastQueryContext = null; yield break; }
public override IEnumerable <T> ExecuteQuery <T>(SparqlQueryContext sparqlQuery) { _lastQuery = sparqlQuery.SparqlQuery; _lastQueryContext = sparqlQuery; yield break; }