/// <summary> /// SPO-flavor ctor /// </summary> public RDFQuadruple(RDFContext context, RDFResource subj, RDFResource pred, RDFResource obj) { //TripleFlavor this.TripleFlavor = RDFModelEnums.RDFTripleFlavors.SPO; //Context this.Context = (context ?? new RDFContext()); //Subject this.Subject = (subj ?? new RDFResource()); //Predicate if (pred != null) { if (pred.IsBlank) { throw new RDFStoreException("Cannot create RDFQuadruple because given \"pred\" parameter is a blank resource"); } this.Predicate = pred; } else { throw new RDFStoreException("Cannot create RDFQuadruple because given \"pred\" parameter is null"); } //Object this.Object = (obj ?? new RDFResource()); //QuadrupleID this.QuadrupleID = RDFModelUtilities.CreateHash(this.ToString()); }
/// <summary> /// SPO-flavor ctor /// </summary> public RDFQuadruple(RDFContext context, RDFResource subj, RDFResource pred, RDFResource obj) { //TripleFlavor this.TripleFlavor = RDFModelEnums.RDFTripleFlavors.SPO; //Context this.Context = context ?? new RDFContext(); //Subject this.Subject = subj ?? new RDFResource(); //Predicate if (pred != null) { if (pred.IsBlank) { throw new RDFStoreException("Cannot create RDFQuadruple because given \"pred\" parameter is a blank resource"); } this.Predicate = pred; } else { throw new RDFStoreException("Cannot create RDFQuadruple because given \"pred\" parameter is null"); } //Object this.Object = obj ?? new RDFResource(); //LazyQuadrupleID this.LazyQuadrupleID = new Lazy <long>(() => RDFModelUtilities.CreateHash(this.ToString())); //LazyReificationSubject this.LazyReificationSubject = new Lazy <RDFResource>(() => new RDFResource(string.Concat("bnode:", this.QuadrupleID.ToString()))); }
/// <summary> /// Gets a store containing quadruples satisfying the given pattern /// </summary> internal override RDFMemoryStore SelectQuadruples(RDFContext contextResource, RDFResource subjectResource, RDFResource predicateResource, RDFResource objectResource, RDFLiteral objectLiteral) { return(new RDFMemoryStore(RDFStoreUtilities.SelectQuadruples(this, contextResource, subjectResource, predicateResource, objectResource, objectLiteral))); }
/// <summary> /// Selects the quadruples indexed by the given context /// </summary> internal HashSet <Int64> SelectIndexByContext(RDFContext contextResource) { if (contextResource != null) { if (this.Contexts.ContainsKey(contextResource.PatternMemberID)) { return(this.Contexts[contextResource.PatternMemberID]); } } return(new HashSet <Int64>()); }
/// <summary> /// Removes the quadruples with the given context /// </summary> public override RDFStore RemoveQuadruplesByContext(RDFContext contextResource) { if (contextResource != null) { foreach (var quadruple in this.SelectQuadruplesByContext(contextResource)) { this.Quadruples.Remove(quadruple.QuadrupleID); this.StoreIndex.RemoveIndex(quadruple); } } return(this); }
/// <summary> /// Selects the quadruples indexed by the given context /// </summary> internal Dictionary <Int64, Object> SelectIndexByContext(RDFContext contextResource) { var result = new Dictionary <Int64, Object>(); if (contextResource != null) { if (this.Contexts.ContainsKey(contextResource.PatternMemberID)) { result = this.Contexts[contextResource.PatternMemberID]; } } return(result); }
/// <summary> /// Removes the quadruples with the given context /// </summary> public override RDFStore RemoveQuadruplesByContext(RDFContext contextResource) { if (contextResource != null) { foreach (RDFQuadruple quad in this.SelectQuadruplesByContext(contextResource)) { //Remove quadruple this.Quadruples.Remove(quad.QuadrupleID); //Remove index this.StoreIndex.RemoveIndex(quad); } } return(this); }
/// <summary> /// Removes the quadruples with the given context and predicate /// </summary> public override RDFStore RemoveQuadruplesByContextPredicate(RDFContext contextResource, RDFResource predicateResource) { if (contextResource != null && predicateResource != null) { foreach (var quad in this.SelectQuadruplesByContext(contextResource) .SelectQuadruplesByPredicate(predicateResource)) { //Remove quadruple this.Quadruples.Remove(quad.QuadrupleID); //Remove index this.StoreIndex.RemoveIndex(quad); } } return(this); }
/// <summary> /// Removes the quadruples with the given context and literal /// </summary> public override RDFStore RemoveQuadruplesByContextLiteral(RDFContext contextResource, RDFLiteral objectLiteral) { if (contextResource != null && objectLiteral != null) { foreach (var quad in this.SelectQuadruplesByContext(contextResource) .SelectQuadruplesByLiteral(objectLiteral)) { //Remove quadruple this.Quadruples.Remove(quad.QuadrupleID); //Remove index this.StoreIndex.RemoveIndex(quad); } } return(this); }
/// <summary> /// Parses the current quadruple of the data reader /// </summary> internal static RDFQuadruple ParseQuadruple(IDataReader fetchedQuadruples) { if (fetchedQuadruples != null) { RDFContext qContext = new RDFContext(fetchedQuadruples["Context"].ToString()); RDFResource qSubject = new RDFResource(fetchedQuadruples["Subject"].ToString()); RDFResource qPredicate = new RDFResource(fetchedQuadruples["Predicate"].ToString()); //SPO-flavour quadruple if (fetchedQuadruples["TripleFlavor"].ToString().Equals("1")) { RDFResource qObject = new RDFResource(fetchedQuadruples["Object"].ToString()); return(new RDFQuadruple(qContext, qSubject, qPredicate, qObject)); } //SPL-flavour quadruple string literal = fetchedQuadruples["Object"].ToString(); //PlainLiteral int lastIndexOfDatatype = literal.LastIndexOf("^^", StringComparison.OrdinalIgnoreCase); int lastIndexOfLanguage = literal.LastIndexOf("@", StringComparison.OrdinalIgnoreCase); if (!literal.Contains("^^") || literal.EndsWith("^^") || RDFModelUtilities.GetUriFromString(literal.Substring(lastIndexOfDatatype + 2)) == null) { RDFPlainLiteral pLit = null; if (RDFNTriples.regexLPL.Match(literal).Success) { string pLitValue = literal.Substring(0, lastIndexOfLanguage); string pLitLang = literal.Substring(lastIndexOfLanguage + 1); pLit = new RDFPlainLiteral(pLitValue, pLitLang); } else { pLit = new RDFPlainLiteral(literal); } return(new RDFQuadruple(qContext, qSubject, qPredicate, pLit)); } //TypedLiteral string tLitValue = literal.Substring(0, lastIndexOfDatatype); string tLitDatatype = literal.Substring(lastIndexOfDatatype + 2); RDFModelEnums.RDFDatatypes dt = RDFModelUtilities.GetDatatypeFromString(tLitDatatype); RDFTypedLiteral tLit = new RDFTypedLiteral(tLitValue, dt); return(new RDFQuadruple(qContext, qSubject, qPredicate, tLit)); } throw new RDFStoreException("Cannot parse quadruple because given \"fetchedQuadruples\" parameter is null."); }
/// <summary> /// Removes the quadruples with the given context /// </summary> public override RDFStore RemoveQuadruplesByContext(RDFContext contextResource) { if (contextResource != null) { foreach (var quad in this.SelectQuadruplesByContext(contextResource)) { //Remove quadruple this.Quadruples.Remove(quad.QuadrupleID); //Remove index this.StoreIndex.RemoveIndex(quad); //Raise event RDFStoreEvents.RaiseOnQuadrupleRemoved(String.Format("Quadruple '{0}' has been removed from the Store '{1}'.", quad, this)); } } return(this); }
/// <summary> /// Removes the quadruples with the given context, subject and object /// </summary> public override RDFStore RemoveQuadruplesByContextSubjectObject(RDFContext contextResource, RDFResource subjectResource, RDFResource objectResource) { if (contextResource != null && subjectResource != null && objectResource != null) { foreach (var quad in this.SelectQuadruplesByContext(contextResource) .SelectQuadruplesBySubject(subjectResource) .SelectQuadruplesByObject(objectResource)) { //Remove quadruple this.Quadruples.Remove(quad.QuadrupleID); //Remove index this.StoreIndex.RemoveIndex(quad); } } return(this); }
/// <summary> /// Removes the quadruples with the given context, predicate and literal /// </summary> public override RDFStore RemoveQuadruplesByContextPredicateLiteral(RDFContext contextResource, RDFResource predicateResource, RDFLiteral objectLiteral) { if (contextResource != null && predicateResource != null && objectLiteral != null) { foreach (var quad in this.SelectQuadruplesByContext(contextResource) .SelectQuadruplesByPredicate(predicateResource) .SelectQuadruplesByLiteral(objectLiteral)) { //Remove quadruple this.Quadruples.Remove(quad.QuadrupleID); //Remove index this.StoreIndex.RemoveIndex(quad); //Raise event RDFStoreEvents.RaiseOnQuadrupleRemoved(String.Format("Quadruple '{0}' has been removed from the Store '{1}'.", quad, this)); } } return(this); }
/// <summary> /// Parses the current quadruple of the data reader /// </summary> public static RDFQuadruple ParseQuadruple(IDataReader fetchedQuadruples) { if (fetchedQuadruples != null) { RDFContext qContext = new RDFContext(fetchedQuadruples["Context"].ToString()); RDFResource qSubject = new RDFResource(fetchedQuadruples["Subject"].ToString()); RDFResource qPredicate = new RDFResource(fetchedQuadruples["Predicate"].ToString()); //SPO-flavour quadruple if (fetchedQuadruples["TripleFlavor"].ToString() == "1") { RDFResource qObject = new RDFResource(fetchedQuadruples["Object"].ToString()); return new RDFQuadruple(qContext, qSubject, qPredicate, qObject); } //SPL-flavour quadruple String literal = fetchedQuadruples["Object"].ToString(); //PlainLiteral if (!literal.Contains("^^") || literal.EndsWith("^^") || RDFModelUtilities.GetUriFromString(literal.Substring(literal.LastIndexOf("^^", StringComparison.Ordinal) + 2)) == null) { RDFPlainLiteral pLit = null; if (literal.Contains("@")) { if (!literal.EndsWith("@")) { Int32 lastAmp = literal.LastIndexOf('@'); pLit = new RDFPlainLiteral(literal.Substring(0, lastAmp), literal.Substring(lastAmp + 1)); } else { pLit = new RDFPlainLiteral(literal); } } else { pLit = new RDFPlainLiteral(literal); } return new RDFQuadruple(qContext, qSubject, qPredicate, pLit); } //TypedLiteral String tLitValue = literal.Substring(0, literal.LastIndexOf("^^", StringComparison.Ordinal)); String tLitDatatype = literal.Substring(literal.LastIndexOf("^^", StringComparison.Ordinal) + 2); RDFDatatype dt = RDFModelUtilities.GetDatatypeFromString(tLitDatatype); RDFTypedLiteral tLit = new RDFTypedLiteral(tLitValue, dt); return new RDFQuadruple(qContext, qSubject, qPredicate, tLit); } throw new RDFStoreException("Cannot parse quadruple because given \"fetchedQuadruples\" parameter is null."); }
/// <summary> /// Merges the given graph into the store, avoiding duplicate insertions /// </summary> public override RDFStore MergeGraph(RDFGraph graph) { if (graph != null) { RDFContext graphCtx = new RDFContext(graph.Context); foreach (RDFTriple t in graph) { if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) { this.AddQuadruple(new RDFQuadruple(graphCtx, (RDFResource)t.Subject, (RDFResource)t.Predicate, (RDFResource)t.Object)); } else { this.AddQuadruple(new RDFQuadruple(graphCtx, (RDFResource)t.Subject, (RDFResource)t.Predicate, (RDFLiteral)t.Object)); } } } return(this); }
/// <summary> /// Selects the quadruples indexed by the given context /// </summary> internal HashSet<Int64> SelectIndexByContext(RDFContext contextResource) { if (contextResource != null) { if (this.Contexts.ContainsKey(contextResource.PatternMemberID)) { return this.Contexts[contextResource.PatternMemberID]; } } return new HashSet<Int64>(); }
/// <summary> /// Removes the quadruples with the given context, subject and literal /// </summary> public abstract RDFStore RemoveQuadruplesByContextSubjectLiteral(RDFContext contextResource, RDFResource subjectResource, RDFLiteral objectLiteral);
/// <summary> /// Removes the quadruples with the given context /// </summary> public abstract RDFStore RemoveQuadruplesByContext(RDFContext contextResource);
/// <summary> /// Gets a memory store containing quadruples with the specified context /// </summary> public RDFMemoryStore SelectQuadruplesByContext(RDFContext contextResource) { return(this.SelectQuadruples(contextResource, null, null, null, null)); }
/// <summary> /// Removes the quadruples with the given context /// </summary> public override RDFStore RemoveQuadruplesByContext(RDFContext contextResource) { if (contextResource != null) { try { //Modify command this.Commands["DELETE"].CommandText = "DELETE FROM main.Quadruples WHERE ContextID = @CTXID"; this.Commands["DELETE"].Parameters.Clear(); //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.Add(new SQLiteParameter("CTXID", DbType.Int64)); this.Commands["DELETE"].Parameters["CTXID"].Value = contextResource.PatternMemberID; //Execute command this.Commands["DELETE"].ExecuteNonQuery(); //Close transaction this.Commands["DELETE"].Transaction.Commit(); //Close connection this.Connection.Close(); //Restore command this.Commands["DELETE"].CommandText = "DELETE FROM main.Quadruples WHERE QuadrupleID = @QID"; this.Commands["DELETE"].Parameters.Clear(); this.Commands["DELETE"].Parameters.Add(new SQLiteParameter("QID", DbType.Int64)); } catch (Exception ex) { //Close transaction this.Commands["DELETE"].Transaction.Rollback(); //Close connection this.Connection.Close(); //Restore command this.Commands["DELETE"].CommandText = "DELETE FROM main.Quadruples WHERE QuadrupleID = @QID"; this.Commands["DELETE"].Parameters.Clear(); this.Commands["DELETE"].Parameters.Add(new SQLiteParameter("QID", DbType.Int64)); //Propagate exception throw new RDFStoreException("Cannot delete data from SQLite store because: " + ex.Message); } } return this; }
/// <summary> /// Deserializes the given N-Quads stream to a memory store. /// </summary> internal static RDFMemoryStore Deserialize(Stream inputStream) { Int64 nquadIndex = 0; try { #region deserialize using (StreamReader sr = new StreamReader(inputStream)) { RDFMemoryStore result = new RDFMemoryStore(); String nquad = String.Empty; String[] tokens = new String[4]; RDFResource S = null; RDFResource P = null; RDFResource O = null; RDFLiteral L = null; RDFContext C = new RDFContext(); while ((nquad = sr.ReadLine()) != null) { nquadIndex++; #region sanitize & tokenize //Cleanup previous data S = null; tokens[0] = String.Empty; P = null; tokens[1] = String.Empty; O = null; L = null; tokens[2] = String.Empty; C = new RDFContext(); tokens[3] = String.Empty; //Preliminary sanitizations: clean trailing space-like chars nquad = nquad.Trim(new Char[] { ' ', '\t', '\r', '\n' }); //Skip empty or comment lines if (nquad == String.Empty || nquad.StartsWith("#")) { continue; } //Tokenizes the sanitized quad tokens = TokenizeNQuad(nquad); #endregion #region subj String subj = tokens[0].TrimStart(new Char[] { '<' }) .TrimEnd(new Char[] { '>' }) .Replace("_:", "bnode:"); S = new RDFResource(RDFModelUtilities.ASCII_To_Unicode(subj)); #endregion #region pred String pred = tokens[1].TrimStart(new Char[] { '<' }) .TrimEnd(new Char[] { '>' }); P = new RDFResource(RDFModelUtilities.ASCII_To_Unicode(pred)); #endregion #region object if (tokens[2].StartsWith("<") || tokens[2].StartsWith("bnode:") || tokens[2].StartsWith("_:")) { String obj = tokens[2].TrimStart(new Char[] { '<' }) .TrimEnd(new Char[] { '>' }) .Replace("_:", "bnode:") .Trim(new Char[] { ' ', '\n', '\t', '\r' }); O = new RDFResource(RDFModelUtilities.ASCII_To_Unicode(obj)); } #endregion #region literal else { #region sanitize tokens[2] = RDFNTriples.regexSqt.Replace(tokens[2], String.Empty); tokens[2] = RDFNTriples.regexEqt.Replace(tokens[2], String.Empty); tokens[2] = tokens[2].Replace("\\\"", "\"") .Replace("\\n", "\n") .Replace("\\t", "\t") .Replace("\\r", "\r"); tokens[2] = RDFModelUtilities.ASCII_To_Unicode(tokens[2]); #endregion #region plain literal if (!tokens[2].Contains("^^") || tokens[2].EndsWith("^^") || tokens[2].Substring(tokens[2].LastIndexOf("^^", StringComparison.Ordinal) + 2, 1) != "<") { if (RDFNTriples.regexLPL.Match(tokens[2]).Success) { tokens[2] = tokens[2].Replace("\"@", "@"); String pLitValue = tokens[2].Substring(0, tokens[2].LastIndexOf("@", StringComparison.Ordinal)); String pLitLang = tokens[2].Substring(tokens[2].LastIndexOf("@", StringComparison.Ordinal) + 1); L = new RDFPlainLiteral(HttpUtility.HtmlDecode(pLitValue), pLitLang); } else { L = new RDFPlainLiteral(HttpUtility.HtmlDecode(tokens[2])); } } #endregion #region typed literal else { tokens[2] = tokens[2].Replace("\"^^", "^^"); String tLitValue = tokens[2].Substring(0, tokens[2].LastIndexOf("^^", StringComparison.Ordinal)); String tLitDatatype = tokens[2].Substring(tokens[2].LastIndexOf("^^", StringComparison.Ordinal) + 2) .TrimStart(new Char[] { '<' }) .TrimEnd(new Char[] { '>' }); RDFModelEnums.RDFDatatype dt = RDFModelUtilities.GetDatatypeFromString(tLitDatatype); L = new RDFTypedLiteral(HttpUtility.HtmlDecode(tLitValue), dt); } #endregion } #endregion #region context if (!String.IsNullOrEmpty(tokens[3])) { String ctx = tokens[3].TrimStart(new Char[] { '<' }) .TrimEnd(new Char[] { '>' }); Uri ctxUri = null; if (Uri.TryCreate(ctx, UriKind.Absolute, out ctxUri)) { C = new RDFContext(RDFModelUtilities.ASCII_To_Unicode(ctxUri.ToString())); } else { throw new RDFModelException("found context '" + ctx + "' which is not a well-formed absolute Uri"); } } #endregion #region addquadruple if (O != null) { result.AddQuadruple(new RDFQuadruple(C, S, P, O)); } else { result.AddQuadruple(new RDFQuadruple(C, S, P, L)); } #endregion } return(result); } #endregion } catch (Exception ex) { throw new RDFModelException("Cannot deserialize N-Quads (line " + nquadIndex + ") because: " + ex.Message, ex); } }
/// <summary> /// Deserializes the given N-Quads stream to a memory store. /// </summary> internal static RDFMemoryStore Deserialize(Stream inputStream) { Int64 nquadIndex = 0; try { #region deserialize using (StreamReader sr = new StreamReader(inputStream, Encoding.ASCII)) { RDFMemoryStore result = new RDFMemoryStore(); String nquad = String.Empty; String[] tokens = new String[4]; RDFResource S = null; RDFResource P = null; RDFResource O = null; RDFLiteral L = null; RDFContext C = new RDFContext(); while((nquad = sr.ReadLine()) != null) { nquadIndex++; #region sanitize & tokenize //Cleanup previous data S = null; tokens[0] = String.Empty; P = null; tokens[1] = String.Empty; O = null; L = null; tokens[2] = String.Empty; C = new RDFContext(); tokens[3] = String.Empty; //Preliminary sanitizations: clean trailing space-like chars nquad = nquad.Trim(new Char[] { ' ', '\t', '\r', '\n' }); //Skip empty or comment lines if (nquad == String.Empty || nquad.StartsWith("#")) { continue; } //Tokenizes the sanitized quad tokens = TokenizeNQuad(nquad); #endregion #region subj String subj = tokens[0].TrimStart(new Char[] { '<' }) .TrimEnd(new Char[] { '>' }) .Replace("_:", "bnode:"); S = new RDFResource(RDFModelUtilities.ASCII_To_Unicode(subj)); #endregion #region pred String pred = tokens[1].TrimStart(new Char[] { '<' }) .TrimEnd(new Char[] { '>' }); P = new RDFResource(RDFModelUtilities.ASCII_To_Unicode(pred)); #endregion #region object if (tokens[2].StartsWith("<") || tokens[2].StartsWith("bnode:") || tokens[2].StartsWith("_:")) { String obj = tokens[2].TrimStart(new Char[] { '<' }) .TrimEnd(new Char[] { '>' }) .Replace("_:", "bnode:") .Trim(new Char[] { ' ', '\n', '\t', '\r' }); O = new RDFResource(RDFModelUtilities.ASCII_To_Unicode(obj)); } #endregion #region literal else { #region sanitize tokens[2] = RDFNTriples.regexSqt.Replace(tokens[2], String.Empty); tokens[2] = RDFNTriples.regexEqt.Replace(tokens[2], String.Empty); tokens[2] = tokens[2].Replace("\\\\", "\\") .Replace("\\\"", "\"") .Replace("\\n", "\n") .Replace("\\t", "\t") .Replace("\\r", "\r"); tokens[2] = RDFModelUtilities.ASCII_To_Unicode(tokens[2]); #endregion #region plain literal if (!tokens[2].Contains("^^") || tokens[2].EndsWith("^^") || tokens[2].Substring(tokens[2].LastIndexOf("^^", StringComparison.Ordinal) + 2, 1) != "<") { if (RDFNTriples.regexLPL.Match(tokens[2]).Success) { tokens[2] = tokens[2].Replace("\"@", "@"); String pLitValue = tokens[2].Substring(0, tokens[2].LastIndexOf("@", StringComparison.Ordinal)); String pLitLang = tokens[2].Substring(tokens[2].LastIndexOf("@", StringComparison.Ordinal) + 1); L = new RDFPlainLiteral(HttpUtility.HtmlDecode(pLitValue), pLitLang); } else { L = new RDFPlainLiteral(HttpUtility.HtmlDecode(tokens[2])); } } #endregion #region typed literal else { tokens[2] = tokens[2].Replace("\"^^", "^^"); String tLitValue = tokens[2].Substring(0, tokens[2].LastIndexOf("^^", StringComparison.Ordinal)); String tLitDatatype = tokens[2].Substring(tokens[2].LastIndexOf("^^", StringComparison.Ordinal) + 2) .TrimStart(new Char[] { '<' }) .TrimEnd(new Char[] { '>' }); RDFModelEnums.RDFDatatypes dt = RDFModelUtilities.GetDatatypeFromString(tLitDatatype); L = new RDFTypedLiteral(HttpUtility.HtmlDecode(tLitValue), dt); } #endregion } #endregion #region context if (!String.IsNullOrEmpty(tokens[3])) { String ctx = tokens[3].TrimStart(new Char[] { '<' }) .TrimEnd(new Char[] { '>' }); Uri ctxUri = null; if (Uri.TryCreate(ctx, UriKind.Absolute, out ctxUri)) { C = new RDFContext(RDFModelUtilities.ASCII_To_Unicode(ctxUri.ToString())); } else { throw new RDFModelException("found context '" + ctx +"' which is not a well-formed absolute Uri"); } } #endregion #region addquadruple if (O != null) { result.AddQuadruple(new RDFQuadruple(C, S, P, O)); } else { result.AddQuadruple(new RDFQuadruple(C, S, P, L)); } #endregion } return result; } #endregion } catch(Exception ex) { throw new RDFModelException("Cannot deserialize N-Quads (line " + nquadIndex + ") because: " + ex.Message, ex); } }
/// <summary> /// Gets a memory store containing quadruples with the specified context /// </summary> public override RDFStore SelectQuadruplesByContext(RDFContext contextResource) { RDFMemoryStore result = new RDFMemoryStore(); if (contextResource != null) { try { //Open connection this.Connection.Open(); //Modify command this.Commands["SELECT"].CommandText += " WHERE contextid = @CTXID"; //Add parameters this.Commands["SELECT"].Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); //Valorize parameters this.Commands["SELECT"].Parameters["CTXID"].Value = contextResource.PatternMemberID; //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; }
/// <summary> /// Gets a store containing quadruples with the specified context /// </summary> public abstract RDFMemoryStore SelectQuadruplesByContext(RDFContext contextResource);
/// <summary> /// Selects the quadruples corresponding to the given pattern from the given store /// </summary> internal static List <RDFQuadruple> SelectQuadruples(RDFMemoryStore store, RDFContext ctx, RDFResource subj, RDFResource pred, RDFResource obj, RDFLiteral lit) { List <RDFQuadruple> matchResult = new List <RDFQuadruple>(); if (store != null) { List <RDFQuadruple> C = new List <RDFQuadruple>(); List <RDFQuadruple> S = new List <RDFQuadruple>(); List <RDFQuadruple> P = new List <RDFQuadruple>(); List <RDFQuadruple> O = new List <RDFQuadruple>(); List <RDFQuadruple> L = new List <RDFQuadruple>(); StringBuilder queryFilters = new StringBuilder(); //Filter by Context if (ctx != null) { queryFilters.Append("C"); foreach (long q in store.StoreIndex.SelectIndexByContext(ctx)) { C.Add(store.Quadruples[q]); } } //Filter by Subject if (subj != null) { queryFilters.Append("S"); foreach (long q in store.StoreIndex.SelectIndexBySubject(subj)) { S.Add(store.Quadruples[q]); } } //Filter by Predicate if (pred != null) { queryFilters.Append("P"); foreach (long q in store.StoreIndex.SelectIndexByPredicate(pred)) { P.Add(store.Quadruples[q]); } } //Filter by Object if (obj != null) { queryFilters.Append("O"); foreach (long q in store.StoreIndex.SelectIndexByObject(obj)) { O.Add(store.Quadruples[q]); } } //Filter by Literal if (lit != null) { queryFilters.Append("L"); foreach (var q in store.StoreIndex.SelectIndexByLiteral(lit)) { L.Add(store.Quadruples[q]); } } //Intersect the filters string queryFilter = queryFilters.ToString(); switch (queryFilter) { case "C": matchResult = C; break; case "S": matchResult = S; break; case "P": matchResult = P; break; case "O": matchResult = O; break; case "L": matchResult = L; break; case "CS": matchResult = C.Intersect(S).ToList(); break; case "CP": matchResult = C.Intersect(P).ToList(); break; case "CO": matchResult = C.Intersect(O).ToList(); break; case "CL": matchResult = C.Intersect(L).ToList(); break; case "CSP": matchResult = C.Intersect(S).Intersect(P).ToList(); break; case "CSO": matchResult = C.Intersect(S).Intersect(O).ToList(); break; case "CSL": matchResult = C.Intersect(S).Intersect(L).ToList(); break; case "CPO": matchResult = C.Intersect(P).Intersect(O).ToList(); break; case "CPL": matchResult = C.Intersect(P).Intersect(L).ToList(); break; case "CSPO": matchResult = C.Intersect(S).Intersect(P).Intersect(O).ToList(); break; case "CSPL": matchResult = C.Intersect(S).Intersect(P).Intersect(L).ToList(); break; case "SP": matchResult = S.Intersect(P).ToList(); break; case "SO": matchResult = S.Intersect(O).ToList(); break; case "SL": matchResult = S.Intersect(L).ToList(); break; case "SPO": matchResult = S.Intersect(P).Intersect(O).ToList(); break; case "SPL": matchResult = S.Intersect(P).Intersect(L).ToList(); break; case "PO": matchResult = P.Intersect(O).ToList(); break; case "PL": matchResult = P.Intersect(L).ToList(); break; default: matchResult = store.ToList(); break; } } return(matchResult); }
/// <summary> /// Removes the quadruples with the given context, predicate and literal /// </summary> public abstract RDFStore RemoveQuadruplesByContextPredicateLiteral(RDFContext contextResource, RDFResource predicateResource, RDFLiteral objectLiteral);
/// <summary> /// Removes the quadruples with the given context, predicate and object /// </summary> public abstract RDFStore RemoveQuadruplesByContextPredicateObject(RDFContext contextResource, RDFResource predicateResource, RDFResource objectResource);
/// <summary> /// Merges the given graph into the store within a single transaction, avoiding duplicate insertions /// </summary> public override RDFStore MergeGraph(RDFGraph graph) { if (graph != null) { var graphCtx = new RDFContext(graph.Context); //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)); try { //Open connection this.Connection.Open(); //Prepare command command.Prepare(); //Open transaction command.Transaction = this.Connection.BeginTransaction(); //Iterate triples foreach(var triple in graph) { //Valorize parameters command.Parameters["QID"].Value = RDFModelUtilities.CreateHash(graphCtx + " " + triple.Subject + " " + triple.Predicate + " " + triple.Object); command.Parameters["TFV"].Value = triple.TripleFlavor; command.Parameters["CTX"].Value = graphCtx.ToString(); command.Parameters["CTXID"].Value = graphCtx.PatternMemberID; command.Parameters["SUBJ"].Value = triple.Subject.ToString(); command.Parameters["SUBJID"].Value = triple.Subject.PatternMemberID; command.Parameters["PRED"].Value = triple.Predicate.ToString(); command.Parameters["PREDID"].Value = triple.Predicate.PatternMemberID; command.Parameters["OBJ"].Value = triple.Object.ToString(); command.Parameters["OBJID"].Value = triple.Object.PatternMemberID; //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 within a single transaction, avoiding duplicate insertions /// </summary> public override RDFStore MergeGraph(RDFGraph graph) { if (graph != null) { var graphCtx = new RDFContext(graph.Context); //Create command 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)); try { //Open connection this.Connection.Open(); //Prepare command command.Prepare(); //Open transaction command.Transaction = this.Connection.BeginTransaction(); //Iterate triples foreach(var triple in graph) { //Valorize parameters command.Parameters["QID"].Value = RDFModelUtilities.CreateHash(graphCtx + " " + triple.Subject + " " + triple.Predicate + " " + triple.Object); command.Parameters["TFV"].Value = triple.TripleFlavor; command.Parameters["CTX"].Value = graphCtx.ToString(); command.Parameters["CTXID"].Value = graphCtx.PatternMemberID; command.Parameters["SUBJ"].Value = triple.Subject.ToString(); command.Parameters["SUBJID"].Value = triple.Subject.PatternMemberID; command.Parameters["PRED"].Value = triple.Predicate.ToString(); command.Parameters["PREDID"].Value = triple.Predicate.PatternMemberID; command.Parameters["OBJ"].Value = triple.Object.ToString(); command.Parameters["OBJID"].Value = triple.Object.PatternMemberID; //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> /// Gets a memory store containing quadruples satisfying the given pattern /// </summary> internal override RDFMemoryStore SelectQuadruples(RDFContext ctx, RDFResource subj, RDFResource pred, RDFResource obj, RDFLiteral lit) { RDFMemoryStore result = new RDFMemoryStore(); NpgsqlCommand command = null; //Intersect the filters if (ctx != null) { if (subj != null) { if (pred != null) { if (obj != null) { //C->S->P->O command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE contextid = @CTXID AND subjectid = @SUBJID AND predicateid = @PREDID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("SUBJID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("PREDID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //C->S->P->L command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE contextid = @CTXID AND subjectid = @SUBJID AND predicateid = @PREDID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("SUBJID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("PREDID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //C->S->P-> command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE contextid = @CTXID AND subjectid = @SUBJID AND predicateid = @PREDID", this.Connection); command.Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("SUBJID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("PREDID", NpgsqlDbType.Bigint)); command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; } } } else { if (obj != null) { //C->S->->O command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE contextid = @CTXID AND subjectid = @SUBJID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("SUBJID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //C->S->->L command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE contextid = @CTXID AND subjectid = @SUBJID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("SUBJID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //C->S->-> command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE contextid = @CTXID AND subjectid = @SUBJID", this.Connection); command.Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("SUBJID", NpgsqlDbType.Bigint)); command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["SUBJID"].Value = subj.PatternMemberID; } } } } else { if (pred != null) { if (obj != null) { //C->->P->O command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE contextid = @CTXID AND predicateid = @PREDID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("PREDID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //C->->P->L command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE contextid = @CTXID AND predicateid = @PREDID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("PREDID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //C->->P-> command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE contextid = @CTXID AND predicateid = @PREDID", this.Connection); command.Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("PREDID", NpgsqlDbType.Bigint)); command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; } } } else { if (obj != null) { //C->->->O command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE contextid = @CTXID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //C->->->L command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE contextid = @CTXID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //C->->-> command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE contextid = @CTXID", this.Connection); command.Parameters.Add(new NpgsqlParameter("CTXID", NpgsqlDbType.Bigint)); command.Parameters["CTXID"].Value = ctx.PatternMemberID; } } } } } else { if (subj != null) { if (pred != null) { if (obj != null) { //->S->P->O command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE subjectid = @SUBJID AND predicateid = @PREDID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("SUBJID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("PREDID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //->S->P->L command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE subjectid = @SUBJID AND predicateid = @PREDID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("SUBJID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("PREDID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //->S->P-> command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE subjectid = @SUBJID AND predicateid = @PREDID", this.Connection); command.Parameters.Add(new NpgsqlParameter("SUBJID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("PREDID", NpgsqlDbType.Bigint)); command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; } } } else { if (obj != null) { //->S->->O command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE subjectid = @SUBJID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("SUBJID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //->S->->L command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE subjectid = @SUBJID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("SUBJID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //->S->-> command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE subjectid = @SUBJID", this.Connection); command.Parameters.Add(new NpgsqlParameter("SUBJID", NpgsqlDbType.Bigint)); command.Parameters["SUBJID"].Value = subj.PatternMemberID; } } } } else { if (pred != null) { if (obj != null) { //->->P->O command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE predicateid = @PREDID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("PREDID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //->->P->L command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE predicateid = @PREDID AND objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("PREDID", NpgsqlDbType.Bigint)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //->->P-> command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE predicateid = @PREDID", this.Connection); command.Parameters.Add(new NpgsqlParameter("PREDID", NpgsqlDbType.Bigint)); command.Parameters["PREDID"].Value = pred.PatternMemberID; } } } else { if (obj != null) { //->->->O command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //->->->L command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\" WHERE objectid = @OBJID AND tripleflavor = @TFV", this.Connection); command.Parameters.Add(new NpgsqlParameter("TFV", NpgsqlDbType.Integer)); command.Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //->->-> command = new NpgsqlCommand("SELECT tripleflavor, context, subject, predicate, object FROM public.\"quadruples\"", this.Connection); } } } } } //Prepare and execute command try { //Open connection this.Connection.Open(); //Prepare command command.Prepare(); //Execute command using (var quadruples = command.ExecuteReader()) { if(quadruples.HasRows) { while (quadruples.Read()) { result.AddQuadruple(RDFStoreUtilities.ParseQuadruple(quadruples)); } } } //Close connection this.Connection.Close(); } catch (Exception ex) { //Close connection this.Connection.Close(); //Propagate exception throw new RDFStoreException("Cannot read data from PostgreSQL store because: " + ex.Message, ex); } return result; }
/// <summary> /// Selects the quadruples corresponding to the given pattern from the given store /// </summary> internal static List<RDFQuadruple> SelectQuadruples(RDFMemoryStore store, RDFContext ctx, RDFResource subj, RDFResource pred, RDFResource obj, RDFLiteral lit) { var matchCtx = new List<RDFQuadruple>(); var matchSubj = new List<RDFQuadruple>(); var matchPred = new List<RDFQuadruple>(); var matchObj = new List<RDFQuadruple>(); var matchLit = new List<RDFQuadruple>(); var matchResult = new List<RDFQuadruple>(); if (store != null) { //Filter by Context if (ctx != null) { foreach (var q in store.StoreIndex.SelectIndexByContext(ctx).Keys) { matchCtx.Add(store.Quadruples[q]); } } //Filter by Subject if (subj != null) { foreach (var q in store.StoreIndex.SelectIndexBySubject(subj).Keys) { matchSubj.Add(store.Quadruples[q]); } } //Filter by Predicate if (pred != null) { foreach (var q in store.StoreIndex.SelectIndexByPredicate(pred).Keys) { matchPred.Add(store.Quadruples[q]); } } //Filter by Object if (obj != null) { foreach (var q in store.StoreIndex.SelectIndexByObject(obj).Keys) { matchObj.Add(store.Quadruples[q]); } } //Filter by Literal if (lit != null) { foreach (var q in store.StoreIndex.SelectIndexByLiteral(lit).Keys) { matchLit.Add(store.Quadruples[q]); } } //Intersect the filters if (ctx != null) { if (subj != null) { if (pred != null) { if (obj != null) { //C->S->P->O matchResult = matchCtx.Intersect(matchSubj) .Intersect(matchPred) .Intersect(matchObj) .ToList<RDFQuadruple>(); } else { if (lit != null) { //C->S->P->L matchResult = matchCtx.Intersect(matchSubj) .Intersect(matchPred) .Intersect(matchLit) .ToList<RDFQuadruple>(); } else { //C->S->P-> matchResult = matchCtx.Intersect(matchSubj) .Intersect(matchPred) .ToList<RDFQuadruple>(); } } } else { if (obj != null) { //C->S->->O matchResult = matchCtx.Intersect(matchSubj) .Intersect(matchObj) .ToList<RDFQuadruple>(); } else { if (lit != null) { //C->S->->L matchResult = matchCtx.Intersect(matchSubj) .Intersect(matchLit) .ToList<RDFQuadruple>(); } else { //C->S->-> matchResult = matchCtx.Intersect(matchSubj) .ToList<RDFQuadruple>(); } } } } else { if (pred != null) { if (obj != null) { //C->->P->O matchResult = matchCtx.Intersect(matchPred) .Intersect(matchObj) .ToList<RDFQuadruple>(); } else { if (lit != null) { //C->->P->L matchResult = matchCtx.Intersect(matchPred) .Intersect(matchLit) .ToList<RDFQuadruple>(); } else { //C->->P-> matchResult = matchCtx.Intersect(matchPred) .ToList<RDFQuadruple>(); } } } else { if (obj != null) { //C->->->O matchResult = matchCtx.Intersect(matchObj) .ToList<RDFQuadruple>(); } else { if (lit != null) { //C->->->L matchResult = matchCtx.Intersect(matchLit) .ToList<RDFQuadruple>(); } else { //C->->-> matchResult = matchCtx; } } } } } else { if (subj != null) { if (pred != null) { if (obj != null) { //->S->P->O matchResult = matchSubj.Intersect(matchPred) .Intersect(matchObj) .ToList<RDFQuadruple>(); } else { if (lit != null) { //->S->P->L matchResult = matchSubj.Intersect(matchPred) .Intersect(matchLit) .ToList<RDFQuadruple>(); } else { //->S->P-> matchResult = matchSubj.Intersect(matchPred) .ToList<RDFQuadruple>(); } } } else { if (obj != null) { //->S->->O matchResult = matchSubj.Intersect(matchObj) .ToList<RDFQuadruple>(); } else { if (lit != null) { //->S->->L matchResult = matchSubj.Intersect(matchLit) .ToList<RDFQuadruple>(); } else { //->S->-> matchResult = matchSubj; } } } } else { if (pred != null) { if (obj != null) { //->->P->O matchResult = matchPred.Intersect(matchObj) .ToList<RDFQuadruple>(); } else { if (lit != null) { //->->P->L matchResult = matchPred.Intersect(matchLit) .ToList<RDFQuadruple>(); } else { //->->P-> matchResult = matchPred; } } } else { if (obj != null) { //->->->O matchResult = matchObj; } else { if (lit != null) { //->->->L matchResult = matchLit; } else { //->->-> matchResult = store.Quadruples.Values.ToList<RDFQuadruple>(); } } } } } } return matchResult; }
/// <summary> /// Selects the quadruples indexed by the given context /// </summary> internal HashSet <long> SelectIndexByContext(RDFContext contextResource) => contextResource != null && this.Contexts.ContainsKey(contextResource.PatternMemberID) ? this.Contexts[contextResource.PatternMemberID] : EmptyHashSet;
/// <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> /// Removes the quadruples with the given context /// </summary> public override RDFStore RemoveQuadruplesByContext(RDFContext contextResource) { if (contextResource != null) { //Create command var command = new MySqlCommand("DELETE FROM Quadruples WHERE ContextID = @CTXID", this.Connection); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); //Valorize parameters command.Parameters["CTXID"].Value = contextResource.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 delete data from MySQL store because: " + ex.Message, ex); } } return this; }
/// <summary> /// Gets a memory store containing quadruples with the specified context /// </summary> public override RDFStore SelectQuadruplesByContext(RDFContext contextResource) { RDFMemoryStore result = new RDFMemoryStore(); if (contextResource != null) { try { //Open connection this.Connection.Open(); //Modify command this.Commands["SELECT"].CommandText += " WHERE ContextID = @CTXID"; //Add parameters this.Commands["SELECT"].Parameters.Add(new SQLiteParameter("CTXID", DbType.Int64)); //Prepare command this.Commands["SELECT"].Prepare(); //Valorize parameters this.Commands["SELECT"].Parameters["CTXID"].Value = contextResource.PatternMemberID; //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; }
/// <summary> /// Gets a memory store containing quadruples with the specified context /// </summary> public override RDFMemoryStore SelectQuadruplesByContext(RDFContext contextResource) { return this.SelectQuadruples(contextResource, null, null, null, null); }
/// <summary> /// Gets a store containing quadruples satisfying the given pattern /// </summary> internal abstract RDFMemoryStore SelectQuadruples(RDFContext contextResource, RDFResource subjectResource, RDFResource predicateResource, RDFResource objectResource, RDFLiteral objectLiteral);
/// <summary> /// Gets a memory store containing quadruples satisfying the given pattern /// </summary> internal override RDFMemoryStore SelectQuadruples(RDFContext ctx, RDFResource subj, RDFResource pred, RDFResource obj, RDFLiteral lit) { RDFMemoryStore result = new RDFMemoryStore(); MySqlCommand command = null; //Intersect the filters if (ctx != null) { if (subj != null) { if (pred != null) { if (obj != null) { //C->S->P->O command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ContextID = @CTXID AND SubjectID = @SUBJID AND PredicateID = @PREDID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("SUBJID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("PREDID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //C->S->P->L command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ContextID = @CTXID AND SubjectID = @SUBJID AND PredicateID = @PREDID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("SUBJID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("PREDID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //C->S->P-> command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ContextID = @CTXID AND SubjectID = @SUBJID AND PredicateID = @PREDID", this.Connection); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("SUBJID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("PREDID", MySqlDbType.Int64)); command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; } } } else { if (obj != null) { //C->S->->O command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ContextID = @CTXID AND SubjectID = @SUBJID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("SUBJID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //C->S->->L command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ContextID = @CTXID AND SubjectID = @SUBJID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("SUBJID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //C->S->-> command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ContextID = @CTXID AND SubjectID = @SUBJID", this.Connection); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("SUBJID", MySqlDbType.Int64)); command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["SUBJID"].Value = subj.PatternMemberID; } } } } else { if (pred != null) { if (obj != null) { //C->->P->O command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ContextID = @CTXID AND PredicateID = @PREDID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("PREDID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //C->->P->L command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ContextID = @CTXID AND PredicateID = @PREDID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("PREDID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //C->->P-> command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ContextID = @CTXID AND PredicateID = @PREDID", this.Connection); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("PREDID", MySqlDbType.Int64)); command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; } } } else { if (obj != null) { //C->->->O command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ContextID = @CTXID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //C->->->L command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ContextID = @CTXID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["CTXID"].Value = ctx.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //C->->-> command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ContextID = @CTXID", this.Connection); command.Parameters.Add(new MySqlParameter("CTXID", MySqlDbType.Int64)); command.Parameters["CTXID"].Value = ctx.PatternMemberID; } } } } } else { if (subj != null) { if (pred != null) { if (obj != null) { //->S->P->O command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE SubjectID = @SUBJID AND PredicateID = @PREDID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("SUBJID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("PREDID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //->S->P->L command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE SubjectID = @SUBJID AND PredicateID = @PREDID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("SUBJID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("PREDID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //->S->P-> command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE SubjectID = @SUBJID AND PredicateID = @PREDID", this.Connection); command.Parameters.Add(new MySqlParameter("SUBJID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("PREDID", MySqlDbType.Int64)); command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["PREDID"].Value = pred.PatternMemberID; } } } else { if (obj != null) { //->S->->O command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE SubjectID = @SUBJID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("SUBJID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //->S->->L command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE SubjectID = @SUBJID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("SUBJID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["SUBJID"].Value = subj.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //->S->-> command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE SubjectID = @SUBJID", this.Connection); command.Parameters.Add(new MySqlParameter("SUBJID", MySqlDbType.Int64)); command.Parameters["SUBJID"].Value = subj.PatternMemberID; } } } } else { if (pred != null) { if (obj != null) { //->->P->O command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE PredicateID = @PREDID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("PREDID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //->->P->L command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE PredicateID = @PREDID AND ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("PREDID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["PREDID"].Value = pred.PatternMemberID; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //->->P-> command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE PredicateID = @PREDID", this.Connection); command.Parameters.Add(new MySqlParameter("PREDID", MySqlDbType.Int64)); command.Parameters["PREDID"].Value = pred.PatternMemberID; } } } else { if (obj != null) { //->->->O command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPO; command.Parameters["OBJID"].Value = obj.PatternMemberID; } else { if (lit != null) { //->->->L command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples WHERE ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; command.Parameters["OBJID"].Value = lit.PatternMemberID; } else { //->->-> command = new MySqlCommand("SELECT TripleFlavor, Context, Subject, Predicate, Object FROM Quadruples", this.Connection); } } } } } //Prepare and execute command try { //Open connection this.Connection.Open(); //Prepare command command.Prepare(); command.CommandTimeout = 120; //Execute command using (var quadruples = command.ExecuteReader()) { if(quadruples.HasRows) { while (quadruples.Read()) { result.AddQuadruple(RDFStoreUtilities.ParseQuadruple(quadruples)); } } } //Close connection this.Connection.Close(); } catch (Exception ex) { //Close connection this.Connection.Close(); //Propagate exception throw new RDFStoreException("Cannot read data from MySQL store because: " + ex.Message, ex); } return result; }
/// <summary> /// Merges the given graph into the store within a single transaction, avoiding duplicate insertions /// </summary> public override RDFStore MergeGraph(RDFGraph graph) { if (graph != null) { var graphCtx = new RDFContext(graph.Context); //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)); try { //Open connection this.Connection.Open(); //Prepare command command.Prepare(); //Open transaction command.Transaction = this.Connection.BeginTransaction(); //Iterate triples foreach(var triple in graph) { //Valorize parameters command.Parameters["QID"].Value = RDFModelUtilities.CreateHash(graphCtx + " " + triple.Subject + " " + triple.Predicate + " " + triple.Object); command.Parameters["TFV"].Value = triple.TripleFlavor; command.Parameters["CTX"].Value = graphCtx.ToString(); command.Parameters["CTXID"].Value = graphCtx.PatternMemberID; command.Parameters["SUBJ"].Value = triple.Subject.ToString(); command.Parameters["SUBJID"].Value = triple.Subject.PatternMemberID; command.Parameters["PRED"].Value = triple.Predicate.ToString(); command.Parameters["PREDID"].Value = triple.Predicate.PatternMemberID; command.Parameters["OBJ"].Value = triple.Object.ToString(); command.Parameters["OBJID"].Value = triple.Object.PatternMemberID; //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> /// Removes the quadruples with the given context, subject and object /// </summary> public abstract RDFStore RemoveQuadruplesByContextSubjectObject(RDFContext contextResource, RDFResource subjectResource, RDFResource objectResource);
/// <summary> /// Selects the quadruples corresponding to the given pattern from the given store /// </summary> internal static List <RDFQuadruple> SelectQuadruples(RDFMemoryStore store, RDFContext ctx, RDFResource subj, RDFResource pred, RDFResource obj, RDFLiteral lit) { var matchCtx = new List <RDFQuadruple>(); var matchSubj = new List <RDFQuadruple>(); var matchPred = new List <RDFQuadruple>(); var matchObj = new List <RDFQuadruple>(); var matchLit = new List <RDFQuadruple>(); var matchResult = new List <RDFQuadruple>(); if (store != null) { //Filter by Context if (ctx != null) { foreach (var q in store.StoreIndex.SelectIndexByContext(ctx).Keys) { matchCtx.Add(store.Quadruples[q]); } } //Filter by Subject if (subj != null) { foreach (var q in store.StoreIndex.SelectIndexBySubject(subj).Keys) { matchSubj.Add(store.Quadruples[q]); } } //Filter by Predicate if (pred != null) { foreach (var q in store.StoreIndex.SelectIndexByPredicate(pred).Keys) { matchPred.Add(store.Quadruples[q]); } } //Filter by Object if (obj != null) { foreach (var q in store.StoreIndex.SelectIndexByObject(obj).Keys) { matchObj.Add(store.Quadruples[q]); } } //Filter by Literal if (lit != null) { foreach (var q in store.StoreIndex.SelectIndexByLiteral(lit).Keys) { matchLit.Add(store.Quadruples[q]); } } //Intersect the filters if (ctx != null) { if (subj != null) { if (pred != null) { if (obj != null) { //C->S->P->O matchResult = matchCtx.Intersect(matchSubj) .Intersect(matchPred) .Intersect(matchObj) .ToList <RDFQuadruple>(); } else { if (lit != null) { //C->S->P->L matchResult = matchCtx.Intersect(matchSubj) .Intersect(matchPred) .Intersect(matchLit) .ToList <RDFQuadruple>(); } else { //C->S->P-> matchResult = matchCtx.Intersect(matchSubj) .Intersect(matchPred) .ToList <RDFQuadruple>(); } } } else { if (obj != null) { //C->S->->O matchResult = matchCtx.Intersect(matchSubj) .Intersect(matchObj) .ToList <RDFQuadruple>(); } else { if (lit != null) { //C->S->->L matchResult = matchCtx.Intersect(matchSubj) .Intersect(matchLit) .ToList <RDFQuadruple>(); } else { //C->S->-> matchResult = matchCtx.Intersect(matchSubj) .ToList <RDFQuadruple>(); } } } } else { if (pred != null) { if (obj != null) { //C->->P->O matchResult = matchCtx.Intersect(matchPred) .Intersect(matchObj) .ToList <RDFQuadruple>(); } else { if (lit != null) { //C->->P->L matchResult = matchCtx.Intersect(matchPred) .Intersect(matchLit) .ToList <RDFQuadruple>(); } else { //C->->P-> matchResult = matchCtx.Intersect(matchPred) .ToList <RDFQuadruple>(); } } } else { if (obj != null) { //C->->->O matchResult = matchCtx.Intersect(matchObj) .ToList <RDFQuadruple>(); } else { if (lit != null) { //C->->->L matchResult = matchCtx.Intersect(matchLit) .ToList <RDFQuadruple>(); } else { //C->->-> matchResult = matchCtx; } } } } } else { if (subj != null) { if (pred != null) { if (obj != null) { //->S->P->O matchResult = matchSubj.Intersect(matchPred) .Intersect(matchObj) .ToList <RDFQuadruple>(); } else { if (lit != null) { //->S->P->L matchResult = matchSubj.Intersect(matchPred) .Intersect(matchLit) .ToList <RDFQuadruple>(); } else { //->S->P-> matchResult = matchSubj.Intersect(matchPred) .ToList <RDFQuadruple>(); } } } else { if (obj != null) { //->S->->O matchResult = matchSubj.Intersect(matchObj) .ToList <RDFQuadruple>(); } else { if (lit != null) { //->S->->L matchResult = matchSubj.Intersect(matchLit) .ToList <RDFQuadruple>(); } else { //->S->-> matchResult = matchSubj; } } } } else { if (pred != null) { if (obj != null) { //->->P->O matchResult = matchPred.Intersect(matchObj) .ToList <RDFQuadruple>(); } else { if (lit != null) { //->->P->L matchResult = matchPred.Intersect(matchLit) .ToList <RDFQuadruple>(); } else { //->->P-> matchResult = matchPred; } } } else { if (obj != null) { //->->->O matchResult = matchObj; } else { if (lit != null) { //->->->L matchResult = matchLit; } else { //->->-> matchResult = store.Quadruples.Values.ToList <RDFQuadruple>(); } } } } } } return(matchResult); }
/// <summary> /// Selects the quadruples indexed by the given context /// </summary> internal Dictionary<Int64, Object> SelectIndexByContext(RDFContext contextResource) { var result = new Dictionary<Int64, Object>(); if (contextResource != null) { if (this.Contexts.ContainsKey(contextResource.PatternMemberID)) { result = this.Contexts[contextResource.PatternMemberID]; } } return result; }