Exemplo n.º 1
0
        public void SingleStatementTwoVariables()
        {
            MemoryTripleStore statements = new MemoryTripleStore();

            statements.Add(new Statement(new UriRef("ex:s"), new UriRef("ex:p"), new UriRef("ex:o")));

            SimpleQueryBuilder builder = new SimpleQueryBuilder();

            builder.AddPattern(new Pattern(new Variable("s"), new UriRef("ex:p"), new Variable("o")));
            Query query = builder.GetQuery();

            QuerySqlMapper mapper = new QuerySqlMapper(query, statements);

            string expected =
                "SELECT rn_s.resourceHash rh_s, rn_s.nodeHash nh_s, rn_s.nodeType nt_s, u_s.uri val_s, NULL sub_s, rn_o.resourceHash rh_o, rn_o.nodeHash nh_o, rn_o.nodeType nt_o, COALESCE(u_o.uri, pl_o.value, l_o.value) val_o, COALESCE(tl_o.value, t_o.value) sub_o " +
                "FROM Statements s1 JOIN ResourceNodes rn_s ON rn_s.resourceHash=s1.subjectHash AND rn_s.graphId=s1.graphId " +
                "LEFT OUTER JOIN UriRefs u_s ON rn_s.nodeHash=u_s.hash AND rn_s.nodeType='u' " +
                "JOIN ResourceNodes rn_o ON rn_o.resourceHash=s1.objectHash AND rn_o.graphId=s1.graphId " +
                "LEFT OUTER JOIN UriRefs u_o ON rn_o.nodeHash=u_o.hash AND rn_o.nodeType='u' " +
                "LEFT OUTER JOIN PlainLiterals pl_o ON rn_o.nodeHash=pl_o.hash AND rn_o.nodeType='p' " +
                "LEFT OUTER JOIN Languages l_o ON pl_o.languageHash=l_o.hash " +
                "LEFT OUTER JOIN TypedLiterals tl_o ON rn_o.nodehash=tl_o.hash AND rn_o.nodeType='t' " +
                "LEFT OUTER JOIN DataTypes t_o ON tl_o.datatypeHash=t_o.hash " +
                "WHERE s1.predicateHash=" + statements.GetResourceDenotedBy(new UriRef("ex:p")).GetHashCode() +
                " AND s1.graphId=" + statements.GetHashCode();


            Assert.AreEqual(expected, mapper.Sql);
        }
