Skip to content

Commit

Permalink
Implemented SHACL Xone constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed May 16, 2020
1 parent 98f20c0 commit d25f6f7
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
Copyright 2012-2020 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.Query;
using System;
using System.Collections.Generic;
using System.Linq;

namespace RDFSharp.Model
{
/// <summary>
/// RDFXoneConstraint represents a SHACL constraint requiring exactly one of the given shapes for a given RDF term
/// </summary>
public class RDFXoneConstraint : RDFConstraint {

#region Properties
/// <summary>
/// Shapes required for the given RDF term
/// </summary>
internal Dictionary<Int64, RDFResource> XoneShapes { get; set; }
#endregion

#region Ctors
/// <summary>
/// Default-ctor to build a xone constraint
/// </summary>
public RDFXoneConstraint() : base() {
this.XoneShapes = new Dictionary<Int64, RDFResource>();
}
#endregion

#region Methods
/// <summary>
/// Adds the given shape to the required shapes of this constraint
/// </summary>
public RDFXoneConstraint AddShape(RDFResource shapeUri) {
if (shapeUri != null && !this.XoneShapes.ContainsKey(shapeUri.PatternMemberID)) {
this.XoneShapes.Add(shapeUri.PatternMemberID, shapeUri);
}
return this;
}

/// <summary>
/// Evaluates this constraint against the given data graph
/// </summary>
internal override RDFValidationReport ValidateConstraint(RDFShapesGraph shapesGraph, RDFGraph dataGraph, RDFShape shape, RDFPatternMember focusNode, List<RDFPatternMember> valueNodes) {
RDFValidationReport report = new RDFValidationReport();

//Search for given xone shapes
List<RDFShape> xoneShapes = new List<RDFShape>();
foreach (RDFResource xoneShapeUri in this.XoneShapes.Values) {
RDFShape xoneShape = shapesGraph.SelectShape(xoneShapeUri.ToString());
if (xoneShape != null)
xoneShapes.Add(xoneShape);
}

#region Evaluation
foreach (RDFPatternMember valueNode in valueNodes) {
Int32 valueNodeConformsCounter = 0;
foreach (RDFShape xoneShape in xoneShapes) {
RDFValidationReport xoneShapeReport = RDFValidationEngine.ValidateShape(shapesGraph, dataGraph, xoneShape, new List<RDFPatternMember>() { valueNode });
if (xoneShapeReport.Conforms)
valueNodeConformsCounter++;
}

if (valueNodeConformsCounter != 1)
report.AddResult(new RDFValidationResult(shape,
RDFVocabulary.SHACL.XONE_CONSTRAINT_COMPONENT,
focusNode,
shape is RDFPropertyShape ? ((RDFPropertyShape)shape).Path : null,
valueNode,
shape.Messages,
shape.Severity));
}
#endregion

return report;
}

/// <summary>
/// Gets a graph representation of this constraint
/// </summary>
internal override RDFGraph ToRDFGraph(RDFShape shape) {
RDFGraph result = new RDFGraph();
if (shape != null) {

//Get collection from xoneShapes
RDFCollection xoneShapes = new RDFCollection(RDFModelEnums.RDFItemTypes.Resource) { InternalReificationSubject = this };
foreach (RDFResource xoneShape in this.XoneShapes.Values)
xoneShapes.AddItem(xoneShape);
result.AddCollection(xoneShapes);

//sh:xone
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.XONE, xoneShapes.ReificationSubject));

}
return result;
}
#endregion

}
}
15 changes: 15 additions & 0 deletions RDFSharp/Model/Validation/RDFValidationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ private static RDFSelectQuery GetShapeQuery() {
.AddPattern(new RDFPattern(new RDFVariable("NSHAPE"), RDFVocabulary.SHACL.QUALIFIED_MIN_COUNT, new RDFVariable("QUALIFIEDMINCOUNT")).Optional())
.AddPattern(new RDFPattern(new RDFVariable("NSHAPE"), RDFVocabulary.SHACL.QUALIFIED_MAX_COUNT, new RDFVariable("QUALIFIEDMAXCOUNT")).Optional())
.AddPattern(new RDFPattern(new RDFVariable("NSHAPE"), RDFVocabulary.SHACL.UNIQUE_LANG, new RDFVariable("UNIQUELANG")).Optional())
.AddPattern(new RDFPattern(new RDFVariable("NSHAPE"), RDFVocabulary.SHACL.XONE, new RDFVariable("XONE")).Optional())
.UnionWithNext()
)
.AddPatternGroup(new RDFPatternGroup("PROPERTYSHAPES")
Expand Down Expand Up @@ -255,6 +256,7 @@ private static RDFSelectQuery GetShapeQuery() {
.AddPattern(new RDFPattern(new RDFVariable("PSHAPE"), RDFVocabulary.SHACL.QUALIFIED_MIN_COUNT, new RDFVariable("QUALIFIEDMINCOUNT")).Optional())
.AddPattern(new RDFPattern(new RDFVariable("PSHAPE"), RDFVocabulary.SHACL.QUALIFIED_MAX_COUNT, new RDFVariable("QUALIFIEDMAXCOUNT")).Optional())
.AddPattern(new RDFPattern(new RDFVariable("PSHAPE"), RDFVocabulary.SHACL.UNIQUE_LANG, new RDFVariable("UNIQUELANG")).Optional())
.AddPattern(new RDFPattern(new RDFVariable("PSHAPE"), RDFVocabulary.SHACL.XONE, new RDFVariable("XONE")).Optional())
);
}
private static RDFShape ParseShapeType(DataRow shapesRow) {
Expand Down Expand Up @@ -628,6 +630,19 @@ private static void ParseShapeConstraints(DataRow shapesRow, RDFGraph graph, RDF
shape.AddConstraint(new RDFUniqueLangConstraint(Boolean.Parse(((RDFTypedLiteral)uniqueLang).Value)));
}

//sh:xone
if (!shapesRow.IsNull("?XONE")) {
RDFPatternMember reifSubj = RDFQueryUtilities.ParseRDFPatternMember(shapesRow.Field<string>("?XONE"));
if (reifSubj is RDFResource) {
RDFXoneConstraint xoneConstraint = new RDFXoneConstraint();
RDFCollection xoneColl = RDFModelUtilities.DeserializeCollectionFromGraph(graph, (RDFResource)reifSubj, RDFModelEnums.RDFTripleFlavors.SPO);
xoneColl.Items.ForEach(item => {
xoneConstraint.AddShape((RDFResource)item);
});
shape.AddConstraint(xoneConstraint);
}
}

}
private static void MergeShape(RDFShapesGraph result, RDFShape shape) {
RDFShape existingShape = result.SelectShape(shape.ToString());
Expand Down

0 comments on commit d25f6f7

Please sign in to comment.