예제 #1
0
        public void SparqlUpdateInsertWithGraphClause1()
        {
            Graph g = new Graph();

            g.Assert(g.CreateUriNode(UriFactory.Create("http://subject")), g.CreateUriNode(UriFactory.Create("http://predicate")), g.CreateUriNode(UriFactory.Create("http://object")));

            InMemoryDataset dataset = new InMemoryDataset(g);

            String updates = "INSERT { GRAPH ?s { ?s ?p ?o } } WHERE { ?s ?p ?o }";
            SparqlUpdateCommandSet commands = new SparqlUpdateParser().ParseFromString(updates);

            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);

            processor.ProcessCommandSet(commands);

            Assert.Equal(2, dataset.GraphUris.Count());
            Assert.True(dataset.HasGraph(UriFactory.Create("http://subject")));
        }
예제 #2
0
        public void SparqlUpdateWithCustomQueryProcessor()
        {
            Graph g = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            g.BaseUri = null;
            TripleStore store = new TripleStore();

            store.Add(g);
            InMemoryDataset dataset = new InMemoryDataset(store);

            SparqlUpdateParser     parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds   = parser.ParseFromString("DELETE { ?s a ?type } WHERE { ?s a ?type }");

            ISparqlUpdateProcessor processor = new ExplainUpdateProcessor(dataset, ExplanationLevel.Full);

            processor.ProcessCommandSet(cmds);
        }
        public void SparqlPropertyPathEvaluationZeroOrMorePath()
        {
            TripleStore store = new TripleStore();
            Graph       g     = new Graph();

            g.LoadFromFile("resources\\InferenceTest.ttl");
            store.Add(g);
            InMemoryDataset dataset = new InMemoryDataset(store, g.BaseUri);

            ZeroOrMore path =
                new ZeroOrMore(new Property(this._factory.CreateUriNode(new Uri(NamespaceMapper.RDFS + "subClassOf"))));
            ISparqlAlgebra algebra = this.GetAlgebra(path);
            BaseMultiset   results = algebra.Evaluate(new SparqlEvaluationContext(null, dataset));

            TestTools.ShowMultiset(results);

            Assert.False(results.IsEmpty, "Results should not be empty");
        }
예제 #4
0
        public void SparqlUpdateDeleteDataCombination()
        {
            SparqlParameterizedString command = new SparqlParameterizedString();

            command.Namespaces.AddNamespace("ex", new Uri("http://example.org/"));
            command.CommandText = "DELETE DATA { ex:a ex:b ex:c GRAPH <http://example.org/graph> { ex:a ex:b ex:c } }";

            SparqlUpdateParser     parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds   = parser.ParseFromString(command);

            Assert.IsFalse(cmds.Commands.All(cmd => cmd.AffectsSingleGraph), "Commands should report that they do not affect a single Graph");
            Assert.IsTrue(cmds.Commands.All(cmd => cmd.AffectsGraph(null)), "Commands should report that they affect the Default Graph");
            Assert.IsTrue(cmds.Commands.All(cmd => cmd.AffectsGraph(new Uri("http://example.org/graph"))), "Commands should report that they affect the named Graph");

            InMemoryDataset dataset = new InMemoryDataset();
            IGraph          def     = new Graph();

            def.NamespaceMap.Import(command.Namespaces);
            def.Assert(new Triple(def.CreateUriNode("ex:a"), def.CreateUriNode("ex:b"), def.CreateUriNode("ex:c")));
            dataset.AddGraph(def);
            IGraph ex = new Graph();

            ex.BaseUri = new Uri("http://example.org/graph");
            ex.NamespaceMap.Import(command.Namespaces);
            ex.Assert(new Triple(ex.CreateUriNode("ex:a"), ex.CreateUriNode("ex:b"), ex.CreateUriNode("ex:c")));
            dataset.AddGraph(ex);

            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);

            processor.ProcessCommandSet(cmds);

            Graph g = new Graph();

            g.NamespaceMap.Import(command.Namespaces);
            Triple t = new Triple(g.CreateUriNode("ex:a"), g.CreateUriNode("ex:b"), g.CreateUriNode("ex:c"));

            def = dataset[null];
            Assert.AreEqual(0, def.Triples.Count, "Should be 0 Triples in the Default Graph");
            Assert.IsFalse(def.ContainsTriple(t), "Should not have the deleted Triple in the Default Graph");

            ex = dataset[new Uri("http://example.org/graph")];
            Assert.AreEqual(0, ex.Triples.Count, "Should be 0 Triples in the Named Graph");
            Assert.IsFalse(ex.ContainsTriple(t), "Should not have the deleted Triple in the Named Graph");
        }
예제 #5
0
        public void SparqlConstructEmptyWhereCore407()
        {
            const String queryStr = @"CONSTRUCT
{
<http://s> <http://p> <http://o> .
}
WHERE
{}";

            SparqlQuery             q         = new SparqlQueryParser().ParseFromString(queryStr);
            InMemoryDataset         dataset   = new InMemoryDataset();
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);

            IGraph g = processor.ProcessQuery(q) as IGraph;

            Assert.NotNull(g);
            Assert.False(g.IsEmpty, "Graph should not be empty");
            Assert.Equal(1, g.Triples.Count);
        }
예제 #6
0
        public void SparqlPropertyPathEvaluationCore395ExactQuery()
        {
            IGraph g = new Graph();

            g.LoadFromFile(@"resources/pp.rdf");

            InMemoryDataset         dataset   = new InMemoryDataset(g);
            SparqlQuery             query     = new SparqlQueryParser().ParseFromFile(@"resources/pp.rq");
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);
            Object results = processor.ProcessQuery(query);

            Assert.NotNull(results);
            TestTools.ShowResults(results);

            Assert.IsType <SparqlResultSet>(results);
            SparqlResultSet rset = (SparqlResultSet)results;

            Assert.Equal(3, rset.Count);
        }
