protected void TestReasoner(DalcRdfStore dalcStore) { var store = new Store(dalcStore); Euler engine = new Euler(new N3Reader(new StringReader(eulerRules))); store.AddReasoner(engine); Console.WriteLine( "Is Adam a parent of Bill? " + store.Contains( new Statement(baseNs + "persons#1", baseNs + "terms#is_parent", (Entity)(baseNs + "persons#4"))).ToString()); Console.WriteLine( "Is Eve a parent of Bill? " + store.Contains( new Statement(baseNs + "persons#2", baseNs + "terms#is_parent", (Entity)(baseNs + "persons#4"))).ToString()); Console.Write("Children of Eve: "); var res = store.Select(new Statement(null, baseNs + "terms#is_child", (Entity)(baseNs + "persons#2"))); foreach (var st in res) { var nameRes = store.Select(new Statement(st.Subject.Uri, ns_foaf_name, null)); Console.Write(String.Format("({0})", dalcStore.GetDataKey(st.Subject).Id)); foreach (var nameSt in nameRes) { Console.Write(nameSt.Object.ToString() + " "); } } Console.WriteLine(); }
protected void ExportToN3(DalcRdfStore store) { using (var n3wr = new N3Writer(Console.Out)) { n3wr.Namespaces.AddNamespace("http://www.nreco.qsh.eu/rdf/", "nreco"); n3wr.Namespaces.AddNamespace("http://www.nreco.qsh.eu/rdf/persons#", "p"); n3wr.Namespaces.AddNamespace(NS.Rdf.BASE, "rdf"); n3wr.Namespaces.AddNamespace(NS.Rdfs.BASE, "rdfs"); store.Select(n3wr); } }
protected DalcRdfStore PrepareRdfMapping(IDalc dalc) { var dalc2Rdf = new DalcRdfStore(); dalc2Rdf.Dalc = dalc; dalc2Rdf.Sources = new[] { new DalcRdfStore.SourceDescriptor() { Ns = baseNs + "persons", IdFieldName = "id", IdFieldType = typeof(int), RdfType = NS.Rdfs.Class, SourceName = "persons", Fields = new [] { new DalcRdfStore.FieldDescriptor() { FieldName = "name", Ns = ns_foaf_name, RdfType = NS.Rdfs.Property, FieldType = typeof(string) }, new DalcRdfStore.FieldDescriptor() { FieldType = typeof(int), FieldName = "mother_id", Ns = baseNs + "terms#has_mother", RdfType = NS.Rdfs.Property, FkSourceName = "persons" }, new DalcRdfStore.FieldDescriptor() { FieldType = typeof(int), FieldName = "father_id", Ns = baseNs + "terms#has_father", RdfType = NS.Rdfs.Property, FkSourceName = "persons" } } } }; dalc2Rdf.Init(); return(dalc2Rdf); }
public void SelectByTemplateTest() { var store = new DalcRdfStore(); store.Dalc = PrepareDalc(); string ns_foaf_name = "http://xmlns.com/foaf/0.1/name"; string ns_accounts = "urn:test:accounts"; string ns_number = "urn:test:number"; string ns_id = "urn:test:id"; string ns_aff_id = "urn:test:aff_id"; store.Sources = new[] { new DalcRdfStore.SourceDescriptor() { Ns = ns_accounts, IdFieldName = "id", RdfType = NS.Rdfs.Class, SourceName = "accounts", Fields = new [] { new DalcRdfStore.FieldDescriptor() { FieldName = "name", Ns = ns_foaf_name, RdfType = NS.Rdf.Property, FieldType = typeof(string) }, new DalcRdfStore.FieldDescriptor() { FieldName = "age", FieldType = typeof(int), Ns = "urn:test:age", RdfType = NS.Rdf.Property }, new DalcRdfStore.FieldDescriptor() { FieldName = "number", FieldType = typeof(int), Ns = ns_number, RdfType = NS.Rdf.Property }, new DalcRdfStore.FieldDescriptor() { FieldType = typeof(int), FieldName = "id", Ns = ns_id, RdfType = NS.Rdf.Property }, new DalcRdfStore.FieldDescriptor() { FieldType = typeof(int), FieldName = "aff_id", Ns = ns_aff_id, RdfType = NS.Rdf.Property, FkSourceName = "accounts" } } } }; store.Init(); //System.Diagnostics.Debugger.Break(); var ns_account1 = "urn:test:accounts#1"; var ns_account2 = "urn:test:accounts#2"; var ns_account3 = "urn:test:accounts#3"; var ns_age = "urn:test:age"; Assert.IsFalse(store.Contains(new Statement(ns_account1, ns_age, new Literal("2")))); Assert.IsTrue(store.Contains(new Statement(ns_account2, ns_age, new Literal("2")))); Assert.IsTrue(store.Contains(new Statement(null, ns_age, new Literal("2")))); var sinkMock = GetSinkMock(); // test case: no subject store.Select(new Statement(null, ns_age, new Literal("2")), sinkMock.Object); sinkMock.Verify(a => a.Add(new Statement(ns_account2, ns_age, new Literal("2"))), Times.Exactly(1)); var sinkMock2 = GetSinkMock(); // test case: only subject store.Select(new Statement(ns_account1, null, null), sinkMock2.Object); sinkMock2.Verify(a => a.Add(new Statement(ns_account1, ns_age, new Literal("26"))), Times.Exactly(1)); sinkMock2.Verify(a => a.Add(new Statement(ns_account1, ns_number, new Literal("2"))), Times.Exactly(1)); sinkMock2.Verify(a => a.Add(new Statement(ns_account1, ns_foaf_name, new Literal("Vitalik"))), Times.Exactly(1)); sinkMock2.Verify(a => a.Add(new Statement(ns_account1, NS.Rdf.type, (Entity)ns_accounts)), Times.Exactly(1)); sinkMock2.Verify(a => a.Add(new Statement(ns_account1, ns_aff_id, (Entity)ns_account3)), Times.Exactly(1)); // test case: select filter var sinkMock3 = GetSinkMock(); store.Select( new SelectFilter(null, new[] { (Entity)ns_age, (Entity)ns_number }, new[] { new Literal("2") }, null), sinkMock3.Object); sinkMock3.Verify(a => a.Add(new Statement(ns_account1, ns_number, new Literal("2"))), Times.Exactly(1)); sinkMock3.Verify(a => a.Add(new Statement(ns_account2, ns_age, new Literal("2"))), Times.Exactly(1)); var sinkMock4 = GetSinkMock(); store.Select(new Statement(null, ns_aff_id, (Entity)ns_account1), sinkMock4.Object); sinkMock4.Verify(a => a.Add(new Statement(ns_account2, ns_aff_id, (Entity)ns_account1)), Times.Exactly(1)); // test case: schema selects Assert.IsTrue(store.Contains(new Statement(ns_aff_id, NS.Rdf.type, NS.Rdf.PropertyEntity))); Assert.IsTrue(store.Contains(new Statement(ns_aff_id, NS.Rdfs.domainEntity, (Entity)ns_accounts))); //store.Select(new N3Writer(Console.Out)); int schemaStatements = 0; var sinkMock5 = GetSinkMock(); sinkMock5.Setup(a => a.Add(It.IsAny <Statement>())).Callback(() => schemaStatements++).Returns(true); store.Select(new Statement(null, NS.Rdf.typeEntity, NS.Rdfs.ClassEntity), sinkMock5.Object); Assert.AreEqual(1, schemaStatements); sinkMock5.Verify(a => a.Add(new Statement(ns_accounts, NS.Rdf.typeEntity, NS.Rdfs.ClassEntity)), Times.Exactly(1)); store.Select(new Statement(null, NS.Rdfs.domainEntity, (Entity)ns_accounts), sinkMock5.Object); Assert.AreEqual(1 + 5, schemaStatements); sinkMock5.Verify(a => a.Add(new Statement(ns_foaf_name, NS.Rdfs.domainEntity, (Entity)ns_accounts)), Times.Exactly(1)); sinkMock5.Verify(a => a.Add(new Statement(ns_age, NS.Rdfs.domainEntity, (Entity)ns_accounts)), Times.Exactly(1)); // test case: literal filter int litFilterStatements = 0; var sinkMock6 = GetSinkMock(); sinkMock6.Setup(a => a.Add(It.IsAny <Statement>())).Callback(() => litFilterStatements++).Returns(true); store.Select( new SelectFilter(new[] { (Entity)ns_account3 }, null, null, null) { LiteralFilters = new [] { LiteralFilter.Create(LiteralFilter.CompType.GT, 20) } }, sinkMock6.Object); Assert.AreEqual(2, litFilterStatements); sinkMock6.Verify(a => a.Add(new Statement(ns_account3, ns_age, new Literal("52"))), Times.Exactly(1)); sinkMock6.Verify(a => a.Add(new Statement(ns_account3, ns_number, new Literal("100"))), Times.Exactly(1)); // test case: select by type int typeStatementsCount = 0; var sinkMock7 = GetSinkMock(); sinkMock7.Setup(a => a.Add(It.IsAny <Statement>())).Callback(() => typeStatementsCount++).Returns(true); store.Select(new Statement(null, NS.Rdf.typeEntity, (Entity)ns_accounts), sinkMock7.Object); Assert.AreEqual(3, typeStatementsCount); typeStatementsCount = 0; store.Select(new Statement(null, NS.Rdf.typeEntity, null), sinkMock7.Object); Assert.AreEqual(9, typeStatementsCount); }
protected void TestReasoner(DalcRdfStore dalcStore) { var store = new Store(dalcStore); Euler engine = new Euler(new N3Reader(new StringReader(eulerRules))); store.AddReasoner(engine); Console.WriteLine( "Is Adam a parent of Bill? "+ store.Contains( new Statement(baseNs + "persons#1", baseNs + "terms#is_parent", (Entity)(baseNs + "persons#4"))).ToString()); Console.WriteLine( "Is Eve a parent of Bill? " + store.Contains( new Statement(baseNs + "persons#2", baseNs + "terms#is_parent", (Entity)(baseNs + "persons#4"))).ToString()); Console.Write("Children of Eve: "); var res = store.Select(new Statement(null, baseNs + "terms#is_child", (Entity)(baseNs + "persons#2") )); foreach (var st in res) { var nameRes = store.Select( new Statement( st.Subject.Uri, ns_foaf_name, null) ); Console.Write(String.Format("({0})", dalcStore.GetDataKey(st.Subject).Id)); foreach (var nameSt in nameRes) Console.Write( nameSt.Object.ToString() + " "); } Console.WriteLine(); }
protected DalcRdfStore PrepareRdfMapping(IDalc dalc) { var dalc2Rdf = new DalcRdfStore(); dalc2Rdf.Dalc = dalc; dalc2Rdf.Sources = new[] { new DalcRdfStore.SourceDescriptor() { Ns = baseNs+"persons", IdFieldName = "id", IdFieldType = typeof(int), RdfType = NS.Rdfs.Class, SourceName = "persons", Fields = new [] { new DalcRdfStore.FieldDescriptor() { FieldName = "name", Ns = ns_foaf_name, RdfType = NS.Rdfs.Property, FieldType = typeof(string) }, new DalcRdfStore.FieldDescriptor() { FieldType = typeof(int), FieldName = "mother_id", Ns = baseNs+"terms#has_mother", RdfType = NS.Rdfs.Property, FkSourceName = "persons" }, new DalcRdfStore.FieldDescriptor() { FieldType = typeof(int), FieldName = "father_id", Ns = baseNs+"terms#has_father", RdfType = NS.Rdfs.Property, FkSourceName = "persons" } } } }; dalc2Rdf.Init(); return dalc2Rdf; }
public void SelectByTemplateTest() { var store = new DalcRdfStore(); store.Dalc = PrepareDalc(); string ns_foaf_name = "http://xmlns.com/foaf/0.1/name"; string ns_accounts = "urn:test:accounts"; string ns_number = "urn:test:number"; string ns_id = "urn:test:id"; string ns_aff_id = "urn:test:aff_id"; store.Sources = new[] { new DalcRdfStore.SourceDescriptor() { Ns = ns_accounts, IdFieldName = "id", RdfType = NS.Rdfs.Class, SourceName = "accounts", Fields = new [] { new DalcRdfStore.FieldDescriptor() { FieldName = "name", Ns = ns_foaf_name, RdfType = NS.Rdf.Property, FieldType = typeof(string) }, new DalcRdfStore.FieldDescriptor() { FieldName = "age", FieldType = typeof(int), Ns = "urn:test:age", RdfType = NS.Rdf.Property }, new DalcRdfStore.FieldDescriptor() { FieldName = "number", FieldType = typeof(int), Ns = ns_number, RdfType = NS.Rdf.Property }, new DalcRdfStore.FieldDescriptor() { FieldType = typeof(int), FieldName = "id", Ns = ns_id, RdfType = NS.Rdf.Property }, new DalcRdfStore.FieldDescriptor() { FieldType = typeof(int), FieldName = "aff_id", Ns = ns_aff_id, RdfType = NS.Rdf.Property, FkSourceName = "accounts" } } } }; store.Init(); //System.Diagnostics.Debugger.Break(); var ns_account1 = "urn:test:accounts#1"; var ns_account2 = "urn:test:accounts#2"; var ns_account3 = "urn:test:accounts#3"; var ns_age = "urn:test:age"; Assert.IsFalse(store.Contains(new Statement(ns_account1, ns_age, new Literal("2")))); Assert.IsTrue(store.Contains(new Statement(ns_account2, ns_age, new Literal("2")))); Assert.IsTrue(store.Contains(new Statement(null, ns_age, new Literal("2")))); var sinkMock = GetSinkMock(); // test case: no subject store.Select(new Statement(null, ns_age, new Literal("2")), sinkMock.Object); sinkMock.Verify(a => a.Add(new Statement(ns_account2, ns_age, new Literal("2"))), Times.Exactly(1)); var sinkMock2 = GetSinkMock(); // test case: only subject store.Select(new Statement(ns_account1, null, null), sinkMock2.Object); sinkMock2.Verify(a => a.Add(new Statement(ns_account1, ns_age, new Literal("26"))), Times.Exactly(1)); sinkMock2.Verify(a => a.Add(new Statement(ns_account1, ns_number, new Literal("2"))), Times.Exactly(1)); sinkMock2.Verify(a => a.Add(new Statement(ns_account1, ns_foaf_name, new Literal("Vitalik"))), Times.Exactly(1)); sinkMock2.Verify(a => a.Add(new Statement(ns_account1, NS.Rdf.type, (Entity)ns_accounts)), Times.Exactly(1)); sinkMock2.Verify(a => a.Add(new Statement(ns_account1, ns_aff_id, (Entity)ns_account3 )), Times.Exactly(1)); // test case: select filter var sinkMock3 = GetSinkMock(); store.Select( new SelectFilter(null, new[] { (Entity)ns_age, (Entity)ns_number }, new[] { new Literal("2") }, null), sinkMock3.Object); sinkMock3.Verify(a => a.Add(new Statement(ns_account1, ns_number, new Literal("2") )), Times.Exactly(1)); sinkMock3.Verify(a => a.Add(new Statement(ns_account2, ns_age, new Literal("2"))), Times.Exactly(1)); var sinkMock4 = GetSinkMock(); store.Select(new Statement(null, ns_aff_id, (Entity)ns_account1), sinkMock4.Object); sinkMock4.Verify(a => a.Add(new Statement(ns_account2, ns_aff_id, (Entity)ns_account1 )), Times.Exactly(1)); // test case: schema selects Assert.IsTrue(store.Contains(new Statement(ns_aff_id, NS.Rdf.type, NS.Rdf.PropertyEntity))); Assert.IsTrue(store.Contains(new Statement(ns_aff_id, NS.Rdfs.domainEntity, (Entity)ns_accounts ))); //store.Select(new N3Writer(Console.Out)); int schemaStatements = 0; var sinkMock5 = GetSinkMock(); sinkMock5.Setup(a => a.Add(It.IsAny<Statement>())).Callback(() => schemaStatements++).Returns(true); store.Select(new Statement(null, NS.Rdf.typeEntity, NS.Rdfs.ClassEntity), sinkMock5.Object); Assert.AreEqual(1, schemaStatements); sinkMock5.Verify(a => a.Add(new Statement(ns_accounts, NS.Rdf.typeEntity, NS.Rdfs.ClassEntity)), Times.Exactly(1)); store.Select(new Statement(null, NS.Rdfs.domainEntity, (Entity)ns_accounts), sinkMock5.Object); Assert.AreEqual(1+5, schemaStatements); sinkMock5.Verify(a => a.Add(new Statement(ns_foaf_name, NS.Rdfs.domainEntity, (Entity)ns_accounts)), Times.Exactly(1)); sinkMock5.Verify(a => a.Add(new Statement(ns_age, NS.Rdfs.domainEntity, (Entity)ns_accounts)), Times.Exactly(1) ); // test case: literal filter int litFilterStatements = 0; var sinkMock6 = GetSinkMock(); sinkMock6.Setup(a => a.Add(It.IsAny<Statement>())).Callback(() => litFilterStatements++).Returns(true); store.Select( new SelectFilter(new[] { (Entity)ns_account3 }, null, null, null) { LiteralFilters = new [] { LiteralFilter.Create(LiteralFilter.CompType.GT, 20) } }, sinkMock6.Object); Assert.AreEqual(2, litFilterStatements); sinkMock6.Verify(a => a.Add(new Statement( ns_account3 , ns_age, new Literal("52") )), Times.Exactly(1)); sinkMock6.Verify(a => a.Add(new Statement(ns_account3, ns_number, new Literal("100"))), Times.Exactly(1)); // test case: select by type int typeStatementsCount = 0; var sinkMock7 = GetSinkMock(); sinkMock7.Setup(a => a.Add(It.IsAny<Statement>())).Callback(() => typeStatementsCount++).Returns(true); store.Select(new Statement(null, NS.Rdf.typeEntity, (Entity)ns_accounts ), sinkMock7.Object); Assert.AreEqual(3, typeStatementsCount); typeStatementsCount = 0; store.Select(new Statement(null, NS.Rdf.typeEntity, null), sinkMock7.Object); Assert.AreEqual(9, typeStatementsCount); }