public TwoVarCollapseGroup(IStore store, TriplePattern tp, IEnumerable<string> sortVars, IEnumerable<string> graphUris ) : base(tp, sortVars) { _store = store; _triplePatterns = new List<TriplePattern> {tp}; _matchLength = 2; _matchEnumerations = new List<IEnumerator<ulong[]>>(); _graphUris = graphUris; }
public SingleVarCollapseGroup(IStore store, TriplePattern tp, IEnumerable<string> globalSortVars, IEnumerable<string> graphUris) : base(tp, globalSortVars) { _store = store; if (tp.GetVariableCount() != 1) throw new ArgumentException("Triple pattern must bind a single variable"); _triplePatterns = new List<TriplePattern>{tp}; _matchEnumerations = new List<IEnumerator<ulong>>(); _graphUris = graphUris; }
private IEnumerable<ulong> GetMatchEnumeration(TriplePattern tp) { if (tp.Object.VariableName != null) { var subjectMatch = tp.Subject as NodeMatchPattern; var predMatch = tp.Predicate as NodeMatchPattern; var subj = (subjectMatch.Node as IUriNode).Uri; var pred = (predMatch.Node as IUriNode).Uri; return _store.GetMatchEnumeration(subj.ToString(), pred.ToString(), null, false,null, null, _graphUris); } if (tp.Subject.VariableName != null) { var predMatch = tp.Predicate as NodeMatchPattern; var objMatch = tp.Object as NodeMatchPattern; if (objMatch.Node is ILiteralNode) { var litNode = (objMatch.Node as ILiteralNode); var pred = (predMatch.Node as IUriNode).Uri; return _store.GetMatchEnumeration(null, pred.ToString(), litNode.Value, true, litNode.DataType == null ? Constants.DefaultDatatypeUri : litNode.DataType.ToString(), litNode.Language, _graphUris); } else { var pred = (predMatch.Node as IUriNode).Uri; var obj = (objMatch.Node as IUriNode).Uri; return _store.GetMatchEnumeration(null, pred.ToString(), obj.ToString(), false, null, null, _graphUris); } } if (tp.Predicate.VariableName != null) { var subjMatch = tp.Subject as NodeMatchPattern; var objMatch = tp.Object as NodeMatchPattern; if (objMatch.Node is ILiteralNode) { var litNode = (objMatch.Node as ILiteralNode); var subj = (subjMatch.Node as IUriNode); return _store.GetMatchEnumeration(subj.ToString(), null, litNode.Value, true, litNode.DataType == null ? Constants.DefaultDatatypeUri : litNode.DataType.ToString(), litNode.Language, _graphUris); } else { var subj = (subjMatch.Node as IUriNode).Uri; var obj = (objMatch.Node as IUriNode).Uri; return _store.GetMatchEnumeration(subj.ToString(), null, obj.ToString(), false, null, null, _graphUris); } } throw new BrightstarInternalException("Error determining match enumeration."); }
/// <summary> /// Generates a Comparer than can be used to do Ordering based on the given Triple Pattern /// </summary> /// <param name="pattern">Triple Pattern</param> /// <returns></returns> public override IComparer<Triple> GetComparer(TriplePattern pattern) { if (this._expr is VariableExpressionTerm) { IComparer<Triple> child = (this._child == null) ? null : this._child.GetComparer(pattern); Func<Triple, Triple, int> compareFunc = null; String var = this._expr.Variables.First(); if (var.Equals(pattern.Subject.VariableName)) { compareFunc = (x, y) => this._comparer.Compare(x.Subject, y.Subject); } else if (var.Equals(pattern.Predicate.VariableName)) { compareFunc = (x, y) => this._comparer.Compare(x.Predicate, y.Predicate); } else if (var.Equals(pattern.Object.VariableName)) { compareFunc = (x, y) => this._comparer.Compare(x.Object, y.Object); } if (compareFunc == null) return null; return new TripleComparer(compareFunc, this.Descending, child); } else { return null; } }
/// <summary> /// Generates a Comparer than can be used to do Ordering based on the given Triple Pattern /// </summary> /// <param name="pattern">Triple Pattern</param> /// <returns></returns> public override IComparer<Triple> GetComparer(TriplePattern pattern) { IComparer<Triple> child = (this._child == null) ? null : this._child.GetComparer(pattern); Func<Triple, Triple, int> compareFunc = null; if (this._varname.Equals(pattern.Subject.VariableName)) { compareFunc = (x, y) => this._comparer.Compare(x.Subject, y.Subject); } else if (this._varname.Equals(pattern.Predicate.VariableName)) { compareFunc = (x, y) => this._comparer.Compare(x.Predicate, y.Predicate); } else if (this._varname.Equals(pattern.Object.VariableName)) { compareFunc = (x, y) => this._comparer.Compare(x.Object, y.Object); } if (compareFunc == null) return null; return new TripleComparer(compareFunc, this.Descending, child); }
/// <summary> /// Generates a Comparer than can be used to do Ordering based on the given Triple Pattern /// </summary> /// <param name="pattern">Triple Pattern</param> /// <returns></returns> public abstract IComparer<Triple> GetComparer(TriplePattern pattern);
public abstract void AddTriplePattern(TriplePattern tp);
public override void AddTriplePattern(TriplePattern tp) { throw new NotImplementedException(); }
public override void AddTriplePattern(TriplePattern tp) { _triplePatterns.Add(tp); }
private IEnumerable<ulong[]> GetObjectSubjectMatchEnumeration(TriplePattern tp) { var predMatch = tp.Predicate as NodeMatchPattern; var pred = (predMatch.Node as UriNode).Uri; if (_haveFixedBinding) { if (!string.IsNullOrEmpty(_fixedSubjectBinding)) { return _store.EnumerateObjectsForPredicate(pred.ToString(), _graphUris, true); } if (!string.IsNullOrEmpty(_fixedObjectBinding)) { return _store.EnumerateSubjectsForPredicate(pred.ToString(), _graphUris, false); } } return _store.GetObjectSubjectMatchEnumeration(pred.ToString(), _graphUris); }
private IEnumerable<ulong[]> GetPredicateObjectMatchEnumeration(TriplePattern tp) { var subjMatch = tp.Subject as NodeMatchPattern; var uri = subjMatch.Node as IUriNode; return _store.GetPredicateObjectMatchEnumeration(uri.Uri.ToString(), _graphUris); }
private IEnumerable<ulong[]> GetObjectPredicateMatchEnumeration(TriplePattern tp) { /* var intermediate = GetPredicateObjectMatchEnumeration(tp).ToList(); var sorted = intermediate.OrderBy(x => x[1]).ThenBy(x => x[0]).Select(x => new ulong[] {x[1], x[0]}).ToList(); return sorted; */ return GetPredicateObjectMatchEnumeration(tp).OrderBy(x => x[1]).ThenBy(x => x[0]).Select(x=>new ulong[]{x[1], x[0]}); }
private IEnumerable<ulong[]> GetSubjectPredicateMatchEnumeration(TriplePattern tp) { return GetPredicateSubjectMatchEnumeration(tp).OrderBy(x => x[1]).ThenBy(x => x[0]).Select(x => new ulong[] { x[1], x[0] }); }
private IEnumerable<ulong[]> GetPredicateSubjectMatchEnumeration(TriplePattern tp) { var objMatch = tp.Object as NodeMatchPattern; if (objMatch.Node is ILiteralNode) { var lit = objMatch.Node as ILiteralNode; return _store.GetPredicateSubjectMatchEnumeration( lit.Value, true, lit.DataType == null ? Constants.DefaultDatatypeUri : lit.DataType.ToString(), lit.Language, _graphUris); } else { var uri = objMatch.Node as IUriNode; return _store.GetPredicateSubjectMatchEnumeration( uri.Uri.ToString(), false, null, null, _graphUris); } }
private IEnumerable<ulong[]> GetMatchEnumeration(TriplePattern tp) { if (tp.Subject.VariableName == null) { if (tp.Predicate.VariableName.Equals(SortVariables[0])) { return GetPredicateObjectMatchEnumeration(tp); } else { return GetObjectPredicateMatchEnumeration(tp); } } if (tp.Predicate.VariableName == null) { if (tp.Subject.VariableName.Equals(SortVariables[0])) { return GetSubjectObjectMatchEnumeration(tp); } else { return GetObjectSubjectMatchEnumeration(tp); } } if (tp.Object.VariableName == null) { if (tp.Subject.VariableName.Equals(SortVariables[0])) { return GetSubjectPredicateMatchEnumeration(tp); } else { return GetPredicateSubjectMatchEnumeration(tp); } } return GetAllTriplesEnumeration(tp); //throw new BrightstarInternalException("Invalid two-variable triple pattern"); }
public void SparqlBgpEvaluation() { //Prepare the Store TripleStore store = new TripleStore(); Graph g = new Graph(); FileLoader.Load(g, "Turtle.ttl"); store.Add(g); SparqlQueryParser parser = new SparqlQueryParser(); SparqlQuery q = parser.ParseFromString(@"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT * WHERE {?s ?p ?o . ?s rdfs:label ?label}"); Object testResult = store.ExecuteQuery(q); ISparqlAlgebra testAlgebra = q.ToAlgebra(); if (testResult is SparqlResultSet) { SparqlResultSet rset = (SparqlResultSet)testResult; Console.WriteLine(rset.Count + " Results"); foreach (SparqlResult r in rset) { Console.WriteLine(r.ToString()); } Console.WriteLine(); } //Create some Triple Patterns TriplePattern t1 = new TriplePattern(new VariablePattern("?s"), new VariablePattern("?p"), new VariablePattern("?o")); TriplePattern t2 = new TriplePattern(new VariablePattern("?s"), new NodeMatchPattern(g.CreateUriNode("rdfs:label")), new VariablePattern("?label")); TriplePattern t3 = new TriplePattern(new VariablePattern("?x"), new VariablePattern("?y"), new VariablePattern("?z")); TriplePattern t4 = new TriplePattern(new VariablePattern("?s"), new NodeMatchPattern(g.CreateUriNode(":name")), new VariablePattern("?name")); //Build some BGPs Bgp selectNothing = new Bgp(); Bgp selectAll = new Bgp(t1); Bgp selectLabelled = new Bgp(new List<ITriplePattern>() { t1, t2 }); Bgp selectAllDisjoint = new Bgp(new List<ITriplePattern>() { t1, t3 }); Bgp selectLabels = new Bgp(t2); Bgp selectNames = new Bgp(t4); //LeftJoin selectOptionalNamed = new LeftJoin(selectAll, new Optional(selectNames)); LeftJoin selectOptionalNamed = new LeftJoin(selectAll, selectNames); Union selectAllUnion = new Union(selectAll, selectAll); Union selectAllUnion2 = new Union(selectAllUnion, selectAll); Filter selectAllUriObjects = new Filter(selectAll, new UnaryExpressionFilter(new IsUriFunction(new VariableExpressionTerm("o")))); //Test out the BGPs //Console.WriteLine("{}"); //this.ShowMultiset(selectNothing.Evaluate(new SparqlEvaluationContext(null, store))); //Console.WriteLine("{?s ?p ?o}"); //this.ShowMultiset(selectAll.Evaluate(new SparqlEvaluationContext(null, store))); //Console.WriteLine("{?s ?p ?o . ?s rdfs:label ?label}"); //SparqlEvaluationContext context = new SparqlEvaluationContext(null, store); //this.ShowMultiset(selectLabelled.Evaluate(context)); //SparqlResultSet lvnResult = new SparqlResultSet(context); //Console.WriteLine("{?s ?p ?o . ?x ?y ?z}"); //this.ShowMultiset(selectAllDisjoint.Evaluate(new SparqlEvaluationContext(null, store))); //Console.WriteLine("{?s ?p ?o . OPTIONAL {?s :name ?name}}"); //this.ShowMultiset(selectOptionalNamed.Evaluate(new SparqlEvaluationContext(null, store))); Console.WriteLine("{{?s ?p ?o} UNION {?s ?p ?o}}"); this.ShowMultiset(selectAllUnion.Evaluate(new SparqlEvaluationContext(null, new InMemoryDataset(store)))); Console.WriteLine("{{?s ?p ?o} UNION {?s ?p ?o} UNION {?s ?p ?o}}"); this.ShowMultiset(selectAllUnion2.Evaluate(new SparqlEvaluationContext(null, new InMemoryDataset(store)))); Console.WriteLine("{?s ?p ?o FILTER (ISURI(?o))}"); this.ShowMultiset(selectAllUriObjects.Evaluate(new SparqlEvaluationContext(null, new InMemoryDataset(store)))); }
public void SparqlStreamingBgpSelectEvaluation() { //Get the Data we want to query TripleStore store = new TripleStore(); Graph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); store.Add(g); //g = new Graph(); //FileLoader.Load(g, "noise.ttl"); //store.Add(g); Console.WriteLine(store.Triples.Count() + " Triples in Store"); //Create the Triple Pattern we want to query with IUriNode fordFiesta = g.CreateUriNode(new Uri("http://example.org/vehicles/FordFiesta")); IUriNode rdfType = g.CreateUriNode(new Uri(RdfSpecsHelper.RdfType)); IUriNode rdfsLabel = g.CreateUriNode(new Uri(NamespaceMapper.RDFS + "label")); IUriNode speed = g.CreateUriNode(new Uri("http://example.org/vehicles/Speed")); IUriNode carClass = g.CreateUriNode(new Uri("http://example.org/vehicles/Car")); TriplePattern allTriples = new TriplePattern(new VariablePattern("?s"), new VariablePattern("?p"), new VariablePattern("?o")); TriplePattern allTriples2 = new TriplePattern(new VariablePattern("?x"), new VariablePattern("?y"), new VariablePattern("?z")); TriplePattern tp1 = new TriplePattern(new VariablePattern("?s"), new NodeMatchPattern(rdfType), new NodeMatchPattern(carClass)); TriplePattern tp2 = new TriplePattern(new VariablePattern("?s"), new NodeMatchPattern(speed), new VariablePattern("?speed")); TriplePattern tp3 = new TriplePattern(new VariablePattern("?s"), new NodeMatchPattern(rdfsLabel), new VariablePattern("?label")); TriplePattern novars = new TriplePattern(new NodeMatchPattern(fordFiesta), new NodeMatchPattern(rdfType), new NodeMatchPattern(carClass)); TriplePattern novars2 = new TriplePattern(new NodeMatchPattern(fordFiesta), new NodeMatchPattern(rdfsLabel), new NodeMatchPattern(carClass)); FilterPattern blankSubject = new FilterPattern(new UnaryExpressionFilter(new IsBlankFunction(new VariableExpressionTerm("?s")))); List<List<ITriplePattern>> tests = new List<List<ITriplePattern>>() { new List<ITriplePattern>() { }, new List<ITriplePattern>() { allTriples }, new List<ITriplePattern>() { allTriples, allTriples2 }, new List<ITriplePattern>() { tp1 }, new List<ITriplePattern>() { tp1, tp2 }, new List<ITriplePattern>() { tp1, tp3 }, new List<ITriplePattern>() { novars }, new List<ITriplePattern>() { novars, tp1 }, new List<ITriplePattern>() { novars, tp1, tp2 }, new List<ITriplePattern>() { novars2 }, new List<ITriplePattern>() { tp1, blankSubject } }; foreach (List<ITriplePattern> tps in tests) { Console.WriteLine(tps.Count + " Triple Patterns in the Query"); foreach (ITriplePattern tp in tps) { Console.WriteLine(tp.ToString()); } Console.WriteLine(); ISparqlAlgebra select = new Bgp(tps); ISparqlAlgebra selectOptimised = new LazyBgp(tps, 10); //Evaluate with timings Stopwatch timer = new Stopwatch(); TimeSpan unopt, opt; timer.Start(); BaseMultiset results1 = select.Evaluate(new SparqlEvaluationContext(null, new InMemoryDataset(store))); timer.Stop(); unopt = timer.Elapsed; timer.Reset(); timer.Start(); BaseMultiset results2 = selectOptimised.Evaluate(new SparqlEvaluationContext(null, new InMemoryDataset(store))); timer.Stop(); opt = timer.Elapsed; Console.WriteLine("SELECT = " + results1.GetType().ToString() + " (" + results1.Count + " Results) in " + unopt.ToString()); Console.WriteLine("SELECT Optimised = " + results2.GetType().ToString() + " (" + results2.Count + " Results) in " + opt.ToString()); Console.WriteLine(); Console.WriteLine("Optimised Results"); foreach (Set s in results2.Sets) { Console.WriteLine(s.ToString()); } Assert.IsTrue(results1.Count >= results2.Count, "Optimised Select should have produced as many/fewer results than Unoptimised Select"); Console.WriteLine(); } }
private IEnumerable<ulong[]> GetAllTriplesEnumeration(TriplePattern tp) { int[] variableIndexes = new int[3]; variableIndexes[0] = SortVariables.IndexOf(tp.Predicate.VariableName); variableIndexes[1] = SortVariables.IndexOf(tp.Subject.VariableName); variableIndexes[2] = SortVariables.IndexOf(tp.Object.VariableName); foreach(var entry in _store.MatchAllTriples(_graphUris)) { bool addRow = true; ulong[] newRow = new ulong[2]; for(int i = 0; i < 3; i++) { ulong currentValue = newRow[variableIndexes[i]]; if (currentValue > 0 && entry[i] != currentValue) { addRow = false; break; } newRow[variableIndexes[i]] = entry[i]; } if (addRow) yield return newRow; } }