예제 #7
0
        public void TestAsyncQueryWithQueryCallback()
        {
            var store = new TripleStore();
            var g     = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            store.Add(g);
            var dataset = new InMemoryDataset(store, g.BaseUri);

            var processor = new LeviathanQueryProcessor(dataset);

            var wait   = new AutoResetEvent(false);
            var parser = new SparqlQueryParser();
            var query  = parser.ParseFromString(
                "SELECT * WHERE { ?instance a ?class }");
            var callbackInvoked = false;
            var syncResultSet   = processor.ProcessQuery(query) as SparqlResultSet;

            Assert.NotNull(syncResultSet);
            var syncResultCount = syncResultSet.Count;
            var resultSet       = new SparqlResultSet();
            var resultHandler   = new ResultSetHandler(resultSet);

            processor.ProcessQuery(null, resultHandler, query, (rdfHandler, rsetHandler, state) =>
            {
                try
                {
                    Assert.IsNotType <AsyncError>(state);
                    Assert.Equal("some state", state);
                }
                finally
                {
                    callbackInvoked = true;
                    wait.Set();
                }
            }, "some state");
            wait.WaitOne();
            var resultCount = resultSet.Count;

            Assert.True(callbackInvoked);
            Assert.True(resultCount > 0);
            Assert.Equal(syncResultCount, resultCount);
        }
예제 #8
0
        public void SparqlUpdateChangesNotReflectedInOriginalGraph2()
        {
            //Test Case originally submitted by Tomasz Pluskiewicz

            // given
            IGraph sourceGraph = new Graph();

            sourceGraph.LoadFromString(@"@prefix ex: <http://www.example.com/> .
ex:Subject ex:hasObject ex:Object .");

            IGraph expectedGraph = new Graph();

            expectedGraph.LoadFromString(@"@prefix ex: <http://www.example.com/> .
ex:Subject ex:hasBlank [ ex:hasObject ex:Object ] .");

            TripleStore store = new TripleStore();

            store.Add(sourceGraph);
            ISparqlDataset dataset = new InMemoryDataset(store, sourceGraph.BaseUri);

            // when
            var command = new SparqlParameterizedString
            {
                CommandText = @"PREFIX ex: <http://www.example.com/>
DELETE { ex:Subject ex:hasObject ex:Object . }
INSERT { ex:Subject ex:hasBlank [ ex:hasObject ex:Object ] . }
WHERE { ?s ?p ?o . }"
            };
            SparqlUpdateCommandSet   cmds      = new SparqlUpdateParser().ParseFromString(command);
            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);

            processor.ProcessCommandSet(cmds);

            Assert.Single(dataset.Graphs);

            IGraph g = dataset.Graphs.First();

            Assert.Equal(2, g.Triples.Count);
            Assert.True(ReferenceEquals(g, sourceGraph), "Result Graph should be the Source Graph");
            Assert.Equal(2, sourceGraph.Triples.Count);
            Assert.Equal(expectedGraph, sourceGraph);
        }
예제 #9
0
        private LoadedGraph LoadGraph()
        {
            var sw = Stopwatch.StartNew();

            try
            {
                // Long running task.
                var g = new Graph();
                FileLoader.Load(g, options.DumpFilePath);
                var ds = new InMemoryDataset(g);
                logger.LogInformation("Initialized graph with {Tuples} tuples. Elapsed time: {Elapsed}.",
                                      g.Triples.Count, sw.Elapsed);
                return(new LoadedGraph(new LeviathanQueryProcessor(ds), g, ds));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Error during initialization. Elapsed time: {Elapsed}.", sw.Elapsed);
                throw;
            }
        }
예제 #10
0
        private void SparqlQueryThreadSafeEvaluationActual()
        {
            String query1 = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/1> { ?s ?p ?o } }";
            String query2 = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/2> { ?s ?p ?o } }";

            SparqlQuery q1 = this._parser.ParseFromString(query1);
            SparqlQuery q2 = this._parser.ParseFromString(query2);

            Assert.False(q1.UsesDefaultDataset, "Query 1 should not be thread safe");
            Assert.False(q2.UsesDefaultDataset, "Query 2 should not be thread safe");

            InMemoryDataset dataset = new InMemoryDataset();
            Graph           g       = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            g.BaseUri = new Uri("http://example.org/1");
            Graph h = new Graph();

            h.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.LeviathanFunctionLibrary.ttl");
            h.BaseUri = new Uri("http://example.org/2");

            dataset.AddGraph(g);
            dataset.AddGraph(h);
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);

            QueryWithGraphDelegate d  = new QueryWithGraphDelegate(this.QueryWithGraph);
            IAsyncResult           r1 = d.BeginInvoke(q1, processor, null, null);
            IAsyncResult           r2 = d.BeginInvoke(q2, processor, null, null);

            WaitHandle.WaitAll(new WaitHandle[] { r1.AsyncWaitHandle, r2.AsyncWaitHandle });

            IGraph gQuery = d.EndInvoke(r1);

            Assert.Equal(g, gQuery);

            IGraph hQuery = d.EndInvoke(r2);

            Assert.Equal(h, hQuery);

            Assert.NotEqual(g, h);
        }
        public void SparqlDatasetDefaultGraphManagementWithUpdate()
        {
            TripleStore store = new TripleStore();
            Graph       g     = new Graph();

            store.Add(g);
            Graph h = new Graph();

            h.BaseUri = new Uri("http://example.org/someOtherGraph");
            store.Add(h);

            InMemoryDataset          dataset   = new InMemoryDataset(store, h.BaseUri);
            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);
            SparqlUpdateParser       parser    = new SparqlUpdateParser();
            SparqlUpdateCommandSet   cmds      = parser.ParseFromString("LOAD <http://www.dotnetrdf.org/configuration#>");

            processor.ProcessCommandSet(cmds);

            Assert.True(g.IsEmpty, "Graph with null URI (normally the default Graph) should be empty as the Default Graph for the Dataset should have been a named Graph so this Graph should not have been filled by the LOAD Command");
            Assert.False(h.IsEmpty, "Graph with name should be non-empty as it should have been the Default Graph for the Dataset and so filled by the LOAD Command");
        }
