Example #1
0
        public void SparqlUpdateLoad()
        {
            TripleStore store = new TripleStore();

            LoadCommand loadLondon      = new LoadCommand(new Uri("http://dbpedia.org/resource/London"));
            LoadCommand loadSouthampton = new LoadCommand(new Uri("http://dbpedia.org/resource/Southampton"), new Uri("http://example.org"));

            store.ExecuteUpdate(loadLondon);
            store.ExecuteUpdate(loadSouthampton);

            Assert.AreEqual(2, store.Graphs.Count, "Should now be 2 Graphs in the Store");
            Assert.AreNotEqual(0, store.Triples.Count(), "Should be some Triples in the Store");

            foreach (IGraph g in store.Graphs)
            {
                foreach (Triple t in g.Triples)
                {
                    Console.Write(t.ToString());
                    if (g.BaseUri != null)
                    {
                        Console.WriteLine(" from " + g.BaseUri.ToString());
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                }
            }
        }
Example #2
0
        public void SparqlUpdateLoad()
        {
            if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing))
            {
                throw new SkipTestException("Test Config marks Remote Parsing as unavailable, test cannot be run");
            }

            TripleStore store = new TripleStore();

            LoadCommand loadLondon      = new LoadCommand(new Uri("http://dbpedia.org/resource/London"));
            LoadCommand loadSouthampton = new LoadCommand(new Uri("http://dbpedia.org/resource/Southampton"), new Uri("http://example.org"));

            store.ExecuteUpdate(loadLondon);
            store.ExecuteUpdate(loadSouthampton);

            Assert.Equal(2, store.Graphs.Count);
            Assert.NotEmpty(store.Triples);

            foreach (IGraph g in store.Graphs)
            {
                foreach (Triple t in g.Triples)
                {
                    Console.Write(t.ToString());
                    if (g.BaseUri != null)
                    {
                        Console.WriteLine(" from " + g.BaseUri.ToString());
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                }
            }
        }
Example #3
0
        public void SparqlUpdateLoad()
        {
            if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing))
            {
                Assert.Inconclusive("Test Config marks Remote Parsing as unavailable, test cannot be run");
            }

            TripleStore store = new TripleStore();

            LoadCommand loadLondon      = new LoadCommand(new Uri("http://dbpedia.org/resource/London"));
            LoadCommand loadSouthampton = new LoadCommand(new Uri("http://dbpedia.org/resource/Southampton"), new Uri("http://example.org"));

            store.ExecuteUpdate(loadLondon);
            store.ExecuteUpdate(loadSouthampton);

            Assert.AreEqual(2, store.Graphs.Count, "Should now be 2 Graphs in the Store");
            Assert.AreNotEqual(0, store.Triples.Count(), "Should be some Triples in the Store");

            foreach (IGraph g in store.Graphs)
            {
                foreach (Triple t in g.Triples)
                {
                    Console.Write(t.ToString());
                    if (g.BaseUri != null)
                    {
                        Console.WriteLine(" from " + g.BaseUri.ToString());
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                }
            }
        }
        public void SparqlGroupByComplex1()
        {
            String data  = @"PREFIX : <http://test/> INSERT DATA { :x :p 1 , 2 . :y :p 5 }";
            String query = @"SELECT ?s (CONCAT('$', STR(SUM(?o))) AS ?Total) WHERE { ?s ?p ?o } GROUP BY ?s";

            TripleStore store = new TripleStore();

            store.ExecuteUpdate(data);
            Assert.Equal(1, store.Graphs.Count);
            Assert.Equal(3, store.Triples.Count());

            //Aggregates may occur in project expressions and should evaluate correctly
            var results = ExecuteQuery(store, query) as SparqlResultSet;

            Assert.NotNull(results);
            Assert.True(results.All(r => r.HasBoundValue("Total")));

            SparqlResult x = results.Where(r => ((IUriNode)r["s"]).Uri.Equals(new Uri("http://test/x"))).FirstOrDefault();

            Assert.NotNull(x);
            Assert.Equal("$3", x["Total"].AsValuedNode().AsString());

            SparqlResult y = results.Where(r => ((IUriNode)r["s"]).Uri.Equals(new Uri("http://test/y"))).FirstOrDefault();

            Assert.NotNull(y);
            Assert.Equal("$5", y["Total"].AsValuedNode().AsString());
        }
