Skip to content

Commit

Permalink
Added RDFMinExclusiveFacet
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco De Salvo committed May 27, 2024
1 parent 170064e commit 9f7c45f
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 11 deletions.
72 changes: 72 additions & 0 deletions RDFSharp.Test/Model/Facets/RDFMinExclusiveFacetTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2012-2024 Marco De Salvo
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using RDFSharp.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

namespace RDFSharp.Test.Model
{
[TestClass]
public class RDFMinExclusiveFacetTest
{
#region Tests
[TestMethod]

public void ShouldCreateMinExclusiveFacet()
{
RDFMinExclusiveFacet facet = new RDFMinExclusiveFacet(6.145);

Assert.IsNotNull(facet);
Assert.IsTrue(facet.ExclusiveLowerBound == 6.145d);
Assert.IsTrue(facet.URI.IsBlank);
}

[TestMethod]

public void ShouldValidateMinExclusiveFacet()
{
RDFMinExclusiveFacet facet = new RDFMinExclusiveFacet(6);

Assert.IsFalse(facet.Validate("-2.0089"));
Assert.IsFalse(facet.Validate("2.047"));
Assert.IsFalse(facet.Validate(null));
Assert.IsFalse(facet.Validate(string.Empty));
Assert.IsTrue(facet.Validate("14.5773"));
Assert.IsFalse(facet.Validate("6"));
Assert.IsFalse(facet.Validate("abcdefgh"));

RDFMinExclusiveFacet facet0 = new RDFMinExclusiveFacet(-12.45);
Assert.IsFalse(facet0.Validate("-16.2442"));
Assert.IsFalse(facet0.Validate("-12.45"));
Assert.IsTrue(facet0.Validate("-12"));
}

[TestMethod]

public void ShouldConvertMinExclusiveFacetToGraph()
{
RDFMinExclusiveFacet facet = new RDFMinExclusiveFacet(6);
RDFGraph graph = facet.ToRDFGraph();

Assert.IsNotNull(graph);
Assert.IsTrue(graph.TriplesCount == 1);
Assert.IsTrue(graph.Single().Predicate.Equals(RDFVocabulary.XSD.MIN_EXCLUSIVE));
Assert.IsTrue(graph.Single().Object.Equals(new RDFTypedLiteral("6", RDFDatatypeRegister.GetDatatype(RDFVocabulary.XSD.DOUBLE.ToString()))));
}
#endregion
}
}
6 changes: 6 additions & 0 deletions RDFSharp.Test/Model/RDFGraphTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,7 @@ public void ShouldExtractDatatypeDefinitionsFromGraph()
RDFDatatype exMaxLength6 = new RDFDatatype(new Uri("ex:maxlength6"), RDFModelEnums.RDFDatatypes.XSD_STRING, [ new RDFMaxLengthFacet(6) ]);
RDFDatatype exMaxInclusive6 = new RDFDatatype(new Uri("ex:maxinclusive6"), RDFModelEnums.RDFDatatypes.XSD_DOUBLE, [new RDFMaxInclusiveFacet(6)]);
RDFDatatype exMaxExclusive6 = new RDFDatatype(new Uri("ex:maxexclusive6"), RDFModelEnums.RDFDatatypes.XSD_DOUBLE, [new RDFMaxExclusiveFacet(6)]);
RDFDatatype exMinExclusive6 = new RDFDatatype(new Uri("ex:minexclusive6"), RDFModelEnums.RDFDatatypes.XSD_DOUBLE, [new RDFMinExclusiveFacet(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()
Expand All @@ -1840,6 +1841,7 @@ public void ShouldExtractDatatypeDefinitionsFromGraph()
.AddDatatype(exMaxLength6)
.AddDatatype(exMaxInclusive6)
.AddDatatype(exMaxExclusive6)
.AddDatatype(exMinExclusive6)
.AddDatatype(exPatternEx)
.AddDatatype(exInteger);
List<RDFDatatype> datatypes = graph.ExtractDatatypeDefinitions();
Expand All @@ -1864,6 +1866,10 @@ public void ShouldExtractDatatypeDefinitionsFromGraph()
&& dt.TargetDatatype == RDFModelEnums.RDFDatatypes.XSD_DOUBLE
&& dt.Facets.Single() is RDFMaxExclusiveFacet maxexclusiveFacet
&& maxexclusiveFacet.ExclusiveUpperBound == 6));
Assert.IsTrue(datatypes.Any(dt => string.Equals(dt.URI.ToString(), "ex:minexclusive6")
&& dt.TargetDatatype == RDFModelEnums.RDFDatatypes.XSD_DOUBLE
&& dt.Facets.Single() is RDFMinExclusiveFacet minexclusiveFacet
&& minexclusiveFacet.ExclusiveLowerBound == 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
Expand Down
2 changes: 1 addition & 1 deletion RDFSharp/Model/Facets/RDFMaxExclusiveFacet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class RDFMaxExclusiveFacet : RDFFacet

#region Ctors
/// <summary>
/// Builds a facet requiring the given exact MaxExclusive
/// Builds a facet requiring the given exclusive upper bound
/// </summary>
public RDFMaxExclusiveFacet(double exclusiveUpperBound)
=> ExclusiveUpperBound = exclusiveUpperBound;
Expand Down
2 changes: 1 addition & 1 deletion RDFSharp/Model/Facets/RDFMaxInclusiveFacet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class RDFMaxInclusiveFacet : RDFFacet

#region Ctors
/// <summary>
/// Builds a facet requiring the given exact MaxInclusive
/// Builds a facet requiring the given inclusive upper bound
/// </summary>
public RDFMaxInclusiveFacet(double inclusiveUpperBound)
=> InclusiveUpperBound = inclusiveUpperBound;
Expand Down
62 changes: 62 additions & 0 deletions RDFSharp/Model/Facets/RDFMinExclusiveFacet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2012-2024 Marco De Salvo
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Globalization;

namespace RDFSharp.Model
{
/// <summary>
/// RDFMinExclusiveFacet represents a constraint requiring the values of a literal to have a minimum numeric lower bound (excluded)
/// </summary>
public class RDFMinExclusiveFacet : RDFFacet
{
#region Properties
/// <summary>
/// Minimum numeric lower bound (excluded) required by the facet
/// </summary>
public double ExclusiveLowerBound { get; internal set; }
#endregion

#region Ctors
/// <summary>
/// Builds a facet requiring the given exclusive lower bound
/// </summary>
public RDFMinExclusiveFacet(double exclusiveLowerBound)
=> ExclusiveLowerBound = exclusiveLowerBound;
#endregion

#region Methods
/// <summary>
/// Gives a graph representation of the MinExclusive facet
/// </summary>
public override RDFGraph ToRDFGraph()
=> new RDFGraph(new List<RDFTriple>() {
new RDFTriple(URI, RDFVocabulary.XSD.MIN_EXCLUSIVE, new RDFTypedLiteral(Convert.ToString(ExclusiveLowerBound, CultureInfo.InvariantCulture), RDFModelEnums.RDFDatatypes.XSD_DOUBLE)) });

/// <summary>
/// Validates the given literal value against the MinExclusive facet
/// </summary>
public override bool Validate(string literalValue)
{
if (double.TryParse(literalValue, NumberStyles.Integer | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out double parseLiteralValue))
return parseLiteralValue > ExclusiveLowerBound;
return false;
}
#endregion
}
}
25 changes: 16 additions & 9 deletions RDFSharp/Model/RDFModelUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,29 +342,36 @@ public static List<RDFDatatype> ExtractDatatypeDefinitions(this RDFGraph graph)
targetFacets.Add(new RDFLengthFacet(facetLengthValue));
continue;
}
//xsd:maxInclusive
if (graph[facet, RDFVocabulary.XSD.MAX_INCLUSIVE, null, null].FirstOrDefault()?.Object is RDFTypedLiteral facetMaxInclusive
&& facetMaxInclusive.HasDecimalDatatype() && double.TryParse(facetMaxInclusive.Value, NumberStyles.Integer | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out double facetMaxInclusiveValue))
{
targetFacets.Add(new RDFMaxInclusiveFacet(facetMaxInclusiveValue));
continue;
}
//xsd:maxExclusive
if (graph[facet, RDFVocabulary.XSD.MAX_EXCLUSIVE, null, null].FirstOrDefault()?.Object is RDFTypedLiteral facetMaxExclusive
&& facetMaxExclusive.HasDecimalDatatype() && double.TryParse(facetMaxExclusive.Value, NumberStyles.Integer | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out double facetMaxExclusiveValue))
{
targetFacets.Add(new RDFMaxExclusiveFacet(facetMaxExclusiveValue));
continue;
}
//xsd:maxInclusive
if (graph[facet, RDFVocabulary.XSD.MAX_INCLUSIVE, null, null].FirstOrDefault()?.Object is RDFTypedLiteral facetMaxInclusive
&& facetMaxInclusive.HasDecimalDatatype() && double.TryParse(facetMaxInclusive.Value, NumberStyles.Integer | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out double facetMaxInclusiveValue))
{
targetFacets.Add(new RDFMaxInclusiveFacet(facetMaxInclusiveValue));
continue;
}
//xsd:maxLength
if (graph[facet, RDFVocabulary.XSD.MAX_LENGTH, null, null].FirstOrDefault()?.Object is RDFTypedLiteral facetMaxLength
&& facetMaxLength.HasDecimalDatatype() && uint.TryParse(facetMaxLength.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint facetMaxLengthValue))
{
targetFacets.Add(new RDFMaxLengthFacet(facetMaxLengthValue));
continue;
}
//xsd:minLength
if (graph[facet, RDFVocabulary.XSD.MIN_LENGTH, null, null].FirstOrDefault()?.Object is RDFTypedLiteral facetMinLength
//xsd:minExclusive
if (graph[facet, RDFVocabulary.XSD.MIN_EXCLUSIVE, null, null].FirstOrDefault()?.Object is RDFTypedLiteral facetMinExclusive
&& facetMinExclusive.HasDecimalDatatype() && double.TryParse(facetMinExclusive.Value, NumberStyles.Integer | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out double facetMinExclusiveValue))
{
targetFacets.Add(new RDFMinExclusiveFacet(facetMinExclusiveValue));
continue;
}
//xsd:minLength
if (graph[facet, RDFVocabulary.XSD.MIN_LENGTH, null, null].FirstOrDefault()?.Object is RDFTypedLiteral facetMinLength
&& facetMinLength.HasDecimalDatatype() && uint.TryParse(facetMinLength.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint facetMinLengthValue))
{
targetFacets.Add(new RDFMinLengthFacet(facetMinLengthValue));
Expand Down

0 comments on commit 9f7c45f

Please sign in to comment.