/// <summary>
        /// Gets the String representation of the Pattern
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder output = new StringBuilder();

            output.Append(_subj.ToString());
            output.Append(' ');
            output.Append(_path.ToString());
            output.Append(' ');
            output.Append(_obj.ToString());
            return(output.ToString());
        }
        /// <summary>
        /// Formats a Pattern Item in nicely formatted SPARQL syntax
        /// </summary>
        /// <param name="item">Pattern Item</param>
        /// <param name="segment">Triple Pattern Segment</param>
        /// <returns></returns>
        public virtual String Format(PatternItem item, TripleSegment? segment)
        {
            if (item is VariablePattern)
            {
                return item.ToString();
            }
            else if (item is NodeMatchPattern)
            {
                NodeMatchPattern match = (NodeMatchPattern)item;
                return this.Format(match.Node, segment);
            }
            else if (item is FixedBlankNodePattern)
            {
                if (segment != null)
                {
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException("Cannot format a Fixed Blank Node Pattern Item as the Predicate of a Triple Pattern as Blank Nodes are not permitted as Predicates");
                }

                return item.ToString();
            }
            else if (item is BlankNodePattern)
            {
                return item.ToString();
            }
            else
            {
                throw new RdfOutputException("Unable to Format an unknown PatternItem implementation as a String");
            }
        }
示例#3
0
 /// <summary>
 /// Gets the String representation of this Pattern.
 /// </summary>
 /// <returns></returns>
 public override string ToString()
 {
     return(_subj.ToString() + " " + _pred.ToString() + " " + _obj.ToString());
 }
示例#4
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");
            }
        }