Example #5
0
        public void SparqlUpdateInsertDataWithSkolemBNodes()
        {
            TripleStore store = new TripleStore();
            Graph g = new Graph();
            store.Add(g);

            String prefixes = "PREFIX rdf: <" + NamespaceMapper.RDF + ">\n PREFIX xsd: <" + NamespaceMapper.XMLSCHEMA + ">\n PREFIX ex: <http://example.org/>\n PREFIX rdl: <http://example.org/roles>\n PREFIX tpl: <http://example.org/template/>\n";
            String insert = prefixes + "INSERT DATA { " + InsertPatterns1 + "}";
            Console.WriteLine(insert.Replace("_:template","<_:template>"));
            Console.WriteLine();
            store.ExecuteUpdate(insert.Replace("_:template", "<_:template>"));
            insert = prefixes + "INSERT DATA {" + InsertPatterns2 + "}";
            Console.WriteLine(insert);
            Console.WriteLine();
            store.ExecuteUpdate(insert);

            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString());
            }
        }
Example #6
0
        public void SparqlUpdateLoadQuads4()
        {
            var tripleStore = new TripleStore();

            String g1 = Path.GetFullPath(@"resources\core-421\g1.nq").Replace('\\', '/');
            String g2 = Path.GetFullPath(@"resources\core-421\g2.nq").Replace('\\', '/');

            tripleStore.ExecuteUpdate("LOAD <file:///" + g1 + "> into graph <http://test.org/user>");
            tripleStore.ExecuteUpdate("LOAD <file:///" + g2 + "> into graph <http://test.org/prodList/>");
            Assert.Equal(3, tripleStore.Triples.Count());
            Assert.Equal(3, tripleStore.Graphs.Count);

            tripleStore.SaveToFile("core-421.nq", new NQuadsWriter());

            var newStore = new TripleStore();

            newStore.LoadFromFile("core-421.nq", new NQuadsParser());

            Assert.Equal(3, newStore.Triples.Count());
            Assert.Equal(2, newStore.Graphs.Count);
        }
Example #7
0
        public void SparqlUpdateInsertBNodesComplex3()
        {
            String update = @"PREFIX : <http://test/>
INSERT DATA { GRAPH :a { :s :p _:b } GRAPH :b { :s :p _:b } };
INSERT { GRAPH :a { ?s ?p ?o } } WHERE { GRAPH :b { ?s ?p ?o } }";

            TripleStore store = new TripleStore();

            store.ExecuteUpdate(update);

            Assert.Equal(3, store.Graphs.Count);
            Assert.Equal(1, store[new Uri("http://test/a")].Triples.Count);
        }
Example #8
0
        /// <summary>
        /// Converts the entity into an rdf graph
        /// </summary>
        /// <param name="entities">List of entities to be converted</param>
        /// <param name="metadataProperties">Metadata of the entity to be converted</param>
        /// <returns>RDf graph of entity</returns>
        private IGraph GetResourceGraph(List <Entity> entities, IList <MetadataProperty> metadataProperties)
        {
            var store = new TripleStore();

            //Create InsertString from resource
            foreach (Entity entity in entities)
            {
                var resourceInsertString = _entityRepository.GenerateInsertQuery(entity, metadataProperties, null);
                store.ExecuteUpdate(resourceInsertString.ToString());
            }

            return(store.Graphs.FirstOrDefault());
        }
