Skip to content

Commit

Permalink
#310 FIX IsInference information lost when compressing to IndexedTriple
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed Aug 27, 2023
1 parent afdf5b7 commit 0dd5dd7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
14 changes: 14 additions & 0 deletions RDFSharp.Test/Model/RDFGraphTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using System.IO;
using System.Linq;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -221,6 +222,19 @@ public void ShouldAddTriple()
Assert.IsTrue(graph.IndexedTriples.ContainsKey(triple.TripleID));
}

[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();
graph.AddTriple(triple);

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

}

[TestMethod]
public void ShouldNotAddDuplicateTriples()
{
Expand Down
6 changes: 3 additions & 3 deletions RDFSharp.Test/RDFSharp.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
<AssemblyTitle>RDFSharp.Test</AssemblyTitle>
<AssemblyName>RDFSharp.Test</AssemblyName>
<AssemblyVersion>$(Version)</AssemblyVersion>
<Version>3.7.0</Version>
<Version>3.7.1</Version>
<Authors>Marco De Salvo</Authors>
<Copyright>Marco De Salvo</Copyright>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<Product>RDFSharp</Product>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="WireMock.Net" Version="1.5.34" />
<PackageReference Include="WireMock.Net" Version="1.5.35" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
16 changes: 14 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])
: new RDFTriple(GraphIndex.ResourcesRegister[indexedTriple.SubjectID], GraphIndex.ResourcesRegister[indexedTriple.PredicateID], GraphIndex.LiteralsRegister[indexedTriple.ObjectID]);
? 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 };
}
}
}
Expand Down Expand Up @@ -595,8 +595,10 @@ public RDFGraph DifferenceWith(RDFGraph graph)
/// </summary>
public void ToFile(RDFModelEnums.RDFFormats rdfFormat, string filepath)
{
#region Guards
if (string.IsNullOrEmpty(filepath))
throw new RDFModelException("Cannot write RDF graph to file because given \"filepath\" parameter is null or empty.");
#endregion

switch (rdfFormat)
{
Expand All @@ -620,8 +622,10 @@ public void ToFile(RDFModelEnums.RDFFormats rdfFormat, string filepath)
/// </summary>
public void ToStream(RDFModelEnums.RDFFormats rdfFormat, Stream outputStream)
{
#region Guards
if (outputStream == null)
throw new RDFModelException("Cannot write RDF graph to stream because given \"outputStream\" parameter is null.");
#endregion

switch (rdfFormat)
{
Expand Down Expand Up @@ -673,10 +677,12 @@ public DataTable ToDataTable()
/// </summary>
public static RDFGraph FromFile(RDFModelEnums.RDFFormats rdfFormat, string filepath)
{
#region Guards
if (string.IsNullOrEmpty(filepath))
throw new RDFModelException("Cannot read RDF graph from file because given \"filepath\" parameter is null or empty.");
if (!File.Exists(filepath))
throw new RDFModelException("Cannot read RDF graph from file because given \"filepath\" parameter (" + filepath + ") does not indicate an existing file.");
#endregion

switch (rdfFormat)
{
Expand All @@ -699,8 +705,10 @@ public static RDFGraph FromFile(RDFModelEnums.RDFFormats rdfFormat, string filep
public static RDFGraph FromStream(RDFModelEnums.RDFFormats rdfFormat, Stream inputStream) => FromStream(rdfFormat, inputStream, null);
internal static RDFGraph FromStream(RDFModelEnums.RDFFormats rdfFormat, Stream inputStream, Uri graphContext)
{
#region Guards
if (inputStream == null)
throw new RDFModelException("Cannot read RDF graph from stream because given \"inputStream\" parameter is null.");
#endregion

switch (rdfFormat)
{
Expand All @@ -722,12 +730,14 @@ internal static RDFGraph FromStream(RDFModelEnums.RDFFormats rdfFormat, Stream i
/// </summary>
public static RDFGraph FromDataTable(DataTable table)
{
#region Guards
if (table == null)
throw new RDFModelException("Cannot read RDF graph from datatable because given \"table\" parameter is null.");
if (table.Columns.Count != 3)
throw new RDFModelException("Cannot read RDF graph from datatable because given \"table\" parameter does not have exactly 3 columns.");
if (!(table.Columns.Contains("?SUBJECT") && table.Columns.Contains("?PREDICATE") && table.Columns.Contains("?OBJECT")))
throw new RDFModelException("Cannot read RDF graph from datatable because given \"table\" parameter does not have the required columns \"?SUBJECT\", \"?PREDICATE\", \"?OBJECT\".");
#endregion

RDFGraph result = new RDFGraph();

Expand Down Expand Up @@ -780,10 +790,12 @@ public static RDFGraph FromDataTable(DataTable table)
/// </summary>
public static RDFGraph FromUri(Uri uri, int timeoutMilliseconds = 20000)
{
#region Guards
if (uri == null)
throw new RDFModelException("Cannot read RDF graph from Uri because given \"uri\" parameter is null.");
if (!uri.IsAbsoluteUri)
throw new RDFModelException("Cannot read RDF graph from Uri because given \"uri\" parameter does not represent an absolute Uri.");
#endregion

RDFGraph result = new RDFGraph();
try
Expand Down
23 changes: 15 additions & 8 deletions RDFSharp/Model/RDFTriple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class RDFTriple : IEquatable<RDFTriple>
/// </summary>
public RDFModelEnums.RDFTripleFlavors TripleFlavor { get; internal set; }

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

/// <summary>
/// Member acting as subject token of the triple
/// </summary>
Expand All @@ -52,11 +57,6 @@ public class RDFTriple : IEquatable<RDFTriple>
/// </summary>
public RDFPatternMember Object { get; internal set; }

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

/// <summary>
/// Subject of the triple's reification
/// </summary>
Expand All @@ -78,10 +78,10 @@ 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();
IsInference = false;
LazyTripleID = new Lazy<long>(() => RDFModelUtilities.CreateHash(ToString()));
LazyReificationSubject = new Lazy<RDFResource>(() => new RDFResource(string.Concat("bnode:", TripleID.ToString())));
}
Expand All @@ -99,10 +99,10 @@ 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);
IsInference = false;
LazyTripleID = new Lazy<long>(() => RDFModelUtilities.CreateHash(ToString()));
LazyReificationSubject = new Lazy<RDFResource>(() => new RDFResource(string.Concat("bnode:", TripleID.ToString())));
}
Expand All @@ -113,6 +113,7 @@ public RDFTriple(RDFResource subj, RDFResource pred, RDFLiteral lit)
internal RDFTriple(RDFIndexedTriple indexedTriple, RDFGraphIndex graphIndex)
{
TripleFlavor = indexedTriple.TripleFlavor;
IsInference = indexedTriple.IsInference;
Subject = graphIndex.ResourcesRegister[indexedTriple.SubjectID];
Predicate = graphIndex.ResourcesRegister[indexedTriple.PredicateID];
if (indexedTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO)
Expand Down Expand Up @@ -204,6 +205,11 @@ internal class RDFIndexedTriple : IEquatable<RDFIndexedTriple>
/// Flavor of the triple
/// </summary>
internal RDFModelEnums.RDFTripleFlavors TripleFlavor { get; set; }

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

#region Ctor
Expand All @@ -213,10 +219,11 @@ internal class RDFIndexedTriple : IEquatable<RDFIndexedTriple>
internal RDFIndexedTriple(RDFTriple triple)
{
TripleFlavor = triple.TripleFlavor;
IsInference = triple.IsInference;
TripleID = triple.TripleID;
SubjectID = triple.Subject.PatternMemberID;
PredicateID = triple.Predicate.PatternMemberID;
ObjectID = triple.Object.PatternMemberID;
ObjectID = triple.Object.PatternMemberID;
}
#endregion

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.7.0</Version>
<Version>3.7.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 0dd5dd7

Please sign in to comment.