From 68f711f70d56badde50cb8cb3f817fde06ffe55d Mon Sep 17 00:00:00 2001 From: mdesalvo Date: Sat, 5 Oct 2024 11:39:45 +0200 Subject: [PATCH] Reapply old code for collection --- RDFSharp.Test/Model/RDFCollectionTest.cs | 26 ++++-------------------- RDFSharp/Model/RDFCollection.cs | 26 ++++++++++++++++-------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/RDFSharp.Test/Model/RDFCollectionTest.cs b/RDFSharp.Test/Model/RDFCollectionTest.cs index 17784e52..ce6b8691 100644 --- a/RDFSharp.Test/Model/RDFCollectionTest.cs +++ b/RDFSharp.Test/Model/RDFCollectionTest.cs @@ -37,6 +37,7 @@ public void ShouldCreateEmptyCollection(RDFModelEnums.RDFItemTypes itemType) Assert.IsTrue(coll.ItemsCount == 0); Assert.IsTrue(coll.ReificationSubject.Equals(RDFVocabulary.RDF.NIL)); Assert.IsTrue(coll.InternalReificationSubject.IsBlank); + Assert.IsFalse(coll.AcceptDuplicates); int i = 0; foreach (RDFPatternMember item in coll) i++; @@ -86,7 +87,7 @@ public void ShouldAddItemsToCollection(RDFModelEnums.RDFItemTypes itemType) coll.AddItem(new RDFResource("http://item/")); } - Assert.IsTrue(coll.ItemsCount == 2); + Assert.IsTrue(coll.ItemsCount == 1); Assert.IsFalse(coll.ReificationSubject.Equals(RDFVocabulary.RDF.NIL)); } @@ -162,7 +163,7 @@ public void ShouldNotRemoveItemsFromCollectionBecauseWrongType(RDFModelEnums.RDF coll.RemoveItem(new RDFPlainLiteral("lit")); } - Assert.IsTrue(coll.ItemsCount == 2); + Assert.IsTrue(coll.ItemsCount == 1); Assert.IsFalse(coll.ReificationSubject.Equals(RDFVocabulary.RDF.NIL)); } @@ -185,7 +186,7 @@ public void ShouldNotRemoveItemsFromCollectionBecauseNull(RDFModelEnums.RDFItemT coll.RemoveItem(null as RDFResource); } - Assert.IsTrue(coll.ItemsCount == 2); + Assert.IsTrue(coll.ItemsCount == 1); Assert.IsFalse(coll.ReificationSubject.Equals(RDFVocabulary.RDF.NIL)); } @@ -279,25 +280,6 @@ public void ShouldReifyCollectionWithTwoItems(RDFModelEnums.RDFItemTypes itemTyp Assert.IsTrue(graph.SelectTriplesByPredicate(RDFVocabulary.RDF.REST) .TriplesCount == 2); } - - [TestMethod] - public void ShouldReifyCollectionWithInternallyMixedItems() - { - RDFCollection coll = new RDFCollection(RDFModelEnums.RDFItemTypes.Resource); - coll.AddItemInternal(new RDFPlainLiteral("lit1")); - coll.AddItemInternal(new RDFResource("http://item1/")); - RDFGraph graph = coll.ReifyCollection(); - - Assert.IsNotNull(graph); - Assert.IsTrue(graph.TriplesCount == 6); - Assert.IsTrue(graph.SelectTriplesByPredicate(RDFVocabulary.RDF.TYPE) - .SelectTriplesByObject(RDFVocabulary.RDF.LIST) - .TriplesCount == 2); - Assert.IsTrue(graph.SelectTriplesByPredicate(RDFVocabulary.RDF.FIRST) - .TriplesCount == 2); - Assert.IsTrue(graph.SelectTriplesByPredicate(RDFVocabulary.RDF.REST) - .TriplesCount == 2); - } #endregion } } \ No newline at end of file diff --git a/RDFSharp/Model/RDFCollection.cs b/RDFSharp/Model/RDFCollection.cs index 51590061..3d6d92ce 100644 --- a/RDFSharp/Model/RDFCollection.cs +++ b/RDFSharp/Model/RDFCollection.cs @@ -53,6 +53,11 @@ public int ItemsCount public IEnumerator ItemsEnumerator => Items.GetEnumerator(); + /// + /// Flag indicating that this collection exceptionally accepts duplicates + /// + internal bool AcceptDuplicates { get; set; } + /// /// List of the items collected by the collection /// @@ -64,11 +69,13 @@ public IEnumerator ItemsEnumerator /// Default ctor to build an empty collection of the given type /// (initial configuration of the collection is "rdf:Nil") /// - public RDFCollection(RDFModelEnums.RDFItemTypes itemType) + public RDFCollection(RDFModelEnums.RDFItemTypes itemType) : this(itemType, false) { } + internal RDFCollection(RDFModelEnums.RDFItemTypes itemType, bool acceptDuplicates) { ItemType = itemType; ReificationSubject = RDFVocabulary.RDF.NIL; InternalReificationSubject = new RDFResource(); + AcceptDuplicates = acceptDuplicates; Items = new List(); } #endregion @@ -115,11 +122,14 @@ public RDFCollection AddItem(RDFLiteral item) /// internal void AddItemInternal(RDFPatternMember item) { - //Add item to collection - Items.Add(item); - //Update ReificationSubject (if collection has left "rdf:nil" configuration) - if (ItemsCount == 1) - ReificationSubject = InternalReificationSubject; + if (AcceptDuplicates || Items.Find(x => x.Equals(item)) == null) + { + //Add item to collection + Items.Add(item); + //Update ReificationSubject (if collection has left "rdf:nil" configuration) + if (ItemsCount == 1) + ReificationSubject = InternalReificationSubject; + } } #endregion @@ -188,8 +198,8 @@ public RDFGraph ReifyCollection() reifColl.AddTriple(new RDFTriple(reifSubj, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.LIST)); // Subject -> rdf:first -> RDFCollection.ITEM[i] - if (collectionItem is RDFResource collectionItemResource) - reifColl.AddTriple(new RDFTriple(reifSubj, RDFVocabulary.RDF.FIRST, collectionItemResource)); + if (ItemType == RDFModelEnums.RDFItemTypes.Resource) + reifColl.AddTriple(new RDFTriple(reifSubj, RDFVocabulary.RDF.FIRST, (RDFResource)collectionItem)); else reifColl.AddTriple(new RDFTriple(reifSubj, RDFVocabulary.RDF.FIRST, (RDFLiteral)collectionItem));