From 189700e12e9be95f29551c28b5bdddec5bd80e22 Mon Sep 17 00:00:00 2001 From: Marco De Salvo Date: Mon, 27 May 2024 09:32:18 +0200 Subject: [PATCH] Renamed method to ExtractDatatypeDefinitions --- RDFSharp.Test/Model/RDFGraphTest.cs | 49 +++++++++++++++++------------ RDFSharp/Model/RDFGraph.cs | 8 ++--- RDFSharp/Model/RDFModelUtilities.cs | 24 ++++++++------ 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/RDFSharp.Test/Model/RDFGraphTest.cs b/RDFSharp.Test/Model/RDFGraphTest.cs index 8182a955..770a3d5f 100644 --- a/RDFSharp.Test/Model/RDFGraphTest.cs +++ b/RDFSharp.Test/Model/RDFGraphTest.cs @@ -1825,32 +1825,41 @@ public void ShouldRaiseExceptionOnImportingFromRelativeUri() => Assert.ThrowsException(() => RDFGraph.FromUri(new Uri("/file/system", UriKind.Relative))); [TestMethod] - public void ShouldLoadDatatypesFromGraph() + public void ShouldExtractDatatypeDefinitionsFromGraph() { RDFDatatype exLength6 = new RDFDatatype(new Uri("ex:length6"), RDFModelEnums.RDFDatatypes.XSD_STRING, [ new RDFLengthFacet(6) ]); RDFDatatype exMinLength6 = new RDFDatatype(new Uri("ex:minlength6"), RDFModelEnums.RDFDatatypes.XSD_STRING, [ new RDFMinLengthFacet(6) ]); RDFDatatype exMaxLength6 = new RDFDatatype(new Uri("ex:maxlength6"), RDFModelEnums.RDFDatatypes.XSD_STRING, [ new RDFMaxLengthFacet(6) ]); RDFDatatype exPatternEx = new RDFDatatype(new Uri("ex:patternex"), RDFModelEnums.RDFDatatypes.XSD_STRING, [ new RDFPatternFacet("^ex") ]); RDFDatatype exInteger = new RDFDatatype(new Uri("ex:integer"), RDFModelEnums.RDFDatatypes.XSD_INTEGER, null); - RDFGraph graph = new RDFGraph() - .AddDatatype(exLength6) - .AddDatatype(exMinLength6) - .AddDatatype(exMaxLength6) - .AddDatatype(exPatternEx) - .AddDatatype(exInteger) - .RegisterDatatypeDefinitions(); - - Assert.IsTrue(RDFDatatypeRegister.GetDatatype("ex:length6").TargetDatatype == RDFModelEnums.RDFDatatypes.XSD_STRING - && RDFDatatypeRegister.GetDatatype("ex:length6").Facets.Single() is RDFLengthFacet lengthFacet && lengthFacet.Length == 6); - Assert.IsTrue(RDFDatatypeRegister.GetDatatype("ex:minlength6").TargetDatatype == RDFModelEnums.RDFDatatypes.XSD_STRING - && RDFDatatypeRegister.GetDatatype("ex:minlength6").Facets.Single() is RDFMinLengthFacet minlengthFacet && minlengthFacet.Length == 6); - Assert.IsTrue(RDFDatatypeRegister.GetDatatype("ex:maxlength6").TargetDatatype == RDFModelEnums.RDFDatatypes.XSD_STRING - && RDFDatatypeRegister.GetDatatype("ex:maxlength6").Facets.Single() is RDFMaxLengthFacet maxlengthFacet && maxlengthFacet.Length == 6); - Assert.IsTrue(RDFDatatypeRegister.GetDatatype("ex:patternex").TargetDatatype == RDFModelEnums.RDFDatatypes.XSD_STRING - && RDFDatatypeRegister.GetDatatype("ex:patternex").Facets.Single() is RDFPatternFacet patternFacet && patternFacet.Pattern == "^ex"); - Assert.IsTrue(RDFDatatypeRegister.GetDatatype("ex:integer").TargetDatatype == RDFModelEnums.RDFDatatypes.XSD_INTEGER - && RDFDatatypeRegister.GetDatatype("ex:integer").Facets.Count == 0); - } + RDFGraph graph = new RDFGraph() + .AddDatatype(exLength6) + .AddDatatype(exMinLength6) + .AddDatatype(exMaxLength6) + .AddDatatype(exPatternEx) + .AddDatatype(exInteger); + List datatypes = graph.ExtractDatatypeDefinitions(); + + Assert.IsTrue(datatypes.Any(dt => string.Equals(dt.URI.ToString(), "ex:length6") + && dt.TargetDatatype == RDFModelEnums.RDFDatatypes.XSD_STRING + && dt.Facets.Single() is RDFLengthFacet lengthFacet + && lengthFacet.Length == 6)); + Assert.IsTrue(datatypes.Any(dt => string.Equals(dt.URI.ToString(), "ex:minlength6") + && dt.TargetDatatype == RDFModelEnums.RDFDatatypes.XSD_STRING + && dt.Facets.Single() is RDFMinLengthFacet minlengthFacet + && minlengthFacet.Length == 6)); + Assert.IsTrue(datatypes.Any(dt => string.Equals(dt.URI.ToString(), "ex:maxlength6") + && dt.TargetDatatype == RDFModelEnums.RDFDatatypes.XSD_STRING + && dt.Facets.Single() is RDFMaxLengthFacet maxlengthFacet + && maxlengthFacet.Length == 6)); + Assert.IsTrue(datatypes.Any(dt => string.Equals(dt.URI.ToString(), "ex:patternex") + && dt.TargetDatatype == RDFModelEnums.RDFDatatypes.XSD_STRING + && dt.Facets.Single() is RDFPatternFacet patternFacet + && string.Equals(patternFacet.Pattern, "^ex"))); + Assert.IsTrue(datatypes.Any(dt => string.Equals(dt.URI.ToString(), "ex:integer") + && dt.TargetDatatype == RDFModelEnums.RDFDatatypes.XSD_INTEGER + && dt.Facets.Count == 0)); + } [TestCleanup] public void Cleanup() diff --git a/RDFSharp/Model/RDFGraph.cs b/RDFSharp/Model/RDFGraph.cs index 533a7486..eac25eb9 100644 --- a/RDFSharp/Model/RDFGraph.cs +++ b/RDFSharp/Model/RDFGraph.cs @@ -656,7 +656,7 @@ public static RDFGraph FromFile(RDFModelEnums.RDFFormats rdfFormat, string filep graph = RDFTriX.Deserialize(filepath); break; } - return graph.RegisterDatatypeDefinitions(); + return graph; } /// @@ -686,7 +686,7 @@ internal static RDFGraph FromStream(RDFModelEnums.RDFFormats rdfFormat, Stream i graph = RDFTriX.Deserialize(inputStream, graphContext); break; } - return graph.RegisterDatatypeDefinitions(); + return graph; } /// @@ -747,7 +747,7 @@ public static RDFGraph FromDataTable(DataTable table) } #endregion - return graph.RegisterDatatypeDefinitions(); + return graph; } /// @@ -819,7 +819,7 @@ public static RDFGraph FromUri(Uri uri, int timeoutMilliseconds = 20000) throw new RDFModelException($"Cannot read RDF graph from Uri {uri} because: " + ex.Message); } - return graph.RegisterDatatypeDefinitions(); + return graph; } #endregion diff --git a/RDFSharp/Model/RDFModelUtilities.cs b/RDFSharp/Model/RDFModelUtilities.cs index 0781e93d..adc26a34 100644 --- a/RDFSharp/Model/RDFModelUtilities.cs +++ b/RDFSharp/Model/RDFModelUtilities.cs @@ -307,15 +307,16 @@ internal static List SelectTriples(RDFGraph graph, RDFResource subj, } /// - /// Loads the datatype definitions contained in the given graphs and sends them to the datatype register + /// Extracts the datatype definitions contained in the given graphs (both faceted and aliases) /// - public static RDFGraph RegisterDatatypeDefinitions(this RDFGraph graph) + public static List ExtractDatatypeDefinitions(this RDFGraph graph) { #region Guards if (graph == null) - return graph; - #endregion + return new List(); + #endregion + List datatypes = new List(); foreach (RDFTriple datatypeTriple in graph[null, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDFS.DATATYPE, null]) { RDFResource datatypeIRI = (RDFResource)datatypeTriple.Subject; @@ -325,7 +326,8 @@ public static RDFGraph RegisterDatatypeDefinitions(this RDFGraph graph) && graph[datatypeIRI, RDFVocabulary.OWL.ON_DATATYPE, null, null].FirstOrDefault()?.Object is RDFResource onDatatype) { //Detect the target datatype (fallback to rdfs:Literal in case not found) - RDFDatatype targetDatatype = RDFDatatypeRegister.GetDatatype(onDatatype.ToString()) ?? RDFDatatypeRegister.RDFSLiteral; + RDFDatatype targetDatatype = RDFDatatypeRegister.GetDatatype(onDatatype.ToString()) + ?? RDFDatatypeRegister.RDFSLiteral; RDFModelEnums.RDFDatatypes targetDatatypeEnum = targetDatatype.ToString().GetEnumFromDatatype(); //Detect the constraining facets @@ -363,21 +365,23 @@ public static RDFGraph RegisterDatatypeDefinitions(this RDFGraph graph) } } - //Finally send the datatype to the register - RDFDatatypeRegister.AddDatatype(new RDFDatatype(datatypeIRI.URI, targetDatatypeEnum, targetFacets)); + //Finally collect the datatype + datatypes.Add(new RDFDatatype(datatypeIRI.URI, targetDatatypeEnum, targetFacets)); } //Try detect an alias datatype else if (graph[datatypeIRI, RDFVocabulary.OWL.EQUIVALENT_CLASS, null, null].FirstOrDefault()?.Object is RDFResource equivalentDatatype) { //Detect the target datatype (fallback to rdfs:Literal in case not found) - RDFDatatype targetDatatype = RDFDatatypeRegister.GetDatatype(equivalentDatatype.ToString()) ?? RDFDatatypeRegister.RDFSLiteral; + RDFDatatype targetDatatype = RDFDatatypeRegister.GetDatatype(equivalentDatatype.ToString()) + ?? RDFDatatypeRegister.RDFSLiteral; RDFModelEnums.RDFDatatypes targetDatatypeEnum = targetDatatype.ToString().GetEnumFromDatatype(); - RDFDatatypeRegister.AddDatatype(new RDFDatatype(datatypeIRI.URI, targetDatatypeEnum, null)); + //Finally collect the datatype + datatypes.Add(new RDFDatatype(datatypeIRI.URI, targetDatatypeEnum, null)); } } - return graph; + return datatypes; } #endregion