/// <summary> /// Adds the given quadruple to the CSPOL index /// </summary> internal RDFStoreIndex AddIndex(RDFQuadruple quadruple) { if (quadruple != null) { //Context if (!this.Contexts.ContainsKey(quadruple.Context.PatternMemberID)) { this.Contexts.Add(quadruple.Context.PatternMemberID, new Dictionary<Int64, Object>() { {quadruple.QuadrupleID, null} }); } else { if (!this.Contexts[quadruple.Context.PatternMemberID].ContainsKey(quadruple.QuadrupleID)) { this.Contexts[quadruple.Context.PatternMemberID].Add(quadruple.QuadrupleID, null); } } //Subject if (!this.Subjects.ContainsKey(quadruple.Subject.PatternMemberID)) { this.Subjects.Add(quadruple.Subject.PatternMemberID, new Dictionary<Int64, Object>() { {quadruple.QuadrupleID, null} }); } else { if (!this.Subjects[quadruple.Subject.PatternMemberID].ContainsKey(quadruple.QuadrupleID)) { this.Subjects[quadruple.Subject.PatternMemberID].Add(quadruple.QuadrupleID, null); } } //Predicate if (!this.Predicates.ContainsKey(quadruple.Predicate.PatternMemberID)) { this.Predicates.Add(quadruple.Predicate.PatternMemberID, new Dictionary<Int64, Object>() { {quadruple.QuadrupleID, null} }); } else { if (!this.Predicates[quadruple.Predicate.PatternMemberID].ContainsKey(quadruple.QuadrupleID)) { this.Predicates[quadruple.Predicate.PatternMemberID].Add(quadruple.QuadrupleID, null); } } //Object if (quadruple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) { if (!this.Objects.ContainsKey(quadruple.Object.PatternMemberID)) { this.Objects.Add(quadruple.Object.PatternMemberID, new Dictionary<Int64, Object>() { {quadruple.QuadrupleID, null} }); } else { if (!this.Objects[quadruple.Object.PatternMemberID].ContainsKey(quadruple.QuadrupleID)) { this.Objects[quadruple.Object.PatternMemberID].Add(quadruple.QuadrupleID, null); } } } //Literal else { if (!this.Literals.ContainsKey(quadruple.Object.PatternMemberID)) { this.Literals.Add(quadruple.Object.PatternMemberID, new Dictionary<Int64, Object>() { {quadruple.QuadrupleID, null} }); } else { if (!this.Literals[quadruple.Object.PatternMemberID].ContainsKey(quadruple.QuadrupleID)) { this.Literals[quadruple.Object.PatternMemberID].Add(quadruple.QuadrupleID, null); } } } } return this; }
public static void GetQuadrupleSubjectTest() { RDFQuadruple quad = new RDFQuadruple( new RDFContext("http://www.wikipedia.com/"), new RDFResource("http://www.waltdisney.com/mickey_mouse"), new RDFResource("http://xmlns.com/foaf/0.1/age"), new RDFTypedLiteral("85", RDFModelEnums.RDFDatatypes.XSD_INT)); Assert.Equal("http://www.waltdisney.com/mickey_mouse", quad.Subject.ToString()); }
public static void CreatingSimpleQuadrupleTest() { var cont = new RDFContext("http://www.wikipedia.com/"); var subj = new RDFResource("http://www.waltdisney.com/mickey_mouse"); var pred = new RDFResource("http://xmlns.com/foaf/0.1/age"); var lit = new RDFTypedLiteral("85", RDFModelEnums.RDFDatatypes.XSD_INT); var quad = new RDFQuadruple(cont, subj, pred, lit); Assert.NotNull(quad); }
public void AddQuadrupleLiteral(string contextUri, string subjUri, string predUri, string literalValue) { RDFContext context = new RDFContext(contextUri); RDFResource subj = new RDFResource(subjUri); RDFResource pred = new RDFResource(predUri); RDFLiteral lit = new RDFPlainLiteral(literalValue); RDFQuadruple quadruple = new RDFQuadruple(context, subj, pred, lit); Store.AddQuadruple(quadruple); }
public void AddQuadrupleObject(string contextUri, string subjUri, string predUri, string objectUri) { RDFContext context = new RDFContext(contextUri); RDFResource subj = new RDFResource(subjUri); RDFResource pred = new RDFResource(predUri); RDFResource obj = new RDFResource(objectUri); RDFQuadruple quadruple = new RDFQuadruple(context, subj, pred, obj); Store.AddQuadruple(quadruple); }
public static void ContainsQuadrupleTest() { RDFMemoryStore rdfms = StoreBuilder.CreateStore(); RDFQuadruple contain = new RDFQuadruple( new RDFContext("http://www.waltdisney.com/"), new RDFResource("http://www.waltdisney.com/mickey_mouse"), new RDFResource("http://xmlns.com/foaf/0.1/age"), new RDFTypedLiteral("85", RDFModelEnums.RDFDatatypes.XSD_INT)); Assert.True(rdfms.ContainsQuadruple(contain)); }
/// <summary> /// Removes the given quadruple from the CSPOL index /// </summary> internal RDFStoreIndex RemoveIndex(RDFQuadruple quadruple) { if (quadruple != null) { //Context if (this.Contexts.ContainsKey(quadruple.Context.PatternMemberID)) { if (this.Contexts[quadruple.Context.PatternMemberID].Contains(quadruple.QuadrupleID)) { this.Contexts[quadruple.Context.PatternMemberID].Remove(quadruple.QuadrupleID); if (this.Contexts[quadruple.Context.PatternMemberID].Count == 0) { this.Contexts.Remove(quadruple.Context.PatternMemberID); } } } //Subject if (this.Subjects.ContainsKey(quadruple.Subject.PatternMemberID)) { if (this.Subjects[quadruple.Subject.PatternMemberID].Contains(quadruple.QuadrupleID)) { this.Subjects[quadruple.Subject.PatternMemberID].Remove(quadruple.QuadrupleID); if (this.Subjects[quadruple.Subject.PatternMemberID].Count == 0) { this.Subjects.Remove(quadruple.Subject.PatternMemberID); } } } //Predicate if (this.Predicates.ContainsKey(quadruple.Predicate.PatternMemberID)) { if (this.Predicates[quadruple.Predicate.PatternMemberID].Contains(quadruple.QuadrupleID)) { this.Predicates[quadruple.Predicate.PatternMemberID].Remove(quadruple.QuadrupleID); if (this.Predicates[quadruple.Predicate.PatternMemberID].Count == 0) { this.Predicates.Remove(quadruple.Predicate.PatternMemberID); } } } //Object if (quadruple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) { if (this.Objects.ContainsKey(quadruple.Object.PatternMemberID)) { if (this.Objects[quadruple.Object.PatternMemberID].Contains(quadruple.QuadrupleID)) { this.Objects[quadruple.Object.PatternMemberID].Remove(quadruple.QuadrupleID); if (this.Objects[quadruple.Object.PatternMemberID].Count == 0) { this.Objects.Remove(quadruple.Object.PatternMemberID); } } } } //Literal else { if (this.Literals.ContainsKey(quadruple.Object.PatternMemberID)) { if (this.Literals[quadruple.Object.PatternMemberID].Contains(quadruple.QuadrupleID)) { this.Literals[quadruple.Object.PatternMemberID].Remove(quadruple.QuadrupleID); if (this.Literals[quadruple.Object.PatternMemberID].Count == 0) { this.Literals.Remove(quadruple.Object.PatternMemberID); } } } } } return this; }
/// <summary> /// Checks if the store contains the given quadruple /// </summary> public override Boolean ContainsQuadruple(RDFQuadruple quadruple) { if (quadruple != null) { if (quadruple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) { return (this.SelectQuadruples((RDFContext)quadruple.Context, (RDFResource)quadruple.Subject, (RDFResource)quadruple.Predicate, (RDFResource)quadruple.Object, null)).QuadruplesCount > 0; } else { return (this.SelectQuadruples((RDFContext)quadruple.Context, (RDFResource)quadruple.Subject, (RDFResource)quadruple.Predicate, null, (RDFLiteral)quadruple.Object)).QuadruplesCount > 0; } } return false; }
/// <summary> /// Adds the given quadruple to the store, avoiding duplicate insertions /// </summary> public override RDFStore AddQuadruple(RDFQuadruple quadruple) { if (quadruple != null) { var command = new NpgsqlCommand("INSERT INTO public.\"quadruples\"(quadrupleid, tripleflavor, context, contextid, subject, subjectid, predicate, predicateid, object, objectid) SELECT @QID, @TFV, @CTX, @CTXID, @SUBJ, @SUBJID, @PRED, @PREDID, @OBJ, @OBJID WHERE NOT EXISTS (SELECT 1 FROM public.\"quadruples\" WHERE quadrupleid = @QID)", this.Connection); command.Parameters.Add(new NpgsqlParameter("QID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("CTX", NpgsqlDbType.Varchar, 1000)); command.Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("SUBJ", NpgsqlDbType.Varchar, 1000)); command.Parameters.Add(new NpgsqlParameter("SUBJID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("PRED", NpgsqlDbType.Varchar, 1000)); command.Parameters.Add(new NpgsqlParameter("PREDID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJ", NpgsqlDbType.Varchar, 5000)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); //Valorize parameters command.Parameters["QID"].Value = quadruple.QuadrupleID; command.Parameters["TFV"].Value = quadruple.TripleFlavor; command.Parameters["CTX"].Value = quadruple.Context.ToString(); command.Parameters["CTXID"].Value = quadruple.Context.PatternMemberID; command.Parameters["SUBJ"].Value = quadruple.Subject.ToString(); command.Parameters["SUBJID"].Value = quadruple.Subject.PatternMemberID; command.Parameters["PRED"].Value = quadruple.Predicate.ToString(); command.Parameters["PREDID"].Value = quadruple.Predicate.PatternMemberID; command.Parameters["OBJ"].Value = quadruple.Object.ToString(); command.Parameters["OBJID"].Value = quadruple.Object.PatternMemberID; try { //Open connection this.Connection.Open(); //Prepare command command.Prepare(); //Open transaction command.Transaction = this.Connection.BeginTransaction(); //Execute command command.ExecuteNonQuery(); //Close transaction command.Transaction.Commit(); //Close connection this.Connection.Close(); } catch (Exception ex) { //Rollback transaction command.Transaction.Rollback(); //Close connection this.Connection.Close(); //Propagate exception throw new RDFStoreException("Cannot insert data into PostgreSQL store because: " + ex.Message, ex); } } return this; }
/// <summary> /// Removes the given quadruples from the store /// </summary> public override RDFStore RemoveQuadruple(RDFQuadruple quadruple) { if (quadruple != null) { try { //Open connection this.Connection.Open(); //Open transaction this.Commands["DELETE"].Transaction = this.Commands["DELETE"].Connection.BeginTransaction(); //Prepare command this.Commands["DELETE"].Prepare(); //Valorize parameters this.Commands["DELETE"].Parameters["QID"].Value = quadruple.QuadrupleID; //Execute command this.Commands["DELETE"].ExecuteNonQuery(); //Close transaction this.Commands["DELETE"].Transaction.Commit(); //Close connection this.Connection.Close(); } catch (Exception ex) { //Close transaction this.Commands["DELETE"].Transaction.Rollback(); //Close connection this.Connection.Close(); //Propagate exception throw new RDFStoreException("Cannot delete data from SQLite store because: " + ex.Message); } } return this; }
private void WorkingWithRdfModels() { // CREATE RESOURCE var donaldduck = new RDFResource("http://www.waltdisney.com/donald_duck"); // CREATE BLANK RESOURCE var disney_group = new RDFResource(); // CREATE PLAIN LITERAL // "Donald Duck" var donaldduck_name = new RDFPlainLiteral("Donald Duck"); // CREATE PLAIN LITERAL WITH LANGUAGE TAG // "Donald Duck"@en-US var donaldduck_name_enusLiteral = new RDFPlainLiteral("Donald Duck", "en-US"); // CREATE TYPED LITERAL // "85"^^xsd:integer var mickeymouse_age = new RDFTypedLiteral("85", RDFModelEnums.RDFDatatypes.XSD_INTEGER); // CREATE TRIPLES // "Mickey Mouse is 85 years old" RDFTriple mickeymouse_is85yr = new RDFTriple( new RDFResource("http://www.waltdisney.com/mickey_mouse"), new RDFResource("http://xmlns.com/foaf/0.1/age"), new RDFTypedLiteral("85", RDFModelEnums.RDFDatatypes.XSD_INTEGER)); // "Donald Duck has english (US) name "Donald Duck"" RDFTriple donaldduck_name_enus = new RDFTriple( new RDFResource("http://www.waltdisney.com/donald_duck"), new RDFResource("http://xmlns.com/foaf/0.1/name"), new RDFPlainLiteral("Donald Duck", "en-US")); // CREATE EMPTY GRAPH var another_graph = new RDFGraph(); var waltdisney_filled = new RDFGraph(); // CREATE GRAPH FROM A LIST OF TRIPLES var triples = new List <RDFTriple> { mickeymouse_is85yr, donaldduck_name_enus }; var waltdisney = new RDFGraph(triples); // SET CONTEXT OF A GRAPH waltdisney.SetContext(new Uri("http://waltdisney.com/")); // GET A DATATABLE FROM A GRAPH var waltdisney_table = waltdisney.ToDataTable(); // GET A GRAPH FROM A DATATABLE var waltdisney_newgraph = RDFGraph.FromDataTable(waltdisney_table); // ITERATE TRIPLES OF A GRAPH WITH FOREACH foreach (var t in waltdisney) { Console.WriteLine("Triple: " + t); Console.WriteLine(" Subject: " + t.Subject); Console.WriteLine(" Predicate: " + t.Predicate); Console.WriteLine(" Object: " + t.Object); } // ITERATE TRIPLES OF A GRAPH WITH ENUMERATOR var triplesEnum = waltdisney.TriplesEnumerator; while (triplesEnum.MoveNext()) { Console.WriteLine("Triple: " + triplesEnum.Current); Console.WriteLine(" Subject: " + triplesEnum.Current.Subject); Console.WriteLine(" Predicate: " + triplesEnum.Current.Predicate); Console.WriteLine(" Object: " + triplesEnum.Current.Object); } // GET COUNT OF TRIPLES CONTAINED IN A GRAPH var triplesCount = waltdisney.TriplesCount; // MULTIPLE SELECTIONS var multiple_selections_graph = waltdisney.SelectTriplesBySubject(new RDFResource("http://www.waltdisney.com/donald_duck")) .SelectTriplesByPredicate(new RDFResource("http://xmlns.com/foaf/0.1/name")); // SET OPERATIONS var set_operations_graph = waltdisney.IntersectWith(waltdisney_filled).UnionWith(another_graph); /* * var ntriplesFormat = RDFModelEnums.RDFFormats.NTriples; * // READ N-TRIPLES FILE * var graph = RDFGraph.FromFile(ntriplesFormat, "C:\\file.nt"); * // READ N-TRIPLES STREAM * var graph = RDFGraph.FromStream(ntriplesFormat, inStream); * // WRITE N-TRIPLES FILE * graph.ToFile(ntriplesFormat, "C:\\newfile.nt"); * // WRITE N-TRIPLES STREAM * graph.ToStream(ntriplesFormat, outStream); */ /* * var turtleFormat = RDFModelEnums.RDFFormats.Turtle; * // READ TURTLE FILE * var graph = RDFGraph.FromFile(turtleFormat, "C:\\file.ttl"); * // READ TURTLE STREAM * var graph = RDFGraph.FromStream(turtleFormat, inStream); * // WRITE TURTLE FILE * graph.ToFile(turtleFormat, "C:\\newfile.ttl"); * // WRITE TURTLE STREAM * graph.ToStream(turtleFormat, outStream); */ /* * var xmlFormat = RDFModelEnums.RDFFormats.RdfXml; * // READ RDF/XML FILE * var graph = RDFGraph.FromFile(xmlFormat, "C:\\file.rdf"); * // READ RDF/XML STREAM * var graph = RDFGraph.FromStream(xmlFormat, inStream); * // WRITE RDF/XML FILE * graph.ToFile(xmlFormat, "C:\\newfile.rdf"); * // WRITE RDF/XML STREAM * graph.ToStream(xmlFormat, outStream); */ // CREATE NAMESPACE var waltdisney_ns = new RDFNamespace("wd", "http://www.waltdisney.com/"); // USE NAMESPACE IN RESOURCE CREATION var duckburg = new RDFResource(waltdisney_ns + "duckburg"); var mouseton = new RDFResource(waltdisney_ns + "mouseton"); RDFNamespaceRegister.AddNamespace(waltdisney_ns); // Retrieves a namespace by seeking presence of its prefix (null if not found). Supports prefix.cc var ns1 = RDFNamespaceRegister.GetByPrefix("dbpedia", false); //local search var ns2 = RDFNamespaceRegister.GetByPrefix("dbpedia", true); //search prefix.cc service if no result // GET DEFAULT NAMESPACE var nSpace = RDFNamespaceRegister.DefaultNamespace; // SET DEFAULT NAMESPACE RDFNamespaceRegister.SetDefaultNamespace(waltdisney_ns); //new graphs will default to this context // ITERATE NAMESPACES OF REGISTER WITH FOREACH foreach (var ns in RDFNamespaceRegister.Instance) { Console.WriteLine("Prefix: " + ns.NamespacePrefix); Console.WriteLine("Namespace: " + ns.NamespaceUri); } // ITERATE NAMESPACES OF REGISTER WITH ENUMERATOR var nspacesEnum = RDFNamespaceRegister.NamespacesEnumerator; while (nspacesEnum.MoveNext()) { Console.WriteLine("Prefix: " + nspacesEnum.Current.NamespacePrefix); Console.WriteLine("Namespace: " + nspacesEnum.Current.NamespaceUri); } // CREATE TRIPLES WITH VOCABULARY FACILITIES // "Goofy Goof is 82 years old" RDFTriple goofygoof_is82yr = new RDFTriple( new RDFResource(new Uri("http://www.waltdisney.com/goofy_goof").ToString()), RDFVocabulary.FOAF.AGE, new RDFPlainLiteral("82") ); // "Donald Duck knows Goofy Goof" RDFTriple donaldduck_knows_goofygoof = new RDFTriple( new RDFResource(new Uri("http://www.waltdisney.com/donald_duck").ToString()), RDFVocabulary.FOAF.KNOWS, new RDFResource(new Uri("http://www.waltdisney.com/goofy_goof").ToString()) ); // CREATE TYPED LITERALS var myAge = new RDFTypedLiteral("34", RDFModelEnums.RDFDatatypes.XSD_INT); var myDate = new RDFTypedLiteral("2017-01-07", RDFModelEnums.RDFDatatypes.XSD_DATE); var myDateTime = new RDFTypedLiteral("2017-01-07T23:11:05", RDFModelEnums.RDFDatatypes.XSD_DATETIME); var myXml = new RDFTypedLiteral("<book>title</book>", RDFModelEnums.RDFDatatypes.RDF_XMLLITERAL); var myLiteral = new RDFTypedLiteral("generic literal", RDFModelEnums.RDFDatatypes.RDFS_LITERAL); /* * The given list of items may be incomplete. * A container is semantically opened to the possibility of having further elements * * Alt: unordered semantic, duplicates not allowed; * Bag: unordered semantic, duplicates allowed; * Seq: ordered semantic, duplicates allowed; */ // CREATE CONTAINER AND ADD ITEMS RDFContainer beatles_cont = new RDFContainer(RDFModelEnums.RDFContainerTypes.Bag, RDFModelEnums.RDFItemTypes.Resource); beatles_cont.AddItem(new RDFResource("http://beatles.com/ringo_starr")); beatles_cont.AddItem(new RDFResource("http://beatles.com/john_lennon")); beatles_cont.AddItem(new RDFResource("http://beatles.com/paul_mc_cartney")); beatles_cont.AddItem(new RDFResource("http://beatles.com/george_harrison")); /* * The given list of items may not be incomplete. * A collection is semantically closed to the possibility of having further elements */ // CREATE COLLECTION AND ADD ITEMS RDFCollection beatles_coll = new RDFCollection(RDFModelEnums.RDFItemTypes.Resource); beatles_coll.AddItem(new RDFResource("http://beatles.com/ringo_starr")); beatles_coll.AddItem(new RDFResource("http://beatles.com/john_lennon")); beatles_coll.AddItem(new RDFResource("http://beatles.com/paul_mc_cartney")); beatles_coll.AddItem(new RDFResource("http://beatles.com/george_harrison")); // ADD CONTAINER/COLLECTION TO GRAPH waltdisney.AddContainer(beatles_cont); waltdisney.AddCollection(beatles_coll); // REIFY TRIPLE AND MERGE IT INTO A GRAPH RDFGraph reifGraph = goofygoof_is82yr.ReifyTriple(); waltdisney = waltdisney.UnionWith(reifGraph); // ASSERT SOMETHING ABOUT REIFIED TRIPLE waltdisney.AddTriple(new RDFTriple( new RDFResource("http://www.wikipedia.com/"), new RDFResource("http://example.org/verb_state"), goofygoof_is82yr.ReificationSubject )); var existingGraph = new RDFGraph(); // REIFY CONTAINER existingGraph.AddContainer(beatles_cont); existingGraph.AddTriple(new RDFTriple( new RDFResource("http://www.thebeatles.com/"), RDFVocabulary.FOAF.GROUP, beatles_cont.ReificationSubject )); // REIFY COLLECTION existingGraph.AddCollection(beatles_coll); existingGraph.AddTriple(new RDFTriple( new RDFResource("http://www.thebeatles.com/"), RDFVocabulary.FOAF.GROUP, beatles_coll.ReificationSubject )); // WORKING WITH RDF STORES // CREATE CONTEXT FROM STRING var wdisney_ctx = new RDFContext("http://www.waltdisney.com/"); // CREATE CONTEXT FROM URI var wdisney_ctx_uri = new RDFContext(new Uri("http://www.waltdisney.com/")); // CREATE DEFAULT CONTEXT (DEFAULT NAMESPACE) var wdisney_ctx_default = new RDFContext(); // CREATE QUADRUPLES // "From Wikipedia.com: Mickey Mouse is 85 years old" RDFQuadruple wk_mickeymouse_is85yr = new RDFQuadruple( new RDFContext("http://www.wikipedia.com/"), new RDFResource("http://www.waltdisney.com/mickey_mouse"), RDFVocabulary.FOAF.AGE, new RDFTypedLiteral("85", RDFModelEnums.RDFDatatypes.XSD_INTEGER) ); // "From WaltDisney.com: Mickey Mouse is 85 years old" RDFQuadruple wd_mickeymouse_is85yr = new RDFQuadruple( new RDFContext("http://www.waltdisney.com/"), new RDFResource("http://www.waltdisney.com/mickey_mouse"), RDFVocabulary.FOAF.AGE, new RDFTypedLiteral("85", RDFModelEnums.RDFDatatypes.XSD_INTEGER) ); // "From Wikipedia.com: Donald Duck has english name "Donald Duck"" RDFQuadruple wk_donald_duck_name_enus = new RDFQuadruple( new RDFContext("http://www.wikipedia.com/"), new RDFResource("http://www.waltdisney.com/donald_duck"), RDFVocabulary.FOAF.NAME, new RDFPlainLiteral("Donald Duck", "en") ); // CREATE EMPTY MEMORY STORE var wdStore = new RDFMemoryStore(); // CREATE MEMORY STORE FROM A LIST OF QUADRUPLES var quadruples = new List <RDFQuadruple> { wk_mickeymouse_is85yr, wk_mickeymouse_is85yr }; var wdStoreFilled = new RDFMemoryStore(); foreach (var q in quadruples) { wdStoreFilled.AddQuadruple(q); } // GET A DATATABLE FROM A MEMORY STORE (any kind of store can be exported to datatable) var wdStore_table = wdStoreFilled.ToDataTable(); // GET A MEMORY STORE FROM A DATATABLE var wdStore_new = RDFMemoryStore.FromDataTable(wdStore_table); // ITERATE QUADRUPLES OF A MEMORY STORE WITH FOREACH foreach (var q in wdStore) { Console.WriteLine("Quadruple: " + q); Console.WriteLine(" Context: " + q.Context); Console.WriteLine(" Subject: " + q.Subject); Console.WriteLine(" Predicate: " + q.Predicate); Console.WriteLine(" Object: " + q.Object); } // ITERATE QUADRUPLES OF A MEMORY STORE WITH ENUMERATOR var quadruplesEnum = wdStore.QuadruplesEnumerator; while (quadruplesEnum.MoveNext()) { Console.WriteLine("Quadruple: " + quadruplesEnum.Current); Console.WriteLine(" Context: " + quadruplesEnum.Current.Context); Console.WriteLine(" Subject: " + quadruplesEnum.Current.Subject); Console.WriteLine(" Predicate: " + quadruplesEnum.Current.Predicate); Console.WriteLine(" Object: " + quadruplesEnum.Current.Object); } var nquadsFormat = RDFStoreEnums.RDFFormats.NQuads; // READ N-QUADS FILE //var myStore = RDFMemoryStore.FromFile(nquadsFormat, "C:\\file.nq"); // READ N-QUADS STREAM //var myStore = RDFMemoryStore.FromStream(nquadsFormat, inStream); // WRITE N-QUADS FILE wdStoreFilled.ToFile(nquadsFormat, @"C:\TEMP\newfile.nq"); // WRITE N-QUADS STREAM //myStore.ToStream(nquadsFormat, outStream); var trixFormat = RDFStoreEnums.RDFFormats.TriX; // READ TRIX FILE //var memStore = RDFMemoryStore.FromFile(trixFormat, "C:\\file.trix"); // READ TRIX STREAM //var memStore = RDFMemoryStore.FromStream(trixFormat, inStream); // WRITE TRIX FILE wdStoreFilled.ToFile(trixFormat, @"C:\TEMP\newfile.trix"); // WRITE TRIX STREAM //myStore.ToStream(trixFormat, outStream); // CONNECT TO SQLSERVER STORE WITH CONNECTION STRING //var sqlServer = new RDFSQLServerStore(sqlServerConnectionString); // CREATE EMPTY FEDERATION var fed = new RDFFederation(); /* * // CREATE FEDERATION FROM A LIST OF STORES * var stores = new List<RDFStore>{ waltDisneyStore, waltDisneyStoreFilled }; * var fedFilled = new RDFFederation(); * foreach(var store in stores) * { * fedFilled.AddStore(store); * } */ // ITERATE STORES OF A FEDERATION foreach (var s in fed) { Console.WriteLine("Store: " + s); Console.WriteLine(" Type: " + s.StoreType); } }
public static void Test() { RDFFederation federation = new RDFFederation(); StoreManager manager = new StoreManager(); federation.AddStore(manager.Store); int storeCount = federation.StoresCount; // ITERATE STORES OF A FEDERATION foreach (RDFStore store in federation) { Console.WriteLine("Store: " + store); Console.WriteLine(" Type: " + store.StoreType); } /* * Create this graph named "http://www.example.org/graph1" * * <http://www.example.org/index.html> <http://purl.org/dc/elements/1.1/creator> <http://www.example.org/staffid/85740> . * <http://www.example.org/index.html> <http://www.example.org/terms/creation-date> "August 16, 1999" . * <http://www.example.org/index.html> <http://purl.org/dc/elements/1.1/language> "en" . */ string contextUri = "http://www.example.org/graph1"; string subjectUri = "http://www.example.org/index.html"; RDFContext context = new RDFContext(contextUri); RDFResource subj = new RDFResource(subjectUri); RDFResource pred1 = new RDFResource("http://purl.org/dc/elements/1.1/creator"); RDFResource obj = new RDFResource("http://www.example.org/staffid/85740"); RDFQuadruple quadruple1 = new RDFQuadruple(context, subj, pred1, obj); manager.Store.AddQuadruple(quadruple1); RDFResource pred2 = new RDFResource("http://www.example.org/terms/creation-date"); RDFLiteral lit = new RDFPlainLiteral("August 16, 1999"); RDFQuadruple quadruple2 = new RDFQuadruple(context, subj, pred2, lit); manager.Store.AddQuadruple(quadruple2); manager.AddQuadrupleLiteral(contextUri, subjectUri, "http://purl.org/dc/elements/1.1/language", "en"); // It is recommended that N-Quads files have the extension ".nq" (all lowercase) on all platforms. manager.Store.ToFile(RDFStoreEnums.RDFFormats.NQuads, @"C:\TEMP\index.nq"); manager.Store.ToFile(RDFStoreEnums.RDFFormats.TriX, @"C:\TEMP\index.trix"); List <RDFGraph> graphs = manager.Store.ExtractGraphs(); foreach (RDFGraph graph in graphs) { graph.ToFile(RDFModelEnums.RDFFormats.NTriples, @"C:\TEMP\graph1.nt"); graph.ToFile(RDFModelEnums.RDFFormats.RdfXml, @"C:\TEMP\graph1.rdf"); graph.ToFile(RDFModelEnums.RDFFormats.Turtle, @"C:\TEMP\graph1.ttl"); graph.ToFile(RDFModelEnums.RDFFormats.TriX, @"C:\TEMP\graph1.trix"); } /* * * // CREATE VARIABLE * RDFVariable x = new RDFVariable("x"); // ?X * RDFVariable y = new RDFVariable("y"); // ?Y * RDFVariable n = new RDFVariable("n"); // ?N (don’t show in SELECT results) * * // CREATE PATTERNS * RDFResource dogOf = new RDFResource(RDFVocabulary.DC.BASE_URI + "dogOf"); * RDFPattern y_dogOf_x = new RDFPattern(y, dogOf, x); // TRIPLE PATTERN * RDFPattern n_y_dogOf_x = new RDFPattern(n, y, dogOf, x); // QUADRUPLE PATTERN * * // CREATE EMPTY PATTERN GROUP * RDFPatternGroup pg1 = new RDFPatternGroup("PG1"); * * // ADD PATTERNS TO PATTERN GROUP * pg1.AddPattern(y_dogOf_x); * * // CREATE PATTERN GROUP FROM A LIST OF PATTERNS * List<RDFPattern> patterns = new List<RDFPattern>() { y_dogOf_x }; * RDFPatternGroup pg2 = new RDFPatternGroup("PG2", patterns); * * // ADD FILTERS TO PATTERN GROUP * pg1.AddFilter(new RDFSameTermFilter(x, donaldduck)); * pg1.AddFilter(new RDFLangMatchesFilter(n, "it-IT")); * pg1.AddFilter(new RDFComparisonFilter( * RDFQueryEnums.RDFComparisonFlavors.LessThan, y, new RDFPlainLiteral("25"))); // ?Y < "25" * * // CREATE SELECT QUERY * RDFSelectQuery selectQuery = new RDFSelectQuery(); * * * // ADD PATTERN GROUPS TO QUERY * selectQuery.AddPatternGroup(pg1); * * //y_dogOf_x.Optional(); * //y_dogOf_x.UnionWithNext(); * * // ADD MODIFIERS TO QUERY * selectQuery.AddModifier(new RDFDistinctModifier()); * selectQuery.AddModifier(new RDFOrderByModifier(x, RDFQueryEnums.RDFOrderByFlavors.DESC)); * selectQuery.AddModifier(new RDFOrderByModifier(n, RDFQueryEnums.RDFOrderByFlavors.ASC)); * selectQuery.AddModifier(new RDFLimitModifier(100)); * selectQuery.AddModifier(new RDFOffsetModifier(25)); */ /* * // APPLY SELECT QUERY TO GRAPH * RDFSelectQueryResult selectQueryResult1 = selectQuery.ApplyToGraph(graph); * // APPLY SELECT QUERY TO STORE * RDFSelectQueryResult selectQueryResult2 = selectQuery.ApplyToStore(store); * // APPLY SELECT QUERY TO FEDERATION * RDFSelectQueryResult selectQueryResult3 = selectQuery.ApplyToFederation(federation); * * * // CREATE ASK QUERY * RDFAskQuery askQuery = new RDFAskQuery(); * * // APPLY ASK QUERY TO GRAPH * RDFAskQueryResult askQueryResult1 = askQuery.ApplyToGraph(graph); * // APPLY ASK QUERY TO STORE * RDFAskQueryResult askQueryResult2 = askQuery.ApplyToStore(store); * // APPLY ASK QUERY TO FEDERATION * RDFAskQueryResult askQueryResult3 = askQuery.ApplyToFederation(federation); * * * // CREATE CONSTRUCT QUERY * RDFConstructQuery constructQuery = new RDFConstructQuery(); * * // APPLY CONSTRUCT QUERY TO GRAPH * RDFConstructQueryResult constructResult1 = constructQuery.ApplyToGraph(graph); * // APPLY CONSTRUCT QUERY TO STORE * RDFConstructQueryResult constructResult2 = constructQuery.ApplyToStore(store); * // APPLY CONSTRUCT QUERY TO FEDERATION * RDFConstructQueryResult constructResult3 = constructQuery.ApplyToFederation(federation); * * // CREATE DESCRIBE QUERY * RDFDescribeQuery describeQuery = new RDFDescribeQuery(); * * // APPLY DESCRIBE QUERY TO GRAPH * RDFDescribeQueryResult describeQueryResult1 = describeQuery.ApplyToGraph(graph); * // APPLY DESCRIBE QUERY TO STORE * RDFDescribeQueryResult describeQueryResult2 = describeQuery.ApplyToStore(store); * // APPLY DESCRIBE QUERY TO FEDERATION * RDFDescribeQueryResult describeQueryResult3 = describeQuery.ApplyToFederation(federation); */ }
/// <summary> /// Checks if the store contains the given quadruple /// </summary> public override Boolean ContainsQuadruple(RDFQuadruple quadruple) { RDFMemoryStore result = new RDFMemoryStore(); if (quadruple != null) { try { //Open connection this.Connection.Open(); //Modify command this.Commands["SELECT"].CommandText += " WHERE quadrupleid = @QID"; //Add parameters this.Commands["SELECT"].Parameters.Add(new NpgsqlParameter("QID", NpgsqlDbType.Bigint)); //Valorize parameters this.Commands["SELECT"].Parameters["QID"].Value = quadruple.QuadrupleID; //Execute command using (NpgsqlDataReader fetchedQuadruples = this.Commands["SELECT"].ExecuteReader()) { if (fetchedQuadruples.HasRows) { //Iterate fetched quadruples while (fetchedQuadruples.Read()) { //Add the fetched quadruple to the result result.AddQuadruple(RDFStoreUtilities.ParseQuadruple(fetchedQuadruples)); } } } //Close connection this.Connection.Close(); //Restore command this.Commands["SELECT"].CommandText = "SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\""; //Restore parameters this.Commands["SELECT"].Parameters.Clear(); } catch (Exception ex) { //Close connection this.Connection.Close(); //Restore command this.Commands["SELECT"].CommandText = "SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\""; //Restore parameters this.Commands["SELECT"].Parameters.Clear(); //Propagate exception throw new RDFStoreException("Cannot read data from PostgreSQL store because: " + ex.Message); } } return (result.QuadruplesCount > 0); }
/// <summary> /// Adds the given quadruple to the store, avoiding duplicate insertions /// </summary> public override RDFStore AddQuadruple(RDFQuadruple quadruple) { if (quadruple != null) { //Create command var command = new SqlCommand("IF NOT EXISTS(SELECT 1 FROM [dbo].[Quadruples] WHERE [QuadrupleID] = @QID) BEGIN INSERT INTO [dbo].[Quadruples]([QuadrupleID], [TripleFlavor], [Context], [ContextID], [Subject], [SubjectID], [Predicate], [PredicateID], [Object], [ObjectID]) VALUES (@QID, @TFV, @CTX, @CTXID, @SUBJ, @SUBJID, @PRED, @PREDID, @OBJ, @OBJID) END", this.Connection); command.Parameters.Add(new SqlParameter("QID", SqlDbType.BigInt)); command.Parameters.Add(new SqlParameter("TFV", SqlDbType.Int)); command.Parameters.Add(new SqlParameter("CTX", SqlDbType.VarChar, 1000)); command.Parameters.Add(new SqlParameter("CTXID", SqlDbType.BigInt)); command.Parameters.Add(new SqlParameter("SUBJ", SqlDbType.VarChar, 1000)); command.Parameters.Add(new SqlParameter("SUBJID", SqlDbType.BigInt)); command.Parameters.Add(new SqlParameter("PRED", SqlDbType.VarChar, 1000)); command.Parameters.Add(new SqlParameter("PREDID", SqlDbType.BigInt)); command.Parameters.Add(new SqlParameter("OBJ", SqlDbType.VarChar, 5000)); command.Parameters.Add(new SqlParameter("OBJID", SqlDbType.BigInt)); //Valorize parameters command.Parameters["QID"].Value = quadruple.QuadrupleID; command.Parameters["TFV"].Value = quadruple.TripleFlavor; command.Parameters["CTX"].Value = quadruple.Context.ToString(); command.Parameters["CTXID"].Value = quadruple.Context.PatternMemberID; command.Parameters["SUBJ"].Value = quadruple.Subject.ToString(); command.Parameters["SUBJID"].Value = quadruple.Subject.PatternMemberID; command.Parameters["PRED"].Value = quadruple.Predicate.ToString(); command.Parameters["PREDID"].Value = quadruple.Predicate.PatternMemberID; command.Parameters["OBJ"].Value = quadruple.Object.ToString(); command.Parameters["OBJID"].Value = quadruple.Object.PatternMemberID; try { //Open connection this.Connection.Open(); //Prepare command command.Prepare(); //Open transaction command.Transaction = this.Connection.BeginTransaction(); //Execute command command.ExecuteNonQuery(); //Close transaction command.Transaction.Commit(); //Close connection this.Connection.Close(); } catch (Exception ex) { //Rollback transaction command.Transaction.Rollback(); //Close connection this.Connection.Close(); //Propagate exception throw new RDFStoreException("Cannot insert data into SQL Server store because: " + ex.Message, ex); } } return this; }
/// <summary> /// Merges the given graph into the store, avoiding duplicate insertions /// </summary> public override RDFStore MergeGraph(RDFGraph graph) { if (graph != null) { try { //Open connection this.Connection.Open(); //Open transaction this.Commands["INSERT"].Transaction = this.Commands["INSERT"].Connection.BeginTransaction(); //Prepare command this.Commands["INSERT"].Prepare(); //Iterate graph triples RDFContext graphCtx = new RDFContext(graph.Context); RDFQuadruple quadruple = null; foreach(RDFTriple t in graph) { if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) { quadruple = new RDFQuadruple(graphCtx, (RDFResource)t.Subject, (RDFResource)t.Predicate, (RDFResource)t.Object); } else { quadruple = new RDFQuadruple(graphCtx, (RDFResource)t.Subject, (RDFResource)t.Predicate, (RDFLiteral)t.Object); } //Valorize parameters this.Commands["INSERT"].Parameters["QID"].Value = quadruple.QuadrupleID; this.Commands["INSERT"].Parameters["TFV"].Value = quadruple.TripleFlavor; this.Commands["INSERT"].Parameters["CTX"].Value = quadruple.Context.ToString(); this.Commands["INSERT"].Parameters["CTXID"].Value = quadruple.Context.PatternMemberID; this.Commands["INSERT"].Parameters["SUBJ"].Value = quadruple.Subject.ToString(); this.Commands["INSERT"].Parameters["SUBJID"].Value = quadruple.Subject.PatternMemberID; this.Commands["INSERT"].Parameters["PRED"].Value = quadruple.Predicate.ToString(); this.Commands["INSERT"].Parameters["PREDID"].Value = quadruple.Predicate.PatternMemberID; this.Commands["INSERT"].Parameters["OBJ"].Value = quadruple.Object.ToString(); this.Commands["INSERT"].Parameters["OBJID"].Value = quadruple.Object.PatternMemberID; //Execute command this.Commands["INSERT"].ExecuteNonQuery(); } //Close transaction this.Commands["INSERT"].Transaction.Commit(); //Close connection this.Connection.Close(); } catch (Exception ex) { //Close transaction this.Commands["INSERT"].Transaction.Rollback(); //Close connection this.Connection.Close(); //Propagate exception throw new RDFStoreException("Cannot insert data into SQLite store because: " + ex.Message); } } return this; }
/// <summary> /// Adds the given quadruple to the store, avoiding duplicate insertions /// </summary> public override RDFStore AddQuadruple(RDFQuadruple quadruple) { if (quadruple != null) { try { //Open connection this.Connection.Open(); //Open transaction this.Commands["INSERT"].Transaction = this.Commands["INSERT"].Connection.BeginTransaction(); //Prepare command this.Commands["INSERT"].Prepare(); //Valorize parameters this.Commands["INSERT"].Parameters["QID"].Value = quadruple.QuadrupleID; this.Commands["INSERT"].Parameters["TFV"].Value = quadruple.TripleFlavor; this.Commands["INSERT"].Parameters["CTX"].Value = quadruple.Context.ToString(); this.Commands["INSERT"].Parameters["CTXID"].Value = quadruple.Context.PatternMemberID; this.Commands["INSERT"].Parameters["SUBJ"].Value = quadruple.Subject.ToString(); this.Commands["INSERT"].Parameters["SUBJID"].Value = quadruple.Subject.PatternMemberID; this.Commands["INSERT"].Parameters["PRED"].Value = quadruple.Predicate.ToString(); this.Commands["INSERT"].Parameters["PREDID"].Value = quadruple.Predicate.PatternMemberID; this.Commands["INSERT"].Parameters["OBJ"].Value = quadruple.Object.ToString(); this.Commands["INSERT"].Parameters["OBJID"].Value = quadruple.Object.PatternMemberID; //Execute command this.Commands["INSERT"].ExecuteNonQuery(); //Close transaction this.Commands["INSERT"].Transaction.Commit(); //Close connection this.Connection.Close(); } catch (Exception ex) { //Close transaction this.Commands["INSERT"].Transaction.Rollback(); //Close connection this.Connection.Close(); //Propagate exception throw new RDFStoreException("Cannot insert data into SQLite store because: " + ex.Message); } } return this; }
/// <summary> /// Adds the given quadruple to the store, avoiding duplicate insertions /// </summary> public override RDFStore AddQuadruple(RDFQuadruple quadruple) { if (quadruple != null) { //Create command var command = new MySqlCommand("INSERT IGNORE INTO Quadruples(QuadrupleID, TripleFlavor, Context, ContextID, Subject, SubjectID, Predicate, PredicateID, Object, ObjectID) VALUES (@QID, @TFV, @CTX, @CTXID, @SUBJ, @SUBJID, @PRED, @PREDID, @OBJ, @OBJID)", this.Connection); command.Parameters.Add(new MySqlParameter("QID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("CTX", MySqlDbType.VarChar, 1000)); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("SUBJ", MySqlDbType.VarChar, 1000)); command.Parameters.Add(new MySqlParameter("SUBJID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("PRED", MySqlDbType.VarChar, 1000)); command.Parameters.Add(new MySqlParameter("PREDID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJ", MySqlDbType.VarChar, 5000)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); //Valorize parameters command.Parameters["QID"].Value = quadruple.QuadrupleID; command.Parameters["TFV"].Value = quadruple.TripleFlavor; command.Parameters["CTX"].Value = quadruple.Context.ToString(); command.Parameters["CTXID"].Value = quadruple.Context.PatternMemberID; command.Parameters["SUBJ"].Value = quadruple.Subject.ToString(); command.Parameters["SUBJID"].Value = quadruple.Subject.PatternMemberID; command.Parameters["PRED"].Value = quadruple.Predicate.ToString(); command.Parameters["PREDID"].Value = quadruple.Predicate.PatternMemberID; command.Parameters["OBJ"].Value = quadruple.Object.ToString(); command.Parameters["OBJID"].Value = quadruple.Object.PatternMemberID; try { //Open connection this.Connection.Open(); //Prepare command command.Prepare(); //Open transaction command.Transaction = this.Connection.BeginTransaction(); //Execute command command.ExecuteNonQuery(); //Close transaction command.Transaction.Commit(); //Close connection this.Connection.Close(); } catch (Exception ex) { //Rollback transaction command.Transaction.Rollback(); //Close connection this.Connection.Close(); //Propagate exception throw new RDFStoreException("Cannot insert data into MySQL store because: " + ex.Message, ex); } } return this; }
/// <summary> /// Checks if the store contains the given quadruple /// </summary> public override Boolean ContainsQuadruple(RDFQuadruple quadruple) { RDFMemoryStore result = new RDFMemoryStore(); if (quadruple != null) { try { //Open connection this.Connection.Open(); //Modify command this.Commands["SELECT"].CommandText += " WHERE QuadrupleID = @QID"; //Add parameters this.Commands["SELECT"].Parameters.Add(new SQLiteParameter("QID", DbType.Int64)); //Prepare command this.Commands["SELECT"].Prepare(); //Valorize parameters this.Commands["SELECT"].Parameters["QID"].Value = quadruple.QuadrupleID; //Execute command using (SQLiteDataReader fetchedQuadruples = this.Commands["SELECT"].ExecuteReader()) { if (fetchedQuadruples.HasRows) { //Iterate fetched quadruples while (fetchedQuadruples.Read()) { //Add the fetched quadruple to the result result.AddQuadruple(RDFStoreUtilities.ParseQuadruple(fetchedQuadruples)); } } } //Close connection this.Connection.Close(); //Restore command this.Commands["SELECT"].CommandText = "SELECT TripleFlavor, Context, Subject, Predicate, Object FROM main.Quadruples"; //Restore parameters this.Commands["SELECT"].Parameters.Clear(); } catch (Exception ex) { //Close connection this.Connection.Close(); //Restore command this.Commands["SELECT"].CommandText = "SELECT TripleFlavor, Context, Subject, Predicate, Object FROM main.Quadruples"; //Restore parameters this.Commands["SELECT"].Parameters.Clear(); //Propagate exception throw new RDFStoreException("Cannot read data from SQLite store because: " + ex.Message); } } return (result.QuadruplesCount > 0); }
/// <summary> /// Removes the given quadruples from the store /// </summary> public override RDFStore RemoveQuadruple(RDFQuadruple quadruple) { if (quadruple != null) { //Create command var command = new MySqlCommand("DELETE FROM Quadruples WHERE QuadrupleID = @QID", this.Connection); command.Parameters.Add(new MySqlParameter("QID", MySqlDbType.Int64)); //Valorize parameters command.Parameters["QID"].Value = quadruple.QuadrupleID; try { //Open connection this.Connection.Open(); //Prepare command command.Prepare(); //Open transaction command.Transaction = this.Connection.BeginTransaction(); //Execute command command.ExecuteNonQuery(); //Close transaction command.Transaction.Commit(); //Close connection this.Connection.Close(); } catch (Exception ex) { //Rollback transaction command.Transaction.Rollback(); //Close connection this.Connection.Close(); //Propagate exception throw new RDFStoreException("Cannot delete data from MySQL store because: " + ex.Message, ex); } } return this; }