Example #9
0
        public void SparqlUpdateInsertBNodesComplex2()
        {
            String update = @"PREFIX : <http://test/>
INSERT { GRAPH :a { :s :p _:b } } WHERE { };
INSERT { GRAPH :b { :s :p _:b } } WHERE { };
INSERT { GRAPH :a { ?s ?p ?o } } WHERE { GRAPH :b { ?s ?p ?o } }";

            TripleStore store = new TripleStore();

            store.ExecuteUpdate(update);

            Assert.AreEqual(3, store.Graphs.Count, "Expected 3 Graphs");
            Assert.AreEqual(2, store[new Uri("http://test/a")].Triples.Count, "Expected 2 Triples");
        }
Example #10
0
        public void SparqlUpdateInsertDataWithSkolemBNodes()
        {
            TripleStore store = new TripleStore();
            Graph       g     = new Graph();

            store.Add(g);

            String prefixes = "PREFIX rdf: <" + NamespaceMapper.RDF + ">\n PREFIX xsd: <" + NamespaceMapper.XMLSCHEMA + ">\n PREFIX ex: <http://example.org/>\n PREFIX rdl: <http://example.org/roles>\n PREFIX tpl: <http://example.org/template/>\n";
            String insert   = prefixes + "INSERT DATA { " + InsertPatterns1 + "}";

            Console.WriteLine(insert.Replace("_:template", "<_:template>"));
            Console.WriteLine();
            store.ExecuteUpdate(insert.Replace("_:template", "<_:template>"));
            insert = prefixes + "INSERT DATA {" + InsertPatterns2 + "}";
            Console.WriteLine(insert);
            Console.WriteLine();
            store.ExecuteUpdate(insert);

            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString());
            }
        }
Example #11
0
        /// <summary>
        /// Converts the entity into an rdf graph
        /// </summary>
        /// <param name="entity">Entity to be converted</param>
        /// <param name="metadataProperties">Metadata of the entity to be converted</param>
        /// <returns>RDf graph of entity</returns>
        private IGraph GetResourceGraph(Entity entity, IList <MetadataProperty> metadataProperties)
        {
            var store = new TripleStore();

            //Create InsertString from resource
            if (metadataProperties.IsNullOrEmpty())
            {
                string entityType = entity.Properties.GetValueOrNull(Graph.Metadata.Constants.RDF.Type, true).ToString();
                metadataProperties = _metadataService.GetMetadataForEntityType(entityType);
            }
            var resourceInsertString = _entityRepository.GenerateInsertQuery(entity, metadataProperties, string.Empty, null);

            store.ExecuteUpdate(resourceInsertString.ToString());

            return(store.Graphs.FirstOrDefault());
        }
Example #12
0
        public void SparqlUpdateLoadQuads2()
        {
            var tripleStore             = new TripleStore();
            SparqlUpdateCommandSet cmds = this._parser.ParseFromFile(@"resources\core-421\test2.ru");

            tripleStore.ExecuteUpdate(cmds);
            Assert.That(tripleStore.Triples.ToList(), Has.Count.EqualTo(3));
            Assert.That(tripleStore.Graphs, Has.Count.EqualTo(3));

            tripleStore.SaveToFile("core-421.nq", new NQuadsWriter());

            var newStore = new TripleStore();

            newStore.LoadFromFile("core-421.nq", new NQuadsParser());

            Assert.That(newStore.Triples.ToList(), Has.Count.EqualTo(3));
            Assert.That(newStore.Graphs, Has.Count.EqualTo(2));
        }
Example #13
0
        public void SparqlUpdateLoadQuads3()
        {
            var tripleStore             = new TripleStore();
            SparqlUpdateCommandSet cmds = this._parser.ParseFromFile(@"resources\core-421\test3.ru");

            tripleStore.ExecuteUpdate(cmds);
            Assert.Equal(3, tripleStore.Triples.Count());
            Assert.Equal(3, tripleStore.Graphs.Count);

            tripleStore.SaveToFile("core-421.nq", new NQuadsWriter());

            var newStore = new TripleStore();

            newStore.LoadFromFile("core-421.nq", new NQuadsParser());

            Assert.Equal(3, newStore.Triples.Count());
            Assert.Equal(2, newStore.Graphs.Count);
        }