Exemplo n.º 2
0
        public void ConstraintBound()
        {
            MemoryTripleStore statements = new MemoryTripleStore();

            statements.Add(new Statement(new UriRef("ex:s"), new UriRef("ex:p"), new UriRef("ex:o")));

            SimpleQueryBuilder builder = new SimpleQueryBuilder();

            builder.AddPattern(new Pattern(new UriRef("ex:s"), new UriRef("ex:p"), new Variable("v")));
            builder.AddConstraint(new Constraint(new Bound(new Variable("v"))));
            Query query = builder.GetQuery();

            QuerySqlMapper mapper = new QuerySqlMapper(query, statements);

            string expected =
                "SELECT rn_v.resourceHash rh_v, rn_v.nodeHash nh_v, rn_v.nodeType nt_v, COALESCE(u_v.uri, pl_v.value, l_v.value) val_v, COALESCE(tl_v.value, t_v.value) sub_v " +
                "FROM Statements s1 JOIN ResourceNodes rn_v ON rn_v.resourceHash=s1.objectHash AND rn_v.graphId=s1.graphId " +
                "LEFT OUTER JOIN UriRefs u_v ON rn_v.nodeHash=u_v.hash AND rn_v.nodeType='u' " +
                "LEFT OUTER JOIN PlainLiterals pl_v ON rn_v.nodeHash=pl_v.hash AND rn_v.nodeType='p' " +
                "LEFT OUTER JOIN Languages l_v ON pl_v.languageHash=l_v.hash " +
                "LEFT OUTER JOIN TypedLiterals tl_v ON rn_v.nodehash=tl_v.hash AND rn_v.nodeType='t' " +
                "LEFT OUTER JOIN DataTypes t_v ON tl_v.datatypeHash=t_v.hash " +
                "WHERE s1.subjectHash=" + statements.GetResourceDenotedBy(new UriRef("ex:s")).GetHashCode() +
                " AND s1.predicateHash=" + statements.GetResourceDenotedBy(new UriRef("ex:p")).GetHashCode() +
                " AND s1.graphId=" + statements.GetHashCode() +
                " AND s1.objectHash IS NOT NULL";


            Assert.AreEqual(expected, mapper.Sql);
        }
        //[Ignore("This is related to the enumerator.reset issue looked at in the two reification tests")]
        public void OrderOfPatternsIsNotSignificantWithMoreThanTwoPatterns()
        {
            MemoryTripleStore statements = new MemoryTripleStore();

            UriRef gran  = new UriRef("http://example.com/gran");
            UriRef mum   = new UriRef("http://example.com/mum");
            UriRef uncle = new UriRef("http://example.com/uncle");
            UriRef child = new UriRef("http://example.com/child");

            UriRef parentOf  = new UriRef("http://exmaple.com/parentOf");
            UriRef siblingOf = new UriRef("http://example.com/siblingOf");

            statements.Add(new Statement(gran, parentOf, mum));
            statements.Add(new Statement(mum, siblingOf, uncle));
            statements.Add(new Statement(mum, parentOf, child));

            Query query = new Query();

            query.AddPattern(new Pattern(new Variable("mum"), parentOf, child));
            query.AddPattern(new Pattern(new Variable("mum"), siblingOf, uncle));
            query.AddPattern(new Pattern(gran, parentOf, new Variable("mum")));

            IEnumerator solutions = statements.Solve(query);

            Assert.IsTrue(solutions.MoveNext(), "No solution found");

            query = new Query();
            query.AddPattern(new Pattern(gran, parentOf, new Variable("mum")));
            query.AddPattern(new Pattern(new Variable("mum"), siblingOf, uncle));
            query.AddPattern(new Pattern(new Variable("mum"), parentOf, child));

            solutions = statements.Solve(query);
            Assert.IsTrue(solutions.MoveNext(), "No solution found");
        }
        private IEnumerator SolveSolvableReificationQuery()
        {
            MemoryTripleStore store = SetUpReificationStatementsInStore();
            Query             query = ConstructReificationTestQuery();

            return(store.Solve(query));
        }
