From 04dfb27e8647d78d5735e4f4c23e057090f04d63 Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 8 Dec 2018 17:12:22 +0100 Subject: [PATCH] Code cleanups --- RDFSharp/Model/RDFGraph.cs | 15 ---- RDFSharp/Store/Engines/RDFMemoryStore.cs | 92 ++++++++++++++---------- RDFSharp/Store/RDFStore.cs | 4 +- 3 files changed, 56 insertions(+), 55 deletions(-) diff --git a/RDFSharp/Model/RDFGraph.cs b/RDFSharp/Model/RDFGraph.cs index 5ee99144..8d5a0d79 100644 --- a/RDFSharp/Model/RDFGraph.cs +++ b/RDFSharp/Model/RDFGraph.cs @@ -146,21 +146,6 @@ public RDFGraph AddTriple(RDFTriple triple) { } return this; } - /// - /// Adds the given triple to the graph, avoiding duplicate insertions - /// (this is internal version which does not raise events) - /// - internal RDFGraph AddTripleInternal(RDFTriple triple) { - if (triple != null) { - if (!this.Triples.ContainsKey(triple.TripleID)) { - //Add triple - this.Triples.Add(triple.TripleID, triple); - //Add index - this.GraphIndex.AddIndex(triple); - } - } - return this; - } /// /// Adds the given container to the graph diff --git a/RDFSharp/Store/Engines/RDFMemoryStore.cs b/RDFSharp/Store/Engines/RDFMemoryStore.cs index 48475a57..b04295ee 100644 --- a/RDFSharp/Store/Engines/RDFMemoryStore.cs +++ b/RDFSharp/Store/Engines/RDFMemoryStore.cs @@ -148,10 +148,12 @@ public override RDFStore MergeGraph(RDFGraph graph) { public override RDFStore AddQuadruple(RDFQuadruple quadruple) { if (quadruple != null) { if (!this.Quadruples.ContainsKey(quadruple.QuadrupleID)) { + //Add quadruple this.Quadruples.Add(quadruple.QuadrupleID, quadruple); - RDFStoreEvents.RaiseOnQuadrupleAdded(String.Format("Quadruple '{0}' has been added to the Store '{1}'.", quadruple, this)); - //Index update + //Add index this.StoreIndex.AddIndex(quadruple); + //Raise event + RDFStoreEvents.RaiseOnQuadrupleAdded(String.Format("Quadruple '{0}' has been added to the Store '{1}'.", quadruple, this)); } } return this; @@ -164,10 +166,12 @@ public override RDFStore AddQuadruple(RDFQuadruple quadruple) { /// public override RDFStore RemoveQuadruple(RDFQuadruple quadruple) { if (this.ContainsQuadruple(quadruple)) { + //Remove quadruple this.Quadruples.Remove(quadruple.QuadrupleID); - RDFStoreEvents.RaiseOnQuadrupleRemoved(String.Format("Quadruple '{0}' has been removed from the Store '{1}'.", quadruple, this)); - //Index update + //Remove index this.StoreIndex.RemoveIndex(quadruple); + //Raise event + RDFStoreEvents.RaiseOnQuadrupleRemoved(String.Format("Quadruple '{0}' has been removed from the Store '{1}'.", quadruple, this)); } return this; } @@ -176,12 +180,14 @@ public override RDFStore RemoveQuadruple(RDFQuadruple quadruple) { /// Removes the quadruples with the given context /// public override RDFStore RemoveQuadruplesByContext(RDFContext contextResource) { - if (contextResource != null) { - foreach (var quadruple in this.SelectQuadruplesByContext(contextResource)) { - this.Quadruples.Remove(quadruple.QuadrupleID); - RDFStoreEvents.RaiseOnQuadrupleRemoved(String.Format("Quadruple '{0}' has been removed from the Store '{1}'.", quadruple, this)); - //Index update - this.StoreIndex.RemoveIndex(quadruple); + 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; @@ -191,12 +197,14 @@ public override RDFStore RemoveQuadruplesByContext(RDFContext contextResource) { /// Removes the quadruples with the given subject /// public override RDFStore RemoveQuadruplesBySubject(RDFResource subjectResource) { - if (subjectResource != null) { - foreach (var quadruple in this.SelectQuadruplesBySubject(subjectResource)) { - this.Quadruples.Remove(quadruple.QuadrupleID); - RDFStoreEvents.RaiseOnQuadrupleRemoved(String.Format("Quadruple '{0}' has been removed from the Store '{1}'.", quadruple, this)); - //Index update - this.StoreIndex.RemoveIndex(quadruple); + if (subjectResource != null) { + foreach (var quad in this.SelectQuadruplesBySubject(subjectResource)) { + //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; @@ -206,12 +214,14 @@ public override RDFStore RemoveQuadruplesBySubject(RDFResource subjectResource) /// Removes the quadruples with the given (non-blank) predicate /// public override RDFStore RemoveQuadruplesByPredicate(RDFResource predicateResource) { - if (predicateResource != null && !predicateResource.IsBlank) { - foreach (var quadruple in this.SelectQuadruplesByPredicate(predicateResource)) { - this.Quadruples.Remove(quadruple.QuadrupleID); - RDFStoreEvents.RaiseOnQuadrupleRemoved(String.Format("Quadruple '{0}' has been removed from the Store '{1}'.", quadruple, this)); - //Index update - this.StoreIndex.RemoveIndex(quadruple); + if (predicateResource != null && !predicateResource.IsBlank) { + foreach (var quad in this.SelectQuadruplesByPredicate(predicateResource)) { + //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; @@ -221,12 +231,14 @@ public override RDFStore RemoveQuadruplesByPredicate(RDFResource predicateResour /// Removes the quadruples with the given resource as object /// public override RDFStore RemoveQuadruplesByObject(RDFResource objectResource) { - if (objectResource != null) { - foreach (var quadruple in this.SelectQuadruplesByObject(objectResource)) { - this.Quadruples.Remove(quadruple.QuadrupleID); - RDFStoreEvents.RaiseOnQuadrupleRemoved(String.Format("Quadruple '{0}' has been removed from the Store '{1}'.", quadruple, this)); - //Index update - this.StoreIndex.RemoveIndex(quadruple); + if (objectResource != null) { + foreach (var quad in this.SelectQuadruplesByObject(objectResource)) { + //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; @@ -236,12 +248,14 @@ public override RDFStore RemoveQuadruplesByObject(RDFResource objectResource) { /// Removes the quadruples with the given literal as object /// public override RDFStore RemoveQuadruplesByLiteral(RDFLiteral literalObject) { - if (literalObject != null) { - foreach (var quadruple in this.SelectQuadruplesByLiteral(literalObject)) { - this.Quadruples.Remove(quadruple.QuadrupleID); - RDFStoreEvents.RaiseOnQuadrupleRemoved(String.Format("Quadruple '{0}' has been removed from the Store '{1}'.", quadruple, this)); - //Index update - this.StoreIndex.RemoveIndex(quadruple); + if (literalObject != null) { + foreach (var quad in this.SelectQuadruplesByLiteral(literalObject)) { + //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; @@ -251,10 +265,12 @@ public override RDFStore RemoveQuadruplesByLiteral(RDFLiteral literalObject) { /// Clears the quadruples of the store /// public override void ClearQuadruples() { + //Clear quadruples this.Quadruples.Clear(); - RDFStoreEvents.RaiseOnStoreCleared(String.Format("Store '{0}' has been cleared.", this)); - //Index update + //Clear index this.StoreIndex.ClearIndex(); + //Raise event + RDFStoreEvents.RaiseOnStoreCleared(String.Format("Store '{0}' has been cleared.", this)); } #endregion @@ -350,12 +366,12 @@ public RDFMemoryStore DifferenceWith(RDFStore store) { #region Convert - #region Importers + #region Import /// /// Creates a memory store from a file of the given RDF format. /// public static RDFMemoryStore FromFile(RDFStoreEnums.RDFFormats rdfFormat, String filepath) { - if (filepath != null) { + if(!String.IsNullOrEmpty(filepath)) { if (File.Exists(filepath)) { switch (rdfFormat) { case RDFStoreEnums.RDFFormats.TriX: diff --git a/RDFSharp/Store/RDFStore.cs b/RDFSharp/Store/RDFStore.cs index 87f9c92b..fa8a4912 100644 --- a/RDFSharp/Store/RDFStore.cs +++ b/RDFSharp/Store/RDFStore.cs @@ -273,12 +273,12 @@ internal abstract RDFMemoryStore SelectQuadruples(RDFContext contextResource, #region Convert - #region Exporters + #region Export /// /// Writes the store into a file in the given RDF format. /// public void ToFile(RDFStoreEnums.RDFFormats rdfFormat, String filepath) { - if (filepath != null) { + if(!String.IsNullOrEmpty(filepath)) { switch (rdfFormat) { case RDFStoreEnums.RDFFormats.NQuads: RDFNQuads.Serialize(this, filepath);