예제 #12
0
        public void SparqlPropertyPathEvaluationCore441NoPath()
        {
            IGraph g = new Graph();

            g.LoadFromFile(@"resources\core-441\data.ttl");

            InMemoryDataset dataset = new InMemoryDataset(g);
            SparqlQuery     query   = new SparqlQueryParser().ParseFromFile(@"resources\core-441\no-path.rq");

            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);
            Object results = processor.ProcessQuery(query);

            Assert.NotNull(results);
            TestTools.ShowResults(results);

            Assert.IsType <SparqlResultSet>(results);
            SparqlResultSet rset = (SparqlResultSet)results;

            Assert.Equal(1, rset.Count);
            Assert.Equal(g.CreateUriNode("Frame:Sheep"), rset[0]["prey"]);
        }
        public void SparqlDatasetDefaultGraphManagementWithUpdate2()
        {
            TripleStore store = new TripleStore();
            Graph       g     = new Graph();

            g.BaseUri = new Uri("http://example.org/graph");
            store.Add(g);
            Graph h = new Graph();

            h.BaseUri = new Uri("http://example.org/someOtherGraph");
            store.Add(h);

            InMemoryDataset          dataset   = new InMemoryDataset(store, h.BaseUri);
            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);
            SparqlUpdateParser       parser    = new SparqlUpdateParser();
            SparqlUpdateCommandSet   cmds      = parser.ParseFromString("LOAD <http://www.dotnetrdf.org/configuration#> INTO GRAPH <http://example.org/graph>");

            processor.ProcessCommandSet(cmds);

            Assert.False(g.IsEmpty, "First Graph should not be empty as should have been filled by the LOAD command");
            Assert.True(h.IsEmpty, "Second Graph should be empty as should not have been filled by the LOAD command");
        }
예제 #14
0
        public void SparqlPropertyPathEvaluationCore441OneOrMorePath()
        {
            IGraph g = new Graph();

            g.LoadFromFile(@"resources\core-441\data.ttl");

            InMemoryDataset dataset = new InMemoryDataset(g);
            SparqlQuery     query   = new SparqlQueryParser().ParseFromFile(@"resources\core-441\plus-path.rq");

            Console.WriteLine(query.ToAlgebra().ToString());

            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);
            Object results = processor.ProcessQuery(query);

            Assert.NotNull(results);
            TestTools.ShowResults(results);

            Assert.IsType <SparqlResultSet>(results);
            SparqlResultSet rset = (SparqlResultSet)results;

            Assert.Equal(0, rset.Count);
        }
예제 #15
0
        public static List <SparqlResult> RunQuery(string url, string queryString)
        {
            TripleStore store = new TripleStore();

            //Assume that we fill our Store with data from somewhere

            //Create a dataset for our queries to operate over
            //We need to explicitly state our default graph or the unnamed graph is used
            //Alternatively you can set the second parameter to true to use the union of all graphs
            //as the default graph
            InMemoryDataset ds = new InMemoryDataset(store, new Uri(url));

            //Get the Query processor
            ISparqlQueryProcessor processor = new LeviathanQueryProcessor(ds);

            //Use the SparqlQueryParser to give us a SparqlQuery object
            //Should get a Graph back from a CONSTRUCT query

            SparqlQueryParser sparqlparser = new SparqlQueryParser();
            SparqlQuery       query        = sparqlparser.ParseFromString(queryString);
            var result = processor.ProcessQuery(query);

            if (result is SparqlResultSet)
            {
                var results = (SparqlResultSet)result;
                return(results.ToList());
            }

            //if (result is IGraph)
            //{
            //    //Print out the Results
            //    IGraph g = (IGraph)result;
            //    NTriplesFormatter formatter = new NTriplesFormatter();
            //    return g.Triples.ToList();
            //}

            return(null);
        }
        public void SparqlDatasetDefaultGraphManagementWithUpdate5()
        {
            TripleStore store = new TripleStore();
            Graph       g     = new Graph();

            g.BaseUri = new Uri("http://example.org/graph");
            store.Add(g);
            Graph h = new Graph();

            h.BaseUri = new Uri("http://example.org/someOtherGraph");
            store.Add(h);

            InMemoryDataset          dataset   = new InMemoryDataset(store, h.BaseUri);
            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);
            SparqlUpdateParser       parser    = new SparqlUpdateParser();
            SparqlUpdateCommandSet   cmds      = parser.ParseFromString("LOAD <http://www.dotnetrdf.org/configuration#>; WITH <http://example.org/graph> INSERT { ?s a ?type } USING <http://example.org/someOtherGraph> WHERE { ?s a ?type }; DELETE WHERE { ?s a ?type }");

            processor.ProcessCommandSet(cmds);

            Assert.False(g.IsEmpty, "First Graph should not be empty as should have been filled by the INSERT command");
            Assert.False(h.IsEmpty, "Second Graph should not be empty as should not have been filled by the  LOAD command");
            Assert.False(h.HasSubGraph(g), "First Graph should not be a subgraph of the Second Graph as the DELETE should have eliminated the subgraph relationship");
        }
