private void TestExtractPatterns(String query, int expectedPatterns, int expectedSubjArgs, int expectedObjArgs) { FullTextPropertyFunctionFactory factory = new FullTextPropertyFunctionFactory(); try { PropertyFunctionFactory.AddFactory(factory); SparqlParameterizedString queryString = new SparqlParameterizedString(query); queryString.Namespaces.AddNamespace("pf", new Uri(FullTextHelper.FullTextMatchNamespace)); SparqlQuery q = this._parser.ParseFromString(queryString); SparqlFormatter formatter = new SparqlFormatter(queryString.Namespaces); Console.WriteLine(formatter.Format(q)); Console.WriteLine(); List <IPropertyFunctionPattern> ps = PropertyFunctionHelper.ExtractPatterns(q.RootGraphPattern.TriplePatterns); Console.WriteLine(ps.Count + " Pattern(s) extracted"); foreach (IPropertyFunctionPattern propFunc in ps.Where(p => p.PropertyFunction is FullTextMatchPropertyFunction)) { Console.WriteLine("Match Variable: " + propFunc.SubjectArgs.First().ToString()); Console.WriteLine("Score Variable: " + (propFunc.SubjectArgs.Count() > 1 ? propFunc.SubjectArgs.Skip(1).First().ToString() : "N/A")); Console.WriteLine("Search Term: " + propFunc.ObjectArgs.First().ToString()); Console.WriteLine("Threshold/Limit: " + (propFunc.ObjectArgs.Count() > 1 ? propFunc.ObjectArgs.Skip(1).First().ToString() : "N/A")); Console.WriteLine("Limit: " + (propFunc.ObjectArgs.Count() > 2 ? propFunc.ObjectArgs.Skip(2).First().ToString() : "N/A")); Console.WriteLine(); if (expectedSubjArgs > 0) { Assert.Equal(expectedSubjArgs, propFunc.SubjectArgs.Count()); } if (expectedObjArgs > 0) { Assert.Equal(expectedObjArgs, propFunc.ObjectArgs.Count()); } } Assert.Equal(expectedPatterns, ps.Count); } finally { PropertyFunctionFactory.RemoveFactory(factory); } }
/// <summary> /// Optimises the algebra to include property functions /// </summary> /// <param name="algebra">Algebra</param> /// <returns></returns> public ISparqlAlgebra Optimise(ISparqlAlgebra algebra) { if (algebra is IBgp) { IBgp current = (IBgp)algebra; if (current.PatternCount == 0) { return(current); } List <ITriplePattern> ps = current.TriplePatterns.ToList(); List <IPropertyFunctionPattern> propFuncs = PropertyFunctionHelper.ExtractPatterns(ps, _factories.Value); if (propFuncs.Count == 0) { return(current); } // Remove raw Triple Patterns pertaining to extracted property functions foreach (IPropertyFunctionPattern propFunc in propFuncs) { // Track where we need to insert the property function back into the BGP ITriplePattern first = propFunc.OriginalPatterns.First(); int origLocation = ps.FindIndex(p => p.Equals(first)); foreach (ITriplePattern tp in propFunc.OriginalPatterns) { int location = ps.FindIndex(p => p.Equals(tp)); if (location >= 0) { if (location < origLocation) { origLocation--; } ps.RemoveAt(location); } else { throw new RdfQueryException("Bad Property Function extraction"); } } // Make the insert if (origLocation >= ps.Count || origLocation < 0 || ps.Count == 0) { ps.Add(propFunc); } else { ps.Insert(origLocation, propFunc); } } // Return a new BGP return(new Bgp(ps)); } else if (algebra is ITerminalOperator) { return(algebra); } else if (algebra is IAbstractJoin) { return(((IAbstractJoin)algebra).Transform(this)); } else if (algebra is IUnaryOperator) { return(((IUnaryOperator)algebra).Transform(this)); } else { return(algebra); } }