/// <summary> /// Creates a new Path Transform Context /// </summary> /// <param name="start">Subject that is the start of the Path</param> /// <param name="end">Object that is the end of the Path</param> public PathTransformContext(PatternItem start, PatternItem end) { this._start = start; this._currSubj = start; this._end = end; this._currObj = end; }
/// <summary> /// Creates a new Negated Property Set /// </summary> /// <param name="start">Path Start</param> /// <param name="end">Path End</param> /// <param name="properties">Negated Properties</param> /// <param name="inverse">Whether this is a set of Inverse Negated Properties</param> public NegatedPropertySet(PatternItem start, PatternItem end, IEnumerable<Property> properties, bool inverse) { this._start = start; this._end = end; this._properties.AddRange(properties.Select(p => p.Predicate)); this._inverse = inverse; }
/// <summary> /// Creates a new Path Evaluation Context /// </summary> /// <param name="context">SPARQL Evaluation Context</param> /// <param name="end">Start point of the Path</param> /// <param name="start">End point of the Path</param> public PathEvaluationContext(SparqlEvaluationContext context, PatternItem start, PatternItem end) { this._context = context; this._start = start; this._end = end; if (this._start.VariableName == null && this._end.VariableName == null) this._earlyAbort = true; }
/// <summary> /// Create a transform that replaces one variable with another /// </summary> /// <param name="findVar">Find Variable</param> /// <param name="replaceVar">Replace Variable</param> public VariableSubstitutionTransformer(String findVar, String replaceVar) { this._findVar = findVar; this._replaceItem = new VariablePattern("?" + replaceVar); this._replaceExpr = new VariableTerm(replaceVar); this._replaceToken = new VariableToken("?" + replaceVar, 0, 0, 0); this._canReplaceObjects = false; }
/// <summary> /// Creates a new Path Transform Context from an existing context /// </summary> /// <param name="context">Context</param> public PathTransformContext(PathTransformContext context) { this._start = context._start; this._end = context._end; this._currSubj = context._currSubj; this._currObj = context._currObj; this._nextID = context._nextID; }
/// <summary> /// Creates a new Full Text Operator /// </summary> /// <param name="provider">Search Provider</param> /// <param name="algebra">Inner Algebra</param> /// <param name="matchVar">Match Variable</param> /// <param name="scoreVar">Score Variable</param> /// <param name="searchTerm">Search Term</param> /// <param name="limit">Limit</param> /// <param name="scoreThreshold">Score Threshold</param> public BaseFullTextOperator(IFullTextSearchProvider provider, ISparqlAlgebra algebra, PatternItem matchVar, PatternItem scoreVar, PatternItem searchTerm, int limit, double scoreThreshold) { this._provider = provider; this.InnerAlgebra = algebra; this._matchVar = matchVar; this._scoreVar = scoreVar; this._searchTerm = searchTerm; this._limit = limit; this._scoreThreshold = scoreThreshold; }
/// <summary> /// Creates a new Property Path Pattern /// </summary> /// <param name="subj">Subject</param> /// <param name="path">Property Path</param> /// <param name="obj">Object</param> public PropertyPathPattern(PatternItem subj, ISparqlPath path, PatternItem obj) { this._subj = subj; this._path = path; this._obj = obj; //Build our list of Variables if (this._subj.VariableName != null) { this._vars.Add(this._subj.VariableName); } if (this._obj.VariableName != null) { if (!this._vars.Contains(this._obj.VariableName)) this._vars.Add(this._obj.VariableName); } this._vars.Sort(); }
/// <summary> /// Creates a new Negated Property Set /// </summary> /// <param name="start">Path Start</param> /// <param name="end">Path End</param> /// <param name="properties">Negated Properties</param> public NegatedPropertySet(PatternItem start, PatternItem end, IEnumerable<Property> properties) : this(start, end, properties, false) { }
/// <summary> /// Creates a new Triple Pattern /// </summary> /// <param name="subj">Subject Pattern</param> /// <param name="pred">Predicate Pattern</param> /// <param name="obj">Object Pattern</param> public TriplePattern(PatternItem subj, PatternItem pred, PatternItem obj) { this._subj = subj; if (pred is BlankNodePattern) { throw new RdfParseException("Cannot use a Triple Pattern with a Blank Node Predicate in a SPARQL Query"); } this._pred = pred; this._obj = obj; //Decide on the Index Type if (this._subj is NodeMatchPattern) { if (Options.FullTripleIndexing) { if (this._pred is NodeMatchPattern) { this._indexType = TripleIndexType.SubjectPredicate; } else if (this._obj is NodeMatchPattern) { this._indexType = TripleIndexType.SubjectObject; } else { this._indexType = TripleIndexType.Subject; } } else { this._indexType = TripleIndexType.Subject; } } else if (this._pred is NodeMatchPattern) { if (Options.FullTripleIndexing) { if (this._obj is NodeMatchPattern) { this._indexType = TripleIndexType.PredicateObject; } else { this._indexType = TripleIndexType.Predicate; } } else { this._indexType = TripleIndexType.Predicate; } } else if (this._obj is NodeMatchPattern) { this._indexType = TripleIndexType.Object; } //Determine variables used if (this._subj.VariableName != null) this._vars.Add(this._subj.VariableName); if (this._pred.VariableName != null) { if (!this._vars.Contains(this._pred.VariableName)) { this._vars.Add(this._pred.VariableName); } else { this._pred.Repeated = true; } } if (this._obj.VariableName != null) { if (!this._vars.Contains(this._obj.VariableName)) { this._vars.Add(this._obj.VariableName); } else { this.Object.Repeated = true; } } this._vars.Sort(); if (this._vars.Count == 0) this._indexType = TripleIndexType.NoVariables; }
/// <summary> /// Used to help extract the patterns that make up a <see cref="FullTextPattern">FullTextPattern</see> /// </summary> /// <param name="key">Key</param> /// <param name="subj">Subject</param> /// <param name="ps">Patterns</param> /// <param name="ftPatterns">Discovered Full Text Patterns</param> internal static void ExtractRelatedPatterns(PatternItem key, PatternItem subj, List<TriplePattern> ps, Dictionary<PatternItem, List<TriplePattern>> ftPatterns) { bool recurse = true; PatternItem nextSubj = null; foreach (TriplePattern tp in ps) { if (tp.Subject.VariableName == subj.VariableName) { NodeMatchPattern predItem = tp.Predicate as NodeMatchPattern; if (predItem == null) continue; IUriNode predNode = predItem.Node as IUriNode; if (predNode == null) continue; if (predNode.Uri.ToString().Equals(RdfSpecsHelper.RdfListFirst)) { ftPatterns[key].Add(tp); } else if (predNode.Uri.ToString().Equals(RdfSpecsHelper.RdfListRest)) { ftPatterns[key].Add(tp); recurse = tp.Object.VariableName != null; nextSubj = tp.Object; } } } ftPatterns[key].ForEach(tp => ps.Remove(tp)); if (nextSubj == null) throw new RdfQueryException("Failed to find expected rdf:rest property"); if (recurse) { ExtractRelatedPatterns(key, nextSubj, ps, ftPatterns); } }
/// <summary> /// Creates a new Full Text Operator /// </summary> /// <param name="provider">Search Provider</param> /// <param name="algebra">Inner Algebra</param> /// <param name="matchVar">Match Variable</param> /// <param name="scoreVar">Score Variable</param> /// <param name="searchTerm">Search Term</param> /// <param name="limit">Limit</param> public BaseFullTextOperator(IFullTextSearchProvider provider, ISparqlAlgebra algebra, PatternItem matchVar, PatternItem scoreVar, PatternItem searchTerm, int limit) : this(provider, algebra, matchVar, scoreVar, searchTerm, limit, Double.NaN) { }
/// <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"); }
/// <summary> /// Creates a new Property Path operator /// </summary> /// <param name="start">Path Start</param> /// <param name="path">Path Expression</param> /// <param name="end">Path End</param> public PropertyPath(PatternItem start, ISparqlPath path, PatternItem end) : base(start, path, end) { }
/// <summary> /// Creates a new Zero Length Path /// </summary> /// <param name="start">Path Start</param> /// <param name="end">Path End</param> /// <param name="path">Property Path</param> public ZeroLengthPath(PatternItem start, PatternItem end, ISparqlPath path) : base(start, path, end) { }
/// <summary> /// Create a transform that replaces a variable with a constant /// </summary> /// <param name="findVar">Find Variable</param> /// <param name="replaceTerm">Replace Constant</param> public VariableSubstitutionTransformer(String findVar, INode replaceTerm) { this._findVar = findVar; this._replaceItem = new NodeMatchPattern(replaceTerm); this._replaceExpr = new ConstantTerm(replaceTerm); if (replaceTerm is IUriNode) { this._replaceToken = new UriToken("<" + ((IUriNode)replaceTerm).Uri.ToString() + ">", 0, 0, 0); } this._canReplaceObjects = true; }
/// <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"); } }
/// <summary> /// Creates a new Path Operator /// </summary> /// <param name="start">Path Start</param> /// <param name="end">Path End</param> /// <param name="path">Property Path</param> public BasePathOperator(PatternItem start, PatternItem end, ISparqlPath path) { this._start = start; this._end = end; this._path = path; if (this._start.VariableName != null) this._vars.Add(this._start.VariableName); if (this._end.VariableName != null) this._vars.Add(this._end.VariableName); }
/// <summary> /// Creates a new Arbitrary Lengh Path Operator /// </summary> /// <param name="start">Path Start</param> /// <param name="end">Path End</param> /// <param name="path">Property Path</param> public BaseArbitraryLengthPathOperator(PatternItem start, PatternItem end, ISparqlPath path) : base(start, end, path) { }
/// <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); } }
/// <summary> /// Creates a new Full Text Operator /// </summary> /// <param name="provider">Full Text Search Provider</param> /// <param name="algebra">Inner Algebra</param> /// <param name="matchVar">Match Variable</param> /// <param name="searchTerm">Search Term</param> /// <param name="scoreThreshold">Score Threshold</param> public FullTextMatch(IFullTextSearchProvider provider, ISparqlAlgebra algebra, PatternItem matchVar, PatternItem searchTerm, double scoreThreshold) : this(provider, algebra, matchVar, null, searchTerm, -1, scoreThreshold) { }
/// <summary> /// Creates a new Full Text Operator /// </summary> /// <param name="provider">Full Text Search Provider</param> /// <param name="algebra">Inner Algebra</param> /// <param name="matchVar">Match Variable</param> /// <param name="scoreVar">Score Variable</param> /// <param name="searchTerm">Search Term</param> /// <param name="limit">Result Limit</param> /// <param name="scoreThreshold">Score Threshold</param> public FullTextMatch(IFullTextSearchProvider provider, ISparqlAlgebra algebra, PatternItem matchVar, PatternItem scoreVar, PatternItem searchTerm, int limit, double scoreThreshold) : base(provider, algebra, matchVar, scoreVar, searchTerm, limit, scoreThreshold) { }
/// <summary> /// Creates a new Full Text Operator /// </summary> /// <param name="provider">Full Text Search Provider</param> /// <param name="algebra">Inner Algebra</param> /// <param name="matchVar">Match Variable</param> /// <param name="searchTerm">Search Term</param> /// <param name="limit">Result Limit</param> public FullTextMatch(IFullTextSearchProvider provider, ISparqlAlgebra algebra, PatternItem matchVar, PatternItem searchTerm, int limit) : this(provider, algebra, matchVar, null, searchTerm, limit, Double.NaN) { }
/// <summary> /// Creates a new Full Text Pattern /// </summary> /// <param name="matchVar">Match Variable</param> /// <param name="scoreVar">Score Variable</param> /// <param name="searchTerm">Search Term</param> public FullTextPattern(PatternItem matchVar, PatternItem scoreVar, PatternItem searchTerm) { this._matchVar = matchVar; this._scoreVar = scoreVar; this._searchTerm = searchTerm; NodeFactory factory = new NodeFactory(); if (this._scoreVar != null) { BlankNodePattern a = new BlankNodePattern(factory.GetNextBlankNodeID()); BlankNodePattern b = new BlankNodePattern(factory.GetNextBlankNodeID()); this._origPatterns.Add(new TriplePattern(a, new NodeMatchPattern(factory.CreateUriNode(new Uri(FullTextHelper.FullTextMatchPredicateUri))), this._searchTerm)); this._origPatterns.Add(new TriplePattern(a, new NodeMatchPattern(factory.CreateUriNode(new Uri(RdfSpecsHelper.RdfListFirst))), this._matchVar)); this._origPatterns.Add(new TriplePattern(a, new NodeMatchPattern(factory.CreateUriNode(new Uri(RdfSpecsHelper.RdfListRest))), b)); this._origPatterns.Add(new TriplePattern(b, new NodeMatchPattern(factory.CreateUriNode(new Uri(RdfSpecsHelper.RdfListFirst))), this._scoreVar)); this._origPatterns.Add(new TriplePattern(b, new NodeMatchPattern(factory.CreateUriNode(new Uri(RdfSpecsHelper.RdfListRest))), new NodeMatchPattern(factory.CreateUriNode(new Uri(RdfSpecsHelper.RdfListNil))))); } else { this._origPatterns.Add(new TriplePattern(this._matchVar, new NodeMatchPattern(factory.CreateUriNode(new Uri(FullTextHelper.FullTextMatchPredicateUri))), this._searchTerm)); } }
/// <summary> /// Creates a new Full Text Operator /// </summary> /// <param name="provider">Full Text Search Provider</param> /// <param name="algebra">Inner Algebra</param> /// <param name="matchVar">Match Variable</param> /// <param name="scoreVar">Score Variable</param> /// <param name="searchTerm">Search Term</param> public FullTextMatch(IFullTextSearchProvider provider, ISparqlAlgebra algebra, PatternItem matchVar, PatternItem scoreVar, PatternItem searchTerm) : this(provider, algebra, matchVar, scoreVar, searchTerm, -1, Double.NaN) { }
/// <summary> /// Creates a new Zero or More Path /// </summary> /// <param name="start">Path Start</param> /// <param name="end">Path End</param> /// <param name="path">Property Path</param> public ZeroOrMorePath(PatternItem start, PatternItem end, ISparqlPath path) : base(start, end, path) { }
/// <summary> /// Creates a new Full Text Operator /// </summary> /// <param name="provider">Search Provider</param> /// <param name="algebra">Inner Algebra</param> /// <param name="matchVar">Match Variable</param> /// <param name="scoreVar">Score Variable</param> /// <param name="searchTerm">Search Term</param> /// <param name="scoreThreshold">Score Threshold</param> public BaseFullTextOperator(IFullTextSearchProvider provider, ISparqlAlgebra algebra, PatternItem matchVar, PatternItem scoreVar, PatternItem searchTerm, double scoreThreshold) : this(provider, algebra, matchVar, scoreVar, searchTerm, -1, scoreThreshold) { }