private ISparqlAlgebra GetAlgebraUntransformed(ISparqlPath path, INode start, INode end)
 {
     PatternItem x, y;
     if (start == null)
     {
         x = new VariablePattern("?x");
     }
     else
     {
         x = new NodeMatchPattern(start);
     }
     if (end == null)
     {
         y = new VariablePattern("?y");
     }
     else
     {
         y = new NodeMatchPattern(end);
     }
     return new Bgp(new PropertyPathPattern(x, path, y));
 }
 private ISparqlAlgebra GetAlgebra(ISparqlPath path, INode start, INode end)
 {
     PatternItem x, y;
     if (start == null)
     {
         x = new VariablePattern("?x");
     }
     else
     {
         x = new NodeMatchPattern(start);
     }
     if (end == null)
     {
         y = new VariablePattern("?y");
     }
     else
     {
         y = new NodeMatchPattern(end);
     }
     PathTransformContext context = new PathTransformContext(x, y);
     return path.ToAlgebra(context);
 }
示例#3
0
 /// <summary>
 /// Creates a Triple Pattern
 /// </summary>
 /// <param name="subj">Subject</param>
 /// <param name="path">Property Path</param>
 /// <param name="obj">Object</param>
 /// <returns></returns>
 public ITriplePattern GetTriplePattern(PatternItem subj, ISparqlPath path, PatternItem obj)
 {
     if (path is Property)
     {
         NodeMatchPattern nodeMatch = new NodeMatchPattern(((Property)path).Predicate);
         return new TriplePattern(subj, nodeMatch, obj);
     }
     else
     {
         return new PropertyPathPattern(subj, path, obj);
     }
 }
        public ISparqlAlgebra Optimise(ISparqlAlgebra algebra)
        {
            try
            {
                if (algebra is Bgp)
                {
                    var bgp = (Bgp) algebra;

                    if (bgp.TriplePatterns.OfType<FilterPattern>().Count() == 1)
                    {
                        var filter = bgp.TriplePatterns.OfType<FilterPattern>().Select(fp=>fp.Filter).First();
                        string var;
                        INode term;
                        bool equals;
                        if (IsIdentityExpression(filter.Expression, out var, out term, out equals))
                        {
                            if (equals)
                            {
                                var triplePatterns = new List<ITriplePattern>();
                                foreach(var tp in bgp.TriplePatterns)
                                {
                                    if (tp is FilterPattern) continue;
                                    if (tp is TriplePattern)
                                    {
                                        var triplePattern = (TriplePattern) tp;
                                        if (triplePattern.Variables.Contains(var))
                                        {
                                            PatternItem subjPattern = triplePattern.Subject,
                                                        predPattern = triplePattern.Predicate,
                                                        objPattern = triplePattern.Object;
                                            if (var.Equals(triplePattern.Subject.VariableName))
                                            {
                                                subjPattern = new NodeMatchPattern(term);
                                            }
                                            if (var.Equals(triplePattern.Predicate.VariableName))
                                            {
                                                predPattern = new NodeMatchPattern(term);
                                            }
                                            if (var.Equals(triplePattern.Object.VariableName))
                                            {
                                                objPattern = new NodeMatchPattern(term);
                                            }
                                            triplePatterns.Add(new TriplePattern(subjPattern, predPattern, objPattern));
                                        }
                                        else
                                        {
                                            triplePatterns.Add(triplePattern);
                                        }
                                    } else
                                    {
                                        triplePatterns.Add(tp);
                                    }
                                }
                                return new Bgp(triplePatterns);
                            }
                        }
                    }
                }
                else if (algebra is IAbstractJoin)
                {
                    return ((IAbstractJoin) algebra).Transform(this);
                }
                else if (algebra is IUnaryOperator)
                {
                    return ((IUnaryOperator) algebra).Transform(this);
                }
                else
                {
                    return algebra;
                }
            }
            catch
            {
                return algebra;
            }
            return algebra;
        }
 private bool OptimiseBgp(Bgp bgp, out ISparqlAlgebra optimisedAlgebra)
 {
     if (bgp.TriplePatterns.OfType<FilterPattern>().Count() == 1)
     {
         var filter = bgp.TriplePatterns.OfType<FilterPattern>().Select(fp => fp.Filter).First();
         string var;
         INode term;
         bool equals;
         if (IsIdentityExpression(filter.Expression, out var, out term, out @equals))
         {
             if (@equals)
             {
                 var triplePatterns = new List<ITriplePattern>();
                 foreach (var tp in bgp.TriplePatterns)
                 {
                     if (tp is FilterPattern) continue;
                     if (tp is TriplePattern)
                     {
                         var triplePattern = (TriplePattern) tp;
                         if (triplePattern.Variables.Contains(var))
                         {
                             PatternItem subjPattern = triplePattern.Subject,
                                 predPattern = triplePattern.Predicate,
                                 objPattern = triplePattern.Object;
                             if (var.Equals(triplePattern.Subject.VariableName))
                             {
                                 subjPattern = new NodeMatchPattern(term);
                             }
                             if (var.Equals(triplePattern.Predicate.VariableName))
                             {
                                 predPattern = new NodeMatchPattern(term);
                             }
                             if (var.Equals(triplePattern.Object.VariableName))
                             {
                                 objPattern = new NodeMatchPattern(term);
                             }
                             triplePatterns.Add(new TriplePattern(subjPattern, predPattern, objPattern));
                         }
                         else
                         {
                             triplePatterns.Add(triplePattern);
                         }
                     }
                     else
                     {
                         triplePatterns.Add(tp);
                     }
                 }
                 {
                     optimisedAlgebra = new Bgp(triplePatterns);
                     return true;
                 }
             }
         }
     }
     optimisedAlgebra = bgp;
     return false;
 }
