/// <summary> /// Default-ctor to build an ontology literal from the given literal. /// </summary> public RDFOntologyLiteral(RDFLiteral literal) { if (literal != null) { this.Value = literal; this.PatternMemberID = literal.PatternMemberID; } else { throw new RDFSemanticsException("Cannot create RDFOntologyLiteral because given \"literal\" parameter is null."); } }
/// <summary> /// Default-ctor to build an ontology fact from the given literal /// </summary> public RDFOntologyFact(RDFLiteral factValue) { if (factValue != null) { this.Value = factValue; this.PatternMemberID = factValue.PatternMemberID; this.ClassTypes = new Dictionary<Int64, RDFOntologyClass>() { //Every literal ontology fact has predefined "rdfs:Literal" class type { RDFVocabulary.RDFS.LITERAL.PatternMemberID, RDFSemanticsUtilities.DefaultClasses[RDFVocabulary.RDFS.LITERAL.PatternMemberID] } }; this.SameAs = new Dictionary<Int64, RDFOntologyFact>(); this.DifferentFrom = new Dictionary<Int64, RDFOntologyFact>(); this.OntologyAttributes = new Dictionary<Int64, RDFOntologyAttribute>(); } else { throw new RDFSemanticsException("Cannot create ontology fact because given \"factValue\" parameter is null."); } }
/// <summary> /// Selects the triples indexed by the given literal /// </summary> internal Dictionary<Int64, Object> SelectIndexByLiteral(RDFLiteral objectLiteral) { var result = new Dictionary<Int64, Object>(); if (objectLiteral != null) { if (this.Literals.ContainsKey(objectLiteral.PatternMemberID)) { result = this.Literals[objectLiteral.PatternMemberID]; } } return result; }
/// <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> /// Gets a memory store containing quadruples with the specified literal /// </summary> public override RDFMemoryStore SelectQuadruplesByLiteral(RDFLiteral objectLiteral) { return this.SelectQuadruples(null, null, null, null, objectLiteral); }
/// <summary> /// Removes the quadruples with the given literal as object /// </summary> public override RDFStore RemoveQuadruplesByLiteral(RDFLiteral literalObject) { if (literalObject != null) { //Create command var command = new MySqlCommand("DELETE FROM Quadruples WHERE ObjectID = @OBJID AND TripleFlavor = @TFV", this.Connection); command.Parameters.Add(new MySqlParameter("OBJID", MySqlDbType.Int64)); command.Parameters.Add(new MySqlParameter("TFV", MySqlDbType.Int32)); //Valorize parameters command.Parameters["OBJID"].Value = literalObject.PatternMemberID; command.Parameters["TFV"].Value = RDFModelEnums.RDFTripleFlavors.SPL; 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> /// Selects the triples corresponding to the given pattern from the given graph /// </summary> internal static List<RDFTriple> SelectTriples(RDFGraph graph, RDFResource subj, RDFResource pred, RDFResource obj, RDFLiteral lit) { var matchSubj = new List<RDFTriple>(); var matchPred = new List<RDFTriple>(); var matchObj = new List<RDFTriple>(); var matchLit = new List<RDFTriple>(); var matchResult = new List<RDFTriple>(); if (graph != null) { //Filter by Subject if (subj != null) { foreach (var t in graph.GraphIndex.SelectIndexBySubject(subj).Keys) { matchSubj.Add(graph.Triples[t]); } } //Filter by Predicate if (pred != null) { foreach (var t in graph.GraphIndex.SelectIndexByPredicate(pred).Keys) { matchPred.Add(graph.Triples[t]); } } //Filter by Object if (obj != null) { foreach (var t in graph.GraphIndex.SelectIndexByObject(obj).Keys) { matchObj.Add(graph.Triples[t]); } } //Filter by Literal if (lit != null) { foreach (var t in graph.GraphIndex.SelectIndexByLiteral(lit).Keys) { matchLit.Add(graph.Triples[t]); } } //Intersect the filters if (subj != null) { if (pred != null) { if (obj != null) { //S->P->O matchResult = matchSubj.Intersect(matchPred) .Intersect(matchObj) .ToList<RDFTriple>(); } else { if (lit != null) { //S->P->L matchResult = matchSubj.Intersect(matchPred) .Intersect(matchLit) .ToList<RDFTriple>(); } else { //S->P-> matchResult = matchSubj.Intersect(matchPred) .ToList<RDFTriple>(); } } } else { if (obj != null) { //S->->O matchResult = matchSubj.Intersect(matchObj) .ToList<RDFTriple>(); } else { if (lit != null) { //S->->L matchResult = matchSubj.Intersect(matchLit) .ToList<RDFTriple>(); } else { //S->-> matchResult = matchSubj; } } } } else { if (pred != null) { if (obj != null) { //->P->O matchResult = matchPred.Intersect(matchObj) .ToList<RDFTriple>(); } else { if (lit != null) { //->P->L matchResult = matchPred.Intersect(matchLit) .ToList<RDFTriple>(); } else { //->P-> matchResult = matchPred; } } } else { if (obj != null) { //->->O matchResult = matchObj; } else { if (lit != null) { //->->L matchResult = matchLit; } else { //->-> matchResult = graph.Triples.Values.ToList<RDFTriple>(); } } } } } return matchResult; }
/// <summary> /// Removes the quadruples with the given literal as object /// </summary> public override RDFStore RemoveQuadruplesByLiteral(RDFLiteral literalObject) { if (literalObject != null) { try { //Modify command this.Commands["DELETE"].CommandText = "DELETE FROM main.Quadruples WHERE TripleFlavor = 2 AND ObjectID = @OBJID"; 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("OBJID", DbType.Int64)); this.Commands["DELETE"].Parameters["OBJID"].Value = literalObject.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> /// Given an element representing a RDF container, iterates on its constituent elements /// to build its standard reification triples. /// </summary> internal static void ParseContainerElements(RDFModelEnums.RDFContainerType contType, XmlNode container, RDFResource subj, RDFResource pred, RDFGraph result) { //Attach the container as the blank object of the current pred RDFResource obj = new RDFResource(); result.AddTriple(new RDFTriple(subj, pred, obj)); //obj -> rdf:type -> rdf:[Bag|Seq|Alt] switch (contType) { case RDFModelEnums.RDFContainerType.Bag: result.AddTriple(new RDFTriple(obj, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.BAG)); break; case RDFModelEnums.RDFContainerType.Seq: result.AddTriple(new RDFTriple(obj, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.SEQ)); break; default: result.AddTriple(new RDFTriple(obj, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.ALT)); break; } //Iterate on the container items if (container.HasChildNodes) { IEnumerator elems = container.ChildNodes.GetEnumerator(); List <String> elemVals = new List <String>(); while (elems != null && elems.MoveNext()) { XmlNode elem = (XmlNode)elems.Current; XmlAttribute elemUri = GetRdfResourceAttribute(elem); #region Container Resource Item //This is a container of resources if (elemUri != null) { //Sanitize eventual blank node value detected by presence of "nodeID" attribute if (elemUri.LocalName.Equals("nodeID", StringComparison.Ordinal)) { if (!elemUri.Value.StartsWith("bnode:")) { elemUri.Value = "bnode:" + elemUri.Value; } } //obj -> rdf:_N -> VALUE if (contType == RDFModelEnums.RDFContainerType.Alt) { if (!elemVals.Contains(elemUri.Value)) { elemVals.Add(elemUri.Value); result.AddTriple(new RDFTriple(obj, new RDFResource(RDFVocabulary.RDF.BASE_URI + elem.LocalName), new RDFResource(elemUri.Value))); } } else { result.AddTriple(new RDFTriple(obj, new RDFResource(RDFVocabulary.RDF.BASE_URI + elem.LocalName), new RDFResource(elemUri.Value))); } } #endregion #region Container Literal Item //This is a container of literals else { //Parse the literal contained in the item RDFLiteral literal = null; XmlAttribute attr = GetRdfDatatypeAttribute(elem); if (attr != null) { literal = new RDFTypedLiteral(elem.InnerText, RDFModelUtilities.GetDatatypeFromString(attr.InnerText)); } else { attr = GetXmlLangAttribute(elem); literal = new RDFPlainLiteral(elem.InnerText, (attr != null ? attr.InnerText : String.Empty)); } //obj -> rdf:_N -> VALUE if (contType == RDFModelEnums.RDFContainerType.Alt) { if (!elemVals.Contains(literal.ToString())) { elemVals.Add(literal.ToString()); result.AddTriple(new RDFTriple(obj, new RDFResource(RDFVocabulary.RDF.BASE_URI + elem.LocalName), literal)); } } else { result.AddTriple(new RDFTriple(obj, new RDFResource(RDFVocabulary.RDF.BASE_URI + elem.LocalName), literal)); } } #endregion } } }
/// <summary> /// Removes the triples with the given predicate and literal /// </summary> public RDFGraph RemoveTriplesByPredicateLiteral(RDFResource predicateResource, RDFLiteral objectLiteral) { if (predicateResource != null && objectLiteral != null) { foreach (RDFTriple triple in this.SelectTriplesByPredicate(predicateResource) .SelectTriplesByLiteral(objectLiteral)) { //Remove triple this.Triples.Remove(triple.TripleID); //Remove index this.GraphIndex.RemoveIndex(triple); } } return(this); }
/// <summary> /// Deserializes the given N-Triples stream to a graph. /// </summary> internal static RDFGraph Deserialize(Stream inputStream, Uri graphContext) { long ntripleIndex = 0; try { #region deserialize using (StreamReader sr = new StreamReader(inputStream, Encoding.ASCII)) { RDFGraph result = new RDFGraph().SetContext(graphContext); string ntriple = string.Empty; string[] tokens = new string[3]; RDFResource S = null; RDFResource P = null; RDFResource O = null; RDFLiteral L = null; while ((ntriple = sr.ReadLine()) != null) { ntripleIndex++; #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; //Preliminary sanitizations: clean trailing space-like chars ntriple = ntriple.Trim(new char[] { ' ', '\t', '\r', '\n' }); //Skip empty or comment lines if (ntriple == string.Empty || ntriple.StartsWith("#")) { continue; } //Tokenizes the sanitized triple tokens = TokenizeNTriple(ntriple); #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] = regexSqt.Replace(tokens[2], string.Empty); tokens[2] = 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 (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 addtriple if (O != null) { result.AddTriple(new RDFTriple(S, P, O)); } else { result.AddTriple(new RDFTriple(S, P, L)); } #endregion } return(result); } #endregion } catch (Exception ex) { throw new RDFModelException("Cannot deserialize N-Triples (line " + ntripleIndex + ") because: " + ex.Message, ex); } }
/// <summary> /// Selects the triples indexed by the given literal /// </summary> internal HashSet <long> SelectIndexByLiteral(RDFLiteral objectLiteral) => objectLiteral != null && this.Literals.ContainsKey(objectLiteral.PatternMemberID) ? this.Literals[objectLiteral.PatternMemberID] : EmptyHashSet;
/// <summary> /// Gets a memory store containing quadruples with the specified literal /// </summary> public override RDFStore SelectQuadruplesByLiteral(RDFLiteral objectLiteral) { RDFMemoryStore result = new RDFMemoryStore(); if (objectLiteral != null) { try { //Open connection this.Connection.Open(); //Modify command this.Commands["SELECT"].CommandText += " WHERE tripleflavor = 2 AND objectid = @OBJID"; //Add parameters this.Commands["SELECT"].Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); //Valorize parameters this.Commands["SELECT"].Parameters["OBJID"].Value = objectLiteral.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> /// Removes the quadruples with the given literal as object /// </summary> public override RDFStore RemoveQuadruplesByLiteral(RDFLiteral literalObject) { if (literalObject != null) { try { //Modify command this.Commands["DELETE"].CommandText = "DELETE FROM public.\"quadruples\" WHERE tripleflavor = 2 AND objectid = @OBJID"; this.Commands["DELETE"].Parameters.Clear(); //Open connection this.Connection.Open(); //Open transaction this.Commands["DELETE"].Transaction = this.Commands["DELETE"].Connection.BeginTransaction(); //Valorize parameters this.Commands["DELETE"].Parameters.Add(new NpgsqlParameter("OBJID", NpgsqlDbType.Bigint)); this.Commands["DELETE"].Parameters["OBJID"].Value = literalObject.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 public.\"quadruples\" WHERE quadrupleid = @QID"; this.Commands["DELETE"].Parameters.Clear(); this.Commands["DELETE"].Parameters.Add(new NpgsqlParameter("QID", NpgsqlDbType.Bigint)); } catch (Exception ex) { //Close transaction this.Commands["DELETE"].Transaction.Rollback(); //Close connection this.Connection.Close(); //Restore command this.Commands["DELETE"].CommandText = "DELETE FROM public.\"quadruples\" WHERE quadrupleid = @QID"; this.Commands["DELETE"].Parameters.Clear(); this.Commands["DELETE"].Parameters.Add(new NpgsqlParameter("QID", NpgsqlDbType.Bigint)); //Propagate exception throw new RDFStoreException("Cannot delete data from PostgreSQL store because: " + ex.Message); } } 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 indexed by the given literal /// </summary> internal HashSet<Int64> SelectIndexByLiteral(RDFLiteral objectLiteral) { if (objectLiteral != null) { if (this.Literals.ContainsKey(objectLiteral.PatternMemberID)) { return this.Literals[objectLiteral.PatternMemberID]; } } return new HashSet<Int64>(); }
/// <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> /// Removes the triples with the given predicate and literal /// </summary> public RDFGraph RemoveTriplesByPredicateLiteral(RDFResource predicateResource, RDFLiteral objectLiteral) { if (predicateResource != null && objectLiteral != null) { foreach (var triple in this.SelectTriplesByPredicate(predicateResource) .SelectTriplesByLiteral(objectLiteral)) { //Remove triple this.Triples.Remove(triple.TripleID); //Remove index this.GraphIndex.RemoveIndex(triple); //Raise event RDFModelEvents.RaiseOnTripleRemoved(String.Format("Triple '{0}' has been removed from the Graph '{1}'.", triple, this)); } } return(this); }
/// <summary> /// Gets a memory store containing quadruples with the specified literal /// </summary> public override RDFStore SelectQuadruplesByLiteral(RDFLiteral objectLiteral) { RDFMemoryStore result = new RDFMemoryStore(); if (objectLiteral != null) { try { //Open connection this.Connection.Open(); //Modify command this.Commands["SELECT"].CommandText += " WHERE TripleFlavor = 2 AND ObjectID = @OBJID"; //Add parameters this.Commands["SELECT"].Parameters.Add(new SQLiteParameter("OBJID", DbType.Int64)); //Prepare command this.Commands["SELECT"].Prepare(); //Valorize parameters this.Commands["SELECT"].Parameters["OBJID"].Value = objectLiteral.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 the subgraph containing triples with the specified literal as object /// </summary> public RDFGraph SelectTriplesByLiteral(RDFLiteral objectLiteral) { return(new RDFGraph(RDFModelUtilities.SelectTriples(this, null, null, null, objectLiteral))); }