Example #1
0
        /// <summary>
        /// Transforms the Algebra into a Graph Pattern
        /// </summary>
        /// <returns></returns>
        public override GraphPattern ToGraphPattern()
        {
            GraphPattern        gp = new GraphPattern();
            PropertyPathPattern pp = new PropertyPathPattern(PathStart, new ZeroOrMore(Path), PathEnd);

            gp.AddTriplePattern(pp);
            return(gp);
        }
Example #2
0
        /// <summary>
        /// Transforms the Algebra back into a Graph Pattern
        /// </summary>
        /// <returns></returns>
        public override GraphPattern ToGraphPattern()
        {
            GraphPattern        gp = new GraphPattern();
            PropertyPathPattern pp = new PropertyPathPattern(this.PathStart, new FixedCardinality(this.Path, 0), this.PathEnd);

            gp.AddTriplePattern(pp);
            return(gp);
        }
Example #3
0
        /// <summary>
        /// Transforms the Algebra back into a Graph Pattern.
        /// </summary>
        /// <returns></returns>
        public GraphPattern ToGraphPattern()
        {
            GraphPattern        gp = new GraphPattern();
            PropertyPathPattern pp;

            if (_inverse)
            {
                pp = new PropertyPathPattern(PathStart, new NegatedSet(Enumerable.Empty <Property>(), _properties.Select(p => new Property(p))), PathEnd);
            }
            else
            {
                pp = new PropertyPathPattern(PathStart, new NegatedSet(_properties.Select(p => new Property(p)), Enumerable.Empty <Property>()), PathEnd);
            }
            gp.AddTriplePattern(pp);
            return(gp);
        }
Example #4
0
        internal static INode ToSpinRdf(this ITriplePattern pattern, IGraph g, SpinVariableTable varTable)
        {
            INode p       = g.CreateBlankNode();
            INode rdfType = g.CreateUriNode(new Uri(RdfSpecsHelper.RdfType));

            if (pattern is TriplePattern)
            {
                TriplePattern tp = (TriplePattern)pattern;
                //g.Assert(p, rdfType, g.CreateUriNode(new Uri(SpinClassTriplePattern)));
                g.Assert(p, g.CreateUriNode(new Uri(SpinPropertySubject)), tp.Subject.ToSpinRdf(g, varTable));
                g.Assert(p, g.CreateUriNode(new Uri(SpinPropertyPredicate)), tp.Predicate.ToSpinRdf(g, varTable));
                g.Assert(p, g.CreateUriNode(new Uri(SpinPropertyObject)), tp.Object.ToSpinRdf(g, varTable));
            }
            else if (pattern is SubQueryPattern)
            {
                g.Assert(p, rdfType, g.CreateUriNode(new Uri(SpinClassSubQuery)));
                g.Assert(p, g.CreateUriNode(new Uri(SpinPropertyQuery)), ((SubQueryPattern)pattern).SubQuery.ToSpinRdf(g));
            }
            else if (pattern is FilterPattern)
            {
                g.Assert(p, rdfType, g.CreateUriNode(new Uri(SpinClassFilter)));
                g.Assert(p, g.CreateUriNode(new Uri(SpinPropertyExpression)), ((FilterPattern)pattern).Filter.Expression.ToSpinRdf(g, varTable));
            }
            else if (pattern is PropertyPathPattern)
            {
                PropertyPathPattern pp = (PropertyPathPattern)pattern;
                g.Assert(p, rdfType, g.CreateUriNode(new Uri(SpinClassTriplePath)));
                g.Assert(p, g.CreateUriNode(new Uri(SpinPropertySubject)), pp.Subject.ToSpinRdf(g, varTable));
                g.Assert(p, g.CreateUriNode(new Uri(SpinPropertyPath)), pp.Path.ToSpinRdf(g, varTable));
                g.Assert(p, g.CreateUriNode(new Uri(SpinPropertyObject)), pp.Object.ToSpinRdf(g, varTable));
            }
            else if (pattern is LetPattern)
            {
                g.Assert(p, rdfType, g.CreateUriNode(new Uri(SpinClassLet)));
                INode var = g.CreateBlankNode();
                g.Assert(p, g.CreateUriNode(new Uri(SpinPropertyVariable)), var);
                g.Assert(var, g.CreateUriNode(new Uri(SpinPropertyVariableName)), g.CreateLiteralNode(((LetPattern)pattern).VariableName, new Uri(XmlSpecsHelper.XmlSchemaDataTypeString)));
                g.Assert(p, g.CreateUriNode(new Uri(SpinPropertyExpression)), ((LetPattern)pattern).AssignExpression.ToSpinRdf(g, varTable));
            }
            else if (pattern is BindPattern)
            {
                throw new SpinException("SPARQL 1.1 BINDs are not representable in SPIN RDF Syntax");
            }

            return(p);
        }