예제 #17
0
        public void SparqlPropertyPathEvaluationCore441ZeroOrMorePath()
        {
            IGraph g = new Graph();

            g.LoadFromFile(@"resources\core-441\data.ttl");

            InMemoryDataset dataset = new InMemoryDataset(g);
            SparqlQuery     query   = new SparqlQueryParser().ParseFromFile(@"resources\core-441\star-path.rq");

            Console.WriteLine(query.ToAlgebra().ToString());

            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);
            Object results = processor.ProcessQuery(query);

            Assert.NotNull(results);
            TestTools.ShowResults(results);

            Assert.IsInstanceOf(typeof(SparqlResultSet), results);
            SparqlResultSet rset = (SparqlResultSet)results;

            Assert.AreEqual(1, rset.Count);
            Assert.AreEqual(g.CreateUriNode("Frame:Sheep"), rset[0]["prey"]);
        }
        public void SparqlParallelEvaluationOptional1()
        {
            String data = @"<http://a> <http://p> <http://x> .
<http://b> <http://p> <http://y> .
<http://c> <http://p> <http://z> .
<http://x> <http://value> ""X"" .
<http://z> <http://value> ""Z"" .";

            String      query = "SELECT * WHERE { ?s <http://p> ?o . OPTIONAL { ?o <http://value> ?value } }";
            SparqlQuery q     = this._parser.ParseFromString(query);

            TripleStore store = new TripleStore();

            StringParser.ParseDataset(store, data, new NQuadsParser());
            InMemoryDataset         dataset   = new InMemoryDataset(store);
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);

            Stopwatch timer = new Stopwatch();

            timer.Start();
            int i;

            for (i = 1; i <= 100000; i++)
            {
                SparqlResultSet results = processor.ProcessQuery(q) as SparqlResultSet;
                Assert.NotNull(results);
                if (results.Count != 3)
                {
                    TestTools.ShowResults(results);
                }
                Assert.Equal(3, results.Count);
            }
            timer.Stop();

            Console.WriteLine("Completed " + i + " Iterations OK");
            Console.WriteLine("Took " + timer.Elapsed);
        }
        public List <string> GetPlacesWithSocialMedia()
        {
            //Then we can parse a SPARQL string into a query
            SparqlQuery query = _parser.ParseFromString(@"
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX foursquare: <http://www.semanticweb.org/hekurankastrati/ontologies/2019/5/foursquare#>
            SELECT ?socialmedia
            WHERE {
              { ?socialmedia rdf:type foursquare:Facebook }
              UNION
              { ?socialmedia rdf:type foursquare:Instagram}
            }");

            InMemoryDataset ds = new InMemoryDataset(_foursquareGraph);

            //Get the Query processor
            ISparqlQueryProcessor processor = new ExplainQueryProcessor(ds);

            List <string> places = new List <string>();

            Object results = processor.ProcessQuery(query);

            if (results is SparqlResultSet resultSet)
            {
                foreach (SparqlResult result in resultSet.Results)
                {
                    string place = result.TryGetValue("socialmedia", out var node)
                        ? ((IUriNode)node).Uri.Fragment.Replace('#', ' ')
                        : string.Empty;

                    places.Add(place);
                }
            }

            return(places);
        }
        private void SparqlQueryThreadSafeEvaluationActual()
        {
            var query1 = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/1> { ?s ?p ?o } }";
            var query2 = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/2> { ?s ?p ?o } }";

            var q1 = _parser.ParseFromString(query1);
            var q2 = _parser.ParseFromString(query2);

            Assert.False(q1.UsesDefaultDataset, "Query 1 should not be thread safe");
            Assert.False(q2.UsesDefaultDataset, "Query 2 should not be thread safe");

            var dataset = new InMemoryDataset();
            var g       = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            g.BaseUri = new Uri("http://example.org/1");
            var h = new Graph();

            h.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.LeviathanFunctionLibrary.ttl");
            h.BaseUri = new Uri("http://example.org/2");

            dataset.AddGraph(g);
            dataset.AddGraph(h);
            var processor = new LeviathanQueryProcessor(dataset);

            var t1 = Task.Factory.StartNew(() => QueryWithGraph(q1, processor));
            var t2 = Task.Factory.StartNew(() => QueryWithGraph(q2, processor));

            Task.WaitAll(t1, t2);
            var gQuery = t1.Result;
            var hQuery = t2.Result;

            Assert.Equal(g, gQuery);
            Assert.Equal(h, hQuery);
            Assert.NotEqual(g, h);
        }
