private void RunTest(ISparqlPath path, IEnumerable <String> expectedOperators) { VariablePattern x = new VariablePattern("?x"); VariablePattern y = new VariablePattern("?y"); PathTransformContext context = new PathTransformContext(x, y); Console.WriteLine("Path: " + path.ToString()); ISparqlAlgebra algebra = path.ToAlgebra(context); String result = algebra.ToString(); Console.WriteLine("Algebra: " + result); try { GraphPattern gp = algebra.ToGraphPattern(); Console.WriteLine("GraphPattern:"); Console.WriteLine(this._formatter.Format(gp)); Console.WriteLine(); } catch { Console.WriteLine("Algebra not translatable to a GraphPattern"); } foreach (String op in expectedOperators) { if (result.Contains(op)) { continue; } Console.WriteLine("Expected Operator '" + op + "' missing"); Assert.True(false, "Expected Operator '" + op + "' missing"); } }
private void RunTest(ISparqlPath path, IEnumerable<String> expectedOperators) { VariablePattern x = new VariablePattern("?x"); VariablePattern y = new VariablePattern("?y"); PathTransformContext context = new PathTransformContext(x, y); Console.WriteLine("Path: " + path.ToString()); ISparqlAlgebra algebra = path.ToAlgebra(context); String result = algebra.ToString(); Console.WriteLine("Algebra: " + result); try { GraphPattern gp = algebra.ToGraphPattern(); Console.WriteLine("GraphPattern:"); Console.WriteLine(this._formatter.Format(gp)); Console.WriteLine(); } catch { Console.WriteLine("Algebra not translatable to a GraphPattern"); } foreach (String op in expectedOperators) { if (!result.Contains(op)) { Console.WriteLine("Expected Operator '" + op + "' missing"); Assert.Fail("Expected Operator '" + op + "' missing"); } } }
private ISparqlPath TryParsePathSequence(SparqlQueryParserContext context, Queue <IToken> tokens) { ISparqlPath path = TryParsePathEltOrInverse(context, tokens); IToken next; while (tokens.Count > 0) { next = tokens.Peek(); switch (next.TokenType) { case Token.DIVIDE: tokens.Dequeue(); path = new SequencePath(path, TryParsePathEltOrInverse(context, tokens)); break; case Token.HAT: tokens.Dequeue(); path = new SequencePath(path, new InversePath(TryParsePathElt(context, tokens))); break; default: return(path); } } return(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 Path Operator /// </summary> /// <param name="start">Path Start</param> /// <param name="path">Property Path</param> /// <param name="end">Path End</param> public BasePathOperator(PatternItem start, ISparqlPath path, PatternItem end) { 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); } }
private void printPath(IResource path, ISparqlPrinter p) { ISparqlPath arqPath = createPath(path); if (p.getUsePrefixes()) { //TODO INamespaceMapper prefixMapping = path.getModel().getPrefixMapping(); String str = arqPath.ToString(); //PathWriter.asString(arqPath, null/*TODO new Prologue(prefixMapping)*/); p.print(str); } else { String str = arqPath.ToString();//PathWriter.asString(arqPath); p.print(str); } }
/// <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 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(); }
private ISparqlPath TryParsePathAlternative(SparqlQueryParserContext context, Queue <IToken> tokens) { ISparqlPath path = TryParsePathSequence(context, tokens); IToken next; while (tokens.Count > 0) { next = tokens.Dequeue(); if (next.TokenType == Token.BITWISEOR) { path = new AlternativePath(path, TryParsePathSequence(context, tokens)); } else { throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a valid path sequence/alternative token", next); } } return(path); }
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 static void BindPath(GraphPattern pattern, ISparqlPath path) { for (var i = 0; i < pattern.TriplePatterns.Count(); i++) { if (pattern.TriplePatterns[i] is TriplePattern triplePattern && triplePattern.Predicate.VariableName == "PATH") { pattern.TriplePatterns.RemoveAt(i); pattern.TriplePatterns.Insert(i, new PropertyPathPattern(triplePattern.Subject, path, triplePattern.Object)); } } foreach (var subPattern in pattern.ChildGraphPatterns) { BindPath(subPattern, path); } foreach (var subQueryPattern in pattern.TriplePatterns.OfType <SubQueryPattern>()) { BindPath(subQueryPattern.SubQuery.RootGraphPattern, path); } }
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); }
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))); }
/// <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) { _subj = subj; _path = path; _obj = obj; _subj.RigorousEvaluation = true; _obj.RigorousEvaluation = true; // Build our list of Variables if (_subj.VariableName != null) { _vars.Add(_subj.VariableName); } if (_obj.VariableName != null) { if (!_vars.Contains(_obj.VariableName)) { _vars.Add(_obj.VariableName); } } _vars.Sort(); }
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)); }
public ISparqlPath Parse(SparqlQueryParserContext context, IToken first) { // Need to gather up all the Tokens which make up the path int openBrackets = 0; Queue <IToken> tokens = new Queue <IToken>(); IToken next; LastPathItemType lastItem = LastPathItemType.None; int lastSequencer = -1; // Add the first token and set the initial last item type tokens.Enqueue(first); switch (first.TokenType) { case Token.LEFTBRACKET: openBrackets = 1; lastItem = LastPathItemType.Predicate; break; case Token.QNAME: case Token.URI: case Token.KEYWORDA: lastItem = LastPathItemType.Predicate; break; case Token.HAT: lastItem = LastPathItemType.Sequencer; break; case Token.NEGATION: lastItem = LastPathItemType.Negation; break; default: throw new RdfParseException("Unexpected Token '" + first.GetType().ToString() + "' encountered, this is not valid as the start of a property path"); } while (true) { next = context.Tokens.Peek(); if (openBrackets > 0) { context.Tokens.Dequeue(); tokens.Enqueue(next); if (next.TokenType == Token.RIGHTBRACKET) { openBrackets--; // Groups are considered predicates for purposes of last item lastItem = LastPathItemType.Predicate; } else if (next.TokenType == Token.LEFTBRACKET) { openBrackets++; } } else { switch (next.TokenType) { case Token.LEFTBRACKET: // Path Group if (lastItem == LastPathItemType.Predicate || lastItem == LastPathItemType.Modifier) { // If it follows a Predicate/Modifier then this is likely a collection as the object of a // Triple pattern so we stop lastItem = LastPathItemType.End; } else if (lastItem == LastPathItemType.Sequencer || lastItem == LastPathItemType.Negation) { // This is a new Path Group if it follows a sequencer/negation openBrackets++; context.Tokens.Dequeue(); tokens.Enqueue(next); lastItem = LastPathItemType.Predicate; } else { throw new RdfParseException("Path Groups can only follow path sequencing tokens", next); } break; case Token.LEFTCURLYBRACKET: // Explicit cardinality modifiers if (context.SyntaxMode == SparqlQuerySyntax.Sparql_1_1) { throw new RdfParseException("The {} forms for property paths are not supported in SPARQL 1.1", next); } if (lastItem != LastPathItemType.Predicate) { throw new RdfParseException("Cardinality Modifiers can only follow Predicates/Path Groups", next); } // Add the opening { to the tokens context.Tokens.Dequeue(); tokens.Enqueue(next); next = context.Tokens.Peek(); // Grab everything up to the next } while (next.TokenType != Token.RIGHTCURLYBRACKET) { // If we see another { this is an error if (next.TokenType == Token.LEFTCURLYBRACKET) { throw new RdfParseException("Nested Cardinality Modifiers for Paths are not permitted", next); } context.Tokens.Dequeue(); tokens.Enqueue(next); next = context.Tokens.Peek(); } // Add the trailing } to the tokens context.Tokens.Dequeue(); tokens.Enqueue(next); lastItem = LastPathItemType.Modifier; break; case Token.PLUS: case Token.QUESTION: case Token.MULTIPLY: // Other Cardinality modifiers are permitted if (lastItem != LastPathItemType.Predicate) { throw new RdfParseException("Cardinality Modifiers can only follow Predicates/Path Groups"); } context.Tokens.Dequeue(); tokens.Enqueue(next); lastItem = LastPathItemType.Modifier; break; case Token.BITWISEOR: case Token.DIVIDE: case Token.HAT: // Path sequencing if (lastItem != LastPathItemType.Predicate && lastItem != LastPathItemType.Modifier) { if (lastItem == LastPathItemType.Sequencer && next.TokenType == Token.HAT && (lastSequencer == Token.DIVIDE || lastSequencer == Token.BITWISEOR)) { // / ^ or | ^ is a valid sequencing } else { throw new RdfParseException("Path sequencing tokens can only follow Predicates/Path Groups", next); } } context.Tokens.Dequeue(); tokens.Enqueue(next); lastItem = LastPathItemType.Sequencer; lastSequencer = next.TokenType; break; case Token.QNAME: case Token.URI: case Token.KEYWORDA: // Predicates if (lastItem != LastPathItemType.None && lastItem != LastPathItemType.Sequencer && lastItem != LastPathItemType.Negation) { // This appears to be the end of the path since we've encountered something that could be // an Object lastItem = LastPathItemType.End; } else { context.Tokens.Dequeue(); tokens.Enqueue(next); lastItem = LastPathItemType.Predicate; } break; case Token.NEGATION: if (lastItem == LastPathItemType.Sequencer) { context.Tokens.Dequeue(); tokens.Enqueue(next); lastItem = LastPathItemType.Negation; } else { throw new RdfParseException("Negated Property Sets can only follow path sequencing tokens", next); } break; default: if (lastItem != LastPathItemType.None && lastItem != LastPathItemType.Sequencer) { // Appears to be the end of the path since we've encountered an unexpected token after seeing // a Predicate lastItem = LastPathItemType.End; } else { throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encounted, this is not a valid token for a Path", next); } break; } if (lastItem == LastPathItemType.End) { break; } } } ISparqlPath path = TryParsePath(context, tokens); return(path); }
/// <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 new Alternative Path /// </summary> /// <param name="lhs">LHS Path</param> /// <param name="rhs">RHS Path</param> public AlternativePath(ISparqlPath lhs, ISparqlPath rhs) : base(lhs, rhs) { }
private ISparqlAlgebra GetAlgebra(ISparqlPath path) { return GetAlgebra(path, null, null); }
private ISparqlPath createPath(IResource path) { if (path.isUri()) { return(new Property(path.getSource())); } else { IResource typeS = path.getResource(RDF.PropertyType); if (typeS != null && typeS.isUri()) { INode type = typeS; if (RDFUtil.sameTerm(SP.ClassAltPath, type)) { ISparqlPath leftPath = createPath(path, SP.PropertyPath1); ISparqlPath rightPath = createPath(path, SP.PropertyPath2); return(new AlternativePath(leftPath, rightPath)); } else if (RDFUtil.sameTerm(SP.ClassModPath, type)) { ISparqlPath subPath = createPath(path, SP.PropertySubPath); int? min = path.getInteger(SP.PropertyModMin); int? max = path.getInteger(SP.PropertyModMax); if (max == null || max < 0) { if (min == 1) { return(new OneOrMore(subPath)); // TODO: is this correct? } else if (min == null || min == -1) { return(new ZeroOrMore(subPath)); } else { // -2 return(new NOrMore(subPath, (int)min)); // TODO: is this correct? } } else { if (min == null || min == -1) { return(new ZeroToN(subPath, (int)max)); } else { return(new NToM(subPath, (int)min, (int)max)); } } } else if (RDFUtil.sameTerm(SP.ClassReversePath, type)) { ISparqlPath subPath = createPath(path, SP.PropertySubPath); return(new InversePath(subPath)); } else if (RDFUtil.sameTerm(SP.ClassSeqPath, type)) { ISparqlPath leftPath = createPath(path, SP.PropertyPath1); ISparqlPath rightPath = createPath(path, SP.PropertyPath2); return(new SequencePath(leftPath, rightPath)); } else if (RDFUtil.sameTerm(SP.ClassReverseLinkPath, type)) { IResource node = path.getObject(SP.PropertyNode); ISparqlPath subPath = createPath(node); // TODO: is this correct ? return(new InversePath(subPath)); } } return(null); } }
private ISparqlPath TryParsePathMod(SparqlQueryParserContext context, Queue<IToken> tokens, ISparqlPath path) { IToken next = tokens.Dequeue(); switch (next.TokenType) { case Token.MULTIPLY: return new ZeroOrMore(path); case Token.QUESTION: return new ZeroOrOne(path); case Token.PLUS: return new OneOrMore(path); case Token.LEFTCURLYBRACKET: next = tokens.Dequeue(); int min, max; if (next.TokenType == Token.PLAINLITERAL) { if (Int32.TryParse(next.Value, out min)) { if (min < 0) throw new RdfParseException("Cannot specify the minimum cardinality of a path as less than zero", next); next = tokens.Dequeue(); if (next.TokenType == Token.COMMA) { next = tokens.Dequeue(); if (next.TokenType == Token.PLAINLITERAL) { if (Int32.TryParse(next.Value, out max)) { if (max < min) throw new RdfParseException("Cannot specify the maximum cardinality of a path as less than the minimum", next); next = tokens.Dequeue(); if (next.TokenType != Token.RIGHTCURLYBRACKET) throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a } to terminate a Path Cardinality modifier", next); if (min == max) { return new FixedCardinality(path, min); } else { return new NToM(path, min, max); } } else { throw new RdfParseException("The value '" + next.Value + "' is not valid for use as a Path Cardinality modifier", next); } } else if (next.TokenType == Token.RIGHTCURLYBRACKET) { if (min == 0) { return new ZeroOrMore(path); } else if (min == 1) { return new OneOrMore(path); } else { return new NOrMore(path, min); } } else { throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected an Integer Plain Literal as part of a Path Cardinality modifier", next); } } else if (next.TokenType == Token.RIGHTCURLYBRACKET) { return new FixedCardinality(path, min); } else { throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a valid token to continue the Path Cardinality modifier", next); } } else { throw new RdfParseException("The value '" + next.Value + "' is not valid for use as a Path Cardinality modifier", next); } } else if (next.TokenType == Token.COMMA) { next = tokens.Dequeue(); if (next.TokenType == Token.PLAINLITERAL) { if (Int32.TryParse(next.Value, out max)) { if (max <= 0) throw new RdfParseException("Cannot specify the maximum cardinality for a path as being less than the minimum", next); next = tokens.Dequeue(); if (next.TokenType != Token.RIGHTCURLYBRACKET) throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a } to terminate a Path Cardinality modifier", next); if (max == 1) { return new ZeroOrOne(path); } else { return new ZeroToN(path, max); } } else { throw new RdfParseException("The value '" + next.Value + "' is not valid for use as a Path Cardinality modifier", next); } } else { throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected an Integer Plain Literal as part of a Path Cardinality modifier", next); } } else { throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected an Integer Plain Literal/Comma as part of a Path Cardinality modifier", next); } default: throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a token which is valid as a Path Cardinality modifier", next); } }
/// <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 Binary Path /// </summary> /// <param name="lhs">LHS Path</param> /// <param name="rhs">RHS Path</param> public BaseBinaryPath(ISparqlPath lhs, ISparqlPath rhs) { this._lhs = lhs; this._rhs = rhs; }
/// <summary> /// Creates a new Inverse Path /// </summary> /// <param name="path">Path</param> public InversePath(ISparqlPath path) : base(path) { }
/// <summary> /// Creates a new N to M cardinality restriction /// </summary> /// <param name="path">Path</param> /// <param name="n">Minimum Cardinality</param> /// <param name="m">Maximum Cardinality</param> public NToM(ISparqlPath path, int n, int m) : base(path) { this._n = n; this._m = m; }
/// <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> /// 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> /// Formats a SPARQL Property Path /// </summary> /// <param name="path">SPARQL Property Path</param> /// <returns></returns> protected virtual String FormatPath(ISparqlPath path) { StringBuilder output = new StringBuilder(); if (path is AlternativePath) { AlternativePath alt = (AlternativePath)path; output.Append('('); output.Append(this.FormatPath(alt.LhsPath)); output.Append(" | "); output.Append(this.FormatPath(alt.RhsPath)); output.Append(')'); } else if (path is FixedCardinality) { FixedCardinality card = (FixedCardinality)path; if (card.Path is BaseBinaryPath) output.Append('('); output.Append(this.FormatPath(card.Path)); if (card.Path is BaseBinaryPath) output.Append(')'); output.Append('{'); output.Append(card.MaxCardinality); output.Append('}'); } else if (path is InversePath) { InversePath inv = (InversePath)path; output.Append('^'); if (inv.Path is BaseBinaryPath) output.Append('('); output.Append(this.FormatPath(inv.Path)); if (inv.Path is BaseBinaryPath) output.Append(')'); } else if (path is NOrMore) { NOrMore nOrMore = (NOrMore)path; if (nOrMore.Path is BaseBinaryPath) output.Append('('); output.Append(this.FormatPath(nOrMore.Path)); if (nOrMore.Path is BaseBinaryPath) output.Append(')'); output.Append('{'); output.Append(nOrMore.MinCardinality); output.Append(",}"); } else if (path is NToM) { NToM nToM = (NToM)path; if (nToM.Path is BaseBinaryPath) output.Append('('); output.Append(this.FormatPath(nToM.Path)); if (nToM.Path is BaseBinaryPath) output.Append(')'); output.Append('{'); output.Append(nToM.MinCardinality); output.Append(','); output.Append(nToM.MaxCardinality); output.Append('}'); } else if (path is OneOrMore) { OneOrMore oneOrMore = (OneOrMore)path; if (oneOrMore.Path is BaseBinaryPath) output.Append('('); output.Append(this.FormatPath(oneOrMore.Path)); if (oneOrMore.Path is BaseBinaryPath) output.Append(')'); output.Append('+'); } else if (path is Property) { Property prop = (Property)path; output.Append(this.Format(prop.Predicate, TripleSegment.Predicate)); } else if (path is SequencePath) { SequencePath seq = (SequencePath)path; output.Append(this.FormatPath(seq.LhsPath)); output.Append(" / "); output.Append(this.FormatPath(seq.RhsPath)); } else if (path is ZeroOrMore) { ZeroOrMore zeroOrMore = (ZeroOrMore)path; if (zeroOrMore.Path is BaseBinaryPath) output.Append('('); output.Append(this.FormatPath(zeroOrMore.Path)); if (zeroOrMore.Path is BaseBinaryPath) output.Append(')'); output.Append('*'); } else if (path is ZeroOrOne) { ZeroOrOne zeroOrOne = (ZeroOrOne)path; if (zeroOrOne.Path is BaseBinaryPath) output.Append('('); output.Append(this.FormatPath(zeroOrOne.Path)); if (zeroOrOne.Path is BaseBinaryPath) output.Append(')'); output.Append('?'); } else if (path is ZeroToN) { ZeroToN zeroToN = (ZeroToN)path; if (zeroToN.Path is BaseBinaryPath) output.Append('('); output.Append(this.FormatPath(zeroToN.Path)); if (zeroToN.Path is BaseBinaryPath) output.Append(')'); output.Append("{,"); output.Append(zeroToN.MaxCardinality); output.Append('}'); } else if (path is NegatedSet) { NegatedSet negSet = (NegatedSet)path; output.Append('!'); if (negSet.Properties.Count() + negSet.InverseProperties.Count() > 1) output.Append('('); foreach (Property p in negSet.Properties) { output.Append(this.FormatPath(p)); output.Append(" | "); } foreach (Property p in negSet.InverseProperties) { output.Append(this.FormatPath(p)); output.Append(" | "); } output.Remove(output.Length - 3, 3); if (negSet.Properties.Count() + negSet.InverseProperties.Count() > 1) output.Append(')'); } else { throw new RdfOutputException("Unable to Format an unknown ISparqlPath implementations as a String"); } return output.ToString(); }
/// <summary> /// Creates a new Path Token /// </summary> /// <param name="path">Path</param> public PathToken(ISparqlPath path) : base(Token.PATH, path.ToString(), 0, 0, 0, 0) { this._path = path; }
/// <summary> /// Creates a new Alternative Path /// </summary> /// <param name="lhs">LHS Path</param> /// <param name="rhs">RHS Path</param> public AlternativePath(ISparqlPath lhs, ISparqlPath rhs) : base(lhs, rhs) { }
/// <summary> /// Creates a new Fixed Cardinality restriction /// </summary> /// <param name="path">Path</param> /// <param name="n">N</param> public FixedCardinality(ISparqlPath path, int n) : base(path) { this._n = n; }
private ISparqlAlgebra GetAlgebraUntransformed(ISparqlPath path) { return(this.GetAlgebraUntransformed(path, null, null)); }
/// <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 Zero or More cardinality restriction /// </summary> /// <param name="path">Path</param> public ZeroOrMore(ISparqlPath path) : base(path) { }
/// <summary> /// Creates a new Sequence Path /// </summary> /// <param name="lhs">LHS Path</param> /// <param name="rhs">RHS Path</param> public SequencePath(ISparqlPath lhs, ISparqlPath rhs) : base(lhs, rhs) { }
/// <summary> /// Creates a new One or More cardinality restriction /// </summary> /// <param name="path">Path</param> public OneOrMore(ISparqlPath path) : base(path) { }
/// <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> /// Creates a new Zero to N cardinality restriction /// </summary> /// <param name="path">Path</param> /// <param name="n">Maximum Cardinality</param> public ZeroToN(ISparqlPath path, int n) : base(path) { this._n = n; }
/// <summary> /// Creates a new Path Token /// </summary> /// <param name="path">Path</param> public PathToken(ISparqlPath path) : base(Token.PATH, path.ToString(), 0, 0, 0, 0) { _path = path; }
/// <summary> /// Creates a new Cardinality Restriction /// </summary> /// <param name="path">Path</param> public Cardinality(ISparqlPath path) : base(path) { }
private ISparqlAlgebra GetAlgebraUntransformed(ISparqlPath path) { return this.GetAlgebraUntransformed(path, null, null); }
/// <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, path, end) { }
private ISparqlPath TryParsePathMod(SparqlQueryParserContext context, Queue <IToken> tokens, ISparqlPath path) { IToken next = tokens.Dequeue(); switch (next.TokenType) { case Token.MULTIPLY: return(new ZeroOrMore(path)); case Token.QUESTION: return(new ZeroOrOne(path)); case Token.PLUS: return(new OneOrMore(path)); case Token.LEFTCURLYBRACKET: next = tokens.Dequeue(); int min, max; if (next.TokenType == Token.PLAINLITERAL) { if (Int32.TryParse(next.Value, out min)) { if (min < 0) { throw new RdfParseException("Cannot specify the minimum cardinality of a path as less than zero", next); } next = tokens.Dequeue(); if (next.TokenType == Token.COMMA) { next = tokens.Dequeue(); if (next.TokenType == Token.PLAINLITERAL) { if (Int32.TryParse(next.Value, out max)) { if (max < min) { throw new RdfParseException("Cannot specify the maximum cardinality of a path as less than the minimum", next); } next = tokens.Dequeue(); if (next.TokenType != Token.RIGHTCURLYBRACKET) { throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a } to terminate a Path Cardinality modifier", next); } if (min == max) { return(new FixedCardinality(path, min)); } else { return(new NToM(path, min, max)); } } else { throw new RdfParseException("The value '" + next.Value + "' is not valid for use as a Path Cardinality modifier", next); } } else if (next.TokenType == Token.RIGHTCURLYBRACKET) { if (min == 0) { return(new ZeroOrMore(path)); } else if (min == 1) { return(new OneOrMore(path)); } else { return(new NOrMore(path, min)); } } else { throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected an Integer Plain Literal as part of a Path Cardinality modifier", next); } } else if (next.TokenType == Token.RIGHTCURLYBRACKET) { return(new FixedCardinality(path, min)); } else { throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a valid token to continue the Path Cardinality modifier", next); } } else { throw new RdfParseException("The value '" + next.Value + "' is not valid for use as a Path Cardinality modifier", next); } } else if (next.TokenType == Token.COMMA) { next = tokens.Dequeue(); if (next.TokenType == Token.PLAINLITERAL) { if (Int32.TryParse(next.Value, out max)) { if (max <= 0) { throw new RdfParseException("Cannot specify the maximum cardinality for a path as being less than the minimum", next); } next = tokens.Dequeue(); if (next.TokenType != Token.RIGHTCURLYBRACKET) { throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a } to terminate a Path Cardinality modifier", next); } if (max == 1) { return(new ZeroOrOne(path)); } else { return(new ZeroToN(path, max)); } } else { throw new RdfParseException("The value '" + next.Value + "' is not valid for use as a Path Cardinality modifier", next); } } else { throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected an Integer Plain Literal as part of a Path Cardinality modifier", next); } } else { throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected an Integer Plain Literal/Comma as part of a Path Cardinality modifier", next); } default: throw new RdfParseException("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a token which is valid as a Path Cardinality modifier", next); } }
/// <summary> /// Creates a new Unary Path /// </summary> /// <param name="path">Path</param> public BaseUnaryPath(ISparqlPath path) { this._path = path; }
/// <summary> /// Creates a new Unary Path. /// </summary> /// <param name="path">Path.</param> public BaseUnaryPath(ISparqlPath path) { _path = path; }
/// <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 Zero or One cardinality restriction /// </summary> /// <param name="path">Path</param> public ZeroOrOne(ISparqlPath path) : base(path) { }
/// <summary> /// Creates a new Zero or More cardinality restriction /// </summary> /// <param name="path">Path</param> public ZeroOrMore(ISparqlPath path) : base(path) { }
/// <summary> /// Creates a new N or More cardinality restriction /// </summary> /// <param name="path">Path</param> /// <param name="n">Minimum Cardinality</param> public NOrMore(ISparqlPath path, int n) : base(path) { this._n = n; }
/// <summary> /// Creates a new Zero or One cardinality restriction /// </summary> /// <param name="path">Path</param> public ZeroOrOne(ISparqlPath path) : base(path) { }
/// <summary> /// Creates a new N to M cardinality restriction /// </summary> /// <param name="path">Path</param> /// <param name="n">Minimum Cardinality</param> /// <param name="m">Maximum Cardinality</param> public NToM(ISparqlPath path, int n, int m) : base(path) { this._n = n; this._m = m; }
/// <summary> /// Creates a new One or More cardinality restriction /// </summary> /// <param name="path">Path</param> public OneOrMore(ISparqlPath path) : base(path) { }
/// <summary> /// Creates a new Fixed Cardinality restriction /// </summary> /// <param name="path">Path</param> /// <param name="n">N</param> public FixedCardinality(ISparqlPath path, int n) : base(path) { this._n = n; }
/// <summary> /// Creates a new N or More cardinality restriction /// </summary> /// <param name="path">Path</param> /// <param name="n">Minimum Cardinality</param> public NOrMore(ISparqlPath path, int n) : base(path) { this._n = n; }
/// <summary> /// Formats a SPARQL Property Path /// </summary> /// <param name="path">SPARQL Property Path</param> /// <returns></returns> protected virtual String FormatPath(ISparqlPath path) { StringBuilder output = new StringBuilder(); if (path is AlternativePath) { AlternativePath alt = (AlternativePath)path; output.Append('('); output.Append(this.FormatPath(alt.LhsPath)); output.Append(" | "); output.Append(this.FormatPath(alt.RhsPath)); output.Append(')'); } else if (path is FixedCardinality) { FixedCardinality card = (FixedCardinality)path; if (card.Path is BaseBinaryPath) { output.Append('('); } output.Append(this.FormatPath(card.Path)); if (card.Path is BaseBinaryPath) { output.Append(')'); } output.Append('{'); output.Append(card.MaxCardinality); output.Append('}'); } else if (path is InversePath) { InversePath inv = (InversePath)path; output.Append('^'); if (inv.Path is BaseBinaryPath) { output.Append('('); } output.Append(this.FormatPath(inv.Path)); if (inv.Path is BaseBinaryPath) { output.Append(')'); } } else if (path is NOrMore) { NOrMore nOrMore = (NOrMore)path; if (nOrMore.Path is BaseBinaryPath) { output.Append('('); } output.Append(this.FormatPath(nOrMore.Path)); if (nOrMore.Path is BaseBinaryPath) { output.Append(')'); } output.Append('{'); output.Append(nOrMore.MinCardinality); output.Append(",}"); } else if (path is NToM) { NToM nToM = (NToM)path; if (nToM.Path is BaseBinaryPath) { output.Append('('); } output.Append(this.FormatPath(nToM.Path)); if (nToM.Path is BaseBinaryPath) { output.Append(')'); } output.Append('{'); output.Append(nToM.MinCardinality); output.Append(','); output.Append(nToM.MaxCardinality); output.Append('}'); } else if (path is OneOrMore) { OneOrMore oneOrMore = (OneOrMore)path; if (oneOrMore.Path is BaseBinaryPath) { output.Append('('); } output.Append(this.FormatPath(oneOrMore.Path)); if (oneOrMore.Path is BaseBinaryPath) { output.Append(')'); } output.Append('+'); } else if (path is Property) { Property prop = (Property)path; output.Append(this.Format(prop.Predicate, TripleSegment.Predicate)); } else if (path is SequencePath) { SequencePath seq = (SequencePath)path; output.Append(this.FormatPath(seq.LhsPath)); output.Append(" / "); output.Append(this.FormatPath(seq.RhsPath)); } else if (path is ZeroOrMore) { ZeroOrMore zeroOrMore = (ZeroOrMore)path; if (zeroOrMore.Path is BaseBinaryPath) { output.Append('('); } output.Append(this.FormatPath(zeroOrMore.Path)); if (zeroOrMore.Path is BaseBinaryPath) { output.Append(')'); } output.Append('*'); } else if (path is ZeroOrOne) { ZeroOrOne zeroOrOne = (ZeroOrOne)path; if (zeroOrOne.Path is BaseBinaryPath) { output.Append('('); } output.Append(this.FormatPath(zeroOrOne.Path)); if (zeroOrOne.Path is BaseBinaryPath) { output.Append(')'); } output.Append('?'); } else if (path is ZeroToN) { ZeroToN zeroToN = (ZeroToN)path; if (zeroToN.Path is BaseBinaryPath) { output.Append('('); } output.Append(this.FormatPath(zeroToN.Path)); if (zeroToN.Path is BaseBinaryPath) { output.Append(')'); } output.Append("{,"); output.Append(zeroToN.MaxCardinality); output.Append('}'); } else if (path is NegatedSet) { NegatedSet negSet = (NegatedSet)path; output.Append('!'); if (negSet.Properties.Count() + negSet.InverseProperties.Count() > 1) { output.Append('('); } foreach (Property p in negSet.Properties) { output.Append(this.FormatPath(p)); output.Append(" | "); } foreach (Property p in negSet.InverseProperties) { output.Append(this.FormatPath(p)); output.Append(" | "); } output.Remove(output.Length - 3, 3); if (negSet.Properties.Count() + negSet.InverseProperties.Count() > 1) { output.Append(')'); } } else { throw new RdfOutputException("Unable to Format an unknown ISparqlPath implementations as a String"); } return(output.ToString()); }
/// <summary> /// Creates a new Zero to N cardinality restriction /// </summary> /// <param name="path">Path</param> /// <param name="n">Maximum Cardinality</param> public ZeroToN(ISparqlPath path, int n) : base(path) { this._n = n; }
/// <summary> /// Creates a new Cardinality Restriction /// </summary> /// <param name="path">Path</param> public Cardinality(ISparqlPath path) : base(path) { }
/// <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) { }