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"); } } }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { bool top = context.Top; //The Object becomes a temporary variable then we transform the LHS of the path context.Object = context.GetNextTemporaryVariable(); context.Top = false; context.AddTriplePattern(context.GetTriplePattern(context.Subject, this._lhs, context.Object)); //The Subject is then the Object that results from the LHS transform since the //Transform may adjust the Object context.Subject = context.Object; //We then reset the Object to be the target Object so that if the RHS is the last part //of the Path then it will complete the path transformation //If it isn't the last part of the path it will be set to a new temporary variable context.Top = top; if (context.Top) { context.ResetObject(); } else { context.Object = context.GetNextTemporaryVariable(); } context.Top = top; context.AddTriplePattern(context.GetTriplePattern(context.Subject, this._rhs, context.Object)); return context.ToAlgebra(); }
/// <summary> /// Converts a Path into its Algebra Form. /// </summary> /// <param name="context">Path Transformation Context.</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { bool top = context.Top; // The Object becomes a temporary variable then we transform the LHS of the path context.Object = context.GetNextTemporaryVariable(); context.Top = false; context.AddTriplePattern(context.GetTriplePattern(context.Subject, _lhs, context.Object)); // The Subject is then the Object that results from the LHS transform since the // Transform may adjust the Object context.Subject = context.Object; // We then reset the Object to be the target Object so that if the RHS is the last part // of the Path then it will complete the path transformation // If it isn't the last part of the path it will be set to a new temporary variable context.Top = top; if (context.Top) { context.ResetObject(); } else { context.Object = context.GetNextTemporaryVariable(); } context.Top = top; context.AddTriplePattern(context.GetTriplePattern(context.Subject, _rhs, context.Object)); return(context.ToAlgebra()); }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { if (this._n > 0) { // Generate a Triple Pattern for each step in the cardinality for (int i = 0; i < this._n; i++) { context.Object = context.GetNextTemporaryVariable(); if (i < this._n - 1 || !context.Top) { context.AddTriplePattern(context.GetTriplePattern(context.Subject, this._path, context.Object)); context.Subject = context.Object; } else { context.ResetObject(); context.AddTriplePattern(context.GetTriplePattern(context.Subject, this._path, context.Object)); } } return(context.ToAlgebra()); } else { return(new ZeroLengthPath(context.Subject, context.Object, this._path)); } }
/// <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> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { PathTransformContext lhsContext = new PathTransformContext(context); PathTransformContext rhsContext = new PathTransformContext(context); ISparqlAlgebra lhs = this._lhs.ToAlgebra(lhsContext); ISparqlAlgebra rhs = this._rhs.ToAlgebra(rhsContext); return new Union(lhs, rhs); }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { PatternItem tempVar = context.GetNextTemporaryVariable(); context.AddTriplePattern(new PropertyPathPattern(context.Subject, new FixedCardinality(this._path, this._n), tempVar)); context.AddTriplePattern(new PropertyPathPattern(tempVar, new ZeroOrMore(this._path), context.Object)); return(context.ToAlgebra()); }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { PathTransformContext lhsContext = new PathTransformContext(context); PathTransformContext rhsContext = new PathTransformContext(context); ISparqlAlgebra lhs = new ZeroLengthPath(lhsContext.Subject, lhsContext.Object, this._path); ISparqlAlgebra rhs = this._path.ToAlgebra(rhsContext); return(new Distinct(new Union(lhs, rhs))); }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { PathTransformContext lhsContext = new PathTransformContext(context); PathTransformContext rhsContext = new PathTransformContext(context); ISparqlAlgebra lhs = this._lhs.ToAlgebra(lhsContext); ISparqlAlgebra rhs = this._rhs.ToAlgebra(rhsContext); return(new Union(lhs, rhs)); }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { //Swap the Subject and Object over PatternItem tempObj = context.Object; PatternItem tempSubj = context.Subject; PatternItem tempEnd = context.End; context.Object = tempSubj; context.Subject = tempObj; context.End = tempSubj; //Then transform the path context.AddTriplePattern(context.GetTriplePattern(context.Subject, this._path, context.Object)); //Then swap the Subject and Object back context.Subject = tempSubj; context.Object = tempObj; context.End = tempEnd; return context.ToAlgebra(); }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { ISparqlAlgebra complex = null; int i = this._n; while (i <= this._m) { PathTransformContext tempContext = new PathTransformContext(context); tempContext.AddTriplePattern(new PropertyPathPattern(context.Subject, new FixedCardinality(this._path, i), context.Object)); if (complex == null) { complex = tempContext.ToAlgebra(); } else { complex = new Union(complex, tempContext.ToAlgebra()); } i++; } return(complex); }
/// <summary> /// Converts a Path into its Algebra Form. /// </summary> /// <param name="context">Path Transformation Context.</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { // Swap the Subject and Object over PatternItem tempObj = context.Object; PatternItem tempSubj = context.Subject; PatternItem tempEnd = context.End; context.Object = tempSubj; context.Subject = tempObj; context.End = tempSubj; // Then transform the path context.AddTriplePattern(context.GetTriplePattern(context.Subject, _path, context.Object)); // Then swap the Subject and Object back context.Subject = tempSubj; context.Object = tempObj; context.End = tempEnd; return(context.ToAlgebra()); }
/// <summary> /// Converts a Path into its Algebra Form. /// </summary> /// <param name="context">Path Transformation Context.</param> /// <returns></returns> public ISparqlAlgebra ToAlgebra(PathTransformContext context) { if (_properties.Count > 0 && _inverseProperties.Count == 0) { return(new NegatedPropertySet(context.Subject, context.Object, _properties)); } else if (_properties.Count == 0 && _inverseProperties.Count > 0) { return(new NegatedPropertySet(context.Object, context.Subject, _inverseProperties, true)); } else { PathTransformContext lhsContext = new PathTransformContext(context); PathTransformContext rhsContext = new PathTransformContext(context); lhsContext.AddTriplePattern(new PropertyPathPattern(lhsContext.Subject, new NegatedSet(_properties, Enumerable.Empty <Property>()), lhsContext.Object)); rhsContext.AddTriplePattern(new PropertyPathPattern(rhsContext.Subject, new NegatedSet(Enumerable.Empty <Property>(), _inverseProperties), rhsContext.Object)); ISparqlAlgebra lhs = lhsContext.ToAlgebra(); ISparqlAlgebra rhs = rhsContext.ToAlgebra(); return(new Union(lhs, rhs)); } }
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); }
/// <summary> /// Evaluates the Path in the given context /// </summary> /// <param name="context">Evaluation Context</param> /// <returns></returns> public override BaseMultiset Evaluate(SparqlEvaluationContext context) { //Try and generate an Algebra expression //Make sure we don't generate clashing temporary variable IDs over the life of the //Evaluation PathTransformContext transformContext = new PathTransformContext(this.PathStart, this.PathEnd); if (context["PathTransformID"] != null) { transformContext.NextID = (int)context["PathTransformID"]; } ISparqlAlgebra algebra = this.Path.ToAlgebra(transformContext); context["PathTransformID"] = transformContext.NextID; //Now we can evaluate the resulting algebra //Note: We may need to preserve Blank Node variables across evaluations //which we usually don't do BUT because of the way we translate only part of the path //into an algebra at a time and may need to do further nested translate calls we do //need to do this here BaseMultiset initialInput = context.InputMultiset; bool trimMode = context.TrimTemporaryVariables; try { context.TrimTemporaryVariables = false; BaseMultiset result = context.Evaluate(algebra);//algebra.Evaluate(context); //Also note that we don't trim temporary variables here even if we've set the setting back //to enabled since a Trim will be done at the end of whatever BGP we are being evaluated in //Once we have our results can join then into our input context.OutputMultiset = initialInput.Join(result); } finally { context.TrimTemporaryVariables = trimMode; } return context.OutputMultiset; }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { context.AddTriplePattern(new PropertyPathPattern(context.Subject, new NToM(this._path, 0, this._n), context.Object)); return context.ToAlgebra(); }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { ISparqlAlgebra complex = null; int i = this._n; while (i <= this._m) { PathTransformContext tempContext = new PathTransformContext(context); tempContext.AddTriplePattern(new PropertyPathPattern(context.Subject, new FixedCardinality(this._path, i), context.Object)); if (complex == null) { complex = tempContext.ToAlgebra(); } else { complex = new Union(complex, tempContext.ToAlgebra()); } i++; } return complex; }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { return new OneOrMorePath(context.Subject, context.Object, this._path); }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { PatternItem tempVar = context.GetNextTemporaryVariable(); context.AddTriplePattern(new PropertyPathPattern(context.Subject, new FixedCardinality(this._path, this._n), tempVar)); context.AddTriplePattern(new PropertyPathPattern(tempVar, new ZeroOrMore(this._path), context.Object)); return context.ToAlgebra(); }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { if (this._n > 0) { //Generate a Triple Pattern for each step in the cardinality for (int i = 0; i < this._n; i++) { context.Object = context.GetNextTemporaryVariable(); if (i < this._n - 1 || !context.Top) { context.AddTriplePattern(context.GetTriplePattern(context.Subject, this._path, context.Object)); context.Subject = context.Object; } else { context.ResetObject(); context.AddTriplePattern(context.GetTriplePattern(context.Subject, this._path, context.Object)); } } return context.ToAlgebra(); } else { return new ZeroLengthPath(context.Subject, context.Object, this._path); } }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { PathTransformContext lhsContext = new PathTransformContext(context); PathTransformContext rhsContext = new PathTransformContext(context); ISparqlAlgebra lhs = new ZeroLengthPath(lhsContext.Subject, lhsContext.Object, this._path); ISparqlAlgebra rhs = this._path.ToAlgebra(context); return new Union(lhs, rhs); }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public ISparqlAlgebra ToAlgebra(PathTransformContext context) { context.AddTriplePattern(context.GetTriplePattern(context.Subject, this, context.Object)); return context.ToAlgebra(); }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public abstract ISparqlAlgebra ToAlgebra(PathTransformContext context);
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public ISparqlAlgebra ToAlgebra(PathTransformContext context) { if (this._properties.Count > 0 && this._inverseProperties.Count == 0) { return new NegatedPropertySet(context.Subject, context.Object, this._properties); } else if (this._properties.Count == 0 && this._inverseProperties.Count > 0) { return new NegatedPropertySet(context.Object, context.Subject, this._inverseProperties, true); } else { PathTransformContext lhsContext = new PathTransformContext(context); PathTransformContext rhsContext = new PathTransformContext(context); lhsContext.AddTriplePattern(new PropertyPathPattern(lhsContext.Subject, new NegatedSet(this._properties, Enumerable.Empty<Property>()), lhsContext.Object)); rhsContext.AddTriplePattern(new PropertyPathPattern(rhsContext.Subject, new NegatedSet(Enumerable.Empty<Property>(), this._inverseProperties), rhsContext.Object)); ISparqlAlgebra lhs = lhsContext.ToAlgebra(); ISparqlAlgebra rhs = rhsContext.ToAlgebra(); return new Union(lhs, rhs); } }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { context.AddTriplePattern(new PropertyPathPattern(context.Subject, new NToM(this._path, 0, this._n), context.Object)); return(context.ToAlgebra()); }
/// <summary> /// Converts a Path into its Algebra Form /// </summary> /// <param name="context">Path Transformation Context</param> /// <returns></returns> public override ISparqlAlgebra ToAlgebra(PathTransformContext context) { return(new OneOrMorePath(context.Subject, context.Object, this._path)); }