예제 #21
0
    protected void Button11_Click(object sender, EventArgs e)
    {
        //Bachelor in Communications Technology Program Details
        TripleStore store = new TripleStore();
        Graph       g1    = new Graph();

        g1.LoadFromFile(Server.MapPath("SVUModeling.rdf"));
        store.Add(g1);
        InMemoryDataset ds = new InMemoryDataset(store);
        //Get the Query processor
        ISparqlQueryProcessor processor = new LeviathanQueryProcessor(ds);

        Label1.Text       = "Bachelor in Communications Technology Program : BACT Details";
        Label1.Visible    = true;
        GridView1.Visible = false;
        Label2.Text       = "Bachelor in Communications Technology Program Informations ";
        Label2.Visible    = true;
        // to select the Communications Technology program's Director Informations
        SparqlQueryParser sparqlparser = new SparqlQueryParser();
        SparqlQuery       query        = sparqlparser.ParseFromString(@"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                prefix owl: <http://www.w3.org/2002/07/owl#>
                prefix foaf: <http://xmlns.com/foaf/0.1/#>
                SELECT   ?BACTDirectorInfo
                WHERE {
                ?t   owl:TelecommunicationsTechnologyDirectorInfoProperty ?BACTDirectorInfo
                }");
        Object            results      = processor.ProcessQuery(query);
        DataTable         DT2          = new DataTable();
        SparqlResultSet   rset         = (SparqlResultSet)results;

        DT2 = FillDataTable(rset);
        GridView2.DataSource = DT2;
        GridView2.DataBind();
        GridView2.Visible = true;
        //to retrival the Teachers of Bachelor in Communications Technology program
        Label3.Text    = "Teachers Of Bachelor in Communications Technology Program";
        Label3.Visible = true;
        SparqlQueryParser sparqlparser3 = new SparqlQueryParser();
        SparqlQuery       query3        = sparqlparser.ParseFromString(@"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                prefix foaf: <http://xmlns.com/foaf/0.1/#>
                prefix owl: <http://www.w3.org/2002/07/owl#>
                SELECT   ?BACTTeachers
                WHERE {
                ?t   owl:TeachersOfTelecommunicationsTechnology ?BACTTeachers
                }");
        Object            results3      = processor.ProcessQuery(query3);
        DataTable         DT3           = new DataTable();
        SparqlResultSet   rset3         = (SparqlResultSet)results3;

        DT3 = FillDataTable(rset3);
        GridView3.DataSource = DT3;
        GridView3.DataBind();
        GridView3.Visible = true;
        //to select Courses Of Bachelor in Communications Technology
        Label4.Text    = "Courses of Bachelor in Communications Technology Program";
        Label4.Visible = true;
        SparqlQueryParser sparqlparser4 = new SparqlQueryParser();
        SparqlQuery       query4        = sparqlparser.ParseFromString(@"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                prefix owl: <http://www.w3.org/2002/07/owl#>
                SELECT   ?BACTCourses
                WHERE {
                ?t   owl:CoursesOfTelecommunicationsTechnology ?BACTCourses
                }");
        Object            results4      = processor.ProcessQuery(query4);
        DataTable         DT4           = new DataTable();
        SparqlResultSet   rset4         = (SparqlResultSet)results4;

        DT4 = FillDataTable(rset4);
        GridView4.DataSource = DT4;
        GridView4.DataBind();
        GridView4.Visible = true;
    }
 public RdfQueryWrapper(InMemoryDataset dataset)
 {
     _dataset = dataset;
 }
예제 #23
0
 public void Cleanup()
 {
     this._parser = null;
     this._data   = null;
 }
예제 #24
0
        public void SparqlUpdateInsertDeleteWithBlankNodes()
        {
            //This test adapted from a contribution by Tomasz Pluskiewicz
            //It demonstrates an issue in SPARQL Update caused by incorrect Graph
            //references that can result when using GraphPersistenceWrapper in a SPARQL Update
            String initData = @"@prefix ex: <http://www.example.com/>.
@prefix rr: <http://www.w3.org/ns/r2rml#>.

ex:triplesMap rr:predicateObjectMap _:blank .
_:blank rr:object ex:Employee, ex:Worker .";

            String update = @"PREFIX rr: <http://www.w3.org/ns/r2rml#>

DELETE { ?map rr:object ?value . }
INSERT { ?map rr:objectMap [ rr:constant ?value ] . }
WHERE { ?map rr:object ?value }";

            String query = @"prefix ex: <http://www.example.com/>
prefix rr: <http://www.w3.org/ns/r2rml#>

select *
where
{
ex:triplesMap rr:predicateObjectMap ?predObjMap .
?predObjMap rr:objectMap ?objMap .
}";

            String expectedData = @"@prefix ex: <http://www.example.com/>.
@prefix rr: <http://www.w3.org/ns/r2rml#>.

ex:triplesMap rr:predicateObjectMap _:blank.
_:blank rr:objectMap _:autos1.
_:autos1 rr:constant ex:Employee.
_:autos2 rr:constant ex:Worker.
_:blank rr:objectMap _:autos2.";

            // given
            IGraph graph = new Graph();

            graph.LoadFromString(initData);
            IGraph expectedGraph = new Graph();

            expectedGraph.LoadFromString(expectedData);

            Console.WriteLine("Initial Graph:");
            TestTools.ShowGraph(graph);
            Console.WriteLine();

            // when
            TripleStore store = new TripleStore();

            store.Add(graph);

            var dataset = new InMemoryDataset(store, graph.BaseUri);
            ISparqlUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);
            var updateParser = new SparqlUpdateParser();

            processor.ProcessCommandSet(updateParser.ParseFromString(update));

            Console.WriteLine("Resulting Graph:");
            TestTools.ShowGraph(graph);
            Console.WriteLine();

            Triple x         = graph.GetTriplesWithPredicate(graph.CreateUriNode("rr:predicateObjectMap")).FirstOrDefault();
            INode  origBNode = x.Object;

            Assert.True(graph.GetTriples(origBNode).Count() > 1, "Should be more than one Triple using the BNode");
            IEnumerable <Triple> ys = graph.GetTriplesWithSubject(origBNode);

            foreach (Triple y in ys)
            {
                Assert.Equal(origBNode, y.Subject);
            }

            //Graphs should be equal
            GraphDiffReport diff = graph.Difference(expectedGraph);

            if (!diff.AreEqual)
            {
                TestTools.ShowDifferences(diff);
            }
            Assert.Equal(expectedGraph, graph);

            //Test the Query
            SparqlResultSet results = graph.ExecuteQuery(query) as SparqlResultSet;

            TestTools.ShowResults(results);
            Assert.False(results.IsEmpty, "Should be some results");
        }
        private void SparqlQueryAndUpdateThreadSafeEvaluationActual()
        {
            var query1  = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/1> { ?s ?p ?o } }";
            var query2  = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/2> { ?s ?p ?o } }";
            var query3  = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/3> { ?s ?p ?o } }";
            var update1 = "INSERT DATA { GRAPH <http://example.org/3> { <ex:subj> <ex:pred> <ex:obj> } }";

            var q1 = _parser.ParseFromString(query1);
            var q2 = _parser.ParseFromString(query2);
            var q3 = _parser.ParseFromString(query3);

            Assert.False(q1.UsesDefaultDataset, "Query 1 should not be thread safe");
            Assert.False(q2.UsesDefaultDataset, "Query 2 should not be thread safe");
            Assert.False(q3.UsesDefaultDataset, "Query 3 should not be thread safe");

            var parser = new SparqlUpdateParser();
            var cmds   = parser.ParseFromString(update1);

            var dataset = new InMemoryDataset();
            var g       = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            g.BaseUri = new Uri("http://example.org/1");
            var h = new Graph();

            h.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.LeviathanFunctionLibrary.ttl");
            h.BaseUri = new Uri("http://example.org/2");
            var i = new Graph {
                BaseUri = new Uri("http://example.org/3")
            };

            dataset.AddGraph(g);
            dataset.AddGraph(h);
            dataset.AddGraph(i);
            var processor   = new LeviathanQueryProcessor(dataset);
            var upProcessor = new LeviathanUpdateProcessor(dataset);

            var d  = new QueryWithGraphDelegate(QueryWithGraph);
            var d2 = new RunUpdateDelegate(RunUpdate);
            var t1 = Task.Factory.StartNew(() => QueryWithGraph(q1, processor));
            var t2 = Task.Factory.StartNew(() => QueryWithGraph(q2, processor));
            var t3 = Task.Factory.StartNew(() => QueryWithGraph(q3, processor));
            var t4 = Task.Factory.StartNew(() => RunUpdate(cmds, upProcessor));

            Task.WaitAll(t1, t2, t3, t4);
            var gQuery = t1.Result;
            var hQuery = t2.Result;
            var iQuery = t3.Result;

            Assert.Equal(g, gQuery);
            Assert.Equal(h, hQuery);
            if (iQuery.IsEmpty)
            {
                _output.WriteLine("Query 3 executed before the INSERT DATA command - running again to get the resulting graph");
                iQuery = QueryWithGraph(q3, processor);
            }
            else
            {
                _output.WriteLine("Query 3 executed after the INSERT DATA command");
            }
            //Test iQuery against an empty Graph
            Assert.False(iQuery.IsEmpty, "Graph should not be empty as INSERT DATA should have inserted a Triple");
            Assert.NotEqual(new Graph(), iQuery);

            Assert.NotEqual(g, h);
        }
