Skip to content

Commit

Permalink
Reapply old code for collection
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed Oct 5, 2024
1 parent b685b72 commit 68f711f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
26 changes: 4 additions & 22 deletions RDFSharp.Test/Model/RDFCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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));
}

Expand All @@ -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));
}

Expand Down Expand Up @@ -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
}
}
26 changes: 18 additions & 8 deletions RDFSharp/Model/RDFCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public int ItemsCount
public IEnumerator<RDFPatternMember> ItemsEnumerator
=> Items.GetEnumerator();

/// <summary>
/// Flag indicating that this collection exceptionally accepts duplicates
/// </summary>
internal bool AcceptDuplicates { get; set; }

/// <summary>
/// List of the items collected by the collection
/// </summary>
Expand All @@ -64,11 +69,13 @@ public IEnumerator<RDFPatternMember> ItemsEnumerator
/// Default ctor to build an empty collection of the given type
/// (initial configuration of the collection is "rdf:Nil")
/// </summary>
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<RDFPatternMember>();
}
#endregion
Expand Down Expand Up @@ -115,11 +122,14 @@ public RDFCollection AddItem(RDFLiteral item)
/// </summary>
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

Expand Down Expand Up @@ -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));

Expand Down

0 comments on commit 68f711f

Please sign in to comment.