Example #5
0
        internal static INode ToSpinRdf(this ITriplePattern pattern, IGraph g, SpinVariableTable varTable)
        {
            INode p = g.CreateBlankNode();

            if (pattern is TriplePattern)
            {
                TriplePattern tp = (TriplePattern)pattern;
                g.Assert(p, RDF.PropertyType, SP.ClassTriplePattern);
                g.Assert(p, SP.PropertySubject, tp.Subject.ToSpinRdf(g, varTable));
                g.Assert(p, SP.PropertyPredicate, tp.Predicate.ToSpinRdf(g, varTable));
                g.Assert(p, SP.PropertyObject, tp.Object.ToSpinRdf(g, varTable));
            }
            else if (pattern is SubQueryPattern)
            {
                g.Assert(p, RDF.PropertyType, SP.ClassSubQuery);
                g.Assert(p, SP.PropertyQuery, ((SubQueryPattern)pattern).SubQuery.ToSpinRdf(g));
            }
            else if (pattern is FilterPattern)
            {
                g.Assert(p, RDF.PropertyType, SP.ClassFilter);
                g.Assert(p, SP.PropertyExpression, ((FilterPattern)pattern).Filter.Expression.ToSpinRdf(g, varTable));
            }
            else if (pattern is PropertyPathPattern)
            {
                PropertyPathPattern pp = (PropertyPathPattern)pattern;
                g.Assert(p, RDF.PropertyType, SP.ClassTriplePath);
                g.Assert(p, SP.PropertySubject, pp.Subject.ToSpinRdf(g, varTable));
                g.Assert(p, SP.PropertyPath, pp.Path.ToSpinRdf(g, varTable));
                g.Assert(p, SP.PropertyObject, pp.Object.ToSpinRdf(g, varTable));
            }
            else if (pattern is LetPattern)
            {
                g.Assert(p, RDF.PropertyType, SP.ClassLet);
                INode var = g.CreateBlankNode();
                g.Assert(p, SP.PropertyVariable, var);
                g.Assert(var, SP.PropertyVarName, g.CreateLiteralNode(((LetPattern)pattern).VariableName, XSD.string_.Uri));
                g.Assert(p, SP.PropertyExpression, ((LetPattern)pattern).AssignExpression.ToSpinRdf(g, varTable));
            }
            else if (pattern is BindPattern)
            {
                throw new SpinException("SPARQL 1.1 BINDs are not representable in SPIN RDF Syntax");
            }

            return(p);
        }