Example #14
0
        public void SparqlUpdateModify()
        {
            TripleStore store = new TripleStore();
            Graph       g     = new Graph();

            FileLoader.Load(g, "resources\\InferenceTest.ttl");
            g.BaseUri = null;
            store.Add(g);

            IUriNode rdfType = g.CreateUriNode(new Uri(RdfSpecsHelper.RdfType));

            Assert.NotEmpty(store.GetTriplesWithPredicate(rdfType));

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

            store.ExecuteUpdate(cmds);

            Assert.Empty(store.GetTriplesWithPredicate(rdfType));
        }
Example #15
0
        public void SparqlUpdateModify()
        {
            TripleStore store = new TripleStore();
            Graph       g     = new Graph();

            FileLoader.Load(g, "InferenceTest.ttl");
            g.BaseUri = null;
            store.Add(g);

            IUriNode rdfType = g.CreateUriNode(new Uri(RdfSpecsHelper.RdfType));

            Assert.AreNotEqual(0, store.GetTriplesWithPredicate(rdfType).Count(), "Store should contain some rdf:type Triples");

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

            store.ExecuteUpdate(cmds);

            Assert.AreEqual(0, store.GetTriplesWithPredicate(rdfType).Count(), "Store should contain no rdf:type Triples after DELETE command executes");
        }
Example #16
0
        public void SparqlUpdateInsertBNodesComplex1()
        {
            String update = @"PREFIX : <http://test/>
INSERT { :s :p _:b } WHERE { };
INSERT { ?o ?p ?s } WHERE { ?s ?p ?o }";

            TripleStore store = new TripleStore();

            store.ExecuteUpdate(update);

            Assert.Equal(1, store.Graphs.Count);
            Assert.Equal(2, store.Triples.Count());

            IGraph def = store[null];
            Triple a   = def.Triples.Where(t => t.Subject.NodeType == NodeType.Blank).FirstOrDefault();

            Assert.NotNull(a);
            Triple b = def.Triples.Where(t => t.Object.NodeType == NodeType.Blank).FirstOrDefault();

            Assert.NotNull(b);

            Assert.Equal(a.Subject, b.Object);
        }
        /// <summary>
        /// Applies reasoning on the Input Graph materialising the generated Triples in the Output Graph.
        /// </summary>
        /// <param name="input">Input Graph.</param>
        /// <param name="output">Output Graph.</param>
        public void Apply(IGraph input, IGraph output)
        {
            TripleStore store = new TripleStore();

            store.Add(input);
            if (!ReferenceEquals(input, output))
            {
                store.Add(output, true);
            }

            // Apply each rule in turn
            foreach (String[] rule in _rules)
            {
                // Build the final version of the rule text for the given input and output
                StringBuilder ruleText = new StringBuilder();

                // If there's a Base URI on the Output Graph need a WITH clause
                if (output.BaseUri != null)
                {
                    ruleText.AppendLine("WITH <" + _formatter.FormatUri(output.BaseUri) + ">");
                }
                ruleText.AppendLine(rule[0]);
                // If there's a Base URI on the Input Graph need a USING clause
                if (input.BaseUri != null)
                {
                    ruleText.AppendLine("USING <" + _formatter.FormatUri(input.BaseUri) + ">");
                }
                ruleText.AppendLine(rule[1]);

                ISyntaxValidationResults results = _validator.Validate(ruleText.ToString());
                if (results.IsValid)
                {
                    store.ExecuteUpdate((SparqlUpdateCommandSet)results.Result);
                }
            }
        }
        /// <summary>
        /// Applies reasoning on the Input Graph materialising the generated Triples in the Output Graph
        /// </summary>
        /// <param name="input">Input Graph</param>
        /// <param name="output">Output Graph</param>
        public void Apply(IGraph input, IGraph output)
        {
            TripleStore store = new TripleStore();
            store.Add(input);
            if (!ReferenceEquals(input, output))
            {
                store.Add(output, true);
            }

            //Apply each rule in turn
            foreach (String[] rule in this._rules)
            {
                //Build the final version of the rule text for the given input and output
                StringBuilder ruleText = new StringBuilder();

                //If there's a Base URI on the Output Graph need a WITH clause
                if (output.BaseUri != null)
                {
                    ruleText.AppendLine("WITH <" + this._formatter.FormatUri(output.BaseUri) + ">");
                }
                ruleText.AppendLine(rule[0]);
                //If there's a Base URI on the Input Graph need a USING clause
                if (input.BaseUri != null)
                {
                    ruleText.AppendLine("USING <" + this._formatter.FormatUri(input.BaseUri) + ">");
                }
                ruleText.AppendLine(rule[1]);

                ISyntaxValidationResults results = this._validator.Validate(ruleText.ToString());
                if (results.IsValid)
                {
                    store.ExecuteUpdate((SparqlUpdateCommandSet)results.Result);
                }
            }
        }
