예제 #1
0
        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");
            }
        }
        /// <summary>
        /// Evaluates a property path pattern
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void 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(_subj, _obj);

            if (context["PathTransformID"] != null)
            {
                transformContext.NextID = (int)context["PathTransformID"];
            }
            ISparqlAlgebra algebra = _path.ToAlgebra(transformContext);

            context["PathTransformID"] = transformContext.NextID + 1;

            // Now we can evaluate the resulting algebra
            BaseMultiset initialInput = context.InputMultiset;
            bool         trimMode     = context.TrimTemporaryVariables;
            bool         rigMode      = Options.RigorousEvaluation;

            try
            {
                // Must enable rigorous evaluation or we get incorrect interactions between property and non-property path patterns
                Options.RigorousEvaluation = true;

                // 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
                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
                if (result is NullMultiset)
                {
                    context.OutputMultiset = new NullMultiset();
                }
                else
                {
                    context.OutputMultiset = initialInput.Join(result);
                }

                // If we reach here we've successfully evaluated the simple pattern and can return
                return;
            }
            finally
            {
                context.TrimTemporaryVariables = trimMode;
                Options.RigorousEvaluation     = rigMode;
            }
        }
예제 #3
0
        /// <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);

                //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);
        }
예제 #4
0
        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));
        }