Skip to content

Commit

Permalink
#345 Added ability to create a quadruple from a triple
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed Nov 6, 2024
1 parent f0cdd3e commit 7161acb
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 10 deletions.
8 changes: 4 additions & 4 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.12.2</Version>
<Version>3.13.0</Version>
<Authors>Marco De Salvo</Authors>
<Copyright>Marco De Salvo</Copyright>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -12,9 +12,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
<PackageReference Include="WireMock.Net" Version="1.6.6" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.2" />
<PackageReference Include="WireMock.Net" Version="1.6.7" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
58 changes: 53 additions & 5 deletions RDFSharp.Test/Store/RDFQuadrupleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,54 @@ namespace RDFSharp.Test.Store
public class RDFQuadrupleTest
{
#region Tests
[TestMethod]
public void ShouldCreateQuadrupleFromSPOTriple()
{
RDFQuadruple quadruple = new RDFQuadruple(new RDFContext(), new RDFTriple(new RDFResource("http://subj/"), new RDFResource("http://pred/"), new RDFResource("http://obj/")));
Assert.IsNotNull(quadruple);
Assert.IsTrue(quadruple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO);
Assert.IsTrue(quadruple.Context.Equals(new RDFContext()));
Assert.IsTrue(quadruple.Subject.Equals(new RDFResource("http://subj/")));
Assert.IsTrue(quadruple.Predicate.Equals(new RDFResource("http://pred/")));
Assert.IsTrue(quadruple.Object.Equals(new RDFResource("http://obj/")));
Assert.IsTrue(quadruple.ReificationSubject.Equals(new RDFResource(string.Concat("bnode:", quadruple.QuadrupleID.ToString()))));

string quadrupleString = quadruple.ToString();
Assert.IsTrue(quadrupleString.Equals(string.Concat(quadruple.Context.ToString(), " ", quadruple.Subject.ToString(), " ", quadruple.Predicate.ToString(), " ", quadruple.Object.ToString())));

long quadrupleID = RDFModelUtilities.CreateHash(quadrupleString);
Assert.IsTrue(quadruple.QuadrupleID.Equals(quadrupleID));

RDFQuadruple quadruple2 = new RDFQuadruple(new RDFContext(), new RDFResource("http://subj/"), new RDFResource("http://pred/"), new RDFResource("http://obj/"));
Assert.IsTrue(quadruple.Equals(quadruple2));
}

[TestMethod]
public void ShouldCreateQuadrupleFromSPLTriple()
{
RDFQuadruple quadruple = new RDFQuadruple(new RDFContext(), new RDFTriple(new RDFResource("http://subj/"), new RDFResource("http://pred/"), new RDFPlainLiteral("lit")));
Assert.IsNotNull(quadruple);
Assert.IsTrue(quadruple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL);
Assert.IsTrue(quadruple.Context.Equals(new RDFContext()));
Assert.IsTrue(quadruple.Subject.Equals(new RDFResource("http://subj/")));
Assert.IsTrue(quadruple.Predicate.Equals(new RDFResource("http://pred/")));
Assert.IsTrue(quadruple.Object.Equals(new RDFPlainLiteral("lit")));
Assert.IsTrue(quadruple.ReificationSubject.Equals(new RDFResource(string.Concat("bnode:", quadruple.QuadrupleID.ToString()))));

string quadrupleString = quadruple.ToString();
Assert.IsTrue(quadrupleString.Equals(string.Concat(quadruple.Context.ToString(), " ", quadruple.Subject.ToString(), " ", quadruple.Predicate.ToString(), " ", quadruple.Object.ToString())));

long quadrupleID = RDFModelUtilities.CreateHash(quadrupleString);
Assert.IsTrue(quadruple.QuadrupleID.Equals(quadrupleID));

RDFQuadruple quadruple2 = new RDFQuadruple(new RDFContext(), new RDFResource("http://subj/"), new RDFResource("http://pred/"), new RDFPlainLiteral("lit"));
Assert.IsTrue(quadruple.Equals(quadruple2));
}

[TestMethod]
public void ShouldThrowExceptionOnCreatingQuadrupleFromTripleBecauseOfNullTriple()
=> Assert.ThrowsException<RDFStoreException>(() => new RDFQuadruple(new RDFContext("ex:ctx"), null));

[DataTestMethod]
[DataRow("http://example.org/subj", "http://example.org/pred", "http://example.org/obj")]
public void ShouldCreateSPOQuadruple(string s, string p, string o)
Expand Down Expand Up @@ -116,22 +164,22 @@ public void ShouldCreateSPLQuadrupleFromNullInputs(string p)

[DataTestMethod]
[DataRow("http://example.org/subj", "bnode:hdh744", "http://example.org/obj")]
public void ShouldNotCreateSPOQuadrupleBecauseOfBlankPredicate(string s, string p, string o)
public void ShouldThrowExceptionOnCreatingSPOQuadrupleBecauseOfBlankPredicate(string s, string p, string o)
=> Assert.ThrowsException<RDFStoreException>(() => new RDFQuadruple(new RDFContext("ex:ctx"), new RDFResource(s), new RDFResource(p), new RDFResource(o)));

[DataTestMethod]
[DataRow("http://example.org/subj", "http://example.org/obj")]
public void ShouldNotCreateSPOQuadrupleBecauseOfNullPredicate(string s, string o)
public void ShouldThrowExceptionOnCreatingSPOQuadrupleBecauseOfNullPredicate(string s, string o)
=> Assert.ThrowsException<RDFStoreException>(() => new RDFQuadruple(new RDFContext("ex:ctx"), new RDFResource(s), null, new RDFResource(o)));

[DataTestMethod]
[DataRow("http://example.org/subj", "bnode:hdh744", "test")]
public void ShouldNotCreateSPLQuadrupleBecauseOfBlankPredicate(string s, string p, string l)
public void ShouldThrowExceptionOnCreatingSPLQuadrupleBecauseOfBlankPredicate(string s, string p, string l)
=> Assert.ThrowsException<RDFStoreException>(() => new RDFQuadruple(new RDFContext("ex:ctx"), new RDFResource(s), new RDFResource(p), new RDFPlainLiteral(l)));

[DataTestMethod]
[DataRow("http://example.org/subj", "test")]
public void ShouldNotCreateSPLQuadrupleBecauseOfNullPredicate(string s, string l)
public void ShouldThrowExceptionOnCreatingSPLQuadrupleBecauseOfNullPredicate(string s, string l)
=> Assert.ThrowsException<RDFStoreException>(() => new RDFQuadruple(new RDFContext("ex:ctx"), new RDFResource(s), null, new RDFPlainLiteral(l)));

[DataTestMethod]
Expand Down Expand Up @@ -173,4 +221,4 @@ public void ShouldReifySPLQuadruple(string s, string p, string l)
}
#endregion
}
}
}
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.12.2</Version>
<Version>3.13.0</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
19 changes: 19 additions & 0 deletions RDFSharp/Store/RDFQuadruple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ public class RDFQuadruple : IEquatable<RDFQuadruple>
#endregion

#region Ctors
/// <summary>
/// Triple-based ctor
/// </summary>
public RDFQuadruple(RDFContext context, RDFTriple triple)
{
#region Guards
if (triple == null)
throw new RDFStoreException("Cannot create RDFQuadruple because given \"triple\" parameter is null");
#endregion

Context = context ?? new RDFContext();
TripleFlavor = triple.TripleFlavor;
Subject = triple.Subject;
Predicate = triple.Predicate;
Object = triple.Object;
LazyQuadrupleID = new Lazy<long>(() => RDFModelUtilities.CreateHash(ToString()));
LazyReificationSubject = new Lazy<RDFResource>(() => new RDFResource(string.Concat("bnode:", QuadrupleID.ToString())));
}

/// <summary>
/// SPO-flavor ctor
/// </summary>
Expand Down

0 comments on commit 7161acb

Please sign in to comment.