Example #19
0
        public void SparqlUpdateCreateDrop()
        {
            TripleStore store = new TripleStore();

            Console.WriteLine("Store has " + store.Graphs.Count + " Graphs");

            //Create a couple of Graphs using Create Commands
            CreateCommand create1 = new CreateCommand(new Uri("http://example.org/1"));
            CreateCommand create2 = new CreateCommand(new Uri("http://example.org/2"));

            store.ExecuteUpdate(create1);
            store.ExecuteUpdate(create2);

            Assert.Equal(3, store.Graphs.Count);
            Assert.Empty(store.Triples);

            //Trying the same Create again should cause an error
            try
            {
                store.ExecuteUpdate(create1);
                Assert.True(false, "Executing a CREATE command twice without the SILENT modifier should error");
            }
            catch (SparqlUpdateException)
            {
                Console.WriteLine("Executing a CREATE command twice without the SILENT modifier errored as expected");
            }

            //Equivalent Create with SILENT should not error
            CreateCommand create3 = new CreateCommand(new Uri("http://example.org/1"), true);

            try
            {
                store.ExecuteUpdate(create3);
                Console.WriteLine("Executing a CREATE for an existing Graph with the SILENT modifier suppressed the error as expected");
            }
            catch (SparqlUpdateException)
            {
                Assert.True(false, "Executing a CREATE for an existing Graph with the SILENT modifier should not error");
            }

            DropCommand drop1 = new DropCommand(new Uri("http://example.org/1"));

            store.ExecuteUpdate(drop1);
            Assert.Equal(2, store.Graphs.Count);

            try
            {
                store.ExecuteUpdate(drop1);
                Assert.True(false, "Trying to DROP a non-existent Graph should error");
            }
            catch (SparqlUpdateException)
            {
                Console.WriteLine("Trying to DROP a non-existent Graph produced an error as expected");
            }

            DropCommand drop2 = new DropCommand(new Uri("http://example.org/1"), ClearMode.Graph, true);

            try
            {
                store.ExecuteUpdate(drop2);
                Console.WriteLine("Trying to DROP a non-existent Graph with the SILENT modifier suppressed the error as expected");
            }
            catch (SparqlUpdateException)
            {
                Assert.True(false, "Trying to DROP a non-existent Graph with the SILENT modifier should suppress the error");
            }
        }
Example #20
0
 private void ExecuteUpdate(string query)
 {
     store.ExecuteUpdate(prefixes + query);
 }
