/// <summary>Executes a SPARQL query and returns resulting quads</summary> /// <param name="queryModel">Query model to be executed.</param> /// <returns>Enumeration of entity quads beeing a result of the query.</returns> public IEnumerable<EntityQuad> ExecuteEntityQuery(Query queryModel) { SparqlQueryVariables variables; var resultSet = ExecuteSelect(GetSparqlQuery(queryModel, out variables)); return from result in resultSet let id = (result[variables.Entity] is IBlankNode ? new BlankId(((IBlankNode)result[variables.Entity]).InternalID, null, ((IUriNode)result[variables.MetaGraph]).Uri) : new EntityId(((IUriNode)result[variables.Entity]).Uri)) let s = result[variables.Subject].WrapNode(id) let p = result[variables.Predicate].WrapNode(id) let o = result[variables.Object].WrapNode(id) let g = result[variables.MetaGraph].WrapNode(id) select new EntityQuad(id, s, p, o, g); }
/// <summary>Returns a resulting collection of a query.</summary> /// <typeparam name="T">Type of elements to be returned.</typeparam> /// <param name="queryModel">Query model to be parsed.</param> /// <returns>Enumeration of resulting entities matching given query.</returns> public IEnumerable <T> ExecuteCollection <T>(QueryModel queryModel) { IEnumerable <CastResultOperator> resultOperators = queryModel.ResultOperators.OfType <CastResultOperator>().ToArray(); foreach (CastResultOperator resultOperator in resultOperators) { queryModel.ResultOperators.Remove(resultOperator); } Model.Query sparqlQuery = VisitQueryModel(queryModel); IEnumerable <EntityId> actualEntities; IEnumerable <RomanticWeb.Model.IEntityQuad> quads = _entitySource.ExecuteEntityQuery(sparqlQuery, out actualEntities); IEnumerable <T> result = (!typeof(IEntity).IsAssignableFrom(typeof(T)) ? CreateLiteralResultSet <T>(quads, actualEntities) : CreateEntityResultSet <T>(quads, actualEntities)); foreach (CastResultOperator resultOperator in resultOperators) { var castMethod = EnumerableCastMethod.MakeGenericMethod(resultOperator.CastItemType); result = (IEnumerable <T>)castMethod.Invoke(null, new object[] { result }); } return(result); }
/// <summary>Visits a select clause.</summary> /// <param name="selectClause">Select clause to be visited.</param> /// <param name="queryModel">Query model containing given select clause.</param> public override void VisitSelectClause(SelectClause selectClause, Remotion.Linq.QueryModel queryModel) { QuerySourceReferenceExpression querySource = FindQuerySource(selectClause.Selector); _visitor.VisitExpression(selectClause.Selector); QueryComponent component = _visitor.RetrieveComponent(); _query = _visitor.Query; _mainFromComponent = _query.FindAllComponents<StrongEntityAccessor>().Where(item => item.SourceExpression == querySource.ReferencedQuerySource).First(); if (_query.Subject == null) { _query.Subject = _mainFromComponent.About; UnboundConstrain genericConstrain = new UnboundConstrain(new Identifier("s"), new Identifier("p"), new Identifier("o"), _mainFromComponent.SourceExpression.FromExpression); _mainFromComponent.Elements.Insert(0, genericConstrain); _query.Select.Add(genericConstrain); } _subject = _query.Subject; queryModel.MainFromClause.Accept(this, queryModel); _query.Select.Add(_mainFromComponent); VisitBodyClauses(queryModel.BodyClauses, queryModel); VisitResultOperators(queryModel.ResultOperators, queryModel); OverrideSelector(component, selectClause.Selector); }
/// <summary>Visit a query.</summary> /// <param name="query">Query to be visited.</param> public override void VisitQuery(Query query) { InitQuery(query.IsSubQuery ? null : query.Prefixes); BeginQuery(query.IsSubQuery, query.QueryForm, query.Select, query.CreateVariableName); _entityAccessorToExpand = ((query.Limit >= 0) || (query.Offset >= 0) || (query.OrderBy.Count > 0) ? (StrongEntityAccessor)query.Elements.LastOrDefault(item => item is StrongEntityAccessor) : null); foreach (QueryElement element in query.Elements) { VisitComponent(element); } EndQuery(query.IsSubQuery, (_entityAccessorToExpand != null ? query.OrderBy : null)); }
/// <summary>Creates a new blank query that can act as a sub query for this instance.</summary> /// <param name="subject">Primary subject of the resulting query.</param> /// <remarks>This method doesn't add the resulting query as a sub query of this instance.</remarks> /// <returns>Query that can act as a sub query for this instance.</returns> public Query CreateSubQuery(Identifier subject) { Query result = new Query(subject, _variableNamingStrategy); result.OwnerQuery = this; return result; }
protected GenericSparqlQueryVisitor VisitModel(Query queryModel) { GenericSparqlQueryVisitor visitor = new GenericSparqlQueryVisitor(); visitor.MetaGraphUri = new Uri("http://app.magi/graphs"); visitor.VisitQuery(queryModel); return visitor; }
/// <summary>Visit a query.</summary> /// <param name="query">Query to be visited.</param> public abstract void VisitQuery(Query query);
/// <summary>Executes a SPARQL ask query.</summary> /// <param name="queryModel">Query model to be executed.</param> /// <returns><b>true</b> in case a given query has solution, otherwise <b>false</b>.</returns> public bool ExecuteAskQuery(Query queryModel) { return ExecuteSelect(GetSparqlQuery(queryModel)).Result; }
/// <summary>Executes a SPARQL query with scalar result.</summary> /// <param name="queryModel">Query model to be executed.</param> /// <returns>Scalar value beeing a result of the query.</returns> public int ExecuteScalarQuery(Query queryModel) { SparqlQueryVariables variables; var resultSet = ExecuteSelect(GetSparqlQuery(queryModel, out variables)); return (from result in resultSet select Int32.Parse(((ILiteralNode)result[variables.Scalar]).Value)).FirstOrDefault(); }
private SparqlQuery GetSparqlQuery(Query sparqlQuery, out SparqlQueryVariables variables) { GenericSparqlQueryVisitor queryVisitor = new SparqlQueryVisitor(); queryVisitor.MetaGraphUri = MetaGraphUri; queryVisitor.VisitQuery(sparqlQuery); variables = queryVisitor.Variables; LogTo.Info("RomanticWeb.dotNetRDF.TripleStoreAdapter parsed query: {0}", queryVisitor.CommandText); SparqlQueryParser parser = new SparqlQueryParser(); return parser.ParseFromString(queryVisitor.CommandText); }
private SparqlQuery GetSparqlQuery(Query sparqlQuery) { SparqlQueryVariables variables; return GetSparqlQuery(sparqlQuery, out variables); }
/// <inheritdoc /> public string GetCommandText(QueryModel queryModel) { Model.Query sparqlQuery = VisitQueryModel(queryModel); return(_entitySource.GetCommandText(sparqlQuery)); }
/// <inheritdoc /> public Query Optimize(Query query) { return query; }
/// <summary>Default constructor with query context provided.</summary> /// <param name="context">Query context to be used as name distinction mechanism.</param> internal UniqueVariableNamingStrategy(Query context) { _context = context; }
internal EntityQueryModelVisitor(Query query, IEntityContext context) { _entityContext = context; _visitor = new EntityQueryVisitor(_query = (Query)(_result = query), _entityContext); _subject = null; }