Exemplo n.º 1
0
        /// <summary>
        /// Builds the triples required to access a given member and accociates its value with a variable.
        /// </summary>
        /// <param name="memberExpression">The member to be accessed.</param>
        /// <returns>The object variable associated with the member value.</returns>
        protected SparqlVariable BuildMemberAccessOptional(MemberExpression memberExpression)
        {
            var optionalBuilder = new GraphPatternBuilder(GraphPatternType.Optional);

            var so = BuildMemberAccess(memberExpression, optionalBuilder);

            Child(optionalBuilder);

            return(so);
        }
Exemplo n.º 2
0
        public override void OnBeforeFromClauseVisited(Expression expression)
        {
            SparqlVariable s = null;
            SparqlVariable o = null;

            if (expression is MemberExpression)
            {
                QuerySourceReferenceExpression sourceExpression = expression.TryGetQuerySourceReference();

                s = VariableGenerator.TryGetSubjectVariable(sourceExpression) ?? VariableGenerator.TryGetObjectVariable(sourceExpression);
                o = VariableGenerator.CreateObjectVariable(expression);

                // The from clause is parsed first when handling a query. This allows us to detect if the
                // query source is a subquery and proceed with implementing it _before_ hanlding its results.
                MemberExpression memberExpression = expression as MemberExpression;

                if (s.IsGlobal())
                {
                    Type type = memberExpression.Member.DeclaringType;

                    if (type.IsSubclassOf(typeof(Resource)))
                    {
                        WhereResourceOfType(s, type);
                    }
                }

                // If the query model has a numeric result operator, we make all the following
                // expressions optional in order to also allow to count zero occurences.
                if (QueryModel.HasNumericResultOperator())
                {
                    GraphPatternBuilder optionalBuilder = new GraphPatternBuilder(GraphPatternType.Optional);

                    Child(optionalBuilder);

                    PatternBuilder = optionalBuilder;
                }
            }
            else
            {
                s = VariableGenerator.TryGetSubjectVariable(expression);
                o = VariableGenerator.TryGetObjectVariable(expression);
            }

            if (s != null && o != null)
            {
                // Set the variable name of the query source reference as subject of the current query.
                SetSubjectVariable(s, true);
                SetObjectVariable(o, true);
            }
        }
Exemplo n.º 3
0
        public IGraphPatternBuilder Child(ISparqlQueryGenerator generator)
        {
            generator.BindSelectVariables();

            var subQuery = generator.QueryBuilder.BuildQuery();

            var childBuilder = new GraphPatternBuilder();

            childBuilder.Where(new SubQueryPattern(subQuery));

            // Note: This sets the enclosing pattern builder as the current pattern
            // builder in order to build subsequent FILTERs on the selected variables
            // into the enclosing block, rather than the parent query. This is because
            // for OpenLink Virtuoso the FILTERs need to be inside the enclosing group
            // of the subquery..
            generator.PatternBuilder = childBuilder;

            return(PatternBuilder.Child(childBuilder));
        }
        /// <summary>
        /// Converts a <see cref="GraphDiffReport">diff</see> to an equivalent <see cref="ModifyCommand">SPARQL Update INSERT/DELETE command</see>.
        /// </summary>
        /// <param name="diff">The <see cref="GraphDiffReport">diff</see> to convert.</param>
        /// <param name="graphUri">Optional <see cref="Uri">URI</see> of the affected graph.</param>
        /// <param name="prefixes">Optional <see cref="INamespaceMapper">mapper</see> used to resolve prefixes.</param>
        /// <returns>A <see cref="ModifyCommand">SPARQL Update INSERT/DELETE command</see> that represents the <see cref="GraphDiffReport">diff</see>.</returns>
        public static ModifyCommand AsUpdate(this GraphDiffReport diff, Uri graphUri = null, INamespaceMapper prefixes = null)
        {
            var delete = new GraphPatternBuilder();
            var insert = new GraphPatternBuilder();

            var where = new GraphPatternBuilder();

            // Removed ground triples are added as is to both delete and where clauses
            foreach (var t in diff.RemovedTriples)
            {
                delete.AddTriplePattern(t);
                where.AddTriplePattern(t);
            }

            foreach (var g in diff.RemovedMSGs)
            {
                // Blank nodes in removed non-ground triples are converted to variables and added to both delete and where clauses
                foreach (var t in g.Triples)
                {
                    delete.AddVariablePattern(t);
                    where.AddVariablePattern(t);
                }

                // An ISBLANK filter is added for each blank node in removed non-ground triples
                foreach (var n in g.BlankNodes())
                {
                    where.AddBlankNodeFilter(n);
                }
            }

            // Added triples (ground or not) are added as is to the insert clause
            foreach (var t in diff.AllAddedTriples())
            {
                insert.AddTriplePattern(t);
            }

            return(new ModifyCommand(
                       delete.BuildGraphPattern(prefixes),
                       insert.BuildGraphPattern(prefixes),
                       where.BuildGraphPattern(prefixes),
                       graphUri));
        }
        /// <summary>
        /// Converts a <see cref="GraphDiffReport">diff</see> to an equivalent <see cref="ModifyCommand">SPARQL Update query</see>
        /// </summary>
        /// <param name="diff">The <see cref="GraphDiffReport">diff</see> to convert</param>
        /// <returns>A <see cref="ModifyCommand">SPARQL Update query</see> that represents the <see cref="GraphDiffReport">diff</see></returns>
        internal static ModifyCommand AsUpdate(this GraphDiffReport diff)
        {
            var delete = new GraphPatternBuilder();
            var insert = new GraphPatternBuilder();

            var where = new GraphPatternBuilder();

            // Groud removed triples are added as is to both delete and where clauses
            foreach (var t in diff.RemovedTriples)
            {
                delete.AddTriplePattern(t);
                where.AddTriplePattern(t);
            }

            foreach (var g in diff.RemovedMSGs)
            {
                // Blank nodes in non-ground removed triples are converted to variables and added to both delete and where clauses
                foreach (var t in g.Triples)
                {
                    delete.AddVariablePattern(t);
                    where.AddVariablePattern(t);
                }

                // An ISBLANK filter is added for each blank node in non-ground removed triples
                foreach (var n in g.BlankNodes())
                {
                    where.AddBlankNodeFilter(n);
                }
            }

            // Added triples (ground or not) are added as is to the insert clause
            foreach (var t in diff.AllAddedTriples())
            {
                insert.AddTriplePattern(t);
            }

            return(new ModifyCommand(
                       delete.BuildGraphPattern(),
                       insert.BuildGraphPattern(),
                       where.BuildGraphPattern()));
        }
Exemplo n.º 6
0
 public IGraphPatternBuilder Child(GraphPatternBuilder patternBuilder)
 {
     return(PatternBuilder.Child(patternBuilder));
 }
Exemplo n.º 7
0
 public void Union(GraphPatternBuilder firstBuilder, params GraphPatternBuilder[] otherBuilders)
 {
     PatternBuilder.Union(firstBuilder, otherBuilders);
 }
        private static GraphPattern BuildGraphPattern(this GraphPatternBuilder builder)
        {
            var buildGraphPattern = typeof(GraphPatternBuilder).GetMethod(nameof(BuildGraphPattern), BindingFlags.Instance | BindingFlags.NonPublic);

            return(buildGraphPattern.Invoke(builder, new[] { null as INamespaceMapper }) as GraphPattern);
        }