Exemplo n.º 5
0
        public void ExecuteXmlReturnsSparqlResultsFormatXml()
        {
            TripleStore store = new MemoryTripleStore();

            store.Add(new Statement(new UriRef("http://example.com/subj"), new UriRef("http://example.com/property"), new UriRef("http://example.com/obj")));

            SparqlQuery query = new SparqlQuery("SELECT ?var WHERE { <http://example.com/subj> <http://example.com/property> ?var }");

            XmlDocument resultsRaw       = query.ExecuteXml(store);
            XmlDocument resultsCanonical = new XmlDocument();

            resultsCanonical.LoadXml(resultsRaw.InnerXml);

            XmlDocument expected = new XmlDocument();

            expected.LoadXml(@"<sparql xmlns=""http://www.w3.org/2005/sparql-results#"">
  <head>
    <variable name=""var""/>
  </head>
  <results ordered=""false"" distinct=""false"">
    <result>
      <binding name=""var""><uri>http://example.com/obj</uri></binding>
    </result>
  </results>
</sparql>");



            Assert.AreEqual(expected.InnerXml, resultsCanonical.InnerXml, "Result of query should equal expected XML");
        }
Exemplo n.º 6
0
        public void SingleStatementPlusUnmatchableOptionalStatementWithVariable()
        {
            MemoryTripleStore statements = new MemoryTripleStore();

            statements.Add(new Statement(new UriRef("ex:s"), new UriRef("ex:p"), new UriRef("ex:o")));

            SimpleQueryBuilder builder = new SimpleQueryBuilder();

            builder.AddPattern(new Pattern(new Variable("v"), new UriRef("ex:p"), new UriRef("ex:o")));
            builder.AddOptional(new Pattern(new Variable("v"), new UriRef("ex:p2"), new Variable("o")));
            Query query = builder.GetQuery();

            QuerySqlMapper mapper = new QuerySqlMapper(query, statements);

            string expected =
                "SELECT rn_v.resourceHash rh_v, rn_v.nodeHash nh_v, rn_v.nodeType nt_v, u_v.uri val_v, NULL sub_v, NULL rh_o, NULL nh_o, NULL nt_o, NULL val_o, NULL sub_o " +
                "FROM Statements s1 JOIN ResourceNodes rn_v ON rn_v.resourceHash=s1.subjectHash AND rn_v.graphId=s1.graphId " +
                "LEFT OUTER JOIN UriRefs u_v ON rn_v.nodeHash=u_v.hash AND rn_v.nodeType='u' " +
                "WHERE s1.predicateHash=" + statements.GetResourceDenotedBy(new UriRef("ex:p")).GetHashCode() +
                " AND s1.objectHash=" + statements.GetResourceDenotedBy(new UriRef("ex:o")).GetHashCode() +
                " AND s1.graphId=" + statements.GetHashCode();


            Assert.AreEqual(expected, mapper.Sql);
        }
Exemplo n.º 7
0
        public void BuildIncludesUriBindings()
        {
            TripleStore store = new MemoryTripleStore();

            store.Add(new Statement(new UriRef("http://example.com/subj"), new UriRef("http://example.com/property"), new UriRef("http://example.com/obj")));

            SparqlQuery query = new SparqlQuery("SELECT ?var WHERE { <http://example.com/subj> <http://example.com/property> ?var }");

            AssertXPath("/s:sparql/s:results/s:result/s:binding[@name='var']/s:uri[ .= 'http://example.com/obj']", query, store);
        }
Exemplo n.º 8
0
        public void BuildReadsOrderBy()
        {
            TripleStore store = new MemoryTripleStore();

            store.Add(new Statement(new UriRef("http://example.com/subj"), new UriRef("http://example.com/property"), new UriRef("http://example.com/object")));

            SparqlQuery query = new SparqlQuery("SELECT ?var ?what WHERE { <http://example.com/subj> <http://example.com/property> ?var } ORDER BY ?var");

            AssertXPath("/s:sparql/s:results[@ordered='true']", query, store);
        }
Exemplo n.º 9
0
        public void BuildIncludesTypedLiteralBindings()
        {
            TripleStore store = new MemoryTripleStore();

            store.Add(new Statement(new UriRef("http://example.com/subj"), new UriRef("http://example.com/property"), new TypedLiteral("true", "http://www.w3.org/2001/XMLSchema#boolean")));

            SparqlQuery query = new SparqlQuery("SELECT ?var WHERE { <http://example.com/subj> <http://example.com/property> ?var }");

            AssertXPath("/s:sparql/s:results/s:result/s:binding[@name='var']/s:literal[@datatype='http://www.w3.org/2001/XMLSchema#boolean' and .= 'true']", query, store);
        }
Exemplo n.º 10
0
        public void BuildIncludesUnboundVariables()
        {
            TripleStore store = new MemoryTripleStore();

            store.Add(new Statement(new UriRef("http://example.com/subj"), new UriRef("http://example.com/property"), new UriRef("http://example.com/object")));

            SparqlQuery query = new SparqlQuery("SELECT ?var ?what WHERE { <http://example.com/subj> <http://example.com/property> ?var OPTIONAL { ?var <http://example.com/other> ?what } }");

            AssertXPath("/s:sparql/s:results/s:result/s:binding[@name='what']/s:unbound", query, store);
        }
Exemplo n.º 11
0
        public void BuildIncludesLanguageLiteralBindings()
        {
            TripleStore store = new MemoryTripleStore();

            store.Add(new Statement(new UriRef("http://example.com/subj"), new UriRef("http://example.com/property"), new PlainLiteral("scooby", "fr")));

            SparqlQuery query = new SparqlQuery("SELECT ?var WHERE { <http://example.com/subj> <http://example.com/property> ?var }");

            AssertXPath("/s:sparql/s:results/s:result/s:binding[@name='var']/s:literal[@xml:lang='fr' and .= 'scooby']", query, store);
        }
Exemplo n.º 12
0
        public void BuildReadsDistinct()
        {
            TripleStore store = new MemoryTripleStore();

            store.Add(new Statement(new UriRef("http://example.com/subj"), new UriRef("http://example.com/property"), new UriRef("http://example.com/object")));

            SparqlQuery query = new SparqlQuery("SELECT ?var ?what WHERE { <http://example.com/subj> <http://example.com/property> ?var OPTIONAL { ?var <http://example.com/other> ?what } }");

            query.IsDistinct = true;
            AssertXPath("/s:sparql/s:results[@distinct='true']", query, store);
        }
Exemplo n.º 13
0
        public void BuildIncludesBlankNodeBindings()
        {
            TripleStore store = new MemoryTripleStore();
            BlankNode   node  = new BlankNode();

            store.Add(new Statement(new UriRef("http://example.com/subj"), new UriRef("http://example.com/property"), node));

            SparqlQuery query = new SparqlQuery("SELECT ?var WHERE { <http://example.com/subj> <http://example.com/property> ?var }");

            AssertXPath("/s:sparql/s:results/s:result/s:binding[@name='var']/s:bnode[ .= '" + node.GetLabel() + "']", query, store);
        }
Exemplo n.º 14
0
        public void emptyRuleMakesNoChangesToSourceOrDestinationTripleStores()
        {
            TripleStore store = new MemoryTripleStore();

            SimpleRuleProcessor ruleProcessor = new SimpleRuleProcessor();

            Rule rule = new Rule();

            ruleProcessor.Process(rule, store);

            Assert.IsTrue(store.IsEmpty());
        }
Exemplo n.º 15
0
        public void NoPatterns()
        {
            MemoryTripleStore statements = new MemoryTripleStore();

            statements.Add(new Statement(new UriRef("ex:s"), new UriRef("ex:p"), new UriRef("ex:o")));

            SimpleQueryBuilder builder = new SimpleQueryBuilder();
            Query query = builder.GetQuery();

            QuerySqlMapper mapper = new QuerySqlMapper(query, statements);

            Assert.AreEqual(false, mapper.IsFeasible);
        }
Exemplo n.º 16
0
        public void nonMatchingAntecedentsAddsNoConsequents()
        {
            TripleStore store = new MemoryTripleStore();

            SimpleRuleProcessor ruleProcessor = new SimpleRuleProcessor();

            Rule rule = new Rule();

            rule.AddAntecedent(new Pattern(new UriRef("http://example.com/subj"), new UriRef("http://example.com/pred"), new UriRef("http://example.com/obj")));
            rule.AddConsequent(new Pattern(new UriRef("http://example.com/subj"), new UriRef("http://example.com/pred"), new UriRef("http://example.com/other")));

            ruleProcessor.Process(rule, store);
            Assert.IsTrue(store.IsEmpty(), "Store is still empty");
        }
Exemplo n.º 17
0
        public void noAntecedentsAutomaticallyAddsConsequents()
        {
            TripleStore store = new MemoryTripleStore();

            SimpleRuleProcessor ruleProcessor = new SimpleRuleProcessor();

            Rule rule = new Rule();

            rule.AddConsequent(new Pattern(new UriRef("http://example.com/subj"), new UriRef("http://example.com/pred"), new UriRef("http://example.com/obj")));

            ruleProcessor.Process(rule, store);

            Assert.IsFalse(store.IsEmpty(), "Store is non-empty");

            Assert.IsTrue(store.Contains(new Statement(new UriRef("http://example.com/subj"), new UriRef("http://example.com/pred"), new UriRef("http://example.com/obj"))), "Destination contains consequent");
        }
Exemplo n.º 18
0
        public void ExecuteTripleStoreWithPatternsQueryReturnsDescriptions()
        {
            SparqlQuery query = new SparqlQuery("DESCRIBE ?var WHERE {<http://example.com/subject> <http://example.com/pred> ?var }");

            TripleStore store = new MemoryTripleStore();

            store.Add(new Statement(new UriRef("http://example.com/subject"), new UriRef("http://example.com/pred"), new UriRef("http://example.com/obj")));
            store.Add(new Statement(new UriRef("http://example.com/obj"), new UriRef("http://example.com/pred"), new UriRef("http://example.com/thing")));

            TripleStoreVerifier verifier = new TripleStoreVerifier();

            verifier.expect("<http://example.com/obj> <http://example.com/pred> <http://example.com/thing> .");

            TripleStore results = query.ExecuteTripleStore(store);

            Assert.IsTrue(verifier.verify(results), "Resulting triple store should contain describing triples");
        }
Exemplo n.º 19
0
        public void consequentsAreAddedUsingAntecedentSolutionBindings()
        {
            TripleStore store = new MemoryTripleStore();

            SimpleRuleProcessor ruleProcessor = new SimpleRuleProcessor();

            store.Add(new Statement(new UriRef("http://example.com/subj"), new UriRef("http://example.com/pred"), new UriRef("http://example.com/obj")));

            Rule rule = new Rule();

            rule.AddAntecedent(new Pattern(new Variable("var1"), new UriRef("http://example.com/pred"), new Variable("var2")));
            rule.AddConsequent(new Pattern(new Variable("var1"), new UriRef("http://example.com/newPred"), new Variable("var2")));

            ruleProcessor.Process(rule, store);

            Assert.IsFalse(store.IsEmpty(), "Destination is non-empty");

            Assert.IsTrue(store.Contains(new Statement(new UriRef("http://example.com/subj"), new UriRef("http://example.com/newPred"), new UriRef("http://example.com/obj"))), "Destination contains consequent");
        }
        private MemoryTripleStore SetUpReificationStatementsInStore()
        {
            // bnode is the statement made by myThing
            BlankNode bnode = new BlankNode();

            MemoryTripleStore store = new MemoryTripleStore();

            store.Add(new Statement(myThing, makesStatement, bnode));
            store.Add(new Statement(bnode, rdfType, rdfStatement));
            store.Add(new Statement(bnode, rdfSubject, mySubject));
            store.Add(new Statement(bnode, rdfPredicate, myPredicate));
            store.Add(new Statement(bnode, rdfObject, myObject));

            //store.Dump();
            //Console.WriteLine("\n\n");
            //store.Write(new RdfXmlWriter(new System.Xml.XmlTextWriter(Console.Out)));
            //Console.WriteLine("\n\n");

            return(store);
        }
Exemplo n.º 21
0
        public void allAntecedentMatchesAreProcessed()
        {
            TripleStore store = new MemoryTripleStore();

            SimpleRuleProcessor ruleProcessor = new SimpleRuleProcessor();

            store.Add(new Statement(new UriRef("http://example.com/subj1"), new UriRef("http://example.com/pred"), new UriRef("http://example.com/obj1")));
            store.Add(new Statement(new UriRef("http://example.com/subj2"), new UriRef("http://example.com/pred"), new UriRef("http://example.com/obj2")));

            Rule rule = new Rule();

            rule.AddAntecedent(new Pattern(new Variable("var1"), new UriRef("http://example.com/pred"), new Variable("var2")));
            rule.AddConsequent(new Pattern(new Variable("var1"), new UriRef("http://example.com/newPred"), new Variable("var2")));

            ruleProcessor.Process(rule, store);

            Assert.IsFalse(store.IsEmpty(), "Destination is non-empty");

            Assert.IsTrue(store.Contains(new Statement(new UriRef("http://example.com/subj1"), new UriRef("http://example.com/newPred"), new UriRef("http://example.com/obj1"))), "Destination contains first match onsequent");
            Assert.IsTrue(store.Contains(new Statement(new UriRef("http://example.com/subj2"), new UriRef("http://example.com/newPred"), new UriRef("http://example.com/obj2"))), "Destination contains second match consequent");
        }
Exemplo n.º 22
0
        public void SingleStatementPlusMultipleOptionalStatementsInOneGroup()
        {
            MemoryTripleStore statements = new MemoryTripleStore();

            statements.Add(new Statement(new UriRef("ex:s"), new UriRef("ex:p"), new UriRef("ex:o")));
            statements.Add(new Statement(new UriRef("ex:s"), new UriRef("ex:p2"), new UriRef("ex:o2")));

            SimpleQueryBuilder builder = new SimpleQueryBuilder();

            builder.AddPattern(new Pattern(new Variable("v"), new UriRef("ex:p"), new UriRef("ex:o")));
            builder.AddOptional(new Pattern(new Variable("v"), new Variable("p"), new UriRef("ex:o2")));
            builder.AddOptional(new Pattern(new Variable("v"), new Variable("p"), new UriRef("ex:o")));
            Query query = builder.GetQuery();

            QuerySqlMapper mapper = new QuerySqlMapper(query, statements);

            string expected =
                "SELECT rn_v.resourceHash rh_v, rn_v.nodeHash nh_v, rn_v.nodeType nt_v, u_v.uri val_v, NULL sub_v, rn_p.resourceHash rh_p, rn_p.nodeHash nh_p, rn_p.nodeType nt_p, u_p.uri val_p, NULL sub_p " +
                "FROM Statements s1 " +
                "LEFT OUTER JOIN Statements s2" +
                " ON s2.subjectHash=s1.subjectHash" +
                " AND s2.objectHash=" + statements.GetResourceDenotedBy(new UriRef("ex:o2")).GetHashCode() +
                " AND s2.graphId=" + statements.GetHashCode() +
                " JOIN Statements s3" +
                " ON s3.subjectHash=s2.subjectHash" +
                " AND s3.predicateHash=s2.predicateHash" +
                " AND s3.objectHash=" + statements.GetResourceDenotedBy(new UriRef("ex:o")).GetHashCode() +
                " AND s3.graphId=" + statements.GetHashCode() +
                " JOIN ResourceNodes rn_v ON rn_v.resourceHash=s1.subjectHash AND rn_v.graphId=s1.graphId " +
                "LEFT OUTER JOIN UriRefs u_v ON rn_v.nodeHash=u_v.hash AND rn_v.nodeType='u' " +
                "JOIN ResourceNodes rn_p ON rn_p.resourceHash=s2.predicateHash AND rn_p.graphId=s2.graphId " +
                "LEFT OUTER JOIN UriRefs u_p ON rn_p.nodeHash=u_p.hash AND rn_p.nodeType='u' " +
                "WHERE s1.predicateHash=" + statements.GetResourceDenotedBy(new UriRef("ex:p")).GetHashCode() +
                " AND s1.objectHash=" + statements.GetResourceDenotedBy(new UriRef("ex:o")).GetHashCode() +
                " AND s1.graphId=" + statements.GetHashCode();


            Assert.AreEqual(expected, mapper.Sql);
        }
Exemplo n.º 23
0
        public TripleStore ExecuteTripleStore(TripleStore defaultStore)
        {
            MemoryTripleStore results = new MemoryTripleStore();

            if (HasPatterns)
            {
                IEnumerator solutions = defaultStore.Solve(this);
                while (solutions.MoveNext())
                {
                    QuerySolution solution = (QuerySolution)solutions.Current;
                    foreach (PatternTerm term in DescribeTerms)
                    {
                        if (term is Variable)
                        {
                            ResourceDescription description = defaultStore.GetDescriptionOf(solution[((Variable)term).Name], this.BoundingStrategy);
                            results.Add(description);
                        }
                    }
                }
            }
            foreach (PatternTerm term in DescribeTerms)
            {
                if (term is UriRef)
                {
                    ResourceDescription description = defaultStore.GetDescriptionOf((UriRef)term, this.BoundingStrategy);
                    results.Add(description);
                }
                else
                {
                    if (!HasPatterns)
                    {
                        throw new SparqlException("DESCRIBE query has no patterns but requests description of a variable");
                    }
                }
            }

            return(results);
        }
Exemplo n.º 24
0
        private void RunTestCase(string queryFile, string dataFile, string resultsFile, bool explain)
        {
            if (explain)
            {
                Console.WriteLine("-------------- TEST ---------------------");
                Console.WriteLine("queryFile=" + queryFile + ", dataFile=" + dataFile + ", resultsFile=" + resultsFile);
            }
            try {
                StreamReader queryReader = File.OpenText("d:\\data\\semplan\\2005\\spiral\\src\\SemPlan.Spiral.Tests.Sparql\\dawg-test-cases\\data-xml\\" + queryFile);
                string       query       = queryReader.ReadToEnd();
                if (explain)
                {
                    Console.WriteLine(">---- Contents of " + queryFile);
                    Console.WriteLine("query=" + query);
                    Console.WriteLine(">----");
                }
                StreamReader dataReader = File.OpenText("d:\\data\\semplan\\2005\\spiral\\src\\SemPlan.Spiral.Tests.Sparql\\dawg-test-cases\\data-xml\\" + dataFile);

                StreamReader resultsReader = File.OpenText("d:\\data\\semplan\\2005\\spiral\\src\\SemPlan.Spiral.Tests.Sparql\\dawg-test-cases\\data-xml\\" + resultsFile);

                Query queryToRun = ParseQuery(query);

                if (explain)
                {
                    Console.WriteLine(">---- Parsed query");
                    Console.WriteLine(queryToRun);
                    Console.WriteLine(">----");
                }

                MemoryTripleStore dataStore = new MemoryTripleStore();

                XsltParserFactory parserFactory = new XsltParserFactory();
                Parser            dataParser    = parserFactory.MakeParser(new ResourceFactory(), new StatementFactory());
                dataParser.NewStatement += new StatementHandler(dataStore.Add);

                try {
                    dataParser.Parse(dataReader, "");
                }
                catch (Exception) { }


                if (explain)
                {
                    Console.WriteLine(">---- Contents of " + dataFile);
                    dataStore.Dump();
                    Console.WriteLine(">----");
                }

                MemoryTripleStore resultsStore = new MemoryTripleStore();

                Parser resultsParser = parserFactory.MakeParser(new ResourceFactory(), new StatementFactory());
                resultsParser.NewStatement += new StatementHandler(resultsStore.Add);

                try {
                    resultsParser.Parse(resultsReader, "");
                }
                catch (Exception) { }

                if (explain)
                {
                    Console.WriteLine(">---- Contents of " + resultsFile);
                    Console.WriteLine(resultsStore.ToString());
                    Console.WriteLine(">----");
                }
                //~ Query resultsQuery = ParseQuery( @"
                //~ PREFIX rs: <http://www.w3.org/2001/sw/DataAccess/tests/result-set#>
                //~ SELECT * {
                //~ ?solution rs:binding ?binding .
                //~ ?binding rs:variable ?variable .
                //~ ?binding rs:value ?value .
                //~ }
                //~ ");
                //~ ?set a rs:ResultSet .
                //~ ?set rs:solution ?solution .
                //~ ?solution rs:binding ?binding .
                //~ ?binding rs:variable ?variable .
                //~ ?binding rs:value ?value .

                Query resultsQuery = new Query();
                resultsQuery.AddPattern(new Pattern(new Variable("solution"), new UriRef("http://www.w3.org/2001/sw/DataAccess/tests/result-set#binding"), new Variable("binding")));
                resultsQuery.AddPattern(new Pattern(new Variable("binding"), new UriRef("http://www.w3.org/2001/sw/DataAccess/tests/result-set#variable"), new Variable("variable")));
                resultsQuery.AddPattern(new Pattern(new Variable("binding"), new UriRef("http://www.w3.org/2001/sw/DataAccess/tests/result-set#value"), new Variable("value")));


                Hashtable     expectedSolutions       = new Hashtable();
                Resource      currentSolutionResource = null;
                QuerySolution newExpectedSolution     = null;

                if (explain)
                {
                    Console.WriteLine(">---- Parsing expected results");
                }
                IEnumerator solutions = resultsStore.Solve(resultsQuery);
                while (solutions.MoveNext())
                {
                    QuerySolution solution = (QuerySolution)solutions.Current;
                    if (explain)
                    {
                        Console.WriteLine("x" + solution);
                    }
                    if (!expectedSolutions.Contains(solution["solution"]))
                    {
                        expectedSolutions[solution["solution"]] = new QuerySolution();
                    }
                    if (explain)
                    {
                        Console.WriteLine("Variable=" + resultsStore.GetBestDenotingNode(solution["variable"]) + "; value=" + resultsStore.GetBestDenotingNode(solution["value"]) + "(" + dataStore.GetResourceDenotedBy(resultsStore.GetBestDenotingNode(solution["value"])) + ")");
                    }

                    ((QuerySolution)expectedSolutions[solution["solution"]])[resultsStore.GetBestDenotingNode(solution["variable"]).GetLabel()] = dataStore.GetResourceDenotedBy(resultsStore.GetBestDenotingNode(solution["value"]));
                }

                if (explain)
                {
                    Console.WriteLine(">----");
                }

                if (explain)
                {
                    Console.WriteLine(">---- Expecting " + expectedSolutions.Keys.Count + " solutions");
                    foreach (QuerySolution expected in expectedSolutions.Values)
                    {
                        //~ Console.WriteLine( expected.ToString(dataStore) );
                    }
                    Console.WriteLine(">----");
                }

                int expectedSolutionCount = expectedSolutions.Keys.Count;
                int actualSolutionCount   = 0;

                IEnumerator actualSolutions           = dataStore.Solve(queryToRun);
                ArrayList   expectedSolutionsReceived = new ArrayList();

                if (explain)
                {
                    Console.WriteLine(">---- Got solutions:");
                }

                while (actualSolutions.MoveNext())
                {
                    QuerySolution actualSolution = (QuerySolution)actualSolutions.Current;
                    ++actualSolutionCount;
                    //~ if (explain) Console.WriteLine(actualSolution.ToString(dataStore) );

                    bool foundMatch = false;
                    foreach (QuerySolution expectedSolution in expectedSolutions.Values)
                    {
                        if (expectedSolution.Equals(actualSolution))
                        {
                            expectedSolutionsReceived.Add(expectedSolution);
                            foundMatch = true;
                            break;
                        }
                    }

                    if (!foundMatch)
                    {
                        //~ Assert.Fail( "Got unexpected solution:" + actualSolution.ToString( dataStore ) );
                    }
                }


                Assert.AreEqual(expectedSolutionCount, actualSolutionCount, "Got same number of solutions");
                Assert.AreEqual(expectedSolutionsReceived.Count, expectedSolutionCount, "All expected solutions were received");
            }
            catch (Exception e) {
                Assert.Fail(e.ToString());
            }
        }