示例#6
0
        /// <summary>
        /// Creates a new Full Text Pattern
        /// </summary>
        /// <param name="origPatterns">Original Patterns</param>
        public FullTextPattern(IEnumerable <TriplePattern> origPatterns)
        {
            this._origPatterns.AddRange(origPatterns.OrderBy(tp => tp.Predicate.ToString()));
            PatternItem matchVar  = null;
            PatternItem searchVar = null;
            Dictionary <String, PatternItem> firsts = new Dictionary <string, PatternItem>();
            Dictionary <String, PatternItem> rests  = new Dictionary <string, PatternItem>();

            foreach (TriplePattern tp in this._origPatterns)
            {
                NodeMatchPattern predItem = tp.Predicate as NodeMatchPattern;
                if (predItem == null)
                {
                    continue;
                }
                IUriNode predUri = predItem.Node as IUriNode;
                if (predUri == null)
                {
                    continue;
                }

                switch (predUri.Uri.ToString())
                {
                case FullTextHelper.FullTextMatchPredicateUri:
                    //Extract the Search Term
                    if (searchVar != null)
                    {
                        throw new RdfQueryException("More than one pf:textMatch property specified");
                    }
                    if (tp.Object.VariableName == null)
                    {
                        this._searchTerm = tp.Object;
                    }
                    else
                    {
                        searchVar = tp.Object;
                    }

                    //Extract the Match Variable
                    if (matchVar != null)
                    {
                        throw new RdfQueryException("More than one pf:textMatch property specified");
                    }
                    if (tp.Subject.VariableName != null && !tp.Subject.VariableName.StartsWith("_:"))
                    {
                        this._matchVar = tp.Subject;
                        if (this._origPatterns.Count > 1 && searchVar == null)
                        {
                            throw new RdfQueryException("Too many patterns provided");
                        }
                    }
                    else
                    {
                        matchVar = tp.Subject;
                    }
                    break;

                case RdfSpecsHelper.RdfListFirst:
                    firsts.Add(tp.Subject.VariableName.ToString(), tp.Object);
                    break;

                case RdfSpecsHelper.RdfListRest:
                    rests.Add(tp.Subject.VariableName.ToString(), tp.Object);
                    break;

                default:
                    throw new RdfQueryException("Unexpected pattern");
                }
            }

            //Use the first and rest lists to determine Match and Score Variables if necessary
            if (this._matchVar == null)
            {
                firsts.TryGetValue(matchVar.VariableName, out this._matchVar);
                String restKey = rests[matchVar.VariableName].VariableName;
                firsts.TryGetValue(restKey, out this._scoreVar);
            }
            //Use the first and rest lists to determine search term, threshold and limit if necessary
            if (this._searchTerm == null)
            {
                firsts.TryGetValue(searchVar.VariableName, out this._searchTerm);
                String restKey = rests[searchVar.VariableName].VariableName;
                firsts.TryGetValue(restKey, out this._thresholdTerm);
                PatternItem last = rests[restKey];
                if (!last.ToString().Equals("<" + RdfSpecsHelper.RdfListNil + ">"))
                {
                    restKey = rests[restKey].VariableName;
                    firsts.TryGetValue(restKey, out this._limitTerm);
                }
                else
                {
                    //If there is only 2 arguments for the search term determine whether it should actually be a
                    //limit rather than a threshold
                    //Essentially if it is an integer assume that it was meant as a limit
                    INode temp = ((NodeMatchPattern)this._thresholdTerm).Node;
                    if (temp is ILiteralNode)
                    {
                        ILiteralNode lit = (ILiteralNode)temp;
                        if (lit.DataType != null)
                        {
                            if (SparqlSpecsHelper.GetNumericTypeFromDataTypeUri(lit.DataType) == VDS.RDF.Query.Expressions.SparqlNumericType.Integer)
                            {
                                //Is actually a limit
                                this._limitTerm     = this._thresholdTerm;
                                this._thresholdTerm = null;
                            }
                        }
                        else
                        {
                            if (SparqlSpecsHelper.IsDecimal(lit.Value) || SparqlSpecsHelper.IsDouble(lit.Value))
                            {
                                //Remains as a Threshold
                            }
                            else if (SparqlSpecsHelper.IsInteger(lit.Value))
                            {
                                //Is actually a limit
                                this._limitTerm     = this._thresholdTerm;
                                this._thresholdTerm = null;
                            }
                        }
                    }
                }
            }

            if (this._matchVar == null)
            {
                throw new RdfQueryException("Failed to specify match variable");
            }
            if (this._searchTerm == null)
            {
                this._searchTerm = searchVar;
            }
            if (this._searchTerm == null)
            {
                throw new RdfQueryException("Failed to specify search terms");
            }
        }