Skip to content

Commit

Permalink
Extend capabilities of applying metadata to triples
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed Nov 1, 2023
1 parent cdee28f commit 61fe617
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 24 deletions.
26 changes: 23 additions & 3 deletions RDFSharp.Test/Model/RDFGraphTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,39 @@ public void ShouldAddTriple()

Assert.IsTrue(graph.TriplesCount == 1);
Assert.IsTrue(graph.IndexedTriples.ContainsKey(triple.TripleID));
Assert.IsTrue(graph.Count(t => t.TripleMetadata is null) == 1);
Assert.IsTrue(graph.Count(t => t.TripleMetadata == RDFModelEnums.RDFTripleMetadata.IsInference) == 0);
Assert.IsTrue(graph.Count(t => t.TripleMetadata == RDFModelEnums.RDFTripleMetadata.IsImport) == 0);
}

[TestMethod]
public void ShouldAddTripleAsInference()
{
RDFGraph graph = new RDFGraph();
RDFTriple triple = new RDFTriple(new RDFResource("http://subj/"), new RDFResource("http://pred/"), new RDFResource("http://obj/")).SetInference();
RDFTriple triple = new RDFTriple(new RDFResource("http://subj/"), new RDFResource("http://pred/"), new RDFResource("http://obj/"))
.SetMetadata(RDFModelEnums.RDFTripleMetadata.IsInference);
graph.AddTriple(triple);

Assert.IsTrue(graph.TriplesCount == 1);
Assert.IsTrue(graph.IndexedTriples.ContainsKey(triple.TripleID));
Assert.IsTrue(graph.Count(t => t.IsInference) == 1);

Assert.IsTrue(graph.Count(t => t.TripleMetadata is null) == 0);
Assert.IsTrue(graph.Count(t => t.TripleMetadata == RDFModelEnums.RDFTripleMetadata.IsInference) == 1);
Assert.IsTrue(graph.Count(t => t.TripleMetadata == RDFModelEnums.RDFTripleMetadata.IsImport) == 0);
}

[TestMethod]
public void ShouldAddTripleAsImport()
{
RDFGraph graph = new RDFGraph();
RDFTriple triple = new RDFTriple(new RDFResource("http://subj/"), new RDFResource("http://pred/"), new RDFResource("http://obj/"))
.SetMetadata(RDFModelEnums.RDFTripleMetadata.IsImport);
graph.AddTriple(triple);

Assert.IsTrue(graph.TriplesCount == 1);
Assert.IsTrue(graph.IndexedTriples.ContainsKey(triple.TripleID));
Assert.IsTrue(graph.Count(t => t.TripleMetadata is null) == 0);
Assert.IsTrue(graph.Count(t => t.TripleMetadata == RDFModelEnums.RDFTripleMetadata.IsInference) == 0);
Assert.IsTrue(graph.Count(t => t.TripleMetadata == RDFModelEnums.RDFTripleMetadata.IsImport) == 1);
}

[TestMethod]
Expand Down
6 changes: 3 additions & 3 deletions RDFSharp.Test/Model/RDFTripleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public void ShouldCreateSPOTriple(string s, string p, string o)
RDFTriple triple2 = new RDFTriple(subj, pred, obj);
Assert.IsTrue(triple.Equals(triple2));

Assert.IsFalse(triple.IsInference);
triple.SetInference();
Assert.IsTrue(triple.IsInference);
Assert.IsNull(triple.TripleMetadata);
triple.SetMetadata(RDFModelEnums.RDFTripleMetadata.IsInference);
Assert.IsTrue(triple.TripleMetadata == RDFModelEnums.RDFTripleMetadata.IsInference);
}

[DataTestMethod]
Expand Down
4 changes: 2 additions & 2 deletions RDFSharp.Test/RDFSharp.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<AssemblyTitle>RDFSharp.Test</AssemblyTitle>
<AssemblyName>RDFSharp.Test</AssemblyName>
<AssemblyVersion>$(Version)</AssemblyVersion>
<Version>3.8.0</Version>
<Version>3.8.1</Version>
<Authors>Marco De Salvo</Authors>
<Copyright>Marco De Salvo</Copyright>
<TargetFramework>net6.0</TargetFramework>
Expand All @@ -14,7 +14,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="WireMock.Net" Version="1.5.37" />
<PackageReference Include="WireMock.Net" Version="1.5.39" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
4 changes: 2 additions & 2 deletions RDFSharp/Model/RDFGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public IEnumerator<RDFTriple> TriplesEnumerator
foreach (RDFIndexedTriple indexedTriple in IndexedTriples.Values)
{
yield return indexedTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO
? new RDFTriple(GraphIndex.ResourcesRegister[indexedTriple.SubjectID], GraphIndex.ResourcesRegister[indexedTriple.PredicateID], GraphIndex.ResourcesRegister[indexedTriple.ObjectID]) { IsInference = indexedTriple.IsInference }
: new RDFTriple(GraphIndex.ResourcesRegister[indexedTriple.SubjectID], GraphIndex.ResourcesRegister[indexedTriple.PredicateID], GraphIndex.LiteralsRegister[indexedTriple.ObjectID]) { IsInference = indexedTriple.IsInference };
? new RDFTriple(GraphIndex.ResourcesRegister[indexedTriple.SubjectID], GraphIndex.ResourcesRegister[indexedTriple.PredicateID], GraphIndex.ResourcesRegister[indexedTriple.ObjectID]) { TripleMetadata = indexedTriple.TripleMetadata }
: new RDFTriple(GraphIndex.ResourcesRegister[indexedTriple.SubjectID], GraphIndex.ResourcesRegister[indexedTriple.PredicateID], GraphIndex.LiteralsRegister[indexedTriple.ObjectID]) { TripleMetadata = indexedTriple.TripleMetadata };
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion RDFSharp/Model/RDFModelEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public enum RDFFormats
};