예제 #26
0
        private void SparqlQueryAndUpdateThreadSafeEvaluationActual()
        {
            String query1  = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/1> { ?s ?p ?o } }";
            String query2  = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/2> { ?s ?p ?o } }";
            String query3  = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/3> { ?s ?p ?o } }";
            String update1 = "INSERT DATA { GRAPH <http://example.org/3> { <ex:subj> <ex:pred> <ex:obj> } }";

            SparqlQuery q1 = this._parser.ParseFromString(query1);
            SparqlQuery q2 = this._parser.ParseFromString(query2);
            SparqlQuery q3 = this._parser.ParseFromString(query3);

            Assert.False(q1.UsesDefaultDataset, "Query 1 should not be thread safe");
            Assert.False(q2.UsesDefaultDataset, "Query 2 should not be thread safe");
            Assert.False(q3.UsesDefaultDataset, "Query 3 should not be thread safe");

            SparqlUpdateParser     parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds   = parser.ParseFromString(update1);

            InMemoryDataset dataset = new InMemoryDataset();
            Graph           g       = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            g.BaseUri = new Uri("http://example.org/1");
            Graph h = new Graph();

            h.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.LeviathanFunctionLibrary.ttl");
            h.BaseUri = new Uri("http://example.org/2");
            Graph i = new Graph();

            i.BaseUri = new Uri("http://example.org/3");

            dataset.AddGraph(g);
            dataset.AddGraph(h);
            dataset.AddGraph(i);
            LeviathanQueryProcessor  processor   = new LeviathanQueryProcessor(dataset);
            LeviathanUpdateProcessor upProcessor = new LeviathanUpdateProcessor(dataset);

            QueryWithGraphDelegate d  = new QueryWithGraphDelegate(this.QueryWithGraph);
            RunUpdateDelegate      d2 = new RunUpdateDelegate(this.RunUpdate);
            IAsyncResult           r1 = d.BeginInvoke(q1, processor, null, null);
            IAsyncResult           r2 = d.BeginInvoke(q2, processor, null, null);
            IAsyncResult           r3 = d.BeginInvoke(q3, processor, null, null);
            IAsyncResult           r4 = d2.BeginInvoke(cmds, upProcessor, null, null);

            WaitHandle.WaitAll(new WaitHandle[] { r1.AsyncWaitHandle, r2.AsyncWaitHandle, r3.AsyncWaitHandle, r4.AsyncWaitHandle });

            IGraph gQuery = d.EndInvoke(r1);

            Assert.Equal(g, gQuery);

            IGraph hQuery = d.EndInvoke(r2);

            Assert.Equal(h, hQuery);

            IGraph iQuery = d.EndInvoke(r3);

            if (iQuery.IsEmpty)
            {
                Console.WriteLine("Query 3 executed before the INSERT DATA command - running again to get the resulting graph");
                iQuery = this.QueryWithGraph(q3, processor);
            }
            else
            {
                Console.WriteLine("Query 3 executed after the INSERT DATA command");
            }
            //Test iQuery against an empty Graph
            Assert.False(iQuery.IsEmpty, "Graph should not be empty as INSERT DATA should have inserted a Triple");
            Assert.NotEqual(new Graph(), iQuery);

            Assert.NotEqual(g, h);
        }
