public void TestBitwiseAnd()
        {
            var storeName = "TestBitwiseAnd_" + DateTime.Now.Ticks;

            _client.CreateStore(storeName);
            var job = _client.ExecuteTransaction(storeName, new UpdateTransactionData {
                InsertData = _testData
            });

            TestHelper.AssertJobCompletesSuccessfully(_client, storeName, job);

            var results = _client.ExecuteQuery(storeName,
                                               @"PREFIX bsfunc: <http://brightstardb.com/.well-known/sparql/functions/>
SELECT ?s WHERE { 
  ?s <http://example.org/p1> ?p1 ;
     <http://example.org/p2> ?p2 . 
  FILTER (bsfunc:bit_and(?p1, ?p2) = 1)
}");
            var resultsDoc = XDocument.Load(results);

            Assert.AreEqual(1, resultsDoc.SparqlResultRows().Count());
            var row = resultsDoc.SparqlResultRows().First();

            Assert.AreEqual("http://example.org/y", row.GetColumnValue("s").ToString());
        }
 public Stream GetResultsStream(SparqlResultsFormat format, RdfFormat graphFormat, DateTime?ifNotModifiedSince, out ISerializationFormat streamFormat)
 {
     if (_commitId > 0)
     {
         var commitPointInfo = _service.GetCommitPoint(_storeName, _commitId);
         if (commitPointInfo == null)
         {
             throw new InvalidCommitPointException();
         }
         return(_service.ExecuteQuery(commitPointInfo, _sparqlRequest.Query, _sparqlRequest.DefaultGraphUri, format, graphFormat, out streamFormat));
     }
     return(_service.ExecuteQuery(_storeName, _sparqlRequest.Query, _sparqlRequest.DefaultGraphUri, ifNotModifiedSince, format, graphFormat, out streamFormat));
 }
Example #3
0
 public static void AssertTriplePatternNotInDefaultGraph(IBrightstarService client, string storeName,
                                                       string triplePattern)
 {
     var sparql = "ASK {{" + triplePattern + "}}";
     var resultsDoc = XDocument.Load(client.ExecuteQuery(storeName, sparql));
     Assert.IsFalse(resultsDoc.SparqlBooleanResult());
 }
Example #4
0
 public static void AssertTriplePatternInGraph(IBrightstarService client, string storeName, string triplePattern,
                               string graphUri)
 {
     var sparql = "ASK { GRAPH <" + graphUri + "> {" + triplePattern + "}}";
     var resultsDoc = XDocument.Load(client.ExecuteQuery(storeName, sparql));
     Assert.IsTrue(resultsDoc.SparqlBooleanResult());
 }
        public SparqlResult ExecuteQuery(SparqlQueryContext queryContext, IList <string> datasetGraphUris)
        {
            ISerializationFormat resultFormat;
            var resultStream = _client.ExecuteQuery(_storeName, queryContext.SparqlQuery, datasetGraphUris, null, queryContext.SparqlResultsFormat,
                                                    queryContext.GraphResultsFormat, out resultFormat);

            return(new SparqlResult(resultStream, resultFormat, queryContext));
        }
        public static void AssertTriplePatternNotInDefaultGraph(IBrightstarService client, string storeName,
                                                                string triplePattern)
        {
            var sparql     = "ASK {{" + triplePattern + "}}";
            var resultsDoc = XDocument.Load(client.ExecuteQuery(storeName, sparql));

            Assert.IsFalse(resultsDoc.SparqlBooleanResult());
        }
        public static void AssertTriplePatternInGraph(IBrightstarService client, string storeName, string triplePattern,
                                                      string graphUri)
        {
            var sparql     = "ASK { GRAPH <" + graphUri + "> {" + triplePattern + "}}";
            var resultsDoc = XDocument.Load(client.ExecuteQuery(storeName, sparql));

            Assert.IsTrue(resultsDoc.SparqlBooleanResult());
        }
Example #8
0
        public void TestQuery()
        {
            XDocument responseDoc = XDocument.Load(_client.ExecuteQuery(_storeName, "SELECT ?s ?p ?o WHERE { ?s ?p ?o }"));

            Assert.IsNotNull(responseDoc);
            Assert.IsNotNull(responseDoc.Root);
            Assert.AreEqual("http://www.w3.org/2005/sparql-results#", responseDoc.Root.Name.Namespace);
            Assert.AreEqual("sparql", responseDoc.Root.Name.LocalName);
        }
        protected string ExecuteQuery(string queryPath, SparqlResultsFormat resultsFormat)
        {
            var queryExp      = File.ReadAllText(queryPath);
            var resultsStream = _service.ExecuteQuery(_storeName, queryExp, resultsFormat: resultsFormat);

            using (var streamReader = new StreamReader(resultsStream))
            {
                return(streamReader.ReadToEnd());
            }
        }
Example #10
0
        public void TestQuery()
        {
            var storeName = Guid.NewGuid().ToString();

            _client.CreateStore(storeName);
            XDocument responseDoc = XDocument.Load(_client.ExecuteQuery(storeName, "SELECT ?s ?p ?o WHERE { ?s ?p ?o }"));

            Assert.IsNotNull(responseDoc);
            Assert.IsNotNull(responseDoc.Root);
            Assert.AreEqual("http://www.w3.org/2005/sparql-results#", responseDoc.Root.Name.Namespace);
            Assert.AreEqual("sparql", responseDoc.Root.Name.LocalName);
        }
Example #11
0
        private static void QueryCommitPoint(IBrightstarService client, ICommitPointInfo commitPointInfo)
        {
            const string sparqlQuery =
                "SELECT ?l WHERE { ?s <http://www.w3.org/2000/01/rdf-schema#label> ?l } ORDER BY ?l";
            var resultsStream = client.ExecuteQuery(commitPointInfo, sparqlQuery);
            var resultsDoc    = XDocument.Load(resultsStream);
            var labels        = new List <string>();

            foreach (var row in resultsDoc.SparqlResultRows())
            {
                labels.Add(row.GetColumnValue("l").ToString());
            }
            Console.WriteLine("Query returns: {0}", string.Join(", ", labels));
        }
Example #12
0
 private void QueryData()
 {
     try
     {
         const string queryTemplate =
             "SELECT ?friend WHERE {{ {{<{0}> <http://purl.org/foaf/0.1/knows> ?friend }} UNION {{ ?friend <http://purl.org/foaf/0.1/knows> <{0}> }} }}";
         var rng   = new Random();
         var timer = new Stopwatch();
         do
         {
             try
             {
                 var randomPerson = String.Format("http://example.org/person/{0}", rng.Next(25000));
                 timer.Restart();
                 var query = String.Format(queryTemplate, randomPerson);
                 int rowCount;
                 using (var stream = _client.ExecuteQuery(_storeName, query))
                 {
                     var resultDoc = XDocument.Load(stream);
                     rowCount = resultDoc.SparqlResultRows().Count();
                 }
                 timer.Stop();
                 _queryTimes.Enqueue(new Tuple <long, int>(timer.ElapsedMilliseconds, rowCount));
             } catch (Exception ex)
             {
                 Console.ForegroundColor = ConsoleColor.Red;
                 Console.WriteLine("Query failed: " + ex.Message);
                 Console.ResetColor();
                 _queryTimes.Enqueue(new Tuple <long, int>(-1, 0));
             }
         } while (!_endQueries);
     }
     catch (Exception ex)
     {
         Console.Error.WriteLine(ex);
     }
 }
        public void TestInsert()
        {
            var storeName = CreateStore("TestInsert");

            ExecuteUpdate(storeName, @"PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA
{ 
  <http://example/book1> dc:title ""A new book"" ;
                         dc:creator ""A.N.Other"" .
}");

            var results = _client.ExecuteQuery(storeName,
                                               "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?o WHERE { <http://example/book1> dc:title ?o }");
            var resultsDoc = XDocument.Load(results);
            var resultRow  = resultsDoc.SparqlResultRows().FirstOrDefault();

            Assert.IsNotNull(resultRow);
            Assert.AreEqual("A new book", resultRow.GetColumnValue("o").ToString());

            ExecuteUpdate(storeName,
                          @"PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT { ?a a <http://example.org/book> }
WHERE { ?a dc:title ""A new book"" }");
            results    = _client.ExecuteQuery(storeName, "SELECT ?b WHERE {?b a <http://example.org/book>}");
            resultsDoc = XDocument.Load(results);
            resultRow  = resultsDoc.SparqlResultRows().FirstOrDefault();
            Assert.IsNotNull(resultRow);
            Assert.AreEqual(new Uri("http://example/book1"), resultRow.GetColumnValue("b"));
        }
Example #14
0
 public Stream ExecuteQuery(string queryExpression, IList <string> datasetGraphUris)
 {
     return(_client.ExecuteQuery(_storeName, queryExpression, datasetGraphUris));
 }
        public void ExecuteQueryForHtml(DateTime?ifNotModifiedSince)
        {
            Stream resultsStream;

            // No query => no results rather than triggering an error
            if (String.IsNullOrEmpty(_sparqlRequest.Query))
            {
                HasFormattedResults = false;
                RawResults          = String.Empty;
                return;
            }

            try
            {
                if (_commitId > 0)
                {
                    var commitPointInfo = _service.GetCommitPoint(_storeName, _commitId);
                    if (commitPointInfo == null)
                    {
                        throw new InvalidCommitPointException();
                    }
                    resultsStream = _service.ExecuteQuery(commitPointInfo, _sparqlRequest.Query,
                                                          _sparqlRequest.DefaultGraphUri, ResultsFormat);
                }
                else
                {
                    resultsStream = _service.ExecuteQuery(_storeName, _sparqlRequest.Query,
                                                          _sparqlRequest.DefaultGraphUri,
                                                          ifNotModifiedSince, ResultsFormat);
                }
            }
            catch (Exception ex)
            {
                ErrorMessage = FormatExceptionMessages(ex);
                return;
            }
            if (ResultsFormat == SparqlResultsFormat.Xml)
            {
                XDocument resultsDoc = XDocument.Load(resultsStream);
                HasFormattedResults = true;
                Variables           = resultsDoc.GetVariableNames().ToList();
                Rows = new List <object[]>();
                var varCount = Variables.Count;
                foreach (var resultRow in resultsDoc.SparqlResultRows())
                {
                    var row = new object[varCount];
                    for (int i = 0; i < varCount; i++)
                    {
                        row[i] = resultRow.GetColumnValue(Variables[i]);
                    }
                    Rows.Add(row);
                }
            }
            else
            {
                HasFormattedResults = false;
                using (var rdr = new StreamReader(resultsStream))
                {
                    RawResults = rdr.ReadToEnd();
                }
            }
        }
Example #16
0
 private static void QueryCommitPoint(IBrightstarService client, ICommitPointInfo commitPointInfo)
 {
     const string sparqlQuery =
         "SELECT ?l WHERE { ?s <http://www.w3.org/2000/01/rdf-schema#label> ?l } ORDER BY ?l";
     var resultsStream = client.ExecuteQuery(commitPointInfo, sparqlQuery);
     var resultsDoc = XDocument.Load(resultsStream);
     var labels = new List<string>();
     foreach(var row in resultsDoc.SparqlResultRows())
     {
         labels.Add(row.GetColumnValue("l").ToString());
     }
     Console.WriteLine("Query returns: {0}", string.Join(", ",labels));
 }