Example #6
0
        /// <summary>
        /// Optimises BGPs in the Algebra to use Filter() and Extend() rather than the embedded FILTER and BIND
        /// </summary>
        /// <param name="algebra">Algebra to optimise</param>
        /// <returns></returns>
        public ISparqlAlgebra Optimise(ISparqlAlgebra algebra)
        {
            if (algebra is IAbstractJoin)
            {
                return(((IAbstractJoin)algebra).Transform(this));
            }
            else if (algebra is IUnaryOperator)
            {
                return(((IUnaryOperator)algebra).Transform(this));
            }
            else if (algebra is IBgp)
            {
                IBgp current = (IBgp)algebra;
                if (current.PatternCount == 0)
                {
                    return(current);
                }
                else
                {
                    ISparqlAlgebra        result   = new Bgp();
                    List <ITriplePattern> patterns = new List <ITriplePattern>();
                    List <ITriplePattern> ps       = new List <ITriplePattern>(current.TriplePatterns.ToList());
                    for (int i = 0; i < current.PatternCount; i++)
                    {
                        if (!(ps[i] is TriplePattern))
                        {
                            //First ensure that if we've found any other Triple Patterns up to this point
                            //we dump this into a BGP and join with the result so far
                            if (patterns.Count > 0)
                            {
                                result = Join.CreateJoin(result, new Bgp(patterns));
                                patterns.Clear();
                            }

                            //Then generate the appropriate strict algebra operator
                            if (ps[i] is FilterPattern)
                            {
                                result = new Filter(result, ((FilterPattern)ps[i]).Filter);
                            }
                            else if (ps[i] is BindPattern)
                            {
                                BindPattern bind = (BindPattern)ps[i];
                                result = new Extend(result, bind.AssignExpression, bind.VariableName);
                            }
                            else if (ps[i] is LetPattern)
                            {
                                LetPattern let = (LetPattern)ps[i];
                                result = new Extend(result, let.AssignExpression, let.VariableName);
                            }
                            else if (ps[i] is SubQueryPattern)
                            {
                                SubQueryPattern sq = (SubQueryPattern)ps[i];
                                result = Join.CreateJoin(result, new SubQuery(sq.SubQuery));
                            }
                            else if (ps[i] is PropertyPathPattern)
                            {
                                PropertyPathPattern pp = (PropertyPathPattern)ps[i];
                                result = Join.CreateJoin(result, new PropertyPath(pp.Subject, pp.Path, pp.Object));
                            }
                        }
                        else
                        {
                            patterns.Add(ps[i]);
                        }
                    }

                    if (patterns.Count == current.PatternCount)
                    {
                        //If count of remaining patterns same as original pattern count there was no optimisation
                        //to do so return as is
                        return(current);
                    }
                    else if (patterns.Count > 0)
                    {
                        //If any patterns left at end join as a BGP with result so far
                        result = Join.CreateJoin(result, new Bgp(patterns));
                        return(result);
                    }
                    else
                    {
                        return(result);
                    }
                }
            }
            else if (algebra is ITerminalOperator)
            {
                return(algebra);
            }
            else
            {
                return(algebra);
            }
        }
Example #7
0
        /// <summary>
        /// Formats a Triple Pattern in nicely formatted SPARQL syntax
        /// </summary>
        /// <param name="tp">Triple Pattern</param>
        /// <returns></returns>
        public virtual String Format(ITriplePattern tp)
        {
            StringBuilder output = new StringBuilder();

            if (tp is TriplePattern)
            {
                TriplePattern match = (TriplePattern)tp;
                output.Append(this.Format(match.Subject, TripleSegment.Subject));
                output.Append(' ');
                output.Append(this.Format(match.Predicate, TripleSegment.Predicate));
                output.Append(' ');
                output.Append(this.Format(match.Object, TripleSegment.Object));
                output.Append(" .");
            }
            else if (tp is FilterPattern)
            {
                FilterPattern filter = (FilterPattern)tp;
                output.Append("FILTER(");
                output.Append(this.FormatExpression(filter.Filter.Expression));
                output.Append(")");
            }
            else if (tp is SubQueryPattern)
            {
                SubQueryPattern subquery = (SubQueryPattern)tp;
                output.AppendLine("{");
                output.AppendLineIndented(this.Format(subquery.SubQuery), 2);
                output.AppendLine("}");
            }
            else if (tp is PropertyPathPattern)
            {
                PropertyPathPattern path = (PropertyPathPattern)tp;
                output.Append(this.Format(path.Subject, TripleSegment.Subject));
                output.Append(' ');
                output.Append(this.FormatPath(path.Path));
                output.Append(' ');
                output.Append(this.Format(path.Object, TripleSegment.Object));
                output.Append(" .");
            }
            else if (tp is LetPattern)
            {
                LetPattern let = (LetPattern)tp;
                output.Append("LET(?");
                output.Append(let.VariableName);
                output.Append(" := ");
                output.Append(this.FormatExpression(let.AssignExpression));
                output.Append(")");
            }
            else if (tp is BindPattern)
            {
                BindPattern bind = (BindPattern)tp;
                output.Append("BIND (");
                output.Append(this.FormatExpression(bind.AssignExpression));
                output.Append(" AS ?");
                output.Append(bind.VariableName);
                output.Append(")");
            }
            else
            {
                throw new RdfOutputException("Unable to Format an unknown ITriplePattern implementation as a String");
            }

            return(output.ToString());
        }