예제 #27
0
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text    = "there is three types of programs at Syrian Virtual University";
        Label1.Visible = true;
        Graph g1 = new Graph();

        g1.LoadFromFile(Server.MapPath("SVUModeling.rdf"));
        TripleStore store = new TripleStore();

        store.Add(g1);
        //Assume that we fill our Store with data from somewhere

        //Create a dataset for our queries to operate over
        //We need to explicitly state our default graph or the unnamed graph is used
        //Alternatively you can set the second parameter to true to use the union of all graphs
        //as the default graph
        InMemoryDataset ds = new InMemoryDataset(store);

        //Get the Query processor
        ISparqlQueryProcessor processor = new LeviathanQueryProcessor(ds);

        //Use the SparqlQueryParser to give us a SparqlQuery object
        //Should get a Graph back from a CONSTRUCT query
        SparqlQueryParser sparqlparser = new SparqlQueryParser();
        SparqlQuery       query        = sparqlparser.ParseFromString(@"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                                                           prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                                                           prefix owl: <http://www.w3.org/2002/07/owl#>
                                                           SELECT   ?ProgramName 
                                                           WHERE {
                                                           ?t   owl:StudyingAtSVU ?ProgramName 
                                                        }");
        Object            results      = processor.ProcessQuery(query);
        DataTable         DT1          = new DataTable();
        SparqlResultSet   rset         = (SparqlResultSet)results;

        DT1 = FillDataTable(rset);
        GridView1.DataSource = DT1;
        GridView1.DataBind();
        GridView1.Visible = true;


        // to select Bachelor Programs
        Label2.Text    = "Bachelor Programs At SVU";
        Label2.Visible = true;
        SparqlQueryParser sparqlparser2 = new SparqlQueryParser();
        SparqlQuery       query2        = sparqlparser.ParseFromString(@"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                 prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                 prefix owl: <http://www.w3.org/2002/07/owl#>
                SELECT ?BachelorPrograms
                WHERE {
                ?t   owl:BachelorProgramAtSVU ?BachelorPrograms
                }");
        Object            results2      = processor.ProcessQuery(query2);
        DataTable         DT2           = new DataTable();
        SparqlResultSet   rset2         = (SparqlResultSet)results2;

        DT2 = FillDataTable(rset2);
        GridView2.DataSource = DT2;
        GridView2.DataBind();
        GridView2.Visible = true;

        // to select Master Programs
        Label3.Text    = "Master Programs At SVU";
        Label3.Visible = true;
        SparqlQueryParser sparqlparser3 = new SparqlQueryParser();
        SparqlQuery       query3        = sparqlparser.ParseFromString(@"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                prefix owl: <http://www.w3.org/2002/07/owl#>
                SELECT ?MasterPrograms 
                WHERE {
                ?t   owl:MasterProgramAtSVU ?MasterPrograms 
                }");
        Object            results3      = processor.ProcessQuery(query3);
        DataTable         DT3           = new DataTable();
        SparqlResultSet   rset3         = (SparqlResultSet)results3;

        DT3 = FillDataTable(rset3);
        GridView3.DataSource = DT3;
        GridView3.DataBind();
        GridView3.Visible = true;

        // to select Training Programs
        Label4.Text    = "Training Programs At SVU";
        Label4.Visible = true;
        SparqlQueryParser sparqlparser4 = new SparqlQueryParser();
        SparqlQuery       query4        = sparqlparser.ParseFromString(@"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                prefix owl: <http://www.w3.org/2002/07/owl#>
                SELECT   ?TrainingPrograms 
                WHERE {
                ?t   owl:TrainingProgramAtSVU ?TrainingPrograms 
                }");
        Object            results4      = processor.ProcessQuery(query4);
        DataTable         DT4           = new DataTable();
        SparqlResultSet   rset4         = (SparqlResultSet)results4;

        DT4 = FillDataTable(rset4);
        GridView4.DataSource = DT4;
        GridView4.DataBind();
        GridView4.Visible = true;
    }
        public PartialViewResult ExecuteQuery(Query data)
        {
            ResultSet _ResultSet = new ResultSet();

            SparqlResultSet _SparqlResultSet = null;
            //string query = Request.Unvalidated["SparqlQuery"];
            // Create a Triple Collection with only a subject index
            BaseTripleCollection tripleCollection = new TreeIndexedTripleCollection(
                true, false, false, false, false, false, MultiDictionaryMode.AVL);
            // Create a Graph using the customized triple collection
            //  Graph g = new Graph(tripleCollection);
            TripleStore _TripleStore = new TripleStore();
            string      _OwlFileName = GetAttachedOwlFile();

            if (!_OwlFileName.Equals("Error"))
            {
                string _OWLFilePath = Server.MapPath("~/DataSets/" + _OwlFileName);
                _TripleStore.LoadFromFile(_OWLFilePath);
                ISparqlDataset          _ISparqlDataset          = new InMemoryDataset(_TripleStore);
                LeviathanQueryProcessor _LeviathanQueryProcessor = new LeviathanQueryProcessor(_ISparqlDataset);
                SparqlQueryParser       _SparqlQueryParser       = new SparqlQueryParser();
                //  Then we can parse a SPARQL string into a query
                if (ModelState.IsValid)
                {
                    try {
                        SparqlQuery _SparqlQuery = _SparqlQueryParser.ParseFromString(data.query);
                        var         results      = _LeviathanQueryProcessor.ProcessQuery(_SparqlQuery);
                        _SparqlResultSet = (SparqlResultSet)results;

                        ///
                        _ResultSet.Columns = _SparqlResultSet.Variables;
                        string value = null;
                        foreach (SparqlResult _SparqlResult in _SparqlResultSet)
                        {
                            foreach (var _Col in _ResultSet.Columns)
                            {
                                INode _Node;
                                if (_SparqlResult.TryGetValue(_Col, out _Node))
                                {
                                    switch (_Node.NodeType)
                                    {
                                    case NodeType.Uri:
                                        value = ((IUriNode)_Node).Uri.AbsoluteUri;
                                        break;

                                    case NodeType.Literal:

                                        value = ((ILiteralNode)_Node).Value;
                                        break;

                                    default:
                                        Console.WriteLine("Error Node");
                                        ModelState.AddModelError("", "Error in The Node Type ");
                                        break;
                                    }
                                }
                                else
                                {
                                    value = String.Empty;
                                }
                                //Data To Rows
                                _ResultSet.Rows.Enqueue(value);
                            }
                        }

                        ///
                    }
                    catch (RdfParseException e)
                    {
                        //  Console.SetError(e.ToString());
                        ModelState.AddModelError("", "Error in The Syntax " + e.Message);
                        TempData["message"] = string.Format("Syntaxerror");
                    }
                }
                else
                {
                    TempData["message"] = string.Format("EmptyQuery");
                }
            }
            else
            {
                TempData["message"] = string.Format("ChooseDb");
            }
            return(PartialView("_ExecuteQuery", _ResultSet));
        }
 public void Dispose()
 {
     this._parser = null;
     this._data   = null;
 }
예제 #30
0
    protected void Button2_Click1(object sender, EventArgs e)
    {
        TripleStore store = new TripleStore();
        Graph       g1    = new Graph();

        g1.LoadFromFile(Server.MapPath("SVUModeling.rdf"));
        store.Add(g1);
        Label1.Text       = "Economic Program Details";
        Label1.Visible    = true;
        GridView1.Visible = false;
        InMemoryDataset ds = new InMemoryDataset(store);
        //Get the Query processor
        ISparqlQueryProcessor processor = new LeviathanQueryProcessor(ds);

        //Use the SparqlQueryParser to give us a SparqlQuery object
        //Should get a Graph back from a CONSTRUCT query
        Label2.Text    = "Economic Director Informations ";
        Label2.Visible = true;
        // to select the Economic Director Informations
        SparqlQueryParser sparqlparser = new SparqlQueryParser();
        SparqlQuery       query        = sparqlparser.ParseFromString(@"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                prefix owl: <http://www.w3.org/2002/07/owl#>
                prefix foaf: <http://xmlns.com/foaf/0.1/#>
                SELECT   ?EconomicDirectorInfo 
                WHERE {
                ?t   owl:EconomicDirectorInfoProperty ?EconomicDirectorInfo
                }");
        Object            results      = processor.ProcessQuery(query);
        DataTable         DT2          = new DataTable();
        SparqlResultSet   rset         = (SparqlResultSet)results;

        DT2 = FillDataTable(rset);
        GridView2.DataSource = DT2;
        GridView2.DataBind();
        GridView2.Visible = true;
        //to retrival the Teachers Economic program
        Label3.Text    = "Teachers Of Economic Program";
        Label3.Visible = true;
        SparqlQueryParser sparqlparser2 = new SparqlQueryParser();
        SparqlQuery       query2        = sparqlparser.ParseFromString(@"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                prefix foaf: <http://xmlns.com/foaf/0.1/#>
                prefix owl: <http://www.w3.org/2002/07/owl#>
                SELECT   ?TeachersEconomic 
                WHERE {
                ?t   owl:TeachersOfEconomic ?TeachersEconomic 
                }");
        Object            results2      = processor.ProcessQuery(query2);
        DataTable         DT3           = new DataTable();
        SparqlResultSet   rset5         = (SparqlResultSet)results2;

        DT3 = FillDataTable(rset5);
        GridView3.DataSource = DT3;
        GridView3.DataBind();
        GridView3.Visible = true;
        //to select Courses Of Economic
        Label4.Text    = "Courses of Economic Program";
        Label4.Visible = true;
        SparqlQueryParser sparqlparser4 = new SparqlQueryParser();
        SparqlQuery       query4        = sparqlparser.ParseFromString(@"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                prefix owl: <http://www.w3.org/2002/07/owl#>
                SELECT   ?CoursesEconomic 
                WHERE {
                ?t   owl:CoursesOfEconomic ?CoursesEconomic 
                }");
        Object            results4      = processor.ProcessQuery(query4);
        DataTable         DT4           = new DataTable();
        SparqlResultSet   rset6         = (SparqlResultSet)results4;

        DT4 = FillDataTable(rset6);
        GridView4.DataSource = DT4;
        GridView4.DataBind();
        GridView4.Visible = true;
    }