Example #21
0
        public void SparqlUpdateModify()
        {
            TripleStore store = new TripleStore();
            Graph g = new Graph();
            FileLoader.Load(g, "InferenceTest.ttl");
            g.BaseUri = null;
            store.Add(g);

            IUriNode rdfType = g.CreateUriNode(new Uri(RdfSpecsHelper.RdfType));

            Assert.AreNotEqual(0, store.GetTriplesWithPredicate(rdfType).Count(), "Store should contain some rdf:type Triples");

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

            Assert.AreEqual(0, store.GetTriplesWithPredicate(rdfType).Count(), "Store should contain no rdf:type Triples after DELETE command executes");
        }
Example #22
0
        public void SparqlUpdateCreateDrop()
        {
            TripleStore store = new TripleStore();

            Console.WriteLine("Store has " + store.Graphs.Count + " Graphs");

            //Create a couple of Graphs using Create Commands
            CreateCommand create1 = new CreateCommand(new Uri("http://example.org/1"));
            CreateCommand create2 = new CreateCommand(new Uri("http://example.org/2"));

            store.ExecuteUpdate(create1);
            store.ExecuteUpdate(create2);

            Assert.AreEqual(3, store.Graphs.Count, "Store should have now had three Graphs");
            Assert.AreEqual(0, store.Triples.Count(), "Store should have no triples at this point");

            //Trying the same Create again should cause an error
            try
            {
                store.ExecuteUpdate(create1);
                Assert.Fail("Executing a CREATE command twice without the SILENT modifier should error");
            }
            catch (SparqlUpdateException)
            {
                Console.WriteLine("Executing a CREATE command twice without the SILENT modifier errored as expected");
            }

            //Equivalent Create with SILENT should not error
            CreateCommand create3 = new CreateCommand(new Uri("http://example.org/1"), true);
            try
            {
                store.ExecuteUpdate(create3);
                Console.WriteLine("Executing a CREATE for an existing Graph with the SILENT modifier suppressed the error as expected");
            }
            catch (SparqlUpdateException)
            {
                Assert.Fail("Executing a CREATE for an existing Graph with the SILENT modifier should not error");
            }

            DropCommand drop1 = new DropCommand(new Uri("http://example.org/1"));
            store.ExecuteUpdate(drop1);
            Assert.AreEqual(2, store.Graphs.Count, "Store should have only 2 Graphs after we executed the DROP command");

            try
            {
                store.ExecuteUpdate(drop1);
                Assert.Fail("Trying to DROP a non-existent Graph should error");
            }
            catch (SparqlUpdateException)
            {
                Console.WriteLine("Trying to DROP a non-existent Graph produced an error as expected");
            }

            DropCommand drop2 = new DropCommand(new Uri("http://example.org/1"), ClearMode.Graph, true);
            try
            {
                store.ExecuteUpdate(drop2);
                Console.WriteLine("Trying to DROP a non-existent Graph with the SILENT modifier suppressed the error as expected");
            }
            catch (SparqlUpdateException)
            {
                Assert.Fail("Trying to DROP a non-existent Graph with the SILENT modifier should suppress the error");
            }
        }
Example #23
0
        public void SparqlUpdateLoad()
        {
            TripleStore store = new TripleStore();

            LoadCommand loadLondon = new LoadCommand(new Uri("http://dbpedia.org/resource/London"));
            LoadCommand loadSouthampton = new LoadCommand(new Uri("http://dbpedia.org/resource/Southampton"), new Uri("http://example.org"));

            store.ExecuteUpdate(loadLondon);
            store.ExecuteUpdate(loadSouthampton);

            Assert.AreEqual(2, store.Graphs.Count, "Should now be 2 Graphs in the Store");
            Assert.AreNotEqual(0, store.Triples.Count(), "Should be some Triples in the Store");

            foreach (IGraph g in store.Graphs)
            {
                foreach (Triple t in g.Triples)
                {
                    Console.Write(t.ToString());
                    if (g.BaseUri != null)
                    {
                        Console.WriteLine(" from " + g.BaseUri.ToString());
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                }
            }
        }