From 7161acb196d0c50236a3070403182c54b5b20598 Mon Sep 17 00:00:00 2001 From: mdesalvo Date: Wed, 6 Nov 2024 17:05:43 +0100 Subject: [PATCH] #345 Added ability to create a quadruple from a triple --- RDFSharp.Test/RDFSharp.Test.csproj | 8 ++-- RDFSharp.Test/Store/RDFQuadrupleTest.cs | 58 ++++++++++++++++++++++--- RDFSharp/RDFSharp.csproj | 2 +- RDFSharp/Store/RDFQuadruple.cs | 19 ++++++++ 4 files changed, 77 insertions(+), 10 deletions(-) diff --git a/RDFSharp.Test/RDFSharp.Test.csproj b/RDFSharp.Test/RDFSharp.Test.csproj index 2c01e9d2..6da98a4d 100644 --- a/RDFSharp.Test/RDFSharp.Test.csproj +++ b/RDFSharp.Test/RDFSharp.Test.csproj @@ -3,7 +3,7 @@ RDFSharp.Test RDFSharp.Test $(Version) - 3.12.2 + 3.13.0 Marco De Salvo Marco De Salvo net8.0 @@ -12,9 +12,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/RDFSharp.Test/Store/RDFQuadrupleTest.cs b/RDFSharp.Test/Store/RDFQuadrupleTest.cs index 4c3e255a..8f156c2e 100644 --- a/RDFSharp.Test/Store/RDFQuadrupleTest.cs +++ b/RDFSharp.Test/Store/RDFQuadrupleTest.cs @@ -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(() => 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) @@ -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(() => 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(() => 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(() => 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(() => new RDFQuadruple(new RDFContext("ex:ctx"), new RDFResource(s), null, new RDFPlainLiteral(l))); [DataTestMethod] @@ -173,4 +221,4 @@ public void ShouldReifySPLQuadruple(string s, string p, string l) } #endregion } -} +} \ No newline at end of file diff --git a/RDFSharp/RDFSharp.csproj b/RDFSharp/RDFSharp.csproj index 7d3e4cd6..c15e6e98 100644 --- a/RDFSharp/RDFSharp.csproj +++ b/RDFSharp/RDFSharp.csproj @@ -3,7 +3,7 @@ RDFSharp RDFSharp $(Version) - 3.12.2 + 3.13.0 Marco De Salvo Marco De Salvo Lightweight and friendly .NET library for realizing Semantic Web applications diff --git a/RDFSharp/Store/RDFQuadruple.cs b/RDFSharp/Store/RDFQuadruple.cs index 71baaed2..f607b9c5 100644 --- a/RDFSharp/Store/RDFQuadruple.cs +++ b/RDFSharp/Store/RDFQuadruple.cs @@ -65,6 +65,25 @@ public class RDFQuadruple : IEquatable #endregion #region Ctors + /// + /// Triple-based ctor + /// + 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(() => RDFModelUtilities.CreateHash(ToString())); + LazyReificationSubject = new Lazy(() => new RDFResource(string.Concat("bnode:", QuadrupleID.ToString()))); + } + /// /// SPO-flavor ctor ///