/// <summary>
/// RDFTripleFlavors represents an enumeration for possible triple pattern flavors.
/// RDFTripleFlavors represents an enumeration for possible triple flavors.
/// </summary>
public enum RDFTripleFlavors
{
Expand All @@ -59,6 +59,21 @@ public enum RDFTripleFlavors
SPL = 2
};

/// <summary>
/// RDFTripleMetadata represents an enumeration for possible types of triple metadata.
/// </summary>
public enum RDFTripleMetadata
{
/// <summary>
/// Indicates that the triple has been emitted in consequence of any kind of reasoning
/// </summary>
IsInference = 1,
/// <summary>
/// Indicates that the triple has been imported from an external source (e.g: an ontology)
/// </summary>
IsImport = 2
};

/// <summary>
/// RDFDatatypes represents an enumeration for supported datatypes (RDF/RDFS/XSD).
/// </summary>
Expand Down
22 changes: 10 additions & 12 deletions RDFSharp/Model/RDFTriple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public class RDFTriple : IEquatable<RDFTriple>
public RDFModelEnums.RDFTripleFlavors TripleFlavor { get; internal set; }

/// <summary>
/// Indicates that the triple has been emitted in consequence of any kind of reasoning
/// Metadata of the triple
/// </summary>
public bool IsInference { get; internal set;}
public RDFModelEnums.RDFTripleMetadata? TripleMetadata { get; internal set; }

/// <summary>
/// Member acting as subject token of the triple
Expand Down Expand Up @@ -78,7 +78,6 @@ public RDFTriple(RDFResource subj, RDFResource pred, RDFResource obj)
#endregion

TripleFlavor = RDFModelEnums.RDFTripleFlavors.SPO;
IsInference = false;
Subject = subj ?? new RDFResource();
Predicate = pred;
Object = obj ?? new RDFResource();
Expand All @@ -99,10 +98,9 @@ public RDFTriple(RDFResource subj, RDFResource pred, RDFLiteral lit)
#endregion

TripleFlavor = RDFModelEnums.RDFTripleFlavors.SPL;
IsInference = false;
Subject = subj ?? new RDFResource();
Predicate = pred;
Object = lit ?? new RDFPlainLiteral(string.Empty);
Object = lit ?? new RDFPlainLiteral(string.Empty);
LazyTripleID = new Lazy<long>(() => RDFModelUtilities.CreateHash(ToString()));
LazyReificationSubject = new Lazy<RDFResource>(() => new RDFResource(string.Concat("bnode:", TripleID.ToString())));
}
Expand All @@ -113,7 +111,7 @@ public RDFTriple(RDFResource subj, RDFResource pred, RDFLiteral lit)
internal RDFTriple(RDFIndexedTriple indexedTriple, RDFGraphIndex graphIndex)
{
TripleFlavor = indexedTriple.TripleFlavor;
IsInference = indexedTriple.IsInference;
TripleMetadata = indexedTriple.TripleMetadata;
Subject = graphIndex.ResourcesRegister[indexedTriple.SubjectID];
Predicate = graphIndex.ResourcesRegister[indexedTriple.PredicateID];
if (indexedTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO)
Expand Down Expand Up @@ -165,11 +163,11 @@ public Task<RDFAsyncGraph> ReifyTripleAsync()
=> Task.Run(() => new RDFAsyncGraph(ReifyTriple()));

/// <summary>
/// Sets the triple as emitted by any kind of reasoning
/// Sets the metadata of the triple
/// </summary>
public RDFTriple SetInference()
public RDFTriple SetMetadata(RDFModelEnums.RDFTripleMetadata? tripleMetadata)
{
IsInference = true;
TripleMetadata = tripleMetadata;
return this;
}
#endregion
Expand Down Expand Up @@ -207,9 +205,9 @@ internal class RDFIndexedTriple : IEquatable<RDFIndexedTriple>
internal RDFModelEnums.RDFTripleFlavors TripleFlavor { get; set; }

/// <summary>
/// Indicates that the triple has been emitted in consequence of any kind of reasoning
/// Metadata of the triple
/// </summary>
internal bool IsInference { get; set; }
internal RDFModelEnums.RDFTripleMetadata? TripleMetadata { get; set; }
#endregion

#region Ctor
Expand All @@ -219,7 +217,7 @@ internal class RDFIndexedTriple : IEquatable<RDFIndexedTriple>
internal RDFIndexedTriple(RDFTriple triple)
{
TripleFlavor = triple.TripleFlavor;
IsInference = triple.IsInference;
TripleMetadata = triple.TripleMetadata;
TripleID = triple.TripleID;
SubjectID = triple.Subject.PatternMemberID;
PredicateID = triple.Predicate.PatternMemberID;
Expand Down
2 changes: 1 addition & 1 deletion RDFSharp/RDFSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<AssemblyTitle>RDFSharp</AssemblyTitle>
<AssemblyName>RDFSharp</AssemblyName>
<AssemblyVersion>$(Version)</AssemblyVersion>
<Version>3.8.0</Version>
<Version>3.8.1</Version>
<Authors>Marco De Salvo</Authors>
<Copyright>Marco De Salvo</Copyright>
<Description>Lightweight and friendly .NET library for realizing Semantic Web applications</Description>
Expand Down

0 comments on commit 61fe617

Please sign in to comment.